-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKKT.py
56 lines (44 loc) · 1.29 KB
/
KKT.py
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
from newton_method import ProblemInterface, NewtonsMethod
from lagrange_multiplier import Problem, LagrangeMultiplier
import numpy as np
class UneqConstraint(ProblemInterface):
def __init__(self, initial_guess: np.array):
x, y = initial_guess[0:2]
self.x = x
self.y = y
def __repr__(self):
return "Constraint(y - x + 3 <= 0)"
def eval(self):
return self.y - self.x + 3
def grad(self):
return np.array([-1, 1, 0.0])
def hessian(self):
return np.eye(3)*0.0
def update_x(self, d_x):
self.x += d_x[0]
self.y += d_x[1]
if __name__ == "__main__":
"""
Problem description:
f(x, y) = x, y.dot(x, y)
g(x, y) = x^2X[1] - 3
h(x, y) = y - x + 3
min{f(x)}
s.t. g(x) = 0
h(x) <= 0
Dual Problem:
L(x, lamb, miu) = f(x) + lamb * g(x) + miu * h(x)
nabla_{L(x)} = 0
s.t. miu >= 0
Reference:
f'(x, y) = 2 * x, y
partial_g(x, y) / partial_x = 2 * x * y
partial_g(x, y) / partial_y = x^2
partial_h(x, y) / partial_x = -1
partial_h(x, y) / partial_y = 1
"""
pb_kkt = LagrangeMultiplier(Problem, UneqConstraint, np.ones(4), True)
print(pb_kkt.eval())
print(pb_kkt.grad())
print(pb_kkt.hessian())
NewtonsMethod(pb_kkt)