{ "cells": [ { "cell_type": "markdown", "id": "baa490fa", "metadata": {}, "source": [ "##### Copyright 2021 Google LLC." ] }, { "cell_type": "markdown", "id": "fa0e1ad5", "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": "ec772168", "metadata": {}, "source": [ "# mip_var_array" ] }, { "cell_type": "markdown", "id": "962e2b78", "metadata": {}, "source": [ "\n", "\n", "\n", "
\n", "Run in Google Colab\n", "\n", "View source on GitHub\n", "
" ] }, { "cell_type": "markdown", "id": "99e6a688", "metadata": {}, "source": [ "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab." ] }, { "cell_type": "code", "execution_count": null, "id": "85f6ab45", "metadata": {}, "outputs": [], "source": [ "!pip install ortools" ] }, { "cell_type": "code", "execution_count": null, "id": "a7c0dbe8", "metadata": {}, "outputs": [], "source": [ "#!/usr/bin/env python3\n", "# Copyright 2010-2021 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 ortools.linear_solver import pywraplp\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", "# [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 SCIP backend.\n", "solver = pywraplp.Solver.CreateSolver('SCIP')\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": 5 }