Advertisement
Guest User

Ellipsoid

a guest
Sep 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2.  
  3. import numpy as np
  4.  
  5. def f(*args):
  6.     if len(args) == 2:
  7.         x1, x2 = args
  8.     elif len(args) == 1:
  9.         x1, x2 = args[0]
  10.     else:
  11.         raise ValueError('Incorrect parameters %s' % str(args))
  12.        
  13.     return -x1 - 2*x2
  14.  
  15. def constr(*args):
  16.     # 0 ≤ x1,2 ≤ 1
  17.     # x1 + x2 ≤ 1.5
  18.  
  19.     if len(args) == 2:
  20.         x1, x2 = args
  21.     elif len(args) == 1:
  22.         x1, x2 = args[0]
  23.     else:
  24.         raise ValueError('Incorrect parameters %s' % str(args))
  25.        
  26.     return 0 <= x1 + x2 <= 1.5
  27.    
  28. def ellipsoid(f, constr, H0, c0, A, b, _c):
  29.     c = c0.copy()
  30.     H = H0.copy()
  31.     n = len(c)
  32.    
  33.     i = 0
  34.     while i < 10:
  35.         # choose w
  36.         if not constr(c):
  37.             # взять строчку А с максимальным значением
  38.             ac_b = A.dot(c) - b
  39.             w = A[ac_b.argmax()]
  40.         else:
  41.             w = _c.copy()
  42.        
  43.         print('\ni', i)
  44.         print('H', H)
  45.         print('c', c)
  46.         print('w', w)
  47.        
  48.         c -= 1./(n + 1.) * (H*w) / np.sqrt(w.T * H * w)
  49.        
  50.         H = n * n / (n * n - 1.) * (H - 2./(n+1)*H*w*w.T*H/(w.T*H*w))
  51.    
  52.         i += 1
  53.        
  54. # -x1 + 0x2 <= 0
  55. # 0x1 -x2 <= 0
  56. # x1 + x2 <= 1.5
  57.    
  58.  
  59. r = 5
  60. H = r * np.eye(2.)
  61. c0 = np.array([0., 1.])
  62.  
  63. A = np.concatenate((-np.eye(2), np.ones((1, 2))))
  64. b = np.array([0, 0, 1.5])
  65. c = np.array([-1, -2])
  66.  
  67. print('c', c)
  68. print('A', A)
  69. print('b', b)
  70.  
  71.  
  72. ellipsoid(f, constr, H, c0, A, b, c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement