Files
ortools-clone/examples/notebook/linear_solver/mip_var_array.ipynb
Corentin Le Molgat 5ff76b487a Update notebook
2020-04-23 15:42:13 +02:00

117 lines
4.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010-2018 Google LLC\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# http://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License.\n",
"\"\"\"MIP example that uses a variable array.\"\"\"\n",
"# [START program]\n",
"# [START import]\n",
"from __future__ import print_function\n",
"from ortools.linear_solver import pywraplp\n",
"\n",
"# [END import]\n",
"\n",
"\n",
"# [START program_part1]\n",
"# [START data_model]\n",
"def create_data_model():\n",
" \"\"\"Stores the data for the problem.\"\"\"\n",
" data = {}\n",
" data['constraint_coeffs'] = [\n",
" [5, 7, 9, 2, 1],\n",
" [18, 4, -9, 10, 12],\n",
" [4, 7, 3, 8, 5],\n",
" [5, 13, 16, 3, -7],\n",
" ]\n",
" data['bounds'] = [250, 285, 211, 315]\n",
" data['obj_coeffs'] = [7, 8, 2, 9, 6]\n",
" data['num_vars'] = 5\n",
" data['num_constraints'] = 4\n",
" return data\n",
"\n",
"\n",
"# [END data_model]\n",
"\n",
"\n",
"# [START data]\n",
"data = create_data_model()\n",
"# [END data]\n",
"# [END program_part1]\n",
"# [START solver]\n",
"# Create the mip solver with the CBC backend.\n",
"solver = pywraplp.Solver('simple_mip_program',\n",
" pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)\n",
"# [END solver]\n",
"\n",
"# [START program_part2]\n",
"# [START variables]\n",
"infinity = solver.infinity()\n",
"x = {}\n",
"for j in range(data['num_vars']):\n",
" x[j] = solver.IntVar(0, infinity, 'x[%i]' % j)\n",
"print('Number of variables =', solver.NumVariables())\n",
"# [END variables]\n",
"\n",
"# [START constraints]\n",
"for i in range(data['num_constraints']):\n",
" constraint = solver.RowConstraint(0, data['bounds'][i], '')\n",
" for j in range(data['num_vars']):\n",
" constraint.SetCoefficient(x[j], data['constraint_coeffs'][i][j])\n",
"print('Number of constraints =', solver.NumConstraints())\n",
"# In Python, you can also set the constraints as follows.\n",
"# for i in range(data['num_constraints']):\n",
"# constraint_expr = \\\n",
"# [data['constraint_coeffs'][i][j] * x[j] for j in range(data['num_vars'])]\n",
"# solver.Add(sum(constraint_expr) <= data['bounds'][i])\n",
"# [END constraints]\n",
"\n",
"# [START objective]\n",
"objective = solver.Objective()\n",
"for j in range(data['num_vars']):\n",
" objective.SetCoefficient(x[j], data['obj_coeffs'][j])\n",
"objective.SetMaximization()\n",
"# In Python, you can also set the objective as follows.\n",
"# obj_expr = [data['obj_coeffs'][j] * x[j] for j in range(data['num_vars'])]\n",
"# solver.Maximize(solver.Sum(obj_expr))\n",
"# [END objective]\n",
"\n",
"# [START solve]\n",
"status = solver.Solve()\n",
"# [END solve]\n",
"\n",
"# [START print_solution]\n",
"if status == pywraplp.Solver.OPTIMAL:\n",
" print('Objective value =', solver.Objective().Value())\n",
" for j in range(data['num_vars']):\n",
" print(x[j].name(), ' = ', x[j].solution_value())\n",
" print()\n",
" print('Problem solved in %f milliseconds' % solver.wall_time())\n",
" print('Problem solved in %d iterations' % solver.iterations())\n",
" print('Problem solved in %d branch-and-bound nodes' % solver.nodes())\n",
"else:\n",
" print('The problem does not have an optimal solution.')\n",
"# [END print_solution]\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}