96 lines
3.5 KiB
Plaintext
96 lines
3.5 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",
|
|
"\"\"\"Linear optimization example.\"\"\"\n",
|
|
"# [START program]\n",
|
|
"from __future__ import print_function\n",
|
|
"# [START import]\n",
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"# [END import]\n",
|
|
"\n",
|
|
"\n",
|
|
"def LinearProgrammingExample():\n",
|
|
" \"\"\"Linear programming sample.\"\"\"\n",
|
|
" # Instantiate a Glop solver, naming it LinearExample.\n",
|
|
" # [START solver]\n",
|
|
" solver = pywraplp.Solver('LinearProgrammingExample',\n",
|
|
" pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)\n",
|
|
" # [END solver]\n",
|
|
"\n",
|
|
" # Create the two variables and let them take on any non-negative value.\n",
|
|
" # [START variables]\n",
|
|
" x = solver.NumVar(0, solver.infinity(), 'x')\n",
|
|
" y = solver.NumVar(0, solver.infinity(), 'y')\n",
|
|
" # [END variables]\n",
|
|
"\n",
|
|
" # [START constraints]\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",
|
|
" # [END constraints]\n",
|
|
"\n",
|
|
" # [START objective]\n",
|
|
" # Objective function: 3x + 4y.\n",
|
|
" objective = solver.Objective()\n",
|
|
" objective.SetCoefficient(x, 3)\n",
|
|
" objective.SetCoefficient(y, 4)\n",
|
|
" objective.SetMaximization()\n",
|
|
" # [END objective]\n",
|
|
"\n",
|
|
" # Solve the system.\n",
|
|
" # [START solve]\n",
|
|
" solver.Solve()\n",
|
|
" # [END solve]\n",
|
|
" # [START print_solution]\n",
|
|
" opt_solution = 3 * x.solution_value() + 4 * y.solution_value()\n",
|
|
" print('Number of variables =', solver.NumVariables())\n",
|
|
" print('Number of constraints =', solver.NumConstraints())\n",
|
|
" # The value of each variable in the solution.\n",
|
|
" print('Solution:')\n",
|
|
" print('x = ', x.solution_value())\n",
|
|
" print('y = ', y.solution_value())\n",
|
|
" # The objective value of the solution.\n",
|
|
" print('Optimal objective value =', opt_solution)\n",
|
|
" # [END print_solution]\n",
|
|
"\n",
|
|
"\n",
|
|
"LinearProgrammingExample()\n",
|
|
"# [END program]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|