diff options
| author | zhang <zch921005@126.com> | 2022-04-09 23:35:00 +0800 |
|---|---|---|
| committer | zhang <zch921005@126.com> | 2022-04-09 23:35:00 +0800 |
| commit | 6a58ed7cc2d8ae0b28321fb4bfe2921e7ef2db08 (patch) | |
| tree | 1791c3f184d382e4d39c76937371f8df63fc8336 /nlp/demo.py | |
| parent | 61533316d8a6ede4574e80862631f04d39ddd460 (diff) | |
update example 16.4
Diffstat (limited to 'nlp/demo.py')
| -rw-r--r-- | nlp/demo.py | 28 |
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() + |
