2021-12-07 09:13:09 +01:00
|
|
|
#!/usr/bin/env python3
|
2025-01-10 11:33:35 +01:00
|
|
|
# Copyright 2010-2025 Google LLC
|
2018-10-12 11:47:16 +02: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.
|
2019-06-29 17:20:42 +02:00
|
|
|
"""Tests for ortools.linear_solver.pywraplp."""
|
|
|
|
|
|
|
|
|
|
import unittest
|
2022-02-14 10:25:55 +01:00
|
|
|
from google.protobuf import text_format
|
2018-10-12 11:47:16 +02:00
|
|
|
from ortools.linear_solver import linear_solver_pb2
|
|
|
|
|
from ortools.linear_solver import pywraplp
|
|
|
|
|
|
|
|
|
|
|
2019-06-29 17:20:42 +02:00
|
|
|
class PyWrapLpTest(unittest.TestCase):
|
|
|
|
|
def RunLinearExampleNaturalLanguageAPI(self, optimization_problem_type):
|
|
|
|
|
"""Example of simple linear program with natural language API."""
|
2024-04-11 10:56:30 +02:00
|
|
|
solver = pywraplp.Solver(
|
|
|
|
|
"RunLinearExampleNaturalLanguageAPI", optimization_problem_type
|
|
|
|
|
)
|
2019-06-29 17:20:42 +02:00
|
|
|
infinity = solver.infinity()
|
|
|
|
|
# x1, x2 and x3 are continuous non-negative variables.
|
2024-04-11 10:56:30 +02:00
|
|
|
x1 = solver.NumVar(0.0, infinity, "x1")
|
|
|
|
|
x2 = solver.NumVar(0.0, infinity, "x2")
|
|
|
|
|
x3 = solver.NumVar(0.0, infinity, "x3")
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
solver.Maximize(10 * x1 + 6 * x2 + 4 * x3)
|
2024-04-11 10:56:30 +02:00
|
|
|
c0 = solver.Add(10 * x1 + 4 * x2 + 5 * x3 <= 600, "ConstraintName0")
|
2019-06-29 17:20:42 +02:00
|
|
|
c1 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300)
|
|
|
|
|
sum_of_vars = sum([x1, x2, x3])
|
2024-04-11 10:56:30 +02:00
|
|
|
c2 = solver.Add(sum_of_vars <= 100.0, "OtherConstraintName")
|
2019-06-29 17:20:42 +02:00
|
|
|
|
2024-04-11 10:56:30 +02:00
|
|
|
self.SolveAndPrint(
|
|
|
|
|
solver,
|
|
|
|
|
[x1, x2, x3],
|
|
|
|
|
[c0, c1, c2],
|
|
|
|
|
optimization_problem_type != pywraplp.Solver.PDLP_LINEAR_PROGRAMMING,
|
|
|
|
|
)
|
2022-02-26 01:07:23 +01:00
|
|
|
|
2019-06-29 17:20:42 +02:00
|
|
|
# Print a linear expression's solution value.
|
2024-04-11 10:56:30 +02:00
|
|
|
print(("Sum of vars: %s = %s" % (sum_of_vars, sum_of_vars.solution_value())))
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
def RunLinearExampleCppStyleAPI(self, optimization_problem_type):
|
|
|
|
|
"""Example of simple linear program with the C++ style API."""
|
2024-04-11 10:56:30 +02:00
|
|
|
solver = pywraplp.Solver("RunLinearExampleCppStyle", optimization_problem_type)
|
2019-06-29 17:20:42 +02:00
|
|
|
infinity = solver.infinity()
|
|
|
|
|
# x1, x2 and x3 are continuous non-negative variables.
|
2024-04-11 10:56:30 +02:00
|
|
|
x1 = solver.NumVar(0.0, infinity, "x1")
|
|
|
|
|
x2 = solver.NumVar(0.0, infinity, "x2")
|
|
|
|
|
x3 = solver.NumVar(0.0, infinity, "x3")
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
# Maximize 10 * x1 + 6 * x2 + 4 * x3.
|
|
|
|
|
objective = solver.Objective()
|
|
|
|
|
objective.SetCoefficient(x1, 10)
|
|
|
|
|
objective.SetCoefficient(x2, 6)
|
|
|
|
|
objective.SetCoefficient(x3, 4)
|
|
|
|
|
objective.SetMaximization()
|
|
|
|
|
|
|
|
|
|
# x1 + x2 + x3 <= 100.
|
2024-04-11 10:56:30 +02:00
|
|
|
c0 = solver.Constraint(-infinity, 100.0, "c0")
|
2019-06-29 17:20:42 +02:00
|
|
|
c0.SetCoefficient(x1, 1)
|
|
|
|
|
c0.SetCoefficient(x2, 1)
|
|
|
|
|
c0.SetCoefficient(x3, 1)
|
|
|
|
|
|
|
|
|
|
# 10 * x1 + 4 * x2 + 5 * x3 <= 600.
|
2024-04-11 10:56:30 +02:00
|
|
|
c1 = solver.Constraint(-infinity, 600.0, "c1")
|
2019-06-29 17:20:42 +02:00
|
|
|
c1.SetCoefficient(x1, 10)
|
|
|
|
|
c1.SetCoefficient(x2, 4)
|
|
|
|
|
c1.SetCoefficient(x3, 5)
|
|
|
|
|
|
|
|
|
|
# 2 * x1 + 2 * x2 + 6 * x3 <= 300.
|
2024-04-11 10:56:30 +02:00
|
|
|
c2 = solver.Constraint(-infinity, 300.0, "c2")
|
2019-06-29 17:20:42 +02:00
|
|
|
c2.SetCoefficient(x1, 2)
|
|
|
|
|
c2.SetCoefficient(x2, 2)
|
|
|
|
|
c2.SetCoefficient(x3, 6)
|
|
|
|
|
|
2024-04-11 10:56:30 +02:00
|
|
|
self.SolveAndPrint(
|
|
|
|
|
solver,
|
|
|
|
|
[x1, x2, x3],
|
|
|
|
|
[c0, c1, c2],
|
|
|
|
|
optimization_problem_type != pywraplp.Solver.PDLP_LINEAR_PROGRAMMING,
|
|
|
|
|
)
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
def RunMixedIntegerExampleCppStyleAPI(self, optimization_problem_type):
|
|
|
|
|
"""Example of simple mixed integer program with the C++ style API."""
|
2024-04-11 10:56:30 +02:00
|
|
|
solver = pywraplp.Solver(
|
|
|
|
|
"RunMixedIntegerExampleCppStyle", optimization_problem_type
|
|
|
|
|
)
|
2019-06-29 17:20:42 +02:00
|
|
|
infinity = solver.infinity()
|
|
|
|
|
# x1 and x2 are integer non-negative variables.
|
2024-04-11 10:56:30 +02:00
|
|
|
x1 = solver.IntVar(0.0, infinity, "x1")
|
|
|
|
|
x2 = solver.IntVar(0.0, infinity, "x2")
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
# Maximize x1 + 10 * x2.
|
|
|
|
|
objective = solver.Objective()
|
|
|
|
|
objective.SetCoefficient(x1, 1)
|
|
|
|
|
objective.SetCoefficient(x2, 10)
|
|
|
|
|
objective.SetMaximization()
|
|
|
|
|
|
|
|
|
|
# x1 + 7 * x2 <= 17.5.
|
2024-04-11 10:56:30 +02:00
|
|
|
c0 = solver.Constraint(-infinity, 17.5, "c0")
|
2019-06-29 17:20:42 +02:00
|
|
|
c0.SetCoefficient(x1, 1)
|
|
|
|
|
c0.SetCoefficient(x2, 7)
|
|
|
|
|
|
|
|
|
|
# x1 <= 3.5.
|
2024-04-11 10:56:30 +02:00
|
|
|
c1 = solver.Constraint(-infinity, 3.5, "c1")
|
2019-06-29 17:20:42 +02:00
|
|
|
c1.SetCoefficient(x1, 1)
|
|
|
|
|
c1.SetCoefficient(x2, 0)
|
|
|
|
|
|
2022-02-26 01:07:23 +01:00
|
|
|
self.SolveAndPrint(solver, [x1, x2], [c0, c1], True)
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
def RunBooleanExampleCppStyleAPI(self, optimization_problem_type):
|
|
|
|
|
"""Example of simple boolean program with the C++ style API."""
|
2024-04-11 10:56:30 +02:00
|
|
|
solver = pywraplp.Solver("RunBooleanExampleCppStyle", optimization_problem_type)
|
2019-06-29 17:20:42 +02:00
|
|
|
# x1 and x2 are integer non-negative variables.
|
2024-04-11 10:56:30 +02:00
|
|
|
x1 = solver.BoolVar("x1")
|
|
|
|
|
x2 = solver.BoolVar("x2")
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
# Minimize 2 * x1 + x2.
|
|
|
|
|
objective = solver.Objective()
|
|
|
|
|
objective.SetCoefficient(x1, 2)
|
|
|
|
|
objective.SetCoefficient(x2, 1)
|
|
|
|
|
objective.SetMinimization()
|
|
|
|
|
|
|
|
|
|
# 1 <= x1 + 2 * x2 <= 3.
|
2024-04-11 10:56:30 +02:00
|
|
|
c0 = solver.Constraint(1, 3, "c0")
|
2019-06-29 17:20:42 +02:00
|
|
|
c0.SetCoefficient(x1, 1)
|
|
|
|
|
c0.SetCoefficient(x2, 2)
|
|
|
|
|
|
2022-02-26 01:07:23 +01:00
|
|
|
self.SolveAndPrint(solver, [x1, x2], [c0], True)
|
2019-06-29 17:20:42 +02:00
|
|
|
|
2022-02-26 01:07:23 +01:00
|
|
|
def SolveAndPrint(self, solver, variable_list, constraint_list, is_precise):
|
2019-06-29 17:20:42 +02:00
|
|
|
"""Solve the problem and print the solution."""
|
2024-04-11 10:56:30 +02:00
|
|
|
print(("Number of variables = %d" % solver.NumVariables()))
|
2020-10-08 13:35:27 +02:00
|
|
|
self.assertEqual(solver.NumVariables(), len(variable_list))
|
|
|
|
|
|
2024-04-11 10:56:30 +02:00
|
|
|
print(("Number of constraints = %d" % solver.NumConstraints()))
|
2020-10-08 13:35:27 +02:00
|
|
|
self.assertEqual(solver.NumConstraints(), len(constraint_list))
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
result_status = solver.Solve()
|
|
|
|
|
|
|
|
|
|
# The problem has an optimal solution.
|
2020-10-08 13:35:27 +02:00
|
|
|
self.assertEqual(result_status, pywraplp.Solver.OPTIMAL)
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
# The solution looks legit (when using solvers others than
|
|
|
|
|
# GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!).
|
2022-02-26 01:07:23 +01:00
|
|
|
if is_precise:
|
|
|
|
|
self.assertTrue(solver.VerifySolution(1e-7, True))
|
2019-06-29 17:20:42 +02:00
|
|
|
|
2024-04-11 10:56:30 +02:00
|
|
|
print(("Problem solved in %f milliseconds" % solver.wall_time()))
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
# The objective value of the solution.
|
2024-04-11 10:56:30 +02:00
|
|
|
print(("Optimal objective value = %f" % solver.Objective().Value()))
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
# The value of each variable in the solution.
|
|
|
|
|
for variable in variable_list:
|
2024-04-11 10:56:30 +02:00
|
|
|
print(("%s = %f" % (variable.name(), variable.solution_value())))
|
2019-06-29 17:20:42 +02:00
|
|
|
|
2024-04-11 10:56:30 +02:00
|
|
|
print("Advanced usage:")
|
|
|
|
|
print(("Problem solved in %d iterations" % solver.iterations()))
|
2024-01-31 15:18:13 +01:00
|
|
|
if not solver.IsMip():
|
|
|
|
|
for variable in variable_list:
|
2024-04-11 10:56:30 +02:00
|
|
|
print(
|
|
|
|
|
(
|
|
|
|
|
"%s: reduced cost = %f"
|
|
|
|
|
% (variable.name(), variable.reduced_cost())
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-01-31 15:18:13 +01:00
|
|
|
activities = solver.ComputeConstraintActivities()
|
|
|
|
|
for i, constraint in enumerate(constraint_list):
|
|
|
|
|
print(
|
2024-04-11 10:56:30 +02:00
|
|
|
(
|
|
|
|
|
"constraint %d: dual value = %f\n"
|
|
|
|
|
" activity = %f"
|
|
|
|
|
% (i, constraint.dual_value(), activities[constraint.index()])
|
|
|
|
|
)
|
|
|
|
|
)
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
def testApi(self):
|
2024-04-11 10:56:30 +02:00
|
|
|
print("testApi", flush=True)
|
|
|
|
|
all_names_and_problem_types = list(
|
|
|
|
|
linear_solver_pb2.MPModelRequest.SolverType.items()
|
|
|
|
|
)
|
2019-06-29 17:20:42 +02:00
|
|
|
for name, problem_type in all_names_and_problem_types:
|
2024-04-11 10:56:30 +02:00
|
|
|
with self.subTest(f"{name}: {problem_type}"):
|
|
|
|
|
print(f"######## {name}:{problem_type} #######", flush=True)
|
2020-01-23 16:48:29 +01:00
|
|
|
if not pywraplp.Solver.SupportsProblemType(problem_type):
|
|
|
|
|
continue
|
2024-04-11 10:56:30 +02:00
|
|
|
if name.startswith("GUROBI"):
|
2020-01-23 16:48:29 +01:00
|
|
|
continue
|
2024-04-11 10:56:30 +02:00
|
|
|
if name.startswith("KNAPSACK"):
|
2022-09-09 16:49:24 +02:00
|
|
|
continue
|
2024-04-11 10:56:30 +02:00
|
|
|
if not name.startswith("SCIP"):
|
2022-09-09 16:49:24 +02:00
|
|
|
continue
|
2024-04-11 10:56:30 +02:00
|
|
|
if name.endswith("LINEAR_PROGRAMMING"):
|
|
|
|
|
print(("\n------ Linear programming example with %s ------" % name))
|
|
|
|
|
print("\n*** Natural language API ***")
|
2020-01-23 16:48:29 +01:00
|
|
|
self.RunLinearExampleNaturalLanguageAPI(problem_type)
|
2024-04-11 10:56:30 +02:00
|
|
|
print("\n*** C++ style API ***")
|
2020-01-23 16:48:29 +01:00
|
|
|
self.RunLinearExampleCppStyleAPI(problem_type)
|
2024-04-11 10:56:30 +02:00
|
|
|
elif name.endswith("MIXED_INTEGER_PROGRAMMING"):
|
|
|
|
|
print(
|
|
|
|
|
(
|
|
|
|
|
"\n------ Mixed Integer programming example with %s ------"
|
|
|
|
|
% name
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
print("\n*** C++ style API ***")
|
2020-01-23 16:48:29 +01:00
|
|
|
self.RunMixedIntegerExampleCppStyleAPI(problem_type)
|
2024-04-11 10:56:30 +02:00
|
|
|
elif name.endswith("INTEGER_PROGRAMMING"):
|
|
|
|
|
print(
|
|
|
|
|
("\n------ Boolean programming example with %s ------" % name)
|
|
|
|
|
)
|
|
|
|
|
print("\n*** C++ style API ***")
|
2020-01-23 16:48:29 +01:00
|
|
|
self.RunBooleanExampleCppStyleAPI(problem_type)
|
|
|
|
|
else:
|
2024-04-11 10:56:30 +02:00
|
|
|
print("ERROR: %s unsupported" % name)
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
def testSetHint(self):
|
2024-04-11 10:56:30 +02:00
|
|
|
print("testSetHint", flush=True)
|
|
|
|
|
solver = pywraplp.Solver(
|
|
|
|
|
"RunBooleanExampleCppStyle", pywraplp.Solver.GLOP_LINEAR_PROGRAMMING
|
|
|
|
|
)
|
2019-06-29 17:20:42 +02:00
|
|
|
infinity = solver.infinity()
|
|
|
|
|
# x1 and x2 are integer non-negative variables.
|
2024-04-11 10:56:30 +02:00
|
|
|
x1 = solver.BoolVar("x1")
|
|
|
|
|
x2 = solver.BoolVar("x2")
|
2019-06-29 17:20:42 +02:00
|
|
|
|
|
|
|
|
# Minimize 2 * x1 + x2.
|
|
|
|
|
objective = solver.Objective()
|
|
|
|
|
objective.SetCoefficient(x1, 2)
|
|
|
|
|
objective.SetCoefficient(x2, 1)
|
|
|
|
|
objective.SetMinimization()
|
|
|
|
|
|
|
|
|
|
# 1 <= x1 + 2 * x2 <= 3.
|
2024-04-11 10:56:30 +02:00
|
|
|
c0 = solver.Constraint(1, 3, "c0")
|
2019-06-29 17:20:42 +02:00
|
|
|
c0.SetCoefficient(x1, 1)
|
|
|
|
|
c0.SetCoefficient(x2, 2)
|
|
|
|
|
|
|
|
|
|
solver.SetHint([x1, x2], [1.0, 0.0])
|
|
|
|
|
self.assertEqual(2, len(solver.variables()))
|
|
|
|
|
self.assertEqual(1, len(solver.constraints()))
|
|
|
|
|
|
2019-07-30 17:19:24 -07:00
|
|
|
def testBopInfeasible(self):
|
2024-04-11 10:56:30 +02:00
|
|
|
print("testBopInfeasible", flush=True)
|
|
|
|
|
solver = pywraplp.Solver("test", pywraplp.Solver.BOP_INTEGER_PROGRAMMING)
|
2019-07-30 17:19:24 -07:00
|
|
|
solver.EnableOutput()
|
|
|
|
|
|
|
|
|
|
x = solver.IntVar(0, 10, "")
|
|
|
|
|
solver.Add(x >= 20)
|
|
|
|
|
|
|
|
|
|
result_status = solver.Solve()
|
2024-04-11 10:56:30 +02:00
|
|
|
print(result_status) # outputs: 0
|
2019-07-30 17:19:24 -07:00
|
|
|
|
2022-02-14 10:25:55 +01:00
|
|
|
def testLoadSolutionFromProto(self):
|
2024-04-11 10:56:30 +02:00
|
|
|
print("testLoadSolutionFromProto", flush=True)
|
|
|
|
|
solver = pywraplp.Solver("", pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
|
2020-08-18 17:16:10 +02:00
|
|
|
solver.LoadSolutionFromProto(linear_solver_pb2.MPSolutionResponse())
|
|
|
|
|
|
2022-02-14 10:25:55 +01:00
|
|
|
def testSolveFromProto(self):
|
2024-04-11 10:56:30 +02:00
|
|
|
print("testSolveFromProto", flush=True)
|
|
|
|
|
request_str = """
|
2022-02-14 10:25:55 +01:00
|
|
|
model {
|
|
|
|
|
maximize: false
|
|
|
|
|
objective_offset: 0
|
|
|
|
|
variable {
|
|
|
|
|
lower_bound: 0
|
|
|
|
|
upper_bound: 4
|
|
|
|
|
objective_coefficient: 1
|
|
|
|
|
is_integer: false
|
|
|
|
|
name: "XONE"
|
|
|
|
|
}
|
|
|
|
|
variable {
|
|
|
|
|
lower_bound: -1
|
|
|
|
|
upper_bound: 1
|
|
|
|
|
objective_coefficient: 4
|
|
|
|
|
is_integer: false
|
|
|
|
|
name: "YTWO"
|
|
|
|
|
}
|
|
|
|
|
variable {
|
|
|
|
|
lower_bound: 0
|
|
|
|
|
upper_bound: inf
|
|
|
|
|
objective_coefficient: 9
|
|
|
|
|
is_integer: false
|
|
|
|
|
name: "ZTHREE"
|
|
|
|
|
}
|
|
|
|
|
constraint {
|
|
|
|
|
lower_bound: -inf
|
|
|
|
|
upper_bound: 5
|
|
|
|
|
name: "LIM1"
|
|
|
|
|
var_index: 0
|
|
|
|
|
var_index: 1
|
|
|
|
|
coefficient: 1
|
|
|
|
|
coefficient: 1
|
|
|
|
|
}
|
|
|
|
|
constraint {
|
|
|
|
|
lower_bound: 10
|
|
|
|
|
upper_bound: inf
|
|
|
|
|
name: "LIM2"
|
|
|
|
|
var_index: 0
|
|
|
|
|
var_index: 2
|
|
|
|
|
coefficient: 1
|
|
|
|
|
coefficient: 1
|
|
|
|
|
}
|
|
|
|
|
constraint {
|
|
|
|
|
lower_bound: 7
|
|
|
|
|
upper_bound: 7
|
|
|
|
|
name: "MYEQN"
|
|
|
|
|
var_index: 1
|
|
|
|
|
var_index: 2
|
|
|
|
|
coefficient: -1
|
|
|
|
|
coefficient: 1
|
|
|
|
|
}
|
|
|
|
|
name: "NAME_LONGER_THAN_8_CHARACTERS"
|
|
|
|
|
}
|
|
|
|
|
solver_type: GLOP_LINEAR_PROGRAMMING
|
|
|
|
|
solver_time_limit_seconds: 1.0
|
|
|
|
|
solver_specific_parameters: ""
|
2024-04-11 10:56:30 +02:00
|
|
|
"""
|
2022-02-14 10:25:55 +01:00
|
|
|
request = linear_solver_pb2.MPModelRequest()
|
|
|
|
|
text_format.Parse(request_str, request)
|
|
|
|
|
response = linear_solver_pb2.MPSolutionResponse()
|
2022-02-14 10:37:34 +01:00
|
|
|
self.assertEqual(len(request.model.variable), 3)
|
2022-02-14 10:25:55 +01:00
|
|
|
pywraplp.Solver.SolveWithProto(model_request=request, response=response)
|
2022-02-14 10:37:34 +01:00
|
|
|
self.assertEqual(
|
2024-04-11 10:56:30 +02:00
|
|
|
linear_solver_pb2.MPSolverResponseStatus.MPSOLVER_OPTIMAL, response.status
|
|
|
|
|
)
|
2022-02-14 10:25:55 +01:00
|
|
|
|
2022-07-11 13:41:19 +02:00
|
|
|
def testExportToMps(self):
|
|
|
|
|
"""Test MPS export."""
|
2024-04-11 10:56:30 +02:00
|
|
|
print("testExportToMps", flush=True)
|
|
|
|
|
solver = pywraplp.Solver("ExportMps", pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
|
2022-07-11 13:41:19 +02:00
|
|
|
infinity = solver.infinity()
|
|
|
|
|
# x1, x2 and x3 are continuous non-negative variables.
|
2024-04-11 10:56:30 +02:00
|
|
|
x1 = solver.NumVar(0.0, infinity, "x1")
|
|
|
|
|
x2 = solver.NumVar(0.0, infinity, "x2")
|
|
|
|
|
x3 = solver.NumVar(0.0, infinity, "x3")
|
2022-07-11 13:41:19 +02:00
|
|
|
|
|
|
|
|
solver.Maximize(10 * x1 + 6 * x2 + 4 * x3)
|
2024-04-11 10:56:30 +02:00
|
|
|
c0 = solver.Add(10 * x1 + 4 * x2 + 5 * x3 <= 600, "ConstraintName0")
|
2022-07-11 13:41:19 +02:00
|
|
|
c1 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300)
|
|
|
|
|
sum_of_vars = sum([x1, x2, x3])
|
2024-04-11 10:56:30 +02:00
|
|
|
c2 = solver.Add(sum_of_vars <= 100.0, "OtherConstraintName")
|
2022-07-11 13:41:19 +02:00
|
|
|
|
2024-10-09 12:37:20 +02:00
|
|
|
mps_str = solver.ExportModelAsMpsFormat(fixed_format=False, obfuscate=False)
|
2024-04-11 10:56:30 +02:00
|
|
|
self.assertIn("ExportMps", mps_str)
|
2022-07-11 13:41:19 +02:00
|
|
|
|
2018-10-12 11:47:16 +02:00
|
|
|
|
2024-04-11 10:56:30 +02:00
|
|
|
if __name__ == "__main__":
|
2019-06-29 17:20:42 +02:00
|
|
|
unittest.main()
|