def gplus if return else return def f1 return def f2 return def g1 ret

 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
def gplus(a):
if a > 0:
return a
else:
return 0
def f1(x):
return 3.0*x[0]**2 + 2.0*x[0]*x[1] - x[0] - 3.0*x[1]
def f2(x):
return (x[0] - 2)**4 + 4.0*(x[1] - 5)**2 + 2.*x[2]**2
def g1(x):
return 2.*x[0] + x[1] + 3.0*x[2] - 12
def g2(x):
return -2.0*x[0] + 3.*x[1] - 10
x0 = [0.0, 0.0, 0.0]
x1 = minimize(lambda alpha: f1(alpha), x0, method='Nelder-Mead').x
print x1
x2 = minimize(lambda alpha: f2(alpha), x0, method='Nelder-Mead').x
print x2
w1 = np.array([0.89, 0.44])
w2 = np.array([1.2, 1.0])
w3 = np.array([1.0, 1.0])
w4 = np.array([0.66, 1.0])
def count(w):
def F(x, r):
return (w[0]*(f1(x) - f1(x1)) + w[1]*(f2(x) - f2(x2)) + (r/2.) * ((gplus(g1(x)) ** 2) + (gplus(g2(x)) ** 2)))
def P(x, r):
return ((r/2.) * ((gplus(g1(x)) ** 2) + (gplus(g2(x)) ** 2)))
r0 = 1.0
C = 7.0
eps = 0.001
k = 0
def Sh(r, k, x0):
res = (minimize(lambda alpha: F(alpha, r), x0, method='SLSQP'))
newx = res.x
if np.fabs(P(newx, r)) <= eps:
print 'количество итераций:', k
return newx
else:
return Sh(C * r, k + 1, newx)
print 'Метод штрафных функций:'
result = Sh(r0, k, [0.0, 100.0, 0.0])
print 'x=', result, 'f1(x)=', f1(result), 'f2(x)=', f2(result)
count(w1)
count(w2)
count(w3)
count(w4)