2021-05-03 12:11:39 +02:00
|
|
|
#!/usr/bin/env python3
|
2025-01-10 11:35:44 +01:00
|
|
|
# Copyright 2010-2025 Google LLC
|
2011-11-03 10:27:53 +00: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-07-01 06:06:53 +02:00
|
|
|
|
2020-06-24 18:11:12 +02:00
|
|
|
"""Linear programming examples that show how to use the APIs."""
|
2018-10-12 11:47:16 +02:00
|
|
|
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.linear_solver import pywraplp
|
2011-11-03 10:27:53 +00:00
|
|
|
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2020-06-24 18:11:12 +02:00
|
|
|
def Announce(solver, api_type):
|
2023-07-01 06:06:53 +02:00
|
|
|
print(
|
|
|
|
|
"---- Linear programming example with " + solver + " (" + api_type + ") -----"
|
|
|
|
|
)
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def RunLinearExampleNaturalLanguageAPI(optimization_problem_type):
|
|
|
|
|
"""Example of simple linear program with natural language API."""
|
2020-08-18 17:16:10 +02:00
|
|
|
solver = pywraplp.Solver.CreateSolver(optimization_problem_type)
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
if not solver:
|
|
|
|
|
return
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
Announce(optimization_problem_type, "natural language API")
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
infinity = solver.infinity()
|
|
|
|
|
# x1, x2 and x3 are continuous non-negative variables.
|
2023-07-01 06:06:53 +02:00
|
|
|
x1 = solver.NumVar(0.0, infinity, "x1")
|
|
|
|
|
x2 = solver.NumVar(0.0, infinity, "x2")
|
|
|
|
|
x3 = solver.NumVar(0.0, infinity, "x3")
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
solver.Maximize(10 * x1 + 6 * x2 + 4 * x3)
|
2023-07-01 06:06:53 +02:00
|
|
|
c0 = solver.Add(10 * x1 + 4 * x2 + 5 * x3 <= 600, "ConstraintName0")
|
2020-06-24 18:11:12 +02:00
|
|
|
c1 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300)
|
|
|
|
|
sum_of_vars = sum([x1, x2, x3])
|
2023-07-01 06:06:53 +02:00
|
|
|
c2 = solver.Add(sum_of_vars <= 100.0, "OtherConstraintName")
|
2018-11-11 09:39:59 +01:00
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
SolveAndPrint(
|
|
|
|
|
solver, [x1, x2, x3], [c0, c1, c2], optimization_problem_type != "PDLP"
|
|
|
|
|
)
|
2020-06-24 18:11:12 +02:00
|
|
|
# Print a linear expression's solution value.
|
2023-07-01 06:06:53 +02:00
|
|
|
print("Sum of vars: %s = %s" % (sum_of_vars, sum_of_vars.solution_value()))
|
2011-11-03 10:27:53 +00:00
|
|
|
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
def RunLinearExampleCppStyleAPI(optimization_problem_type):
|
|
|
|
|
"""Example of simple linear program with the C++ style API."""
|
2020-08-18 17:16:10 +02:00
|
|
|
solver = pywraplp.Solver.CreateSolver(optimization_problem_type)
|
2020-06-24 18:11:12 +02:00
|
|
|
if not solver:
|
|
|
|
|
return
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
Announce(optimization_problem_type, "C++ style API")
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
infinity = solver.infinity()
|
|
|
|
|
# x1, x2 and x3 are continuous non-negative variables.
|
2023-07-01 06:06:53 +02:00
|
|
|
x1 = solver.NumVar(0.0, infinity, "x1")
|
|
|
|
|
x2 = solver.NumVar(0.0, infinity, "x2")
|
|
|
|
|
x3 = solver.NumVar(0.0, infinity, "x3")
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
# Maximize 10 * x1 + 6 * x2 + 4 * x3.
|
2018-11-11 09:39:59 +01:00
|
|
|
objective = solver.Objective()
|
2020-06-24 18:11:12 +02:00
|
|
|
objective.SetCoefficient(x1, 10)
|
|
|
|
|
objective.SetCoefficient(x2, 6)
|
|
|
|
|
objective.SetCoefficient(x3, 4)
|
2018-11-11 09:39:59 +01:00
|
|
|
objective.SetMaximization()
|
2011-11-03 10:27:53 +00:00
|
|
|
|
2020-06-24 18:11:12 +02:00
|
|
|
# x1 + x2 + x3 <= 100.
|
2023-07-01 06:06:53 +02:00
|
|
|
c0 = solver.Constraint(-infinity, 100.0, "c0")
|
2020-06-24 18:11:12 +02:00
|
|
|
c0.SetCoefficient(x1, 1)
|
|
|
|
|
c0.SetCoefficient(x2, 1)
|
|
|
|
|
c0.SetCoefficient(x3, 1)
|
|
|
|
|
|
|
|
|
|
# 10 * x1 + 4 * x2 + 5 * x3 <= 600.
|
2023-07-01 06:06:53 +02:00
|
|
|
c1 = solver.Constraint(-infinity, 600.0, "c1")
|
2020-06-24 18:11:12 +02:00
|
|
|
c1.SetCoefficient(x1, 10)
|
|
|
|
|
c1.SetCoefficient(x2, 4)
|
|
|
|
|
c1.SetCoefficient(x3, 5)
|
|
|
|
|
|
|
|
|
|
# 2 * x1 + 2 * x2 + 6 * x3 <= 300.
|
2023-07-01 06:06:53 +02:00
|
|
|
c2 = solver.Constraint(-infinity, 300.0, "c2")
|
2020-06-24 18:11:12 +02:00
|
|
|
c2.SetCoefficient(x1, 2)
|
|
|
|
|
c2.SetCoefficient(x2, 2)
|
|
|
|
|
c2.SetCoefficient(x3, 6)
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
SolveAndPrint(
|
|
|
|
|
solver, [x1, x2, x3], [c0, c1, c2], optimization_problem_type != "PDLP"
|
|
|
|
|
)
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
|
2022-02-25 19:30:02 +01:00
|
|
|
def SolveAndPrint(solver, variable_list, constraint_list, is_precise):
|
2020-06-24 18:11:12 +02:00
|
|
|
"""Solve the problem and print the solution."""
|
2023-07-01 06:06:53 +02:00
|
|
|
print("Number of variables = %d" % solver.NumVariables())
|
|
|
|
|
print("Number of constraints = %d" % solver.NumConstraints())
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
result_status = solver.Solve()
|
|
|
|
|
|
|
|
|
|
# The problem has an optimal solution.
|
|
|
|
|
assert result_status == pywraplp.Solver.OPTIMAL
|
|
|
|
|
|
|
|
|
|
# The solution looks legit (when using solvers others than
|
|
|
|
|
# GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!).
|
2022-02-25 19:30:02 +01:00
|
|
|
if is_precise:
|
|
|
|
|
assert solver.VerifySolution(1e-7, True)
|
2020-06-24 18:11:12 +02:00
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
print("Problem solved in %f milliseconds" % solver.wall_time())
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
# The objective value of the solution.
|
2023-07-01 06:06:53 +02:00
|
|
|
print("Optimal objective value = %f" % solver.Objective().Value())
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
# The value of each variable in the solution.
|
|
|
|
|
for variable in variable_list:
|
2023-07-01 06:06:53 +02:00
|
|
|
print("%s = %f" % (variable.name(), variable.solution_value()))
|
2020-06-24 18:11:12 +02:00
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
print("Advanced usage:")
|
|
|
|
|
print("Problem solved in %d iterations" % solver.iterations())
|
2020-06-24 18:11:12 +02:00
|
|
|
for variable in variable_list:
|
2023-07-01 06:06:53 +02:00
|
|
|
print("%s: reduced cost = %f" % (variable.name(), variable.reduced_cost()))
|
2018-11-11 09:39:59 +01:00
|
|
|
activities = solver.ComputeConstraintActivities()
|
2020-06-24 18:11:12 +02:00
|
|
|
for i, constraint in enumerate(constraint_list):
|
2023-07-01 06:06:53 +02:00
|
|
|
print(
|
|
|
|
|
(
|
|
|
|
|
"constraint %d: dual value = %f\n activity = %f"
|
|
|
|
|
% (i, constraint.dual_value(), activities[constraint.index()])
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-06-24 18:11:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-07-01 06:06:53 +02:00
|
|
|
RunLinearExampleNaturalLanguageAPI("GLOP")
|
|
|
|
|
RunLinearExampleNaturalLanguageAPI("GLPK_LP")
|
|
|
|
|
RunLinearExampleNaturalLanguageAPI("CLP")
|
|
|
|
|
RunLinearExampleNaturalLanguageAPI("PDLP")
|
2023-11-20 12:43:41 +01:00
|
|
|
RunLinearExampleNaturalLanguageAPI("XPRESS_LP")
|
2020-06-24 18:11:12 +02:00
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
RunLinearExampleCppStyleAPI("GLOP")
|
|
|
|
|
RunLinearExampleCppStyleAPI("GLPK_LP")
|
|
|
|
|
RunLinearExampleCppStyleAPI("CLP")
|
|
|
|
|
RunLinearExampleCppStyleAPI("PDLP")
|
2023-11-20 12:43:41 +01:00
|
|
|
RunLinearExampleCppStyleAPI("XPRESS_LP")
|
2018-10-12 11:47:16 +02:00
|
|
|
|
2011-11-03 10:27:53 +00:00
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
if __name__ == "__main__":
|
2018-11-11 09:39:59 +01:00
|
|
|
main()
|