7

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import seaborn as sb
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.optimize import minimize
from sklearn.cluster import KMeans
def Shekel(x):
a = 2
b = 1
c = 2
res = 0.0
for i in range (0, 3):
res += a/(-1.0/c + b *((x[0] - 4)**2 + (x[1] - 4)**2 + (x[2] - 4)**2))
return res
left = 1
right = 11
def competing_points(n):
#generate points
x = np.array([np.random.uniform(left, right, 3) for i in range(0, n)])
#minimize from each x
y = np.array(([[Shekel(minimize(lambda alpha: Shekel(alpha), x[i], method='SLSQP', bounds=((left, right), (left, right), (left, right))).x)] for i in range(0, n)]))
#clasterization
estimator = KMeans()
estimator.fit(y)
labels = estimator.labels_
min_arr = np.array([])
clust = np.array([])
for i in range (left, right):
for j in range (left, n):
if labels[j] == i:
clust = np.append(clust, y[j])
#find min
if len(clust) > 0:
min = clust[0]
for k in range (left+1, len(clust)):
if clust[k] < min:
min = clust[k]
min_arr = np.append(min_arr, min)
for i in range (left, n):
if labels[i] == np.argmin(min_arr):
it = i
break
p1 = minimize(lambda alpha: Shekel(alpha), x[it], method='SLSQP', bounds=((left, right), (left, right), (left, right)))
p2 = minimize(lambda alpha: Shekel(alpha), p1.x, method='SLSQP', bounds=((left, right), (left, right), (left, right)))
return p2.x, Shekel(p2.x)
x, f_x = competing_points(500)
print 'x: ',x,' Shekel(x): ',Shekel(x)