Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- def f(*args):
- if len(args) == 2:
- x1, x2 = args
- elif len(args) == 1:
- x1, x2 = args[0]
- else:
- raise ValueError('Incorrect parameters %s' % str(args))
- return -x1 - 2*x2
- def constr(*args):
- # 0 ≤ x1,2 ≤ 1
- # x1 + x2 ≤ 1.5
- if len(args) == 2:
- x1, x2 = args
- elif len(args) == 1:
- x1, x2 = args[0]
- else:
- raise ValueError('Incorrect parameters %s' % str(args))
- return 0 <= x1 + x2 <= 1.5
- def ellipsoid(f, constr, H0, c0, A, b, _c):
- c = c0.copy()
- H = H0.copy()
- n = len(c)
- i = 0
- while i < 10:
- # choose w
- if not constr(c):
- # взять строчку А с максимальным значением
- ac_b = A.dot(c) - b
- w = A[ac_b.argmax()]
- else:
- w = _c.copy()
- print('\ni', i)
- print('H', H)
- print('c', c)
- print('w', w)
- c -= 1./(n + 1.) * (H*w) / np.sqrt(w.T * H * w)
- H = n * n / (n * n - 1.) * (H - 2./(n+1)*H*w*w.T*H/(w.T*H*w))
- i += 1
- # -x1 + 0x2 <= 0
- # 0x1 -x2 <= 0
- # x1 + x2 <= 1.5
- r = 5
- H = r * np.eye(2.)
- c0 = np.array([0., 1.])
- A = np.concatenate((-np.eye(2), np.ones((1, 2))))
- b = np.array([0, 0, 1.5])
- c = np.array([-1, -2])
- print('c', c)
- print('A', A)
- print('b', b)
- ellipsoid(f, constr, H, c0, A, b, c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement