From 9f45c1775e69b8c0ebc38433777e9e71e81f557b Mon Sep 17 00:00:00 2001 From: zhang Date: Sat, 9 Apr 2022 18:33:47 +0800 Subject: =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nlp/__init__.py | 0 nlp/demo.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 nlp/__init__.py create mode 100644 nlp/demo.py (limited to 'nlp') diff --git a/nlp/__init__.py b/nlp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nlp/demo.py b/nlp/demo.py new file mode 100644 index 0000000..9eb7154 --- /dev/null +++ b/nlp/demo.py @@ -0,0 +1,31 @@ +from scipy.optimize import minimize, LinearConstraint +import numpy as np + + +def test1(): + fun = lambda x: x**2 + 2*x - 3 + x0 = 1 + res = minimize(fun, [x0], bounds=[[0, None]], ) + print(res) + + +def test2(): + fun = lambda x: (x[0]-2)**2 + 4*(x[1]-1)**2 + x0 = [0, 0] + cons = ({'type': 'ineq', 'fun': lambda x: 2 - x[0] - 2*x[1]}) + res = minimize(fun, np.asarray(x0), + method='slsqp', + constraints=cons, options={'disp': True}) + print(res) + + +def test3(): + fun = lambda x: -x[0]**2*x[1] + x0 = np.asarray([0, 0]) + cons = ({'type': 'eq', 'fun': lambda x: x[0]**2+x[1]**2-1}) + res = minimize(fun, x0, constraints=cons, options={'disp': True}) + print(res) + + +if __name__ == '__main__': + test3() -- cgit v1.2.3