summaryrefslogtreecommitdiff
path: root/nlp/demo.py
diff options
context:
space:
mode:
Diffstat (limited to 'nlp/demo.py')
-rw-r--r--nlp/demo.py28
1 files changed, 26 insertions, 2 deletions
diff --git a/nlp/demo.py b/nlp/demo.py
index f4090ca..545df8b 100644
--- a/nlp/demo.py
+++ b/nlp/demo.py
@@ -1,5 +1,6 @@
-from scipy.optimize import minimize, LinearConstraint
+from scipy.optimize import minimize, LinearConstraint, rosen, rosen_der, rosen_hess
import numpy as np
+import matplotlib.pyplot as plt
def test1():
@@ -27,5 +28,28 @@ def test3():
print(res)
+def example_16_4():
+ func = lambda x: (x[0]-1)**2 + (x[1] - 2.5)**2
+ x0 = np.asarray([0, 0])
+
+ # bounds = np.asarray([[0, None],
+ # [0, None]])
+
+ cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2*x[1] + 2},
+ {'type': 'ineq', 'fun': lambda x: -x[0] - 2*x[1] + 6},
+ {'type': 'ineq', 'fun': lambda x: -x[0] + 2*x[1] + 2},
+ {'type': 'ineq', 'fun': lambda x: x[0]},
+ {'type': 'ineq', 'fun': lambda x: x[1]})
+
+ res = minimize(func, x0,
+ # bounds=bounds,
+ constraints=cons,
+ jac=lambda x: np.asarray([2*(x[0]-1), 2*(x[1] - 2.5)]),
+ # hess=lambda x: np.asarray([[2, 0], [0, 2]]),
+ method='SLSQP')
+ print(res)
+
+
if __name__ == '__main__':
- test2()
+ example_16_4()
+