diff options
| author | zhang <zch921005@126.com> | 2022-04-09 18:33:47 +0800 |
|---|---|---|
| committer | zhang <zch921005@126.com> | 2022-04-09 18:33:47 +0800 |
| commit | 9f45c1775e69b8c0ebc38433777e9e71e81f557b (patch) | |
| tree | 2b23347db6c46d08177b286508ef6624b2ab8844 | |
| parent | 3ed1c5d0e4018fc70012a6209a859a059f7127b5 (diff) | |
更新脚本
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | classic_or/__init__.py | 0 | ||||
| -rw-r--r-- | classic_or/kp/__init__.py | 0 | ||||
| -rw-r--r-- | classic_or/kp/simple_knapsack.py | 18 | ||||
| -rw-r--r-- | data_science/__init__.py | 0 | ||||
| -rw-r--r-- | data_science/pandas_demo/__init__.py | 0 | ||||
| -rw-r--r-- | data_science/pandas_demo/groupby_demo.py | 11 | ||||
| -rw-r--r-- | nlp/__init__.py | 0 | ||||
| -rw-r--r-- | nlp/demo.py | 31 | ||||
| -rw-r--r-- | opt/__init__.py | 0 | ||||
| -rw-r--r-- | opt/dual/__init__.py | 0 | ||||
| -rw-r--r-- | opt/dual/dual_demo.py | 45 |
12 files changed, 106 insertions, 1 deletions
@@ -2,4 +2,4 @@ .idea/bilibili_vlogs.iml .idea/modules.xml .idea/workspace.xml - +data/
\ No newline at end of file diff --git a/classic_or/__init__.py b/classic_or/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/classic_or/__init__.py diff --git a/classic_or/kp/__init__.py b/classic_or/kp/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/classic_or/kp/__init__.py diff --git a/classic_or/kp/simple_knapsack.py b/classic_or/kp/simple_knapsack.py new file mode 100644 index 0000000..7244862 --- /dev/null +++ b/classic_or/kp/simple_knapsack.py @@ -0,0 +1,18 @@ +from pyomo.environ import * + +A = ['hammer', 'wrench', 'screwdriver', 'towel'] +b = {'hammer': 8, 'wrench': 3, 'screwdriver': 6, 'towel': 11} +w = {'hammer': 5, 'wrench': 7, 'screwdriver': 4, 'towel': 3} + +W_max = 14 +model = ConcreteModel() +model.x = Var(A, within=Binary) + +model.value = Objective( + expr=sum(b[i] * model.x[i] for i in A), sense=maximize) +model.weight = Constraint( + expr=sum(w[i] * model.x[i] * model.x[i] for i in A) <= W_max) +opt = SolverFactory('ipopt') +result_obj = opt.solve(model, tee=True) + +model.pprint()
\ No newline at end of file diff --git a/data_science/__init__.py b/data_science/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/data_science/__init__.py diff --git a/data_science/pandas_demo/__init__.py b/data_science/pandas_demo/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/data_science/pandas_demo/__init__.py diff --git a/data_science/pandas_demo/groupby_demo.py b/data_science/pandas_demo/groupby_demo.py new file mode 100644 index 0000000..d271010 --- /dev/null +++ b/data_science/pandas_demo/groupby_demo.py @@ -0,0 +1,11 @@ +import pandas as pd +import numpy as np + +df = pd.DataFrame([('bird', 'Falconiformes', 389.0), + ('bird', 'Psittaciformes', 24.0), + ('mammal', 'Carnivora', 80.2), + ('mammal', 'Primates', np.nan), + ('mammal', 'Carnivora', 58)], + index=['falcon', 'parrot', 'lion', 'monkey', 'leopard'], + columns=('class', 'order', 'max_speed')) +print(df) diff --git a/nlp/__init__.py b/nlp/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/nlp/__init__.py 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() diff --git a/opt/__init__.py b/opt/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/opt/__init__.py diff --git a/opt/dual/__init__.py b/opt/dual/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/opt/dual/__init__.py diff --git a/opt/dual/dual_demo.py b/opt/dual/dual_demo.py new file mode 100644 index 0000000..22eb8f0 --- /dev/null +++ b/opt/dual/dual_demo.py @@ -0,0 +1,45 @@ +from scipy import optimize +from scipy.spatial import distance + +from ortools.sat.python import cp_model + + +def obj(x1, x2): + return -(3*x1 + 4*x2) + + +if __name__ == '__main__': + + # 原始问题 + c = [-3, -4] + A = [[1/2, 2], [3, 1]] + b = [30, 25] + + res = optimize.linprog(c, A_ub=A, b_ub=b, bounds=[[0, None], [0, None]]) + print(res) + + print('=============') + + c = [30, 25] + A = [[-1/2, -3], [-2, -1]] + b = [-3, -4] + res = optimize.linprog(c, A_ub=A, b_ub=b, bounds=[[0, None], [0, None]]) + print(res) + print('=============') + + c = [5, 1] + A = [[-2, -1], [-1, -1], [-2, -20]] + b = [-6, -4, -21] + res = optimize.linprog(c, A_ub=A, b_ub=b, bounds=[[0, None], [0, None]]) + print(res) + print('=============') + + c = [-3, -5] + A = [[1, 3], [3, 4]] + b = [60, 120] + res = optimize.linprog(c, A_ub=A, b_ub=b, bounds=[[10, None], [0, None]]) + print(res) + print('=============') + + model = cp_model.CpModel() + model.AddBoolOr() |
