2021-04-16 00:21:07 +02:00
|
|
|
#!/usr/bin/env python3
|
2025-01-10 11:33:35 +01:00
|
|
|
# Copyright 2010-2025 Google LLC
|
2018-11-26 17:30:10 +01:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
2023-06-28 15:57:32 +02:00
|
|
|
|
2018-11-26 17:30:10 +01:00
|
|
|
"""Linear optimization example."""
|
|
|
|
|
# [START program]
|
|
|
|
|
# [START import]
|
|
|
|
|
from ortools.linear_solver import pywraplp
|
|
|
|
|
# [END import]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def LinearProgrammingExample():
|
|
|
|
|
"""Linear programming sample."""
|
|
|
|
|
# Instantiate a Glop solver, naming it LinearExample.
|
|
|
|
|
# [START solver]
|
2023-06-28 15:57:32 +02:00
|
|
|
solver = pywraplp.Solver.CreateSolver("GLOP")
|
2022-06-03 17:09:29 +02:00
|
|
|
if not solver:
|
|
|
|
|
return
|
2018-11-26 17:30:10 +01:00
|
|
|
# [END solver]
|
|
|
|
|
|
2019-09-23 11:19:03 -04:00
|
|
|
# Create the two variables and let them take on any non-negative value.
|
2018-11-26 17:30:10 +01:00
|
|
|
# [START variables]
|
2023-06-28 15:57:32 +02:00
|
|
|
x = solver.NumVar(0, solver.infinity(), "x")
|
|
|
|
|
y = solver.NumVar(0, solver.infinity(), "y")
|
2020-12-07 14:57:58 +01:00
|
|
|
|
2023-06-28 15:57:32 +02:00
|
|
|
print("Number of variables =", solver.NumVariables())
|
2018-11-26 17:30:10 +01:00
|
|
|
# [END variables]
|
|
|
|
|
|
|
|
|
|
# [START constraints]
|
|
|
|
|
# Constraint 0: x + 2y <= 14.
|
2020-12-09 16:46:40 +01:00
|
|
|
solver.Add(x + 2 * y <= 14.0)
|
2018-11-26 17:30:10 +01:00
|
|
|
|
|
|
|
|
# Constraint 1: 3x - y >= 0.
|
2020-12-09 16:46:40 +01:00
|
|
|
solver.Add(3 * x - y >= 0.0)
|
2018-11-26 17:30:10 +01:00
|
|
|
|
|
|
|
|
# Constraint 2: x - y <= 2.
|
2020-12-09 16:46:40 +01:00
|
|
|
solver.Add(x - y <= 2.0)
|
2020-12-07 14:57:58 +01:00
|
|
|
|
2023-06-28 15:57:32 +02:00
|
|
|
print("Number of constraints =", solver.NumConstraints())
|
2018-11-26 17:30:10 +01:00
|
|
|
# [END constraints]
|
|
|
|
|
|
|
|
|
|
# [START objective]
|
|
|
|
|
# Objective function: 3x + 4y.
|
2020-12-09 16:46:40 +01:00
|
|
|
solver.Maximize(3 * x + 4 * y)
|
2018-11-26 17:30:10 +01:00
|
|
|
# [END objective]
|
|
|
|
|
|
|
|
|
|
# Solve the system.
|
|
|
|
|
# [START solve]
|
2023-10-25 13:57:55 +02:00
|
|
|
print(f"Solving with {solver.SolverVersion()}")
|
2020-12-07 14:57:58 +01:00
|
|
|
status = solver.Solve()
|
2018-11-26 17:30:10 +01:00
|
|
|
# [END solve]
|
2020-12-07 14:57:58 +01:00
|
|
|
|
2018-11-26 17:30:10 +01:00
|
|
|
# [START print_solution]
|
2020-12-07 14:57:58 +01:00
|
|
|
if status == pywraplp.Solver.OPTIMAL:
|
2023-06-28 15:57:32 +02:00
|
|
|
print("Solution:")
|
2023-10-25 13:57:55 +02:00
|
|
|
print(f"Objective value = {solver.Objective().Value():0.1f}")
|
|
|
|
|
print(f"x = {x.solution_value():0.1f}")
|
|
|
|
|
print(f"y = {y.solution_value():0.1f}")
|
2020-12-07 14:57:58 +01:00
|
|
|
else:
|
2023-06-28 15:57:32 +02:00
|
|
|
print("The problem does not have an optimal solution.")
|
2018-11-26 17:30:10 +01:00
|
|
|
# [END print_solution]
|
|
|
|
|
|
2020-12-07 14:57:58 +01:00
|
|
|
# [START advanced]
|
2023-06-28 15:57:32 +02:00
|
|
|
print("\nAdvanced usage:")
|
2023-10-25 13:57:55 +02:00
|
|
|
print(f"Problem solved in {solver.wall_time():d} milliseconds")
|
|
|
|
|
print(f"Problem solved in {solver.iterations():d} iterations")
|
2020-12-07 14:57:58 +01:00
|
|
|
# [END advanced]
|
|
|
|
|
|
2018-11-26 17:30:10 +01:00
|
|
|
|
|
|
|
|
LinearProgrammingExample()
|
|
|
|
|
# [END program]
|