2021-04-16 00:21:07 +02:00
|
|
|
#!/usr/bin/env python3
|
2025-01-10 11:35:44 +01:00
|
|
|
# Copyright 2010-2025 Google LLC
|
2018-11-08 11:20:51 +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-08 11:20:51 +01:00
|
|
|
"""Minimal example to call the GLOP solver."""
|
|
|
|
|
# [START program]
|
2019-05-13 11:05:42 +02:00
|
|
|
# [START import]
|
2018-11-08 11:20:51 +01:00
|
|
|
from ortools.linear_solver import pywraplp
|
2025-07-23 15:07:49 +02:00
|
|
|
|
2019-05-13 11:05:42 +02:00
|
|
|
# [END import]
|
2018-11-08 11:20:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-05-13 11:05:42 +02:00
|
|
|
# [START solver]
|
2018-11-22 09:29:22 +01:00
|
|
|
# Create the linear solver with the GLOP backend.
|
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
|
2019-05-13 11:05:42 +02:00
|
|
|
# [END solver]
|
2018-11-22 09:29:22 +01:00
|
|
|
|
2019-05-13 11:05:42 +02:00
|
|
|
# [START variables]
|
2020-12-07 14:57:58 +01:00
|
|
|
infinity = solver.infinity()
|
2018-11-22 09:29:22 +01:00
|
|
|
# Create the variables x and y.
|
2023-06-28 15:57:32 +02:00
|
|
|
x = solver.NumVar(0.0, infinity, "x")
|
|
|
|
|
y = solver.NumVar(0.0, infinity, "y")
|
2018-11-22 09:29:22 +01:00
|
|
|
|
2023-06-28 15:57:32 +02:00
|
|
|
print("Number of variables =", solver.NumVariables())
|
2019-05-13 11:05:42 +02:00
|
|
|
# [END variables]
|
|
|
|
|
|
|
|
|
|
# [START constraints]
|
2020-12-07 14:57:58 +01:00
|
|
|
# x + 7 * y <= 17.5.
|
|
|
|
|
solver.Add(x + 7 * y <= 17.5)
|
|
|
|
|
|
|
|
|
|
# x <= 3.5.
|
|
|
|
|
solver.Add(x <= 3.5)
|
2018-11-22 09:29:22 +01:00
|
|
|
|
2023-06-28 15:57:32 +02:00
|
|
|
print("Number of constraints =", solver.NumConstraints())
|
2019-05-13 11:05:42 +02:00
|
|
|
# [END constraints]
|
|
|
|
|
|
|
|
|
|
# [START objective]
|
2020-12-07 14:57:58 +01:00
|
|
|
# Maximize x + 10 * y.
|
|
|
|
|
solver.Maximize(x + 10 * y)
|
2019-05-13 11:05:42 +02:00
|
|
|
# [END objective]
|
2018-11-22 09:29:22 +01:00
|
|
|
|
2019-05-13 11:05:42 +02:00
|
|
|
# [START solve]
|
2023-06-28 15:57:32 +02:00
|
|
|
print(f"Solving with {solver.SolverVersion()}")
|
2020-12-07 14:57:58 +01:00
|
|
|
status = solver.Solve()
|
2019-05-13 11:05:42 +02:00
|
|
|
# [END solve]
|
|
|
|
|
|
|
|
|
|
# [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:")
|
|
|
|
|
print("Objective value =", solver.Objective().Value())
|
|
|
|
|
print("x =", x.solution_value())
|
|
|
|
|
print("y =", y.solution_value())
|
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.")
|
2019-05-13 11:05:42 +02:00
|
|
|
# [END print_solution]
|
2018-11-08 11:20:51 +01:00
|
|
|
|
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-08 11:20:51 +01:00
|
|
|
|
2023-06-28 15:57:32 +02:00
|
|
|
if __name__ == "__main__":
|
2018-11-22 09:29:22 +01:00
|
|
|
main()
|
2018-11-08 11:20:51 +01:00
|
|
|
# [END program]
|