Skip to content

Instantly share code, notes, and snippets.

@etrepum
Created November 1, 2018 03:30
Show Gist options
  • Save etrepum/514dcad6bce0ecf6a53a4dfda2ac8ca0 to your computer and use it in GitHub Desktop.
Save etrepum/514dcad6bce0ecf6a53a4dfda2ac8ca0 to your computer and use it in GitHub Desktop.
Solve for wings
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Max orders for three digits of wings: 7.0\n"
]
}
],
"source": [
"from ortools.linear_solver import pywraplp\n",
"\n",
"COUNTS = [\n",
" 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, \n",
" 24, 25, 26, 27, 28, 29, 30, 35, 40, 45, 50, 60, 70, 75, 80, 90, 100, 125, \n",
" 150, 200\n",
"]\n",
" \n",
"# PRICES = dict(\n",
"# zip(\n",
"# wingCounts,\n",
"# [\n",
"# 4.55, 5.70, 6.80, 7.95, 9.10, 10.20, 11.35, 12.50, 13.60, 14.75, 15.90, 17.00, \n",
"# 18.15, 19.30, 20.40, 21.55, 22.70, 23.80, 24.95, 26.10, 27.25, 27.80, 28.95, \n",
"# 30.10, 31.20, 32.35, 33.50, 39.15, 44.80, 50.50, 55.60, 67.00, 78.30, 83.45, \n",
"# 89.10, 100.45, 111.25, 139.00, 166.85, 222.50\n",
"# ]\n",
"# )\n",
"# )\n",
"\n",
"def solve_for_wings(n):\n",
" solver = pywraplp.Solver(\n",
" 'SolveIntegerProblem',\n",
" pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING\n",
" )\n",
" objective = solver.Objective()\n",
" constraint = solver.Constraint(n, n)\n",
" for count in COUNTS:\n",
" xn = solver.IntVar(0, n, 'x%d' % count)\n",
" objective.SetCoefficient(xn, 1)\n",
" constraint.SetCoefficient(xn, count)\n",
" objective.SetMinimization()\n",
" result_status = solver.Solve()\n",
" assert result_status == pywraplp.Solver.OPTIMAL\n",
" return solver.Objective().Value()\n",
"\n",
"print('Max orders for three digits of wings: {}'.format(max(map(solve_for_wings, range(100, 1000)))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment