summaryrefslogtreecommitdiff
path: root/ordemo
diff options
context:
space:
mode:
Diffstat (limited to 'ordemo')
-rw-r--r--ordemo/lp_mip.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/ordemo/lp_mip.py b/ordemo/lp_mip.py
new file mode 100644
index 0000000..f135523
--- /dev/null
+++ b/ordemo/lp_mip.py
@@ -0,0 +1,38 @@
+from ortools.linear_solver import pywraplp
+import numpy as np
+
+
+# solver = pywraplp.Solver('Maximize army power', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
+# solver = pywraplp.Solver('Maximize army power', pywraplp.Solver.GLPK_LINEAR_PROGRAMMING)
+# solver = pywraplp.Solver('Maximize army power', pywraplp.Solver.CLP_LINEAR_PROGRAMMING)
+solver = pywraplp.Solver('Maximize army power', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
+
+
+swordsmen = solver.IntVar(0, solver.infinity(), 'swordsmen')
+bowmen = solver.IntVar(0, solver.infinity(), 'bowmen')
+horsemen = solver.IntVar(0, solver.infinity(), 'horsemen')
+
+x = np.asarray([swordsmen, bowmen, horsemen])
+A = np.asarray([[60, 20, 0], [80, 10, 40], [140, 0, 100]])
+b = np.asarray([1200, 800, 600])
+c = np.asarray([70, 95, 230])
+
+Ax = A.T.dot(x)
+for i in range(len(b)):
+ solver.Add(Ax[i] <= b[i])
+
+solver.Maximize(np.inner(x, c))
+
+status = solver.Solve()
+# If an optimal solution has been found, print results
+if status == pywraplp.Solver.OPTIMAL:
+ print('================= Solution =================')
+ print(f'Solved in {solver.wall_time():.2f} milliseconds in {solver.iterations()} iterations')
+ print()
+ print(f'Optimal power = {solver.Objective().Value()} 💪power')
+ print('Army:')
+ print(f' - 🗡️Swordsmen = {swordsmen.solution_value()}')
+ print(f' - 🏹Bowmen = {bowmen.solution_value()}')
+ print(f' - 🐎Horsemen = {horsemen.solution_value()}')
+else:
+ print('The solver could not find an optimal solution.') \ No newline at end of file