Files
ortools-clone/examples/notebook/contrib/least_square.ipynb
2020-11-18 11:44:51 +01:00

166 lines
5.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Copyright 2020 Google LLC."
]
},
{
"cell_type": "markdown",
"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",
"metadata": {},
"source": [
"# least_square"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/contrib/least_square.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/examples/contrib/least_square.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",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2011 Hakan Kjellerstrand hakank@gmail.com\n",
"#\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",
"\"\"\"\n",
"\n",
" Least square optimization problem in Google or-tools.\n",
"\n",
" Solving a fourth grade least square equation.\n",
"\n",
" From the Swedish book 'Optimeringslara' [Optimization Theory],\n",
" page 286f.\n",
"\n",
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
" Also see my other Google CP Solver models:\n",
" http://www.hakank.org/google_or_tools/\n",
"\"\"\"\n",
"from __future__ import print_function\n",
"import sys\n",
"from ortools.linear_solver import pywraplp\n",
"\n",
"\n",
"\n",
"# Create the solver.\n",
"\n",
"# using GLPK\n",
"if sol == 'GLPK':\n",
" solver = pywraplp.Solver('CoinsGridGLPK',\n",
" pywraplp.Solver.GLPK_LINEAR_PROGRAMMING)\n",
"else:\n",
" # Using CLP\n",
" solver = pywraplp.Solver('CoinsGridCLP',\n",
" pywraplp.Solver.CLP_LINEAR_PROGRAMMING)\n",
"\n",
"# data\n",
"# number of points\n",
"num = 14\n",
"\n",
"# temperature\n",
"t = [20, 30, 80, 125, 175, 225, 275, 325, 360, 420, 495, 540, 630, 700]\n",
"\n",
"# percentage gas\n",
"F = [\n",
" 0.0, 5.8, 14.7, 31.6, 43.2, 58.3, 78.4, 89.4, 96.4, 99.1, 99.5, 99.9,\n",
" 100.0, 100.0\n",
"]\n",
"\n",
"p = 4\n",
"\n",
"#\n",
"# declare variables\n",
"#\n",
"a = [solver.NumVar(-100, 100, 'a[%i]' % i) for i in range(p + 1)]\n",
"\n",
"# to minimize\n",
"z = solver.Sum([\n",
" (F[i] - (sum([a[j] * t[i]**j for j in range(p + 1)]))) for i in range(num)\n",
"])\n",
"\n",
"#\n",
"# constraints\n",
"#\n",
"solver.Add(solver.Sum([20**i * a[i] for i in range(p + 1)]) == 0)\n",
"\n",
"solver.Add((a[0] + sum([700.0**j * a[j] for j in range(1, p + 1)])) == 100.0)\n",
"\n",
"for i in range(num):\n",
" solver.Add(\n",
" solver.Sum([j * a[j] * t[i]**(j - 1) for j in range(p + 1)]) >= 0)\n",
"\n",
"objective = solver.Minimize(z)\n",
"\n",
"solver.Solve()\n",
"\n",
"print()\n",
"print('z = ', solver.Objective().Value())\n",
"for i in range(p + 1):\n",
" print(a[i].SolutionValue(), end=' ')\n",
"print()\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}