import seaborn as sb import matplotlib pyplot as plt from mpl_toolkits

 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
%pylab inline
def Shekel(x):
res = 0.0
for i in range (0, 3):
res += 2.0/(-1.0/3.0 + 2 *((x[0] - 4)**2 + (x[1] - 4)**2 + (x[2] - 4)**2))
return res
def concurpoints():
N = 600
low = 0
high = 8
x = np.array([np.random.uniform(low, high, 3) for i in range(0, N)])
bnds = ((low, high), (low, high), (low, high))
y = np.array(([[Shekel(minimize(lambda alpha: Shekel(alpha), x[i], method='SLSQP', bounds=bnds).x)] for i in range(0, N)]))
estimator = KMeans()
estimator.fit(y)
labels = estimator.labels_
minimums = np.array([])
for i in range (0, 8):
clusteri = np.array([])
for j in range (0, N):
if labels[j] == i:
clusteri = np.append(clusteri, y[j])
if len(clusteri) > 0:
minind = 0
minval = clusteri[minind]
for k in range (1, len(clusteri)):
if clusteri[k] < minval:
minval = clusteri[k]
minind = k
minimums = np.append(minimums, minval)
num = 0
for i in range (0, N):
if labels[i] == np.argmin(minimums):
num = i
break
p = minimize(lambda alpha: Shekel(alpha), x[num], method='SLSQP', bounds=bnds)
newp = minimize(lambda alpha: Shekel(alpha), p.x, method='SLSQP', bounds=bnds)
print 'x=', newp.x, 'f(x)=', Shekel(newp.x)
concurpoints()
%timeit concurpoints()