Skip to content

Instantly share code, notes, and snippets.

@jeeger
Created July 11, 2019 09:19
Show Gist options
  • Save jeeger/eb4aac58dd26b4faf327aa758b31c76c to your computer and use it in GitHub Desktop.
Save jeeger/eb4aac58dd26b4faf327aa758b31c76c to your computer and use it in GitHub Desktop.
Engine allocation with ILP
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"# default imports\n",
"import matplotlib\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"matplotlib.pyplot.rcParams['figure.figsize'] = [12, 8]\n",
"\n",
"import pulp\n",
"import collections"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"Engine = collections.namedtuple(\"Engine\", [\"name\", \"space\", \"thrust\", \"turn\"])\n",
"engines = [Engine(\"X1050\", 20, 40, 1100) # has both thrust and turning!\n",
" , Engine(\"X1200\", 12, 0, 1600)\n",
" , Engine(\"X1700\", 16, 60, 0)\n",
" , Engine(\"X2200\", 20, 0, 3070)\n",
" , Engine(\"X2700\", 27, 115, 0)\n",
" , Engine(\"X3200\", 35, 0, 5900)\n",
" , Engine(\"X3700\", 46, 221, 0)\n",
" , Engine(\"X4200\", 59, 0, 11320)\n",
" , Engine(\"X4700\", 79, 425, 0)\n",
" , Engine(\"X5200\", 100, 0, 21740)\n",
" , Engine(\"X5700\", 134, 815, 0)\n",
" , Engine(\"Chipmunk Thruster\", 20, 96, 0)\n",
" , Engine(\"Chipmunk Steering\", 15, 0, 2560)\n",
" , Engine(\"Greyhound Steering\", 26, 0, 4920)\n",
" , Engine(\"Greyhound Thruster\", 34, 184, 0)\n",
" , Engine(\"Impala Steering\", 43, 0, 9440)\n",
" , Engine(\"Impala Thruster\", 58, 354, 0)\n",
" , Engine(\"Orca Steering\", 74, 0, 18120)\n",
" , Engine(\"Orca Thruster\", 98, 679, 0)\n",
" , Engine(\"Tyrant Steering\", 125, 0, 34790)\n",
" , Engine(\"Tyrant Thruster\", 167, 1305, 0)\n",
" , Engine(\"A120 Thruster\", 22, 154, 0)\n",
" , Engine(\"A125 Steering\", 16, 0, 3920)\n",
" , Engine(\"A250 Thruster\", 34, 273, 0)\n",
" , Engine(\"A255 Steering\", 25, 0, 6870)\n",
" , Engine(\"A370 Thruster\", 53, 476, 0)\n",
" , Engine(\"A375 Steering\", 38, 0, 11920)\n",
" , Engine(\"A520 Thruster\", 82, 819, 0)\n",
" , Engine(\"A525 Steering\", 60, 0, 20500)\n",
" , Engine(\"A860 Thruster\", 127, 1397, 0)\n",
" , Engine(\"A865 Steering\", 92, 0, 35090)\n",
" , Engine(\"Baellie\", 24, 101, 2500) # hai\n",
" , Engine(\"Basrem Thruster\", 18, 132, 0)\n",
" , Engine(\"Benga Thruster\", 28, 236, 0)\n",
" , Engine(\"Biroo Thruster\", 44, 415, 0)\n",
" , Engine(\"Bondir Thruster\", 63, 661, 0)\n",
" , Engine(\"Bufaer Thruster\", 104, 1201, 0)\n",
" , Engine(\"Basrem Steering\", 12, 0, 3090)\n",
" , Engine(\"Benga Steering\", 20, 0, 5770)\n",
" , Engine(\"Biroo Steering\", 32, 0, 10540)\n",
" , Engine(\"Bondir Steering\", 49, 0, 17580)\n",
" , Engine(\"Bufaer Steering\", 76, 0, 30430)\n",
" , Engine(\"Coalition Large Steering\", 25, 0, 7119) # coalition\n",
" , Engine(\"Coalition Large Thruster\", 32, 262, 0)\n",
" , Engine(\"Coalition Small Steering\", 7, 0, 1788)\n",
" , Engine(\"Coalition Small Thruster\", 9, 66, 0)\n",
" , Engine(\"Korath Asteroid Steering\", 10, 0, 2800) # Korath\n",
" , Engine(\"Korath Asteroid Thruster\", 14, 112, 0)\n",
" , Engine(\"Korath Comet Steering\", 18, 0, 5688)\n",
" , Engine(\"Korath Comet Thruster\", 24, 218, 0)\n",
" , Engine(\"Korath Lunar Steering\", 30, 0, 10560)\n",
" , Engine(\"Korath Lunar Thruster\", 40, 412, 0)\n",
" , Engine(\"Korath Planetary Steering\", 52, 0, 20696)\n",
" , Engine(\"Korath Planetary Thruster\", 69, 800, 0)\n",
" , Engine(\"Korath Stellar Steering\", 89, 0, 40050)\n",
" , Engine(\"Korath Stellar Thruster\", 118, 1534, 0)\n",
" , Engine(\"Pug Akfar Thruster\", 43, 280, 0) # pug\n",
" , Engine(\"Pug Akfar Steering\", 33, 0, 7500)\n",
" , Engine(\"Pug Cormet Thruster\", 60, 440, 0)\n",
" , Engine(\"Pug Comet Steering\", 46, 0, 11300)\n",
" , Engine(\"Pug Lohmar Thruster\", 84, 660, 0)\n",
" , Engine(\"Pug Lohmar Steering\", 64, 0, 17000)\n",
" , Engine(\"Quarg Medium Thruster\", 70, 800, 0) # quarg\n",
" , Engine(\"Quarg Medium Steering\", 50, 0, 16000)\n",
" , Engine(\"Crucible Thruster\", 20, 180, 0) # remnant\n",
" , Engine(\"Crucible Steering\", 14, 0, 4480)\n",
" , Engine(\"Forge Thruster\", 39, 370, 0)\n",
" , Engine(\"Forge Steering\", 28, 0, 9520)\n",
" , Engine(\"Smelter Thruster\", 76, 768, 0)\n",
" , Engine(\"Smelter Steering\", 55, 0, 19800)\n",
" , Engine(\"Type 1 Radiant Thruster\", 12, 66, 0) # wanderer\n",
" , Engine(\"Type 1 Radiant Steering\", 9, 0, 1728)\n",
" , Engine(\"Type 2 Radiant Thruster\", 27, 176, 0)\n",
" , Engine(\"Type 2 Radiant Steering\", 20, 0, 4540)\n",
" , Engine(\"Type 3 Radiant Thruster\", 42, 315, 0)\n",
" , Engine(\"Type 3 Radiant Steering\", 30, 0, 7860)\n",
" , Engine(\"Type 4 Radiant Thruster\", 64, 552, 0)\n",
" , Engine(\"Type 4 Radiant Steering\", 47, 0, 13959)\n",
" ]\n"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"p = pulp.LpProblem(\"Engines\", sense=pulp.constants.LpMaximize)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"amountOfEngines = {engine.name: pulp.LpVariable(\"amountOf_%s\" % engine.name, lowBound=0, cat='Integer') for engine in engines}"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"constraint = pulp.lpSum([amountOfEngines[engine.name] * engine.space for engine in engines]) <= 210, 'Total space'"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"p += constraint"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"totalThrust = pulp.lpSum([amountOfEngines[engine.name] * engine.thrust for engine in engines])\n",
"totalTurn = pulp.lpSum([amountOfEngines[engine.name] * engine.turn for engine in engines])"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"p += totalThrust * 36 + totalTurn"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.solve()"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X1050: 0.0\n",
"X1200: 0.0\n",
"X1700: 0.0\n",
"X2200: 0.0\n",
"X2700: 0.0\n",
"X3200: 0.0\n",
"X3700: 0.0\n",
"X4200: 0.0\n",
"X4700: 0.0\n",
"X5200: 0.0\n",
"X5700: 0.0\n",
"Chipmunk Thruster: 0.0\n",
"Chipmunk Steering: 0.0\n",
"Greyhound Steering: 0.0\n",
"Greyhound Thruster: 0.0\n",
"Impala Steering: 0.0\n",
"Impala Thruster: 0.0\n",
"Orca Steering: 0.0\n",
"Orca Thruster: 0.0\n",
"Tyrant Steering: 0.0\n",
"Tyrant Thruster: 0.0\n",
"A120 Thruster: 0.0\n",
"A125 Steering: 0.0\n",
"A250 Thruster: 0.0\n",
"A255 Steering: 0.0\n",
"A370 Thruster: 0.0\n",
"A375 Steering: 0.0\n",
"A520 Thruster: 0.0\n",
"A525 Steering: 0.0\n",
"A860 Thruster: 0.0\n",
"A865 Steering: 0.0\n",
"Baellie: 0.0\n",
"Basrem Thruster: 0.0\n",
"Benga Thruster: 0.0\n",
"Biroo Thruster: 0.0\n",
"Bondir Thruster: 0.0\n",
"Bufaer Thruster: 0.0\n",
"Basrem Steering: 0.0\n",
"Benga Steering: 0.0\n",
"Biroo Steering: 0.0\n",
"Bondir Steering: 0.0\n",
"Bufaer Steering: 0.0\n",
"Coalition Large Steering: 0.0\n",
"Coalition Large Thruster: 0.0\n",
"Coalition Small Steering: 0.0\n",
"Coalition Small Thruster: 0.0\n",
"Korath Asteroid Steering: 0.0\n",
"Korath Asteroid Thruster: 0.0\n",
"Korath Comet Steering: 0.0\n",
"Korath Comet Thruster: 0.0\n",
"Korath Lunar Steering: 0.0\n",
"Korath Lunar Thruster: 0.0\n",
"Korath Planetary Steering: 0.0\n",
"Korath Planetary Thruster: 0.0\n",
"Korath Stellar Steering: 1.0\n",
"Korath Stellar Thruster: 1.0\n",
"Pug Akfar Thruster: 0.0\n",
"Pug Akfar Steering: 0.0\n",
"Pug Cormet Thruster: 0.0\n",
"Pug Comet Steering: 0.0\n",
"Pug Lohmar Thruster: 0.0\n",
"Pug Lohmar Steering: 0.0\n",
"Quarg Medium Thruster: 0.0\n",
"Quarg Medium Steering: 0.0\n",
"Crucible Thruster: 0.0\n",
"Crucible Steering: 0.0\n",
"Forge Thruster: 0.0\n",
"Forge Steering: 0.0\n",
"Smelter Thruster: 0.0\n",
"Smelter Steering: 0.0\n",
"Type 1 Radiant Thruster: 0.0\n",
"Type 1 Radiant Steering: 0.0\n",
"Type 2 Radiant Thruster: 0.0\n",
"Type 2 Radiant Steering: 0.0\n",
"Type 3 Radiant Thruster: 0.0\n",
"Type 3 Radiant Steering: 0.0\n",
"Type 4 Radiant Thruster: 0.0\n",
"Type 4 Radiant Steering: 0.0\n"
]
}
],
"source": [
"for engine in engines:\n",
" print(\"{}: {}\".format(engine.name, pulp.value(amountOfEngines[engine.name])))"
]
},
{
"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