158 lines
5.1 KiB
Plaintext
158 lines
5.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2022 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "apache",
|
|
"metadata": {},
|
|
"source": [
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "basename",
|
|
"metadata": {},
|
|
"source": [
|
|
"# mip_var_array"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "link",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/linear_solver/mip_var_array.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/master/ortools/linear_solver/samples/mip_var_array.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "doc",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "install",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "description",
|
|
"metadata": {},
|
|
"source": [
|
|
"MIP example that uses a variable array."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\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",
|
|
"\n",
|
|
"def main():\n",
|
|
" data = create_data_model()\n",
|
|
" # Create the mip solver with the SCIP backend.\n",
|
|
" solver = pywraplp.Solver.CreateSolver('SCIP')\n",
|
|
"\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",
|
|
"\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",
|
|
"\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",
|
|
"\n",
|
|
" status = solver.Solve()\n",
|
|
"\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",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|