72

 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
51
52
53
54
55
56
57
58
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 pos(x):
if x > 0:
return x
return 0
def f1(x):
return 2*x[0]**2 + 3*x[0]*x[1] - 4.0*x[1]**2 - 5.0*x[0] - x[1]
#return 2.0*x[0]**2 + 4.0*x[0]*x[1] + x[1]**2 - x[0] - 5.0*x[1]
def f2(x):
return 2.0*(x[0]-2)**2 + (x[1]-5)**2 + 2.0*x[2]**2
#return 4.0*(x[0]-3)**2 + (x[1]-5)**2 + 3.0*x[2]**2
def g1(x):
return x[0] - x[1] + 3*x[2] - 6
#return x[0] + x[1] + 2.0*x[2] - 16
def g2(x):
return 3.0*x[1] - 2.0*x[0] - 12
#return -2.0*x[0] + x[1] + x[2] - 10
arr0 = [0.0, 0.0, 0.0]
arr1 = minimize(lambda alpha: f1(alpha), arr0, method='nelder-Mead').x
print arr1
arr2 = minimize(lambda alpha: f2(alpha), arr0, method='nelder-Mead').x
print arr2
vect1 = np.array([0.89, 0.44])
vect2 = np.array([1.2, 1.0])
vect3 = np.array([1.0, 1.0])
vect4 = np.array([0.66, 1.0])
eps = 0.01
def Sh(arr0, gap, it, vect):
def F(alpha, r):
return (vect[0]*(f1(alpha) - f1(arr1)) + vect[1]*(f2(alpha) - f2(arr2)) + (r/2.) * ((pos(g1(alpha)) ** 2) + (pos(g2(alpha)) ** 2)))
def P(x, r):
return ((r/2.) * ((pos(g1(x)) ** 2) + (pos(g2(x)) ** 2)))
res = (minimize(lambda alpha: F(alpha, gap), arr0, method='SLSQP'))
x1 = res.x
if np.fabs(P(x1, gap)) <= eps:
print 'it: ', it
return x1
else:
return Sh(x1, 5.0 * gap, it+1, vect)
def start(vect):
print 'Sh:'
result = Sh([0.0, 100.0, 0.0], 1.0, 0, vect)
print 'x=', result, 'f1(x)=', f1(result), 'f2(x)=', f2(result)
start(vect1)
start(vect2)
start(vect3)
start(vect4)