Files
ortools-clone/ortools/linear_solver/samples/mip_var_array.py

108 lines
3.4 KiB
Python
Raw Normal View History

2021-04-16 00:21:07 +02:00
#!/usr/bin/env python3
2022-06-17 08:40:20 +02:00
# Copyright 2010-2022 Google LLC
# 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.
"""MIP example that uses a variable array."""
# [START program]
# [START import]
from ortools.linear_solver import pywraplp
# [END import]
2020-03-09 17:52:25 +01:00
# [START program_part1]
# [START data_model]
def create_data_model():
2019-11-19 14:37:31 -08:00
"""Stores the data for the problem."""
data = {}
data["constraint_coeffs"] = [
2019-11-19 14:37:31 -08:00
[5, 7, 9, 2, 1],
[18, 4, -9, 10, 12],
[4, 7, 3, 8, 5],
[5, 13, 16, 3, -7],
]
data["bounds"] = [250, 285, 211, 315]
data["obj_coeffs"] = [7, 8, 2, 9, 6]
data["num_vars"] = 5
data["num_constraints"] = 4
2019-11-19 14:37:31 -08:00
return data
# [END data_model]
def main():
2019-11-19 14:37:31 -08:00
# [START data]
data = create_data_model()
# [END data]
2020-03-09 17:52:25 +01:00
# [END program_part1]
2019-11-19 14:37:31 -08:00
# [START solver]
# Create the mip solver with the SCIP backend.
solver = pywraplp.Solver.CreateSolver("SCIP")
if not solver:
return
# [END solver]
2020-03-09 17:52:25 +01:00
# [START program_part2]
2019-11-19 14:37:31 -08:00
# [START variables]
infinity = solver.infinity()
x = {}
for j in range(data["num_vars"]):
x[j] = solver.IntVar(0, infinity, "x[%i]" % j)
print("Number of variables =", solver.NumVariables())
2019-11-19 14:37:31 -08:00
# [END variables]
2019-11-19 14:37:31 -08:00
# [START constraints]
for i in range(data["num_constraints"]):
constraint = solver.RowConstraint(0, data["bounds"][i], "")
for j in range(data["num_vars"]):
constraint.SetCoefficient(x[j], data["constraint_coeffs"][i][j])
print("Number of constraints =", solver.NumConstraints())
2019-11-19 14:37:31 -08:00
# In Python, you can also set the constraints as follows.
# for i in range(data['num_constraints']):
# constraint_expr = \
# [data['constraint_coeffs'][i][j] * x[j] for j in range(data['num_vars'])]
# solver.Add(sum(constraint_expr) <= data['bounds'][i])
# [END constraints]
2019-11-19 14:37:31 -08:00
# [START objective]
objective = solver.Objective()
for j in range(data["num_vars"]):
objective.SetCoefficient(x[j], data["obj_coeffs"][j])
2019-11-19 14:37:31 -08:00
objective.SetMaximization()
# In Python, you can also set the objective as follows.
# obj_expr = [data['obj_coeffs'][j] * x[j] for j in range(data['num_vars'])]
# solver.Maximize(solver.Sum(obj_expr))
# [END objective]
# [START solve]
status = solver.Solve()
# [END solve]
# [START print_solution]
if status == pywraplp.Solver.OPTIMAL:
print("Objective value =", solver.Objective().Value())
for j in range(data["num_vars"]):
print(x[j].name(), " = ", x[j].solution_value())
2019-11-19 14:37:31 -08:00
print()
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())
2019-11-19 14:37:31 -08:00
else:
print("The problem does not have an optimal solution.")
2019-11-19 14:37:31 -08:00
# [END print_solution]
if __name__ == "__main__":
2019-11-19 14:37:31 -08:00
main()
2020-03-09 17:52:25 +01:00
# [END program_part2]
# [END program]