example: fix chemical_balance notebook

note: need to use """ to have our script correctly parsing
This commit is contained in:
Corentin Le Molgat
2022-12-21 09:54:01 +01:00
parent 757a8e884b
commit 928bfbfedb
4 changed files with 98 additions and 106 deletions

View File

@@ -67,6 +67,18 @@
"!pip install ortools"
]
},
{
"cell_type": "markdown",
"id": "description",
"metadata": {},
"source": [
"We are trying to group items in equal sized groups.\n",
"Each item has a color and a value. We want the sum of values of each group to\n",
"be as close to the average as possible.\n",
"Furthermore, if one color is an a group, at least k items with this color must\n",
"be in that group.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -74,72 +86,60 @@
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010-2022 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",
"\n",
"# We are trying to group items in equal sized groups.\n",
"# Each item has a color and a value. We want the sum of values of each group to\n",
"# be as close to the average as possible.\n",
"# Furthermore, if one color is an a group, at least k items with this color must\n",
"# be in that group.\n",
"\n",
"\n",
"from ortools.linear_solver import pywraplp\n",
"\n",
"import math\n",
"\n",
"# Data\n",
"\n",
"max_quantities = [[\"N_Total\", 1944], [\"P2O5\", 1166.4], [\"K2O\", 1822.5],\n",
" [\"CaO\", 1458], [\"MgO\", 486], [\"Fe\", 9.7], [\"B\", 2.4]]\n",
"max_quantities = [\n",
" [\"N_Total\", 1944],\n",
" [\"P2O5\", 1166.4],\n",
" [\"K2O\", 1822.5],\n",
" [\"CaO\", 1458],\n",
" [\"MgO\", 486],\n",
" [\"Fe\", 9.7],\n",
" [\"B\", 2.4],\n",
"]\n",
"\n",
"chemical_set = [[\"A\", 0, 0, 510, 540, 0, 0, 0], [\"B\", 110, 0, 0, 0, 160, 0, 0],\n",
" [\"C\", 61, 149, 384, 0, 30, 1,\n",
" 0.2], [\"D\", 148, 70, 245, 0, 15, 1,\n",
" 0.2], [\"E\", 160, 158, 161, 0, 10, 1, 0.2]]\n",
"chemical_set = [\n",
" [\"A\", 0, 0, 510, 540, 0, 0, 0],\n",
" [\"B\", 110, 0, 0, 0, 160, 0, 0],\n",
" [\"C\", 61, 149, 384, 0, 30, 1, 0.2],\n",
" [\"D\", 148, 70, 245, 0, 15, 1, 0.2],\n",
" [\"E\", 160, 158, 161, 0, 10, 1, 0.2],\n",
"]\n",
"\n",
"num_products = len(max_quantities)\n",
"all_products = range(num_products)\n",
"NUM_PRODUCTS = len(max_quantities)\n",
"ALL_PRODUCTS = range(NUM_PRODUCTS)\n",
"\n",
"num_sets = len(chemical_set)\n",
"all_sets = range(num_sets)\n",
"NUM_SETS = len(chemical_set)\n",
"ALL_SETS = range(NUM_SETS)\n",
"\n",
"# Model\n",
"\n",
"max_set = [\n",
" min(max_quantities[q][1] / chemical_set[s][q + 1] for q in all_products\n",
" if chemical_set[s][q + 1] != 0.0) for s in all_sets\n",
" min(max_quantities[q][1] / chemical_set[s][q + 1] for q in ALL_PRODUCTS\n",
" if chemical_set[s][q + 1] != 0.0) for s in ALL_SETS\n",
"]\n",
"\n",
"solver = pywraplp.Solver(\"chemical_set_lp\",\n",
" pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)\n",
"\n",
"set_vars = [solver.NumVar(0, max_set[s], \"set_%i\" % s) for s in all_sets]\n",
"set_vars = [solver.NumVar(0, max_set[s], f\"set_{s}\") for s in ALL_SETS]\n",
"\n",
"epsilon = solver.NumVar(0, 1000, \"epsilon\")\n",
"\n",
"for p in all_products:\n",
"for p in ALL_PRODUCTS:\n",
" solver.Add(\n",
" sum(chemical_set[s][p + 1] * set_vars[s]\n",
" for s in all_sets) <= max_quantities[p][1])\n",
" for s in ALL_SETS) <= max_quantities[p][1])\n",
" solver.Add(\n",
" sum(chemical_set[s][p + 1] * set_vars[s]\n",
" for s in all_sets) >= max_quantities[p][1] - epsilon)\n",
" for s in ALL_SETS) >= max_quantities[p][1] - epsilon)\n",
"\n",
"solver.Minimize(epsilon)\n",
"\n",
"print((\"Number of variables = %d\" % solver.NumVariables()))\n",
"print((\"Number of constraints = %d\" % solver.NumConstraints()))\n",
"print(f\"Number of variables = {solver.NumVariables()}\")\n",
"print(f\"Number of constraints = {solver.NumConstraints()}\")\n",
"\n",
"result_status = solver.Solve()\n",
"\n",
@@ -148,22 +148,20 @@
"\n",
"assert solver.VerifySolution(1e-7, True)\n",
"\n",
"print((\"Problem solved in %f milliseconds\" % solver.wall_time()))\n",
"print(f\"Problem solved in {solver.wall_time()} milliseconds\")\n",
"\n",
"# The objective value of the solution.\n",
"print((\"Optimal objective value = %f\" % solver.Objective().Value()))\n",
"print(f\"Optimal objective value = {solver.Objective().Value()}\")\n",
"\n",
"for s in all_sets:\n",
" print(\n",
" \" %s = %f\" % (chemical_set[s][0], set_vars[s].solution_value()),\n",
" end=\" \")\n",
"for s in ALL_SETS:\n",
" print(f\" {chemical_set[s][0]} = {set_vars[s].solution_value()}\", end=\" \")\n",
" print()\n",
"for p in all_products:\n",
"for p in ALL_PRODUCTS:\n",
" name = max_quantities[p][0]\n",
" max_quantity = max_quantities[p][1]\n",
" quantity = sum(\n",
" set_vars[s].solution_value() * chemical_set[s][p + 1] for s in all_sets)\n",
" print(\"%s: %f out of %f\" % (name, quantity, max_quantity))\n",
" quantity = sum(set_vars[s].solution_value() * chemical_set[s][p + 1]\n",
" for s in ALL_SETS)\n",
" print(f\"{name}: {quantity} out of {max_quantity}\")\n",
"\n"
]
}

View File

@@ -67,6 +67,18 @@
"!pip install ortools"
]
},
{
"cell_type": "markdown",
"id": "description",
"metadata": {},
"source": [
"We are trying to group items in equal sized groups.\n",
"Each item has a color and a value. We want the sum of values of each group to\n",
"be as close to the average as possible.\n",
"Furthermore, if one color is an a group, at least k items with this color must\n",
"be in that group.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -74,44 +86,34 @@
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2010-2022 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",
"\n",
"# We are trying to group items in equal sized groups.\n",
"# Each item has a color and a value. We want the sum of values of each group to\n",
"# be as close to the average as possible.\n",
"# Furthermore, if one color is an a group, at least k items with this color must\n",
"# be in that group.\n",
"\n",
"\n",
"from ortools.sat.python import cp_model\n",
"import math\n",
"from ortools.sat.python import cp_model\n",
"\n",
"# Data\n",
"\n",
"max_quantities = [[\"N_Total\", 1944], [\"P2O5\", 1166.4], [\"K2O\", 1822.5],\n",
" [\"CaO\", 1458], [\"MgO\", 486], [\"Fe\", 9.7], [\"B\", 2.4]]\n",
"max_quantities = [\n",
" [\"N_Total\", 1944],\n",
" [\"P2O5\", 1166.4],\n",
" [\"K2O\", 1822.5],\n",
" [\"CaO\", 1458],\n",
" [\"MgO\", 486],\n",
" [\"Fe\", 9.7],\n",
" [\"B\", 2.4],\n",
"]\n",
"\n",
"chemical_set = [[\"A\", 0, 0, 510, 540, 0, 0, 0], [\"B\", 110, 0, 0, 0, 160, 0, 0],\n",
" [\"C\", 61, 149, 384, 0, 30, 1,\n",
" 0.2], [\"D\", 148, 70, 245, 0, 15, 1,\n",
" 0.2], [\"E\", 160, 158, 161, 0, 10, 1, 0.2]]\n",
"chemical_set = [\n",
" [\"A\", 0, 0, 510, 540, 0, 0, 0],\n",
" [\"B\", 110, 0, 0, 0, 160, 0, 0],\n",
" [\"C\", 61, 149, 384, 0, 30, 1, 0.2],\n",
" [\"D\", 148, 70, 245, 0, 15, 1, 0.2],\n",
" [\"E\", 160, 158, 161, 0, 10, 1, 0.2],\n",
"]\n",
"\n",
"num_products = len(max_quantities)\n",
"all_products = range(num_products)\n",
"NUM_PRODUCTS = len(max_quantities)\n",
"ALL_PRODUCTS = range(NUM_PRODUCTS)\n",
"\n",
"num_sets = len(chemical_set)\n",
"all_sets = range(num_sets)\n",
"NUM_SETS = len(chemical_set)\n",
"ALL_SETS = range(NUM_SETS)\n",
"\n",
"# Model\n",
"\n",
@@ -122,43 +124,41 @@
" int(\n",
" math.ceil(\n",
" min(max_quantities[q][1] * 1000 / chemical_set[s][q + 1]\n",
" for q in all_products if chemical_set[s][q + 1] != 0)))\n",
" for s in all_sets\n",
" for q in ALL_PRODUCTS if chemical_set[s][q + 1] != 0)))\n",
" for s in ALL_SETS\n",
"]\n",
"\n",
"set_vars = [model.NewIntVar(0, max_set[s], \"set_%i\" % s) for s in all_sets]\n",
"set_vars = [model.NewIntVar(0, max_set[s], f\"set_{s}\") for s in ALL_SETS]\n",
"\n",
"epsilon = model.NewIntVar(0, 10000000, \"epsilon\")\n",
"\n",
"for p in all_products:\n",
"for p in ALL_PRODUCTS:\n",
" model.Add(\n",
" sum(int(chemical_set[s][p + 1] * 10) * set_vars[s]\n",
" for s in all_sets) <= int(max_quantities[p][1] * 10000))\n",
" for s in ALL_SETS) <= int(max_quantities[p][1] * 10000))\n",
" model.Add(\n",
" sum(int(chemical_set[s][p + 1] * 10) * set_vars[s]\n",
" for s in all_sets) >= int(max_quantities[p][1] * 10000) - epsilon)\n",
" for s in ALL_SETS) >= int(max_quantities[p][1] * 10000) - epsilon)\n",
"\n",
"model.Minimize(epsilon)\n",
"\n",
"# Creates a solver and solves.\n",
"solver = cp_model.CpSolver()\n",
"status = solver.Solve(model)\n",
"print(\"Status = %s\" % solver.StatusName(status))\n",
"print(f\"Status = {solver.StatusName(status)}\")\n",
"# The objective value of the solution.\n",
"print(\"Optimal objective value = %f\" % (solver.ObjectiveValue() / 10000.0))\n",
"print(f\"Optimal objective value = {solver.ObjectiveValue() / 10000.0}\")\n",
"\n",
"for s in all_sets:\n",
" print(\n",
" \" %s = %f\" % (chemical_set[s][0], solver.Value(set_vars[s]) / 1000.0),\n",
" end=\" \")\n",
"for s in ALL_SETS:\n",
" print(f\" {chemical_set[s][0]} = {solver.Value(set_vars[s]) / 1000.0}\", end=\" \")\n",
" print()\n",
"for p in all_products:\n",
"for p in ALL_PRODUCTS:\n",
" name = max_quantities[p][0]\n",
" max_quantity = max_quantities[p][1]\n",
" quantity = sum(\n",
" solver.Value(set_vars[s]) / 1000.0 * chemical_set[s][p + 1]\n",
" for s in all_sets)\n",
" print(\"%s: %f out of %f\" % (name, quantity, max_quantity))\n",
" for s in ALL_SETS)\n",
" print(f\"{name}: {quantity} out of {max_quantity}\")\n",
"\n"
]
}