SHARE
TWEET

Untitled

a guest Sep 16th, 2019 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "code",
  5.    "execution_count": 1,
  6.    "metadata": {},
  7.    "outputs": [],
  8.    "source": [
  9.     "from pulp import *\n",
  10.     "import pandas as pd"
  11.    ]
  12.   },
  13.   {
  14.    "cell_type": "code",
  15.    "execution_count": 2,
  16.    "metadata": {},
  17.    "outputs": [
  18.     {
  19.      "data": {
  20.       "text/html": [
  21.        "<div>\n",
  22.        "<style scoped>\n",
  23.        "    .dataframe tbody tr th:only-of-type {\n",
  24.        "        vertical-align: middle;\n",
  25.        "    }\n",
  26.        "\n",
  27.        "    .dataframe tbody tr th {\n",
  28.        "        vertical-align: top;\n",
  29.        "    }\n",
  30.        "\n",
  31.        "    .dataframe thead th {\n",
  32.        "        text-align: right;\n",
  33.        "    }\n",
  34.        "</style>\n",
  35.        "<table border=\"1\" class=\"dataframe\">\n",
  36.        "  <thead>\n",
  37.        "    <tr style=\"text-align: right;\">\n",
  38.        "      <th></th>\n",
  39.        "      <th>dmd</th>\n",
  40.        "    </tr>\n",
  41.        "    <tr>\n",
  42.        "      <th>supply_region</th>\n",
  43.        "      <th></th>\n",
  44.        "    </tr>\n",
  45.        "  </thead>\n",
  46.        "  <tbody>\n",
  47.        "    <tr>\n",
  48.        "      <th>usa</th>\n",
  49.        "      <td>2719.6</td>\n",
  50.        "    </tr>\n",
  51.        "    <tr>\n",
  52.        "      <th>germany</th>\n",
  53.        "      <td>84.1</td>\n",
  54.        "    </tr>\n",
  55.        "    <tr>\n",
  56.        "      <th>japan</th>\n",
  57.        "      <td>1676.8</td>\n",
  58.        "    </tr>\n",
  59.        "    <tr>\n",
  60.        "      <th>brazil</th>\n",
  61.        "      <td>145.4</td>\n",
  62.        "    </tr>\n",
  63.        "    <tr>\n",
  64.        "      <th>india</th>\n",
  65.        "      <td>156.4</td>\n",
  66.        "    </tr>\n",
  67.        "  </tbody>\n",
  68.        "</table>\n",
  69.        "</div>"
  70.       ],
  71.       "text/plain": [
  72.        "                  dmd\n",
  73.        "supply_region        \n",
  74.        "usa            2719.6\n",
  75.        "germany          84.1\n",
  76.        "japan          1676.8\n",
  77.        "brazil          145.4\n",
  78.        "india           156.4"
  79.       ]
  80.      },
  81.      "execution_count": 2,
  82.      "metadata": {},
  83.      "output_type": "execute_result"
  84.     }
  85.    ],
  86.    "source": [
  87.     "# first we import the data\n",
  88.     "demand = pd.read_csv('C:/Users/jperrigan/Documents/plant_demand.csv', index_col = ['supply_region'])\n",
  89.     "demand"
  90.    ]
  91.   },
  92.   {
  93.    "cell_type": "code",
  94.    "execution_count": 3,
  95.    "metadata": {},
  96.    "outputs": [
  97.     {
  98.      "data": {
  99.       "text/html": [
  100.        "<div>\n",
  101.        "<style scoped>\n",
  102.        "    .dataframe tbody tr th:only-of-type {\n",
  103.        "        vertical-align: middle;\n",
  104.        "    }\n",
  105.        "\n",
  106.        "    .dataframe tbody tr th {\n",
  107.        "        vertical-align: top;\n",
  108.        "    }\n",
  109.        "\n",
  110.        "    .dataframe thead th {\n",
  111.        "        text-align: right;\n",
  112.        "    }\n",
  113.        "</style>\n",
  114.        "<table border=\"1\" class=\"dataframe\">\n",
  115.        "  <thead>\n",
  116.        "    <tr style=\"text-align: right;\">\n",
  117.        "      <th></th>\n",
  118.        "      <th>usa</th>\n",
  119.        "      <th>germany</th>\n",
  120.        "      <th>japan</th>\n",
  121.        "      <th>brazil</th>\n",
  122.        "      <th>india</th>\n",
  123.        "    </tr>\n",
  124.        "    <tr>\n",
  125.        "      <th>supply_region</th>\n",
  126.        "      <th></th>\n",
  127.        "      <th></th>\n",
  128.        "      <th></th>\n",
  129.        "      <th></th>\n",
  130.        "      <th></th>\n",
  131.        "    </tr>\n",
  132.        "  </thead>\n",
  133.        "  <tbody>\n",
  134.        "    <tr>\n",
  135.        "      <th>usa</th>\n",
  136.        "      <td>6</td>\n",
  137.        "      <td>13</td>\n",
  138.        "      <td>20</td>\n",
  139.        "      <td>12</td>\n",
  140.        "      <td>17</td>\n",
  141.        "    </tr>\n",
  142.        "    <tr>\n",
  143.        "      <th>germany</th>\n",
  144.        "      <td>13</td>\n",
  145.        "      <td>6</td>\n",
  146.        "      <td>14</td>\n",
  147.        "      <td>14</td>\n",
  148.        "      <td>13</td>\n",
  149.        "    </tr>\n",
  150.        "    <tr>\n",
  151.        "      <th>japan</th>\n",
  152.        "      <td>20</td>\n",
  153.        "      <td>14</td>\n",
  154.        "      <td>3</td>\n",
  155.        "      <td>21</td>\n",
  156.        "      <td>9</td>\n",
  157.        "    </tr>\n",
  158.        "    <tr>\n",
  159.        "      <th>brazil</th>\n",
  160.        "      <td>12</td>\n",
  161.        "      <td>14</td>\n",
  162.        "      <td>21</td>\n",
  163.        "      <td>8</td>\n",
  164.        "      <td>21</td>\n",
  165.        "    </tr>\n",
  166.        "    <tr>\n",
  167.        "      <th>india</th>\n",
  168.        "      <td>22</td>\n",
  169.        "      <td>13</td>\n",
  170.        "      <td>10</td>\n",
  171.        "      <td>23</td>\n",
  172.        "      <td>8</td>\n",
  173.        "    </tr>\n",
  174.        "  </tbody>\n",
  175.        "</table>\n",
  176.        "</div>"
  177.       ],
  178.       "text/plain": [
  179.        "               usa  germany  japan  brazil  india\n",
  180.        "supply_region                                    \n",
  181.        "usa              6       13     20      12     17\n",
  182.        "germany         13        6     14      14     13\n",
  183.        "japan           20       14      3      21      9\n",
  184.        "brazil          12       14     21       8     21\n",
  185.        "india           22       13     10      23      8"
  186.       ]
  187.      },
  188.      "execution_count": 3,
  189.      "metadata": {},
  190.      "output_type": "execute_result"
  191.     }
  192.    ],
  193.    "source": [
  194.     "var_cost = pd.read_csv('C:/Users/jperrigan/Documents/plant_var_cost.csv', index_col = ['supply_region'])\n",
  195.     "var_cost"
  196.    ]
  197.   },
  198.   {
  199.    "cell_type": "code",
  200.    "execution_count": 4,
  201.    "metadata": {},
  202.    "outputs": [
  203.     {
  204.      "data": {
  205.       "text/html": [
  206.        "<div>\n",
  207.        "<style scoped>\n",
  208.        "    .dataframe tbody tr th:only-of-type {\n",
  209.        "        vertical-align: middle;\n",
  210.        "    }\n",
  211.        "\n",
  212.        "    .dataframe tbody tr th {\n",
  213.        "        vertical-align: top;\n",
  214.        "    }\n",
  215.        "\n",
  216.        "    .dataframe thead th {\n",
  217.        "        text-align: right;\n",
  218.        "    }\n",
  219.        "</style>\n",
  220.        "<table border=\"1\" class=\"dataframe\">\n",
  221.        "  <thead>\n",
  222.        "    <tr style=\"text-align: right;\">\n",
  223.        "      <th></th>\n",
  224.        "      <th>lowcap</th>\n",
  225.        "      <th>highcap</th>\n",
  226.        "    </tr>\n",
  227.        "    <tr>\n",
  228.        "      <th>supply_region</th>\n",
  229.        "      <th></th>\n",
  230.        "      <th></th>\n",
  231.        "    </tr>\n",
  232.        "  </thead>\n",
  233.        "  <tbody>\n",
  234.        "    <tr>\n",
  235.        "      <th>usa</th>\n",
  236.        "      <td>6500</td>\n",
  237.        "      <td>9500</td>\n",
  238.        "    </tr>\n",
  239.        "    <tr>\n",
  240.        "      <th>germany</th>\n",
  241.        "      <td>4980</td>\n",
  242.        "      <td>7270</td>\n",
  243.        "    </tr>\n",
  244.        "    <tr>\n",
  245.        "      <th>japan</th>\n",
  246.        "      <td>6230</td>\n",
  247.        "      <td>9100</td>\n",
  248.        "    </tr>\n",
  249.        "    <tr>\n",
  250.        "      <th>brazil</th>\n",
  251.        "      <td>3230</td>\n",
  252.        "      <td>4730</td>\n",
  253.        "    </tr>\n",
  254.        "    <tr>\n",
  255.        "      <th>india</th>\n",
  256.        "      <td>2110</td>\n",
  257.        "      <td>3080</td>\n",
  258.        "    </tr>\n",
  259.        "  </tbody>\n",
  260.        "</table>\n",
  261.        "</div>"
  262.       ],
  263.       "text/plain": [
  264.        "               lowcap  highcap\n",
  265.        "supply_region                 \n",
  266.        "usa              6500     9500\n",
  267.        "germany          4980     7270\n",
  268.        "japan            6230     9100\n",
  269.        "brazil           3230     4730\n",
  270.        "india            2110     3080"
  271.       ]
  272.      },
  273.      "execution_count": 4,
  274.      "metadata": {},
  275.      "output_type": "execute_result"
  276.     }
  277.    ],
  278.    "source": [
  279.     "fix_cost = pd.read_csv('C:/Users/jperrigan/Documents/plant_fix_cost.csv', index_col = ['supply_region'])\n",
  280.     "fix_cost"
  281.    ]
  282.   },
  283.   {
  284.    "cell_type": "code",
  285.    "execution_count": 5,
  286.    "metadata": {},
  287.    "outputs": [
  288.     {
  289.      "data": {
  290.       "text/html": [
  291.        "<div>\n",
  292.        "<style scoped>\n",
  293.        "    .dataframe tbody tr th:only-of-type {\n",
  294.        "        vertical-align: middle;\n",
  295.        "    }\n",
  296.        "\n",
  297.        "    .dataframe tbody tr th {\n",
  298.        "        vertical-align: top;\n",
  299.        "    }\n",
  300.        "\n",
  301.        "    .dataframe thead th {\n",
  302.        "        text-align: right;\n",
  303.        "    }\n",
  304.        "</style>\n",
  305.        "<table border=\"1\" class=\"dataframe\">\n",
  306.        "  <thead>\n",
  307.        "    <tr style=\"text-align: right;\">\n",
  308.        "      <th></th>\n",
  309.        "      <th>lowcap</th>\n",
  310.        "      <th>highcap</th>\n",
  311.        "    </tr>\n",
  312.        "    <tr>\n",
  313.        "      <th>supply_region</th>\n",
  314.        "      <th></th>\n",
  315.        "      <th></th>\n",
  316.        "    </tr>\n",
  317.        "  </thead>\n",
  318.        "  <tbody>\n",
  319.        "    <tr>\n",
  320.        "      <th>usa</th>\n",
  321.        "      <td>500</td>\n",
  322.        "      <td>1500</td>\n",
  323.        "    </tr>\n",
  324.        "    <tr>\n",
  325.        "      <th>germany</th>\n",
  326.        "      <td>500</td>\n",
  327.        "      <td>1500</td>\n",
  328.        "    </tr>\n",
  329.        "    <tr>\n",
  330.        "      <th>japan</th>\n",
  331.        "      <td>500</td>\n",
  332.        "      <td>1500</td>\n",
  333.        "    </tr>\n",
  334.        "    <tr>\n",
  335.        "      <th>brazil</th>\n",
  336.        "      <td>500</td>\n",
  337.        "      <td>1500</td>\n",
  338.        "    </tr>\n",
  339.        "    <tr>\n",
  340.        "      <th>india</th>\n",
  341.        "      <td>500</td>\n",
  342.        "      <td>1500</td>\n",
  343.        "    </tr>\n",
  344.        "  </tbody>\n",
  345.        "</table>\n",
  346.        "</div>"
  347.       ],
  348.       "text/plain": [
  349.        "               lowcap  highcap\n",
  350.        "supply_region                 \n",
  351.        "usa               500     1500\n",
  352.        "germany           500     1500\n",
  353.        "japan             500     1500\n",
  354.        "brazil            500     1500\n",
  355.        "india             500     1500"
  356.       ]
  357.      },
  358.      "execution_count": 5,
  359.      "metadata": {},
  360.      "output_type": "execute_result"
  361.     }
  362.    ],
  363.    "source": [
  364.     "cap = pd.read_csv('C:/Users/jperrigan/Documents/plant_cap.csv', index_col = ['supply_region'])\n",
  365.     "cap"
  366.    ]
  367.   },
  368.   {
  369.    "cell_type": "code",
  370.    "execution_count": 6,
  371.    "metadata": {},
  372.    "outputs": [],
  373.    "source": [
  374.     "# define the model and initialize class of model\n",
  375.     "# out goal is to minimize costs, so it's an LpMinimize problem\n",
  376.     "model = LpProblem(\"Capacitated Plant Location Model\", LpMinimize)"
  377.    ]
  378.   },
  379.   {
  380.    "cell_type": "code",
  381.    "execution_count": 7,
  382.    "metadata": {},
  383.    "outputs": [
  384.     {
  385.      "data": {
  386.       "text/plain": [
  387.        "['lowcap', 'highcap']"
  388.       ]
  389.      },
  390.      "execution_count": 7,
  391.      "metadata": {},
  392.      "output_type": "execute_result"
  393.     }
  394.    ],
  395.    "source": [
  396.     "# let's create a list with our capacity options using the dataframes\n",
  397.     "capacity = list(cap.columns.values)\n",
  398.     "capacity"
  399.    ]
  400.   },
  401.   {
  402.    "cell_type": "code",
  403.    "execution_count": 8,
  404.    "metadata": {},
  405.    "outputs": [
  406.     {
  407.      "data": {
  408.       "text/plain": [
  409.        "['usa', 'germany', 'japan', 'brazil', 'india']"
  410.       ]
  411.      },
  412.      "execution_count": 8,
  413.      "metadata": {},
  414.      "output_type": "execute_result"
  415.     }
  416.    ],
  417.    "source": [
  418.     "location = list(cap.index.values)\n",
  419.     "location"
  420.    ]
  421.   },
  422.   {
  423.    "cell_type": "code",
  424.    "execution_count": 9,
  425.    "metadata": {},
  426.    "outputs": [
  427.     {
  428.      "data": {
  429.       "text/plain": [
  430.        "{('usa', 'usa'): production_amount__('usa',_'usa'),\n",
  431.        " ('usa', 'germany'): production_amount__('usa',_'germany'),\n",
  432.        " ('usa', 'japan'): production_amount__('usa',_'japan'),\n",
  433.        " ('usa', 'brazil'): production_amount__('usa',_'brazil'),\n",
  434.        " ('usa', 'india'): production_amount__('usa',_'india'),\n",
  435.        " ('germany', 'usa'): production_amount__('germany',_'usa'),\n",
  436.        " ('germany', 'germany'): production_amount__('germany',_'germany'),\n",
  437.        " ('germany', 'japan'): production_amount__('germany',_'japan'),\n",
  438.        " ('germany', 'brazil'): production_amount__('germany',_'brazil'),\n",
  439.        " ('germany', 'india'): production_amount__('germany',_'india'),\n",
  440.        " ('japan', 'usa'): production_amount__('japan',_'usa'),\n",
  441.        " ('japan', 'germany'): production_amount__('japan',_'germany'),\n",
  442.        " ('japan', 'japan'): production_amount__('japan',_'japan'),\n",
  443.        " ('japan', 'brazil'): production_amount__('japan',_'brazil'),\n",
  444.        " ('japan', 'india'): production_amount__('japan',_'india'),\n",
  445.        " ('brazil', 'usa'): production_amount__('brazil',_'usa'),\n",
  446.        " ('brazil', 'germany'): production_amount__('brazil',_'germany'),\n",
  447.        " ('brazil', 'japan'): production_amount__('brazil',_'japan'),\n",
  448.        " ('brazil', 'brazil'): production_amount__('brazil',_'brazil'),\n",
  449.        " ('brazil', 'india'): production_amount__('brazil',_'india'),\n",
  450.        " ('india', 'usa'): production_amount__('india',_'usa'),\n",
  451.        " ('india', 'germany'): production_amount__('india',_'germany'),\n",
  452.        " ('india', 'japan'): production_amount__('india',_'japan'),\n",
  453.        " ('india', 'brazil'): production_amount__('india',_'brazil'),\n",
  454.        " ('india', 'india'): production_amount__('india',_'india')}"
  455.       ]
  456.      },
  457.      "execution_count": 9,
  458.      "metadata": {},
  459.      "output_type": "execute_result"
  460.     }
  461.    ],
  462.    "source": [
  463.     "# next we define our decision variables\n",
  464.     "# what variables can we control?\n",
  465.     "# we can control the quantity that we produce in each location and ship to each location\n",
  466.     "production = LpVariable.dicts(\"production_amount_\", [(i, j) for i in location for j in location], lowBound = 0, cat = 'Continuous')\n",
  467.     "production"
  468.    ]
  469.   },
  470.   {
  471.    "cell_type": "code",
  472.    "execution_count": 10,
  473.    "metadata": {},
  474.    "outputs": [
  475.     {
  476.      "data": {
  477.       "text/plain": [
  478.        "{('usa', 'lowcap'): plant_status__('usa',_'lowcap'),\n",
  479.        " ('germany', 'lowcap'): plant_status__('germany',_'lowcap'),\n",
  480.        " ('japan', 'lowcap'): plant_status__('japan',_'lowcap'),\n",
  481.        " ('brazil', 'lowcap'): plant_status__('brazil',_'lowcap'),\n",
  482.        " ('india', 'lowcap'): plant_status__('india',_'lowcap'),\n",
  483.        " ('usa', 'highcap'): plant_status__('usa',_'highcap'),\n",
  484.        " ('germany', 'highcap'): plant_status__('germany',_'highcap'),\n",
  485.        " ('japan', 'highcap'): plant_status__('japan',_'highcap'),\n",
  486.        " ('brazil', 'highcap'): plant_status__('brazil',_'highcap'),\n",
  487.        " ('india', 'highcap'): plant_status__('india',_'highcap')}"
  488.       ]
  489.      },
  490.      "execution_count": 10,
  491.      "metadata": {},
  492.      "output_type": "execute_result"
  493.     }
  494.    ],
  495.    "source": [
  496.     "# we can choose whether to produce at a location\n",
  497.     "# binary variable because the plant is either one or off\n",
  498.     "status = LpVariable.dicts(\"plant_status_\", \n",
  499.     "                     [(i,s) for s in capacity for i in location], cat='Binary')\n",
  500.     "status"
  501.    ]
  502.   },
  503.   {
  504.    "cell_type": "code",
  505.    "execution_count": 11,
  506.    "metadata": {},
  507.    "outputs": [],
  508.    "source": [
  509.     "# now let's create out objective function\n",
  510.     "# we're trying to minimize production costs\n",
  511.     "# which means we're trying to minimize the sum of fixed costs for each location (dependent on whether it's on or off)\n",
  512.     "# plus the variable costs for each location (dependent on how many units produced)\n",
  513.     "# the first part contains the fixed costs multiplied by the binary of off or on\n",
  514.     "# the second part sums up the variable costs for each location-location tuple (produced in location A, shipped to A, etc.)\n",
  515.     "model += (lpSum([fix_cost.loc[i,s] * status[(i,s)] \n",
  516.     "                 for s in capacity for i in location])\n",
  517.     "          + lpSum([var_cost.loc[i,j] * production[(i,j)] \n",
  518.     "                   for i in location for j in location]))"
  519.    ]
  520.   },
  521.   {
  522.    "cell_type": "code",
  523.    "execution_count": 12,
  524.    "metadata": {},
  525.    "outputs": [],
  526.    "source": [
  527.     "# next step: constraints\n",
  528.     "# the constraints are that total production equals total demand\n",
  529.     "# the sum of the quantity produced by all locations (i) and shipped to location j should equal the demand for each location j\n",
  530.     "# We'll need to loop over all the regions and sum the quantity arriving to that region\n",
  531.     "# which should be equal to the demand from that region\n",
  532.     "# the for loop basically means for each \"row\"\n",
  533.     "for j in location:\n",
  534.     "    model += lpSum([production[(i, j)] for i in location]) == demand.loc[j,'dmd']"
  535.    ]
  536.   },
  537.   {
  538.    "cell_type": "code",
  539.    "execution_count": 13,
  540.    "metadata": {},
  541.    "outputs": [],
  542.    "source": [
  543.     "# the next constraint is that total production of a particular region is less than or equal to the total production capacity\n",
  544.     "# of that region\n",
  545.     "# total production in a particular region <= total production capacity of that region, \n",
  546.     "# which depends on whether that plant is on (binary variable)\n",
  547.     "for i in location:\n",
  548.     "    model += lpSum([production[(i, j)] for j in location]) <= lpSum([cap.loc[i,s] * status[i,s] \n",
  549.     "                                                       for s in capacity])"
  550.    ]
  551.   },
  552.   {
  553.    "cell_type": "code",
  554.    "execution_count": 14,
  555.    "metadata": {},
  556.    "outputs": [],
  557.    "source": [
  558.     "# now let's add a logical constraint\n",
  559.     "# the first is that if a high capacity plant in the USA is open, then the low capacity plant in germany is also opened\n",
  560.     "# this is a binary variable, so if y(usa, high) = 1 then y(germany, low) also equals one\n",
  561.     "# so y(usa, high) - y(germany, low) must equal zero, or be less than or equal to zero\n",
  562.     "#model += status['usa', 'highcap'] - status['germany', 'lowcap'] <= 0"
  563.    ]
  564.   },
  565.   {
  566.    "cell_type": "code",
  567.    "execution_count": 15,
  568.    "metadata": {},
  569.    "outputs": [
  570.     {
  571.      "name": "stdout",
  572.      "output_type": "stream",
  573.      "text": [
  574.       "Status: Optimal\n"
  575.      ]
  576.     }
  577.    ],
  578.    "source": [
  579.     "model.solve()\n",
  580.     "print(\"Status:\", LpStatus[model.status])"
  581.    ]
  582.   },
  583.   {
  584.    "cell_type": "code",
  585.    "execution_count": 16,
  586.    "metadata": {},
  587.    "outputs": [
  588.     {
  589.      "name": "stdout",
  590.      "output_type": "stream",
  591.      "text": [
  592.       "plant_status__('brazil',_'highcap') = 1.0\n",
  593.       "plant_status__('brazil',_'lowcap') = 0.0\n",
  594.       "plant_status__('germany',_'highcap') = 0.0\n",
  595.       "plant_status__('germany',_'lowcap') = 0.0\n",
  596.       "plant_status__('india',_'highcap') = 0.0\n",
  597.       "plant_status__('india',_'lowcap') = 1.0\n",
  598.       "plant_status__('japan',_'highcap') = 1.0\n",
  599.       "plant_status__('japan',_'lowcap') = 0.0\n",
  600.       "plant_status__('usa',_'highcap') = 1.0\n",
  601.       "plant_status__('usa',_'lowcap') = 0.0\n",
  602.       "production_amount__('brazil',_'brazil') = 145.4\n",
  603.       "production_amount__('brazil',_'germany') = 0.0\n",
  604.       "production_amount__('brazil',_'india') = 0.0\n",
  605.       "production_amount__('brazil',_'japan') = 0.0\n",
  606.       "production_amount__('brazil',_'usa') = 1219.6\n",
  607.       "production_amount__('germany',_'brazil') = 0.0\n",
  608.       "production_amount__('germany',_'germany') = 0.0\n",
  609.       "production_amount__('germany',_'india') = 0.0\n",
  610.       "production_amount__('germany',_'japan') = 0.0\n",
  611.       "production_amount__('germany',_'usa') = 0.0\n",
  612.       "production_amount__('india',_'brazil') = 0.0\n",
  613.       "production_amount__('india',_'germany') = 84.1\n",
  614.       "production_amount__('india',_'india') = 156.4\n",
  615.       "production_amount__('india',_'japan') = 176.8\n",
  616.       "production_amount__('india',_'usa') = 0.0\n",
  617.       "production_amount__('japan',_'brazil') = 0.0\n",
  618.       "production_amount__('japan',_'germany') = 0.0\n",
  619.       "production_amount__('japan',_'india') = 0.0\n",
  620.       "production_amount__('japan',_'japan') = 1500.0\n",
  621.       "production_amount__('japan',_'usa') = 0.0\n",
  622.       "production_amount__('usa',_'brazil') = 0.0\n",
  623.       "production_amount__('usa',_'germany') = 0.0\n",
  624.       "production_amount__('usa',_'india') = 0.0\n",
  625.       "production_amount__('usa',_'japan') = 0.0\n",
  626.       "production_amount__('usa',_'usa') = 1500.0\n"
  627.      ]
  628.     }
  629.    ],
  630.    "source": [
  631.     "for v in model.variables():\n",
  632.     "    print(v.name, \"=\", v.varValue)"
  633.    ]
  634.   },
  635.   {
  636.    "cell_type": "code",
  637.    "execution_count": 17,
  638.    "metadata": {},
  639.    "outputs": [
  640.     {
  641.      "name": "stdout",
  642.      "output_type": "stream",
  643.      "text": [
  644.       "The total cost is $ 58850.899999999994\n"
  645.      ]
  646.     }
  647.    ],
  648.    "source": [
  649.     "print(\"The total cost is $\",model.objective.value())"
  650.    ]
  651.   },
  652.   {
  653.    "cell_type": "code",
  654.    "execution_count": 18,
  655.    "metadata": {},
  656.    "outputs": [],
  657.    "source": [
  658.     "# prints all the model specifications to a text file\n",
  659.     "model.writeLP('C:/Users/jperrigan/Documents/solution.lp')"
  660.    ]
  661.   },
  662.   {
  663.    "cell_type": "code",
  664.    "execution_count": 20,
  665.    "metadata": {},
  666.    "outputs": [
  667.     {
  668.      "data": {
  669.       "text/html": [
  670.        "<div>\n",
  671.        "<style scoped>\n",
  672.        "    .dataframe tbody tr th:only-of-type {\n",
  673.        "        vertical-align: middle;\n",
  674.        "    }\n",
  675.        "\n",
  676.        "    .dataframe tbody tr th {\n",
  677.        "        vertical-align: top;\n",
  678.        "    }\n",
  679.        "\n",
  680.        "    .dataframe thead th {\n",
  681.        "        text-align: right;\n",
  682.        "    }\n",
  683.        "</style>\n",
  684.        "<table border=\"1\" class=\"dataframe\">\n",
  685.        "  <thead>\n",
  686.        "    <tr style=\"text-align: right;\">\n",
  687.        "      <th></th>\n",
  688.        "      <th>prod</th>\n",
  689.        "      <th>quant</th>\n",
  690.        "    </tr>\n",
  691.        "  </thead>\n",
  692.        "  <tbody>\n",
  693.        "    <tr>\n",
  694.        "      <th>0</th>\n",
  695.        "      <td>usa to usa</td>\n",
  696.        "      <td>1500.0</td>\n",
  697.        "    </tr>\n",
  698.        "    <tr>\n",
  699.        "      <th>1</th>\n",
  700.        "      <td>usa to germany</td>\n",
  701.        "      <td>0.0</td>\n",
  702.        "    </tr>\n",
  703.        "    <tr>\n",
  704.        "      <th>2</th>\n",
  705.        "      <td>usa to japan</td>\n",
  706.        "      <td>0.0</td>\n",
  707.        "    </tr>\n",
  708.        "    <tr>\n",
  709.        "      <th>3</th>\n",
  710.        "      <td>usa to brazil</td>\n",
  711.        "      <td>0.0</td>\n",
  712.        "    </tr>\n",
  713.        "    <tr>\n",
  714.        "      <th>4</th>\n",
  715.        "      <td>usa to india</td>\n",
  716.        "      <td>0.0</td>\n",
  717.        "    </tr>\n",
  718.        "    <tr>\n",
  719.        "      <th>5</th>\n",
  720.        "      <td>germany to usa</td>\n",
  721.        "      <td>0.0</td>\n",
  722.        "    </tr>\n",
  723.        "    <tr>\n",
  724.        "      <th>6</th>\n",
  725.        "      <td>germany to germany</td>\n",
  726.        "      <td>0.0</td>\n",
  727.        "    </tr>\n",
  728.        "    <tr>\n",
  729.        "      <th>7</th>\n",
  730.        "      <td>germany to japan</td>\n",
  731.        "      <td>0.0</td>\n",
  732.        "    </tr>\n",
  733.        "    <tr>\n",
  734.        "      <th>8</th>\n",
  735.        "      <td>germany to brazil</td>\n",
  736.        "      <td>0.0</td>\n",
  737.        "    </tr>\n",
  738.        "    <tr>\n",
  739.        "      <th>9</th>\n",
  740.        "      <td>germany to india</td>\n",
  741.        "      <td>0.0</td>\n",
  742.        "    </tr>\n",
  743.        "    <tr>\n",
  744.        "      <th>10</th>\n",
  745.        "      <td>japan to usa</td>\n",
  746.        "      <td>0.0</td>\n",
  747.        "    </tr>\n",
  748.        "    <tr>\n",
  749.        "      <th>11</th>\n",
  750.        "      <td>japan to germany</td>\n",
  751.        "      <td>0.0</td>\n",
  752.        "    </tr>\n",
  753.        "    <tr>\n",
  754.        "      <th>12</th>\n",
  755.        "      <td>japan to japan</td>\n",
  756.        "      <td>1500.0</td>\n",
  757.        "    </tr>\n",
  758.        "    <tr>\n",
  759.        "      <th>13</th>\n",
  760.        "      <td>japan to brazil</td>\n",
  761.        "      <td>0.0</td>\n",
  762.        "    </tr>\n",
  763.        "    <tr>\n",
  764.        "      <th>14</th>\n",
  765.        "      <td>japan to india</td>\n",
  766.        "      <td>0.0</td>\n",
  767.        "    </tr>\n",
  768.        "    <tr>\n",
  769.        "      <th>15</th>\n",
  770.        "      <td>brazil to usa</td>\n",
  771.        "      <td>1219.6</td>\n",
  772.        "    </tr>\n",
  773.        "    <tr>\n",
  774.        "      <th>16</th>\n",
  775.        "      <td>brazil to germany</td>\n",
  776.        "      <td>0.0</td>\n",
  777.        "    </tr>\n",
  778.        "    <tr>\n",
  779.        "      <th>17</th>\n",
  780.        "      <td>brazil to japan</td>\n",
  781.        "      <td>0.0</td>\n",
  782.        "    </tr>\n",
  783.        "    <tr>\n",
  784.        "      <th>18</th>\n",
  785.        "      <td>brazil to brazil</td>\n",
  786.        "      <td>145.4</td>\n",
  787.        "    </tr>\n",
  788.        "    <tr>\n",
  789.        "      <th>19</th>\n",
  790.        "      <td>brazil to india</td>\n",
  791.        "      <td>0.0</td>\n",
  792.        "    </tr>\n",
  793.        "    <tr>\n",
  794.        "      <th>20</th>\n",
  795.        "      <td>india to usa</td>\n",
  796.        "      <td>0.0</td>\n",
  797.        "    </tr>\n",
  798.        "    <tr>\n",
  799.        "      <th>21</th>\n",
  800.        "      <td>india to germany</td>\n",
  801.        "      <td>84.1</td>\n",
  802.        "    </tr>\n",
  803.        "    <tr>\n",
  804.        "      <th>22</th>\n",
  805.        "      <td>india to japan</td>\n",
  806.        "      <td>176.8</td>\n",
  807.        "    </tr>\n",
  808.        "    <tr>\n",
  809.        "      <th>23</th>\n",
  810.        "      <td>india to brazil</td>\n",
  811.        "      <td>0.0</td>\n",
  812.        "    </tr>\n",
  813.        "    <tr>\n",
  814.        "      <th>24</th>\n",
  815.        "      <td>india to india</td>\n",
  816.        "      <td>156.4</td>\n",
  817.        "    </tr>\n",
  818.        "  </tbody>\n",
  819.        "</table>\n",
  820.        "</div>"
  821.       ],
  822.       "text/plain": [
  823.        "                  prod   quant\n",
  824.        "0           usa to usa  1500.0\n",
  825.        "1       usa to germany     0.0\n",
  826.        "2         usa to japan     0.0\n",
  827.        "3        usa to brazil     0.0\n",
  828.        "4         usa to india     0.0\n",
  829.        "5       germany to usa     0.0\n",
  830.        "6   germany to germany     0.0\n",
  831.        "7     germany to japan     0.0\n",
  832.        "8    germany to brazil     0.0\n",
  833.        "9     germany to india     0.0\n",
  834.        "10        japan to usa     0.0\n",
  835.        "11    japan to germany     0.0\n",
  836.        "12      japan to japan  1500.0\n",
  837.        "13     japan to brazil     0.0\n",
  838.        "14      japan to india     0.0\n",
  839.        "15       brazil to usa  1219.6\n",
  840.        "16   brazil to germany     0.0\n",
  841.        "17     brazil to japan     0.0\n",
  842.        "18    brazil to brazil   145.4\n",
  843.        "19     brazil to india     0.0\n",
  844.        "20        india to usa     0.0\n",
  845.        "21    india to germany    84.1\n",
  846.        "22      india to japan   176.8\n",
  847.        "23     india to brazil     0.0\n",
  848.        "24      india to india   156.4"
  849.       ]
  850.      },
  851.      "execution_count": 20,
  852.      "metadata": {},
  853.      "output_type": "execute_result"
  854.     }
  855.    ],
  856.    "source": [
  857.     "prod = [{'prod': \"{} to {}\".format(i, j), 'quant':production[(i, j)].varValue} for i in location for j in location]\n",
  858.     "prod = pd.DataFrame(prod)\n",
  859.     "prod"
  860.    ]
  861.   },
  862.   {
  863.    "cell_type": "code",
  864.    "execution_count": 21,
  865.    "metadata": {},
  866.    "outputs": [
  867.     {
  868.      "data": {
  869.       "text/html": [
  870.        "<div>\n",
  871.        "<style scoped>\n",
  872.        "    .dataframe tbody tr th:only-of-type {\n",
  873.        "        vertical-align: middle;\n",
  874.        "    }\n",
  875.        "\n",
  876.        "    .dataframe tbody tr th {\n",
  877.        "        vertical-align: top;\n",
  878.        "    }\n",
  879.        "\n",
  880.        "    .dataframe thead th {\n",
  881.        "        text-align: right;\n",
  882.        "    }\n",
  883.        "</style>\n",
  884.        "<table border=\"1\" class=\"dataframe\">\n",
  885.        "  <thead>\n",
  886.        "    <tr style=\"text-align: right;\">\n",
  887.        "      <th></th>\n",
  888.        "      <th>highcap</th>\n",
  889.        "      <th>lowcap</th>\n",
  890.        "    </tr>\n",
  891.        "    <tr>\n",
  892.        "      <th>location</th>\n",
  893.        "      <th></th>\n",
  894.        "      <th></th>\n",
  895.        "    </tr>\n",
  896.        "  </thead>\n",
  897.        "  <tbody>\n",
  898.        "    <tr>\n",
  899.        "      <th>usa</th>\n",
  900.        "      <td>1.0</td>\n",
  901.        "      <td>0.0</td>\n",
  902.        "    </tr>\n",
  903.        "    <tr>\n",
  904.        "      <th>germany</th>\n",
  905.        "      <td>0.0</td>\n",
  906.        "      <td>0.0</td>\n",
  907.        "    </tr>\n",
  908.        "    <tr>\n",
  909.        "      <th>japan</th>\n",
  910.        "      <td>1.0</td>\n",
  911.        "      <td>0.0</td>\n",
  912.        "    </tr>\n",
  913.        "    <tr>\n",
  914.        "      <th>brazil</th>\n",
  915.        "      <td>1.0</td>\n",
  916.        "      <td>0.0</td>\n",
  917.        "    </tr>\n",
  918.        "    <tr>\n",
  919.        "      <th>india</th>\n",
  920.        "      <td>0.0</td>\n",
  921.        "      <td>1.0</td>\n",
  922.        "    </tr>\n",
  923.        "  </tbody>\n",
  924.        "</table>\n",
  925.        "</div>"
  926.       ],
  927.       "text/plain": [
  928.        "          highcap  lowcap\n",
  929.        "location                 \n",
  930.        "usa           1.0     0.0\n",
  931.        "germany       0.0     0.0\n",
  932.        "japan         1.0     0.0\n",
  933.        "brazil        1.0     0.0\n",
  934.        "india         0.0     1.0"
  935.       ]
  936.      },
  937.      "execution_count": 21,
  938.      "metadata": {},
  939.      "output_type": "execute_result"
  940.     }
  941.    ],
  942.    "source": [
  943.     "stat = [{'location':i, 'lowcap':status[(i, capacity[0])].varValue, 'highcap':status[(i, capacity[1])].varValue} \n",
  944.     "        for i in location]\n",
  945.     "stat = pd.DataFrame(stat).set_index('location')\n",
  946.     "stat"
  947.    ]
  948.   },
  949.   {
  950.    "cell_type": "code",
  951.    "execution_count": 22,
  952.    "metadata": {},
  953.    "outputs": [
  954.     {
  955.      "data": {
  956.       "text/plain": [
  957.        "[[('_C1',\n",
  958.        "   1*production_amount__('brazil',_'usa') + 1*production_amount__('germany',_'usa') + 1*production_amount__('india',_'usa') + 1*production_amount__('japan',_'usa') + 1*production_amount__('usa',_'usa') + -2719.6 = 0)],\n",
  959.        " [('_C2',\n",
  960.        "   1*production_amount__('brazil',_'germany') + 1*production_amount__('germany',_'germany') + 1*production_amount__('india',_'germany') + 1*production_amount__('japan',_'germany') + 1*production_amount__('usa',_'germany') + -84.1 = 0)],\n",
  961.        " [('_C3',\n",
  962.        "   1*production_amount__('brazil',_'japan') + 1*production_amount__('germany',_'japan') + 1*production_amount__('india',_'japan') + 1*production_amount__('japan',_'japan') + 1*production_amount__('usa',_'japan') + -1676.8 = 0)],\n",
  963.        " [('_C4',\n",
  964.        "   1*production_amount__('brazil',_'brazil') + 1*production_amount__('germany',_'brazil') + 1*production_amount__('india',_'brazil') + 1*production_amount__('japan',_'brazil') + 1*production_amount__('usa',_'brazil') + -145.4 = 0)],\n",
  965.        " [('_C5',\n",
  966.        "   1*production_amount__('brazil',_'india') + 1*production_amount__('germany',_'india') + 1*production_amount__('india',_'india') + 1*production_amount__('japan',_'india') + 1*production_amount__('usa',_'india') + -156.4 = 0)],\n",
  967.        " [('_C6',\n",
  968.        "   -1500*plant_status__('usa',_'highcap') + -500*plant_status__('usa',_'lowcap') + 1*production_amount__('usa',_'brazil') + 1*production_amount__('usa',_'germany') + 1*production_amount__('usa',_'india') + 1*production_amount__('usa',_'japan') + 1*production_amount__('usa',_'usa') + 0 <= 0)],\n",
  969.        " [('_C7',\n",
  970.        "   -1500*plant_status__('germany',_'highcap') + -500*plant_status__('germany',_'lowcap') + 1*production_amount__('germany',_'brazil') + 1*production_amount__('germany',_'germany') + 1*production_amount__('germany',_'india') + 1*production_amount__('germany',_'japan') + 1*production_amount__('germany',_'usa') + 0 <= 0)],\n",
  971.        " [('_C8',\n",
  972.        "   -1500*plant_status__('japan',_'highcap') + -500*plant_status__('japan',_'lowcap') + 1*production_amount__('japan',_'brazil') + 1*production_amount__('japan',_'germany') + 1*production_amount__('japan',_'india') + 1*production_amount__('japan',_'japan') + 1*production_amount__('japan',_'usa') + 0 <= 0)],\n",
  973.        " [('_C9',\n",
  974.        "   -1500*plant_status__('brazil',_'highcap') + -500*plant_status__('brazil',_'lowcap') + 1*production_amount__('brazil',_'brazil') + 1*production_amount__('brazil',_'germany') + 1*production_amount__('brazil',_'india') + 1*production_amount__('brazil',_'japan') + 1*production_amount__('brazil',_'usa') + 0 <= 0)],\n",
  975.        " [('_C10',\n",
  976.        "   -1500*plant_status__('india',_'highcap') + -500*plant_status__('india',_'lowcap') + 1*production_amount__('india',_'brazil') + 1*production_amount__('india',_'germany') + 1*production_amount__('india',_'india') + 1*production_amount__('india',_'japan') + 1*production_amount__('india',_'usa') + 0 <= 0)]]"
  977.       ]
  978.      },
  979.      "execution_count": 22,
  980.      "metadata": {},
  981.      "output_type": "execute_result"
  982.     }
  983.    ],
  984.    "source": [
  985.     "conList = []\n",
  986.     "for value in model.constraints.items():\n",
  987.     "    temp = [value]\n",
  988.     "    conList.append(temp)\n",
  989.     "#model.constraints.items()\n",
  990.     "conList"
  991.    ]
  992.   },
  993.   {
  994.    "cell_type": "code",
  995.    "execution_count": 23,
  996.    "metadata": {},
  997.    "outputs": [
  998.     {
  999.      "data": {
  1000.       "text/html": [
  1001.        "<div>\n",
  1002.        "<style scoped>\n",
  1003.        "    .dataframe tbody tr th:only-of-type {\n",
  1004.        "        vertical-align: middle;\n",
  1005.        "    }\n",
  1006.        "\n",
  1007.        "    .dataframe tbody tr th {\n",
  1008.        "        vertical-align: top;\n",
  1009.        "    }\n",
  1010.        "\n",
  1011.        "    .dataframe thead th {\n",
  1012.        "        text-align: right;\n",
  1013.        "    }\n",
  1014.        "</style>\n",
  1015.        "<table border=\"1\" class=\"dataframe\">\n",
  1016.        "  <thead>\n",
  1017.        "    <tr style=\"text-align: right;\">\n",
  1018.        "      <th></th>\n",
  1019.        "      <th>name</th>\n",
  1020.        "      <th>shadow price</th>\n",
  1021.        "      <th>slack</th>\n",
  1022.        "    </tr>\n",
  1023.        "  </thead>\n",
  1024.        "  <tbody>\n",
  1025.        "    <tr>\n",
  1026.        "      <th>0</th>\n",
  1027.        "      <td>_C1</td>\n",
  1028.        "      <td>12.0</td>\n",
  1029.        "      <td>-0.0</td>\n",
  1030.        "    </tr>\n",
  1031.        "    <tr>\n",
  1032.        "      <th>1</th>\n",
  1033.        "      <td>_C2</td>\n",
  1034.        "      <td>13.0</td>\n",
  1035.        "      <td>-0.0</td>\n",
  1036.        "    </tr>\n",
  1037.        "    <tr>\n",
  1038.        "      <th>2</th>\n",
  1039.        "      <td>_C3</td>\n",
  1040.        "      <td>10.0</td>\n",
  1041.        "      <td>-0.0</td>\n",
  1042.        "    </tr>\n",
  1043.        "    <tr>\n",
  1044.        "      <th>3</th>\n",
  1045.        "      <td>_C4</td>\n",
  1046.        "      <td>8.0</td>\n",
  1047.        "      <td>-0.0</td>\n",
  1048.        "    </tr>\n",
  1049.        "    <tr>\n",
  1050.        "      <th>4</th>\n",
  1051.        "      <td>_C5</td>\n",
  1052.        "      <td>8.0</td>\n",
  1053.        "      <td>-0.0</td>\n",
  1054.        "    </tr>\n",
  1055.        "    <tr>\n",
  1056.        "      <th>5</th>\n",
  1057.        "      <td>_C6</td>\n",
  1058.        "      <td>-6.0</td>\n",
  1059.        "      <td>-0.0</td>\n",
  1060.        "    </tr>\n",
  1061.        "    <tr>\n",
  1062.        "      <th>6</th>\n",
  1063.        "      <td>_C7</td>\n",
  1064.        "      <td>-7.0</td>\n",
  1065.        "      <td>-0.0</td>\n",
  1066.        "    </tr>\n",
  1067.        "    <tr>\n",
  1068.        "      <th>7</th>\n",
  1069.        "      <td>_C8</td>\n",
  1070.        "      <td>-7.0</td>\n",
  1071.        "      <td>-0.0</td>\n",
  1072.        "    </tr>\n",
  1073.        "    <tr>\n",
  1074.        "      <th>8</th>\n",
  1075.        "      <td>_C9</td>\n",
  1076.        "      <td>0.0</td>\n",
  1077.        "      <td>135.0</td>\n",
  1078.        "    </tr>\n",
  1079.        "    <tr>\n",
  1080.        "      <th>9</th>\n",
  1081.        "      <td>_C10</td>\n",
  1082.        "      <td>0.0</td>\n",
  1083.        "      <td>82.7</td>\n",
  1084.        "    </tr>\n",
  1085.        "  </tbody>\n",
  1086.        "</table>\n",
  1087.        "</div>"
  1088.       ],
  1089.       "text/plain": [
  1090.        "   name  shadow price  slack\n",
  1091.        "0   _C1          12.0   -0.0\n",
  1092.        "1   _C2          13.0   -0.0\n",
  1093.        "2   _C3          10.0   -0.0\n",
  1094.        "3   _C4           8.0   -0.0\n",
  1095.        "4   _C5           8.0   -0.0\n",
  1096.        "5   _C6          -6.0   -0.0\n",
  1097.        "6   _C7          -7.0   -0.0\n",
  1098.        "7   _C8          -7.0   -0.0\n",
  1099.        "8   _C9           0.0  135.0\n",
  1100.        "9  _C10           0.0   82.7"
  1101.       ]
  1102.      },
  1103.      "execution_count": 23,
  1104.      "metadata": {},
  1105.      "output_type": "execute_result"
  1106.     }
  1107.    ],
  1108.    "source": [
  1109.     "# print shadow price and slack\n",
  1110.     "sp = [{'name':name, 'shadow price':c.pi, 'slack':c.slack}\n",
  1111.     "     for name, c in model.constraints.items()]\n",
  1112.     "sp = pd.DataFrame(sp)\n",
  1113.     "sp"
  1114.    ]
  1115.   }
  1116.  ],
  1117.  "metadata": {
  1118.   "kernelspec": {
  1119.    "display_name": "Python 3",
  1120.    "language": "python",
  1121.    "name": "python3"
  1122.   },
  1123.   "language_info": {
  1124.    "codemirror_mode": {
  1125.     "name": "ipython",
  1126.     "version": 3
  1127.    },
  1128.    "file_extension": ".py",
  1129.    "mimetype": "text/x-python",
  1130.    "name": "python",
  1131.    "nbconvert_exporter": "python",
  1132.    "pygments_lexer": "ipython3",
  1133.    "version": "3.7.3"
  1134.   }
  1135.  },
  1136.  "nbformat": 4,
  1137.  "nbformat_minor": 2
  1138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top