summaryrefslogtreecommitdiff
path: root/nlp/demo.py
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2022-04-09 18:33:47 +0800
committerzhang <zch921005@126.com>2022-04-09 18:33:47 +0800
commit9f45c1775e69b8c0ebc38433777e9e71e81f557b (patch)
tree2b23347db6c46d08177b286508ef6624b2ab8844 /nlp/demo.py
parent3ed1c5d0e4018fc70012a6209a859a059f7127b5 (diff)
更新脚本
Diffstat (limited to 'nlp/demo.py')
-rw-r--r--nlp/demo.py31
1 files changed, 31 insertions, 0 deletions
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()