summaryrefslogtreecommitdiff
path: root/ordemo
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2022-03-25 00:11:34 +0800
committerzhang <zch921005@126.com>2022-03-25 00:11:34 +0800
commit3ed1c5d0e4018fc70012a6209a859a059f7127b5 (patch)
tree2ca318fb2e23daf2964189a751fb61b20aa7515a /ordemo
parent6f68e1818229e0d2dad760062e6b5bb137b88f5b (diff)
更新脚本
Diffstat (limited to 'ordemo')
-rw-r--r--ordemo/polygon.py45
-rw-r--r--ordemo/send_more_money.py4
-rw-r--r--ordemo/two_sum.py36
3 files changed, 83 insertions, 2 deletions
diff --git a/ordemo/polygon.py b/ordemo/polygon.py
new file mode 100644
index 0000000..6a157f4
--- /dev/null
+++ b/ordemo/polygon.py
@@ -0,0 +1,45 @@
+from ortools.linear_solver import pywraplp
+
+
+def main():
+ # Create the mip solver with the SCIP backend.
+ solver = pywraplp.Solver.CreateSolver('SCIP')
+
+ infinity = solver.infinity()
+ # x and y are integer non-negative variables.
+ x = solver.IntVar(3, infinity, 'x')
+ y = solver.IntVar(0, infinity, 'y')
+ t = solver.IntVar(0, infinity, 't')
+
+ print('Number of variables =', solver.NumVariables())
+
+ solver.Add(t == x-2)
+ # x + 7 * y <= 17.5.
+ solver.Add(y == 2*x/t)
+
+ # x <= 3.5.
+ # solver.Add(x <= 3.5)
+
+ print('Number of constraints =', solver.NumConstraints())
+
+ # Maximize x + 10 * y.
+ solver.Maximize(0)
+
+ status = solver.Solve()
+
+ if status == pywraplp.Solver.OPTIMAL:
+ print('Solution:')
+ print('Objective value =', solver.Objective().Value())
+ print('x =', x.solution_value())
+ print('y =', y.solution_value())
+ else:
+ print('The problem does not have an optimal solution.')
+
+ print('\nAdvanced usage:')
+ print('Problem solved in %f milliseconds' % solver.wall_time())
+ print('Problem solved in %d iterations' % solver.iterations())
+ print('Problem solved in %d branch-and-bound nodes' % solver.nodes())
+
+
+if __name__ == '__main__':
+ main() \ No newline at end of file
diff --git a/ordemo/send_more_money.py b/ordemo/send_more_money.py
index 77fddda..d8e10fa 100644
--- a/ordemo/send_more_money.py
+++ b/ordemo/send_more_money.py
@@ -50,5 +50,5 @@ solver.parameters.enumerate_all_solutions = True
solver.Solve(model, solution_printer)
-print(
- f" {solver.Value(S)}{solver.Value(E)}{solver.Value(N)}{solver.Value(D)}\n+ {solver.Value(M)}{solver.Value(O)}{solver.Value(R)}{solver.Value(E)}\n={solver.Value(M)}{solver.Value(O)}{solver.Value(N)}{solver.Value(E)}{solver.Value(Y)}")
+# print(
+# f" {solver.Value(S)}{solver.Value(E)}{solver.Value(N)}{solver.Value(D)}\n+ {solver.Value(M)}{solver.Value(O)}{solver.Value(R)}{solver.Value(E)}\n={solver.Value(M)}{solver.Value(O)}{solver.Value(N)}{solver.Value(E)}{solver.Value(Y)}")
diff --git a/ordemo/two_sum.py b/ordemo/two_sum.py
new file mode 100644
index 0000000..5652998
--- /dev/null
+++ b/ordemo/two_sum.py
@@ -0,0 +1,36 @@
+from ortools.sat.python import cp_model
+
+
+class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
+ """Print intermediate solutions."""
+
+ def __init__(self, variables):
+ cp_model.CpSolverSolutionCallback.__init__(self)
+ self.__variables = variables
+ self.__solution_count = 0
+
+ def on_solution_callback(self):
+ self.__solution_count += 1
+ for v in self.__variables:
+ print('%s=%i' % (v, self.Value(v)), end=' ')
+ print()
+
+ def solution_count(self):
+ return self.__solution_count
+
+
+l = [2, 7, 1, 3, 6, 9, 10, -1, 12, 22]
+target = 9
+
+model = cp_model.CpModel()
+
+xs = [model.NewBoolVar(str(i)) for i in range(len(l))]
+
+model.Add(sum(xs) == 2)
+
+model.Add(sum([xs[i]*l[i] for i in range(len(l))]) == target)
+solver = cp_model.CpSolver()
+solution_printer = VarArraySolutionPrinter(xs)
+solver.parameters.enumerate_all_solutions = True
+solver.Solve(model, solution_printer)
+