Files
ortools-clone/examples/notebook/examples/linear_programming.ipynb
Corentin Le Molgat 27121a1068 Update examples/notebook
generated using ./tools/gen_all_notebook.sh
2020-03-04 14:34:33 +01:00

97 lines
3.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python\n",
"# This Python file uses the following encoding: utf-8\n",
"# Copyright 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",
"\"\"\"Linear optimization example\"\"\"\n",
"\n",
"from __future__ import print_function\n",
"from ortools.linear_solver import pywraplp\n",
"\n",
"\n",
"\"\"\"Entry point of the program\"\"\"\n",
"# Instantiate a Glop solver, naming it LinearExample.\n",
"solver = pywraplp.Solver('LinearExample',\n",
" pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)\n",
"\n",
"# Create the two variables and let them take on any value.\n",
"x = solver.NumVar(-solver.infinity(), solver.infinity(), 'x')\n",
"y = solver.NumVar(-solver.infinity(), solver.infinity(), 'y')\n",
"\n",
"# Objective function: Maximize 3x + 4y.\n",
"objective = solver.Objective()\n",
"objective.SetCoefficient(x, 3)\n",
"objective.SetCoefficient(y, 4)\n",
"objective.SetMaximization()\n",
"\n",
"# Constraint 0: x + 2y <= 14.\n",
"constraint0 = solver.Constraint(-solver.infinity(), 14)\n",
"constraint0.SetCoefficient(x, 1)\n",
"constraint0.SetCoefficient(y, 2)\n",
"\n",
"# Constraint 1: 3x - y >= 0.\n",
"constraint1 = solver.Constraint(0, solver.infinity())\n",
"constraint1.SetCoefficient(x, 3)\n",
"constraint1.SetCoefficient(y, -1)\n",
"\n",
"# Constraint 2: x - y <= 2.\n",
"constraint2 = solver.Constraint(-solver.infinity(), 2)\n",
"constraint2.SetCoefficient(x, 1)\n",
"constraint2.SetCoefficient(y, -1)\n",
"\n",
"print('Number of variables =', solver.NumVariables())\n",
"print('Number of constraints =', solver.NumConstraints())\n",
"\n",
"# Solve the system.\n",
"status = solver.Solve()\n",
"# Check that the problem has an optimal solution.\n",
"if status != pywraplp.Solver.OPTIMAL:\n",
" print(\"The problem does not have an optimal solution!\")\n",
" exit(1)\n",
"\n",
"print('Solution:')\n",
"print('x =', x.solution_value())\n",
"print('y =', y.solution_value())\n",
"print('Optimal objective value =', objective.Value())\n",
"print('')\n",
"print('Advanced usage:')\n",
"print('Problem solved in ', solver.wall_time(), ' milliseconds')\n",
"print('Problem solved in ', solver.iterations(), ' iterations')\n",
"print('x: reduced cost =', x.reduced_cost())\n",
"print('y: reduced cost =', y.reduced_cost())\n",
"activities = solver.ComputeConstraintActivities()\n",
"print('constraint0: dual value =',\n",
" constraint0.dual_value(), ' activities =',\n",
" activities[constraint0.index()])\n",
"print('constraint1: dual value =',\n",
" constraint1.dual_value(), ' activities =',\n",
" activities[constraint1.index()])\n",
"print('constraint2: dual value =',\n",
" constraint2.dual_value(), ' activities =',\n",
" activities[constraint2.index()])\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}