File size: 3,163 Bytes
8bb3480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ee2f87
 
8bb3480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ee2f87
8bb3480
 
 
 
 
 
 
 
 
 
 
 
6ee2f87
8bb3480
 
 
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
from pulp import *
import pandas as pd
from read_data import load_excel_data
from create_optimization_problem import create_sourcing_problem

def solve_sourcing_problem(prob, Warehouses, Products, Stock, Priority, Orders, Quantity, Variable):
    """
    Solves the sourcing optimization problem using PuLP.

    Parameters:
    prob (LpProblem): The linear programming problem instance.
    Warehouses (list): List of warehouse names.
    Products (list): List of product names.
    Stock (dict): Dictionary containing initial stock levels for each warehouse and product.
    Priority (dict): Dictionary containing priority levels for each order.
    Orders (list): List of order identifiers.
    Quantity (dict): Dictionary of order quantities for each product.
    Variable (dict): Dictionary of PuLP variables representing the decision variables.

    Returns:
    tuple: A tuple containing a string and two DataFrames:
        - LpStatus[prob.status] (str): The status of the solution.
        - fulfillment_solution: Contains details of supply quantities for each order from each warehouse.
        - warehouse_stock_status: Contains initial stock, supplied stock, and remaining stock levels.
    """
    
    # The problem is solved using PuLP's choice of Solver
    prob.solve()

    # The status of the solution is printed to the screen
    print("***** Solution Status *****")
    print("Status:", LpStatus[prob.status])


    # Fulfillment Solution
    fulfillment_solution = []
    for w in Warehouses:
        for p in Products:
            for o in Orders:
                fulfillment_solution.append({
                    "Warehouse": w,
                    "Product": p,
                    "Order": o,
                    "Supply Quantity": Variable[w][o][p].varValue
                })
    fulfillment_solution = pd.DataFrame(fulfillment_solution)

    # Stock Status
    warehouse_stock_status = []

    for w in Warehouses:
        for p in Products:
            warehouse_stock_status.append({
                "Warehouse": w,
                "Product": p,
                "Initial Stock": Stock[w][p],
                "Supplied Stock": sum(Variable[w][o][p].varValue for o in Orders),
                "Remaining Stock": Stock[w][p] - sum(Variable[w][o][p].varValue for o in Orders)
            })
    warehouse_stock_status = pd.DataFrame(warehouse_stock_status)

    return LpStatus[prob.status], fulfillment_solution, warehouse_stock_status



if __name__ == "__main__":
    filepath = 'Intelligent_Sourcing.xlsx'  # Example file path
    weightage_dict, priority_df, warehouse_df, order_df, cost_df, distance_df, days_df,  = load_excel_data(filepath)
    
    # Create LP Problem
    prob, Warehouses, Products, Stock, Priority, Orders, Quantity, Variable = create_sourcing_problem(weightage_dict, priority_df, warehouse_df, order_df, cost_df, distance_df, days_df)
    #print(prob.objective)

    # Solve LP Problem
    status, fulfillment_solution, warehouse_stock_status = solve_sourcing_problem(prob, Warehouses, Products, Stock, Priority, Orders, Quantity, Variable)

    print(fulfillment_solution)
    print(warehouse_stock_status)