Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "from pulp import *\n",
- "import pandas as pd"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>dmd</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>supply_region</th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>usa</th>\n",
- " <td>2719.6</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>germany</th>\n",
- " <td>84.1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>japan</th>\n",
- " <td>1676.8</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>brazil</th>\n",
- " <td>145.4</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>india</th>\n",
- " <td>156.4</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " dmd\n",
- "supply_region \n",
- "usa 2719.6\n",
- "germany 84.1\n",
- "japan 1676.8\n",
- "brazil 145.4\n",
- "india 156.4"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# first we import the data\n",
- "demand = pd.read_csv('C:/Users/jperrigan/Documents/plant_demand.csv', index_col = ['supply_region'])\n",
- "demand"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>usa</th>\n",
- " <th>germany</th>\n",
- " <th>japan</th>\n",
- " <th>brazil</th>\n",
- " <th>india</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>supply_region</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>usa</th>\n",
- " <td>6</td>\n",
- " <td>13</td>\n",
- " <td>20</td>\n",
- " <td>12</td>\n",
- " <td>17</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>germany</th>\n",
- " <td>13</td>\n",
- " <td>6</td>\n",
- " <td>14</td>\n",
- " <td>14</td>\n",
- " <td>13</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>japan</th>\n",
- " <td>20</td>\n",
- " <td>14</td>\n",
- " <td>3</td>\n",
- " <td>21</td>\n",
- " <td>9</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>brazil</th>\n",
- " <td>12</td>\n",
- " <td>14</td>\n",
- " <td>21</td>\n",
- " <td>8</td>\n",
- " <td>21</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>india</th>\n",
- " <td>22</td>\n",
- " <td>13</td>\n",
- " <td>10</td>\n",
- " <td>23</td>\n",
- " <td>8</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " usa germany japan brazil india\n",
- "supply_region \n",
- "usa 6 13 20 12 17\n",
- "germany 13 6 14 14 13\n",
- "japan 20 14 3 21 9\n",
- "brazil 12 14 21 8 21\n",
- "india 22 13 10 23 8"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "var_cost = pd.read_csv('C:/Users/jperrigan/Documents/plant_var_cost.csv', index_col = ['supply_region'])\n",
- "var_cost"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>lowcap</th>\n",
- " <th>highcap</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>supply_region</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>usa</th>\n",
- " <td>6500</td>\n",
- " <td>9500</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>germany</th>\n",
- " <td>4980</td>\n",
- " <td>7270</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>japan</th>\n",
- " <td>6230</td>\n",
- " <td>9100</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>brazil</th>\n",
- " <td>3230</td>\n",
- " <td>4730</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>india</th>\n",
- " <td>2110</td>\n",
- " <td>3080</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " lowcap highcap\n",
- "supply_region \n",
- "usa 6500 9500\n",
- "germany 4980 7270\n",
- "japan 6230 9100\n",
- "brazil 3230 4730\n",
- "india 2110 3080"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "fix_cost = pd.read_csv('C:/Users/jperrigan/Documents/plant_fix_cost.csv', index_col = ['supply_region'])\n",
- "fix_cost"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>lowcap</th>\n",
- " <th>highcap</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>supply_region</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>usa</th>\n",
- " <td>500</td>\n",
- " <td>1500</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>germany</th>\n",
- " <td>500</td>\n",
- " <td>1500</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>japan</th>\n",
- " <td>500</td>\n",
- " <td>1500</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>brazil</th>\n",
- " <td>500</td>\n",
- " <td>1500</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>india</th>\n",
- " <td>500</td>\n",
- " <td>1500</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " lowcap highcap\n",
- "supply_region \n",
- "usa 500 1500\n",
- "germany 500 1500\n",
- "japan 500 1500\n",
- "brazil 500 1500\n",
- "india 500 1500"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "cap = pd.read_csv('C:/Users/jperrigan/Documents/plant_cap.csv', index_col = ['supply_region'])\n",
- "cap"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [],
- "source": [
- "# define the model and initialize class of model\n",
- "# out goal is to minimize costs, so it's an LpMinimize problem\n",
- "model = LpProblem(\"Capacitated Plant Location Model\", LpMinimize)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['lowcap', 'highcap']"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# let's create a list with our capacity options using the dataframes\n",
- "capacity = list(cap.columns.values)\n",
- "capacity"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['usa', 'germany', 'japan', 'brazil', 'india']"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "location = list(cap.index.values)\n",
- "location"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{('usa', 'usa'): production_amount__('usa',_'usa'),\n",
- " ('usa', 'germany'): production_amount__('usa',_'germany'),\n",
- " ('usa', 'japan'): production_amount__('usa',_'japan'),\n",
- " ('usa', 'brazil'): production_amount__('usa',_'brazil'),\n",
- " ('usa', 'india'): production_amount__('usa',_'india'),\n",
- " ('germany', 'usa'): production_amount__('germany',_'usa'),\n",
- " ('germany', 'germany'): production_amount__('germany',_'germany'),\n",
- " ('germany', 'japan'): production_amount__('germany',_'japan'),\n",
- " ('germany', 'brazil'): production_amount__('germany',_'brazil'),\n",
- " ('germany', 'india'): production_amount__('germany',_'india'),\n",
- " ('japan', 'usa'): production_amount__('japan',_'usa'),\n",
- " ('japan', 'germany'): production_amount__('japan',_'germany'),\n",
- " ('japan', 'japan'): production_amount__('japan',_'japan'),\n",
- " ('japan', 'brazil'): production_amount__('japan',_'brazil'),\n",
- " ('japan', 'india'): production_amount__('japan',_'india'),\n",
- " ('brazil', 'usa'): production_amount__('brazil',_'usa'),\n",
- " ('brazil', 'germany'): production_amount__('brazil',_'germany'),\n",
- " ('brazil', 'japan'): production_amount__('brazil',_'japan'),\n",
- " ('brazil', 'brazil'): production_amount__('brazil',_'brazil'),\n",
- " ('brazil', 'india'): production_amount__('brazil',_'india'),\n",
- " ('india', 'usa'): production_amount__('india',_'usa'),\n",
- " ('india', 'germany'): production_amount__('india',_'germany'),\n",
- " ('india', 'japan'): production_amount__('india',_'japan'),\n",
- " ('india', 'brazil'): production_amount__('india',_'brazil'),\n",
- " ('india', 'india'): production_amount__('india',_'india')}"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# next we define our decision variables\n",
- "# what variables can we control?\n",
- "# we can control the quantity that we produce in each location and ship to each location\n",
- "production = LpVariable.dicts(\"production_amount_\", [(i, j) for i in location for j in location], lowBound = 0, cat = 'Continuous')\n",
- "production"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{('usa', 'lowcap'): plant_status__('usa',_'lowcap'),\n",
- " ('germany', 'lowcap'): plant_status__('germany',_'lowcap'),\n",
- " ('japan', 'lowcap'): plant_status__('japan',_'lowcap'),\n",
- " ('brazil', 'lowcap'): plant_status__('brazil',_'lowcap'),\n",
- " ('india', 'lowcap'): plant_status__('india',_'lowcap'),\n",
- " ('usa', 'highcap'): plant_status__('usa',_'highcap'),\n",
- " ('germany', 'highcap'): plant_status__('germany',_'highcap'),\n",
- " ('japan', 'highcap'): plant_status__('japan',_'highcap'),\n",
- " ('brazil', 'highcap'): plant_status__('brazil',_'highcap'),\n",
- " ('india', 'highcap'): plant_status__('india',_'highcap')}"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# we can choose whether to produce at a location\n",
- "# binary variable because the plant is either one or off\n",
- "status = LpVariable.dicts(\"plant_status_\", \n",
- " [(i,s) for s in capacity for i in location], cat='Binary')\n",
- "status"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [],
- "source": [
- "# now let's create out objective function\n",
- "# we're trying to minimize production costs\n",
- "# which means we're trying to minimize the sum of fixed costs for each location (dependent on whether it's on or off)\n",
- "# plus the variable costs for each location (dependent on how many units produced)\n",
- "# the first part contains the fixed costs multiplied by the binary of off or on\n",
- "# the second part sums up the variable costs for each location-location tuple (produced in location A, shipped to A, etc.)\n",
- "model += (lpSum([fix_cost.loc[i,s] * status[(i,s)] \n",
- " for s in capacity for i in location])\n",
- " + lpSum([var_cost.loc[i,j] * production[(i,j)] \n",
- " for i in location for j in location]))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [],
- "source": [
- "# next step: constraints\n",
- "# the constraints are that total production equals total demand\n",
- "# the sum of the quantity produced by all locations (i) and shipped to location j should equal the demand for each location j\n",
- "# We'll need to loop over all the regions and sum the quantity arriving to that region\n",
- "# which should be equal to the demand from that region\n",
- "# the for loop basically means for each \"row\"\n",
- "for j in location:\n",
- " model += lpSum([production[(i, j)] for i in location]) == demand.loc[j,'dmd']"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [],
- "source": [
- "# the next constraint is that total production of a particular region is less than or equal to the total production capacity\n",
- "# of that region\n",
- "# total production in a particular region <= total production capacity of that region, \n",
- "# which depends on whether that plant is on (binary variable)\n",
- "for i in location:\n",
- " model += lpSum([production[(i, j)] for j in location]) <= lpSum([cap.loc[i,s] * status[i,s] \n",
- " for s in capacity])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [],
- "source": [
- "# now let's add a logical constraint\n",
- "# 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",
- "# this is a binary variable, so if y(usa, high) = 1 then y(germany, low) also equals one\n",
- "# so y(usa, high) - y(germany, low) must equal zero, or be less than or equal to zero\n",
- "#model += status['usa', 'highcap'] - status['germany', 'lowcap'] <= 0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Status: Optimal\n"
- ]
- }
- ],
- "source": [
- "model.solve()\n",
- "print(\"Status:\", LpStatus[model.status])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "plant_status__('brazil',_'highcap') = 1.0\n",
- "plant_status__('brazil',_'lowcap') = 0.0\n",
- "plant_status__('germany',_'highcap') = 0.0\n",
- "plant_status__('germany',_'lowcap') = 0.0\n",
- "plant_status__('india',_'highcap') = 0.0\n",
- "plant_status__('india',_'lowcap') = 1.0\n",
- "plant_status__('japan',_'highcap') = 1.0\n",
- "plant_status__('japan',_'lowcap') = 0.0\n",
- "plant_status__('usa',_'highcap') = 1.0\n",
- "plant_status__('usa',_'lowcap') = 0.0\n",
- "production_amount__('brazil',_'brazil') = 145.4\n",
- "production_amount__('brazil',_'germany') = 0.0\n",
- "production_amount__('brazil',_'india') = 0.0\n",
- "production_amount__('brazil',_'japan') = 0.0\n",
- "production_amount__('brazil',_'usa') = 1219.6\n",
- "production_amount__('germany',_'brazil') = 0.0\n",
- "production_amount__('germany',_'germany') = 0.0\n",
- "production_amount__('germany',_'india') = 0.0\n",
- "production_amount__('germany',_'japan') = 0.0\n",
- "production_amount__('germany',_'usa') = 0.0\n",
- "production_amount__('india',_'brazil') = 0.0\n",
- "production_amount__('india',_'germany') = 84.1\n",
- "production_amount__('india',_'india') = 156.4\n",
- "production_amount__('india',_'japan') = 176.8\n",
- "production_amount__('india',_'usa') = 0.0\n",
- "production_amount__('japan',_'brazil') = 0.0\n",
- "production_amount__('japan',_'germany') = 0.0\n",
- "production_amount__('japan',_'india') = 0.0\n",
- "production_amount__('japan',_'japan') = 1500.0\n",
- "production_amount__('japan',_'usa') = 0.0\n",
- "production_amount__('usa',_'brazil') = 0.0\n",
- "production_amount__('usa',_'germany') = 0.0\n",
- "production_amount__('usa',_'india') = 0.0\n",
- "production_amount__('usa',_'japan') = 0.0\n",
- "production_amount__('usa',_'usa') = 1500.0\n"
- ]
- }
- ],
- "source": [
- "for v in model.variables():\n",
- " print(v.name, \"=\", v.varValue)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "The total cost is $ 58850.899999999994\n"
- ]
- }
- ],
- "source": [
- "print(\"The total cost is $\",model.objective.value())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [],
- "source": [
- "# prints all the model specifications to a text file\n",
- "model.writeLP('C:/Users/jperrigan/Documents/solution.lp')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>prod</th>\n",
- " <th>quant</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>usa to usa</td>\n",
- " <td>1500.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>usa to germany</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2</th>\n",
- " <td>usa to japan</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>3</th>\n",
- " <td>usa to brazil</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>4</th>\n",
- " <td>usa to india</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>5</th>\n",
- " <td>germany to usa</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>6</th>\n",
- " <td>germany to germany</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>7</th>\n",
- " <td>germany to japan</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>8</th>\n",
- " <td>germany to brazil</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>9</th>\n",
- " <td>germany to india</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>10</th>\n",
- " <td>japan to usa</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>11</th>\n",
- " <td>japan to germany</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>12</th>\n",
- " <td>japan to japan</td>\n",
- " <td>1500.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>13</th>\n",
- " <td>japan to brazil</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>14</th>\n",
- " <td>japan to india</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>15</th>\n",
- " <td>brazil to usa</td>\n",
- " <td>1219.6</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>16</th>\n",
- " <td>brazil to germany</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>17</th>\n",
- " <td>brazil to japan</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>18</th>\n",
- " <td>brazil to brazil</td>\n",
- " <td>145.4</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>19</th>\n",
- " <td>brazil to india</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>20</th>\n",
- " <td>india to usa</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>21</th>\n",
- " <td>india to germany</td>\n",
- " <td>84.1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>22</th>\n",
- " <td>india to japan</td>\n",
- " <td>176.8</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>23</th>\n",
- " <td>india to brazil</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>24</th>\n",
- " <td>india to india</td>\n",
- " <td>156.4</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " prod quant\n",
- "0 usa to usa 1500.0\n",
- "1 usa to germany 0.0\n",
- "2 usa to japan 0.0\n",
- "3 usa to brazil 0.0\n",
- "4 usa to india 0.0\n",
- "5 germany to usa 0.0\n",
- "6 germany to germany 0.0\n",
- "7 germany to japan 0.0\n",
- "8 germany to brazil 0.0\n",
- "9 germany to india 0.0\n",
- "10 japan to usa 0.0\n",
- "11 japan to germany 0.0\n",
- "12 japan to japan 1500.0\n",
- "13 japan to brazil 0.0\n",
- "14 japan to india 0.0\n",
- "15 brazil to usa 1219.6\n",
- "16 brazil to germany 0.0\n",
- "17 brazil to japan 0.0\n",
- "18 brazil to brazil 145.4\n",
- "19 brazil to india 0.0\n",
- "20 india to usa 0.0\n",
- "21 india to germany 84.1\n",
- "22 india to japan 176.8\n",
- "23 india to brazil 0.0\n",
- "24 india to india 156.4"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "prod = [{'prod': \"{} to {}\".format(i, j), 'quant':production[(i, j)].varValue} for i in location for j in location]\n",
- "prod = pd.DataFrame(prod)\n",
- "prod"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>highcap</th>\n",
- " <th>lowcap</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>location</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>usa</th>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>germany</th>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>japan</th>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>brazil</th>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>india</th>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " highcap lowcap\n",
- "location \n",
- "usa 1.0 0.0\n",
- "germany 0.0 0.0\n",
- "japan 1.0 0.0\n",
- "brazil 1.0 0.0\n",
- "india 0.0 1.0"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "stat = [{'location':i, 'lowcap':status[(i, capacity[0])].varValue, 'highcap':status[(i, capacity[1])].varValue} \n",
- " for i in location]\n",
- "stat = pd.DataFrame(stat).set_index('location')\n",
- "stat"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[[('_C1',\n",
- " 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",
- " [('_C2',\n",
- " 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",
- " [('_C3',\n",
- " 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",
- " [('_C4',\n",
- " 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",
- " [('_C5',\n",
- " 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",
- " [('_C6',\n",
- " -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",
- " [('_C7',\n",
- " -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",
- " [('_C8',\n",
- " -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",
- " [('_C9',\n",
- " -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",
- " [('_C10',\n",
- " -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)]]"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "conList = []\n",
- "for value in model.constraints.items():\n",
- " temp = [value]\n",
- " conList.append(temp)\n",
- "#model.constraints.items()\n",
- "conList"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>name</th>\n",
- " <th>shadow price</th>\n",
- " <th>slack</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>_C1</td>\n",
- " <td>12.0</td>\n",
- " <td>-0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>_C2</td>\n",
- " <td>13.0</td>\n",
- " <td>-0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2</th>\n",
- " <td>_C3</td>\n",
- " <td>10.0</td>\n",
- " <td>-0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>3</th>\n",
- " <td>_C4</td>\n",
- " <td>8.0</td>\n",
- " <td>-0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>4</th>\n",
- " <td>_C5</td>\n",
- " <td>8.0</td>\n",
- " <td>-0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>5</th>\n",
- " <td>_C6</td>\n",
- " <td>-6.0</td>\n",
- " <td>-0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>6</th>\n",
- " <td>_C7</td>\n",
- " <td>-7.0</td>\n",
- " <td>-0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>7</th>\n",
- " <td>_C8</td>\n",
- " <td>-7.0</td>\n",
- " <td>-0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>8</th>\n",
- " <td>_C9</td>\n",
- " <td>0.0</td>\n",
- " <td>135.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>9</th>\n",
- " <td>_C10</td>\n",
- " <td>0.0</td>\n",
- " <td>82.7</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " name shadow price slack\n",
- "0 _C1 12.0 -0.0\n",
- "1 _C2 13.0 -0.0\n",
- "2 _C3 10.0 -0.0\n",
- "3 _C4 8.0 -0.0\n",
- "4 _C5 8.0 -0.0\n",
- "5 _C6 -6.0 -0.0\n",
- "6 _C7 -7.0 -0.0\n",
- "7 _C8 -7.0 -0.0\n",
- "8 _C9 0.0 135.0\n",
- "9 _C10 0.0 82.7"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# print shadow price and slack\n",
- "sp = [{'name':name, 'shadow price':c.pi, 'slack':c.slack}\n",
- " for name, c in model.constraints.items()]\n",
- "sp = pd.DataFrame(sp)\n",
- "sp"
- ]
- }
- ],
- "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.7.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement