Spaces:
Sleeping
Sleeping
Krishna Kumar S commited on
Commit ·
5b228ad
1
Parent(s): b8f8c5f
cc
Browse files- Intelligent_Sourcing.ipynb +1 -4
- Setup_Optimization_Problem.py +88 -3
Intelligent_Sourcing.ipynb
CHANGED
|
@@ -231,10 +231,7 @@
|
|
| 231 |
},
|
| 232 |
"outputs": [],
|
| 233 |
"source": [
|
| 234 |
-
"
|
| 235 |
-
"Weightage_Priority = 0.8\n",
|
| 236 |
-
"Weightage_distance = 0.6\n",
|
| 237 |
-
"Weightage_days = 0.4"
|
| 238 |
]
|
| 239 |
},
|
| 240 |
{
|
|
|
|
| 231 |
},
|
| 232 |
"outputs": [],
|
| 233 |
"source": [
|
| 234 |
+
"def"
|
|
|
|
|
|
|
|
|
|
| 235 |
]
|
| 236 |
},
|
| 237 |
{
|
Setup_Optimization_Problem.py
CHANGED
|
@@ -1,6 +1,91 @@
|
|
| 1 |
from pulp import *
|
| 2 |
import pandas as pd
|
| 3 |
-
from Read_Data import load_excel_data
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pulp import *
|
| 2 |
import pandas as pd
|
| 3 |
+
from Read_Data import load_excel_data
|
| 4 |
|
| 5 |
+
def create_sourcing_problem(weightage_dict, priority_df, warehouse_df, order_df, cost_df, distance_df, days_df):
|
| 6 |
+
"""
|
| 7 |
+
Creates and returns a linear programming problem for intelligent sourcing optimization.
|
| 8 |
+
|
| 9 |
+
Parameters:
|
| 10 |
+
weightage_dict (dict): Dictionary containing weightages for cost, priority, distance, and days.
|
| 11 |
+
priority_df (DataFrame): DataFrame containing priority values for each warehouse.
|
| 12 |
+
warehouse_df (DataFrame): DataFrame containing stock availability for each warehouse.
|
| 13 |
+
order_df (DataFrame): DataFrame containing order quantities for each product.
|
| 14 |
+
cost_df (DataFrame): DataFrame containing cost values.
|
| 15 |
+
distance_df (DataFrame): DataFrame containing distance values.
|
| 16 |
+
days_df (DataFrame): DataFrame containing expected delivery days.
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
LpProblem: Linear programming problem formulated for sourcing optimization.
|
| 20 |
+
"""
|
| 21 |
+
def min_max_scale(df, column):
|
| 22 |
+
min_val = df[column].min()
|
| 23 |
+
max_val = df[column].max()
|
| 24 |
+
df[column] = (df[column] - min_val) / (max_val - min_val)
|
| 25 |
+
return df
|
| 26 |
+
|
| 27 |
+
# Extract weightages
|
| 28 |
+
weightage_Cost = weightage_dict["Cost"]
|
| 29 |
+
weightage_Priority = weightage_dict["Priority"]
|
| 30 |
+
weightage_distance = weightage_dict["Distance"]
|
| 31 |
+
weightage_days = weightage_dict["Days"]
|
| 32 |
+
|
| 33 |
+
# Normalize data
|
| 34 |
+
priority_df = min_max_scale(priority_df, "Priority")
|
| 35 |
+
cost_df = min_max_scale(cost_df, "Cost")
|
| 36 |
+
distance_df = min_max_scale(distance_df, "Distance")
|
| 37 |
+
days_df = min_max_scale(days_df, "Days")
|
| 38 |
+
|
| 39 |
+
# Create dictionaries for optimization
|
| 40 |
+
Warehouses = warehouse_df['Warehouse'].tolist()
|
| 41 |
+
Products = warehouse_df.columns[1:]
|
| 42 |
+
stock = makeDict([Warehouses, Products], warehouse_df.drop('Warehouse', axis=1).values, default=0)
|
| 43 |
+
priority = makeDict([Warehouses], priority_df.drop('Warehouse', axis=1).values.reshape(len(Warehouses)), default=0)
|
| 44 |
+
Orders = order_df['Order'].tolist()
|
| 45 |
+
quantity = makeDict([Orders, Products], order_df.drop('Order', axis=1).values, default=0)
|
| 46 |
+
|
| 47 |
+
cost_values = cost_df["Cost"].values.reshape(len(Warehouses), len(Orders), len(Products))
|
| 48 |
+
cost = makeDict([Warehouses, Orders, Products], cost_values, default=0)
|
| 49 |
+
|
| 50 |
+
distance_values = distance_df["Distance"].values.reshape(len(Warehouses), len(Orders), len(Products))
|
| 51 |
+
distance = makeDict([Warehouses, Orders, Products], distance_values, default=0)
|
| 52 |
+
|
| 53 |
+
days_values = days_df["Days"].values.reshape(len(Warehouses), len(Orders), len(Products))
|
| 54 |
+
days = makeDict([Warehouses, Orders, Products], days_values, default=0)
|
| 55 |
+
|
| 56 |
+
# Define variables and routes
|
| 57 |
+
routes = [(w, o, s) for w in Warehouses for o in Orders for s in Products]
|
| 58 |
+
variable = LpVariable.dicts("Route", (Warehouses, Orders, Products), 0, None, LpInteger)
|
| 59 |
+
|
| 60 |
+
# Create optimization problem
|
| 61 |
+
prob = LpProblem("Sourcing_Problem", LpMinimize)
|
| 62 |
+
|
| 63 |
+
# Objective function
|
| 64 |
+
prob += lpSum(
|
| 65 |
+
variable[w][o][p] * (
|
| 66 |
+
(weightage_Cost * cost[w][o][p]) +
|
| 67 |
+
(weightage_Priority * -priority[w]) +
|
| 68 |
+
(weightage_distance * distance[w][o][p]) +
|
| 69 |
+
(weightage_days * days[w][o][p])
|
| 70 |
+
)
|
| 71 |
+
for (w, o, p) in routes
|
| 72 |
+
), "Sum_of_Costs"
|
| 73 |
+
|
| 74 |
+
# Stock Constraints
|
| 75 |
+
for w in Warehouses:
|
| 76 |
+
for p in Products:
|
| 77 |
+
prob += lpSum([variable[w][o][p] for o in Orders]) <= stock[w][p], f"Stock_Constraint_{p}_in_{w}"
|
| 78 |
+
|
| 79 |
+
# Order Constraints
|
| 80 |
+
for o in Orders:
|
| 81 |
+
for p in Products:
|
| 82 |
+
prob += lpSum([variable[w][o][p] for w in Warehouses]) == quantity[o][p], f"Order_Fulfillment_{p}_to_{o}"
|
| 83 |
+
|
| 84 |
+
return prob
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
filepath = 'Intelligent_Sourcing.xlsx' # Example file path
|
| 88 |
+
weightage_dict, priority_df, warehouse_df, order_df, cost_df, distance_df, days_df = load_excel_data(filepath)
|
| 89 |
+
|
| 90 |
+
prob = create_sourcing_problem(weightage_dict, priority_df, warehouse_df, order_df, cost_df, distance_df, days_df)
|
| 91 |
+
print(prob.objective)
|