From 3ed1c5d0e4018fc70012a6209a859a059f7127b5 Mon Sep 17 00:00:00 2001 From: zhang Date: Fri, 25 Mar 2022 00:11:34 +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 --- ordemo/polygon.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ordemo/polygon.py (limited to 'ordemo/polygon.py') 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 -- cgit v1.2.3