-
Notifications
You must be signed in to change notification settings - Fork 0
/
transportation_simplex.py
224 lines (148 loc) · 4.86 KB
/
transportation_simplex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- coding: utf-8 -*-
"""Transportation_simplex.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1_PIKv9i3sjIQHXmAKOYrdKS455a0LmSx
# **Importing dataset file on python**
"""
from google.colab import drive
drive.mount('/content/drive')
#linking google drive
import pandas as pd
import io
#reading csv
df=pd.read_csv('/content/drive/MyDrive/Miniproject/Supplychain_dataset.csv')
df.head()
"""Dataset description"""
df.describe()
"""### **Supply Data**"""
#extracting supply data from csv file dataframe
supply_data=df.loc[0:9216,['Origin Plant','Supply']]
supply_data=supply_data.drop_duplicates()
supply_data=supply_data.reset_index(drop=True)
supply_data
"""Reordring according to our problem"""
new_index=[1,3,4,6,0]
supply_data=supply_data.reindex(new_index)
supply_data=supply_data.reset_index(drop=True)
supply_data
"""List of plants with supply"""
plants=supply_data['Origin Plant'].tolist()
plants
"""Modifying to fit into simplex problem"""
import numpy as np
supply_data_sim=supply_data.drop('Origin Plant', axis=1)
supply_data_sim.columns=np.arange(1)
supply_data_sim #dataframe to be inserted in problem dataframe
"""### **Demand Data**"""
#extracting demand data from csv file dataframe
demand_data=df.loc[0:9216,['Destination Port','Demand']]
demand_data=demand_data.drop_duplicates()
demand_data=demand_data.reset_index(drop=True)
demand_data
"""Reordring according to our problem"""
new_index=[1,2,0]
demand_data=demand_data.reindex(new_index)
demand_data=demand_data.reset_index(drop=True)
demand_data
"""List of ports with demand """
ports=demand_data['Destination Port'].tolist()
ports
"""Modifying to fit into simplex problem"""
demand_data_sim=demand_data.swapaxes("index","columns")
demand_data_sim=demand_data_sim.drop('Destination Port')
demand_data_sim=demand_data_sim.reset_index(drop=True)
demand_data_sim #dataframe to be inserted in problem dataframe
"""### **Plant count**"""
df['Origin Plant'].value_counts()
"""### **Port count**"""
df['Destination Port'].value_counts()
"""# **Simplex problem**"""
pr=pd.read_csv('/content/drive/MyDrive/Miniproject/Supplychain_problemset.csv', header=None)
#problem dataframe
pr
"""Appending demand and supply values"""
pr=pr.append(demand_data_sim)
pr=pr.reset_index(drop=True)
pr
pr=pd.concat([pr,supply_data_sim],axis=1)
pr.columns=[0,1,2,3]
pr
"""Renaming axes"""
pr.columns=['PORT04','PORT05','PORT09','Supply']
pr.index=['PLANT03','PLANT04','PLANT12','PLANT13','PLANT16','Demand']
pr=pr.swapaxes("index","columns")
pr
"""### Validation check for demand and supply equality"""
tot_demand=pr['Demand'].sum()
tot_supply=supply_data['Supply'].sum()
if tot_demand>tot_supply:
dif=tot_demand-tot_supply
print("Demand is greater than supply by ",dif)
elif tot_demand<tot_supply:
dif=tot_supply-tot_demand
print("Demand is lesser than supply by ",dif)
"""### Create dummy supply plant"""
dummysupply=[0,0,0,dif]
pr.insert(5,'Dummy_plant',dummysupply,True)
pr
"""Dataframe without demand supply values"""
prdf=pr.drop(['Demand'],axis=1)
prdf=prdf.drop(['Supply'],axis=0)
prdf
"""### **Converting dataframe to dictionary to be solved using PuLP**"""
pr_dict=prdf.to_dict()
pr_dict
"""Updated plants list"""
plants=list(pr_dict.keys())
plants
"""Updated supply data dataframe"""
newsupply=['Dummy_plant',dif]
supply_data.loc[len(supply_data.index)]=newsupply
supply_data
"""# **FINAL PROBLEM**"""
pr
#problem dataframe
plants
ports
pr_dict
#problem dictionary
demand_data
demand={demand_data['Destination Port'].tolist()[i]:demand_data['Demand'].tolist()[i] for i in range (len(ports))}
demand #dictionary of ports and demand
supply_data
supply={supply_data['Origin Plant'].tolist()[i]:supply_data['Supply'].tolist()[i] for i in range (len(plants))}
supply #dictionary of plants and supply
"""# **Solving Transportation Simplex**
### **Installing PuLP for solving transportation simplex**
"""
!pip install PuLP
from pulp import *
prob = LpProblem("Transportation", LpMinimize)
"""## List of avaliable routes between plants and ports"""
routes=[(i,j) for i in plants for j in ports]
routes
"""## Defining decision variable"""
amount_vars=LpVariable.dicts("X",(plants,ports),0)
amount_vars
"""## Defining objective function"""
prob += lpSum(amount_vars[i][j]*pr_dict[i][j] for (i,j) in routes)
prob
"""## Subject to constraints"""
for j in ports:
prob += lpSum(amount_vars[i][j] for i in plants) >= demand[j]
for i in plants:
prob += lpSum(amount_vars[i][j] for j in ports) <= supply[i]
prob
"""Problem solution status code"""
prob.solve()
#1 for feasible
#-1 for infeasible
"""Solution status: optimal?"""
print("Status:", LpStatus[prob.status])
"""## Solution for all variables"""
for v in prob.variables():
if v.varValue > 0:
print(v.name, "=", v.varValue)
"""# **Final Answer**"""
print("Minimization solution = ", value(prob.objective))