Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

In below code it suppose to incur setup cost each time the production occurs, but when I…

  1. User

    In below code it suppose to incur setup cost each time the production occurs, but when I printed the binary decisions it shows nothing works, actually binary has to be 1 if production occurs otherwise 0

    Code: import pandas as pd import pulp from pulp import LpVariable, LpProblem, lpSum, LpMinimize, LpInteger, LpBinary

    filename = "Multi Period Multi Commodity SCNWD.xlsx"

    Read data from the file demand_df = pd.read_excel(filename, sheet_name="Demand") plant_capacity_df = pd.read_excel(filename, sheet_name="Plant Capacity") warehouse_capacity_df = pd.read_excel(filename, sheet_name="Warehouse Capacity per Product") fixed_plant_cost_df = pd.read_excel(filename, sheet_name="Fixed Plant Cost") variable_production_cost_df = pd.read_excel(filename, sheet_name="Variable Production Cost") inbound_cost_df = pd.read_excel(filename, sheet_name="Inbound Cost") outbound_cost_df = pd.read_excel(filename, sheet_name="Outbound Cost") fixed_warehouse_cost_df = pd.read_excel(filename, sheet_name="Fixed Warehouse Cost") holding_cost_df = pd.read_excel(filename, sheet_name="Holding Cost") setup_cost_df = pd.read_excel(filename, sheet_name="Setup Cost")

    Create dictionaries demand = demand_df.set_index(["customer", "period", "product"])["demand"].to_dict() plant_capacity = plant_capacity_df.set_index(["plant", "product"])["capacity"].to_dict() warehouse_capacity = warehouse_capacity_df.set_index(["warehouse", "product"])["capacity"].to_dict() fixed_plant_cost = fixed_plant_cost_df.set_index("plant")["cost"].to_dict() variable_production_cost = variable_production_cost_df.set_index(["plant", "product"])["cost"].to_dict() inbound_cost = inbound_cost_df.set_index(["plant","warehouse", "product"])["cost"].to_dict() outbound_cost = outbound_cost_df.set_index(["warehouse", "customer", "product"])["cost"].to_dict() fixed_warehouse_cost = fixed_warehouse_cost_df.set_index("warehouse")["cost"].to_dict() holding_cost = holding_cost_df.set_index("product")["cost"].to_dict() setup_cost= setup_cost_df.set_index(["plant", "product"])["cost"].to_dict()

    Define Variables customers= demand_df['customer'].unique().tolist() plants= plant_capacity_df['plant'].unique().tolist() warehouses= warehouse_capacity_df['warehouse'].unique().tolist() products= demand_df['product'].unique().tolist() periods= demand_df['period'].unique().tolist()

    Problem definition problem = pulp.LpProblem("Multi-Commodity_Flow_Problem", pulp.LpMinimize)

    Decision variables x = pulp.LpVariable.dicts("x", (plants, warehouses, products, periods), lowBound=0, cat="Continuous") y = pulp.LpVariable.dicts("y", (warehouses, customers, products, periods), lowBound=0, cat="Continuous") z_plant = pulp.LpVariable.dicts("z_plant", plants, cat="Binary") z_warehouse = pulp.LpVariable.dicts("z_warehouse", warehouses, cat="Binary") inventory = pulp.LpVariable.dicts("inv", (products, range(0,13)), lowBound=0, cat="Continuous")

    binary = pulp.LpVariable.dicts("binary", (plants, products, periods), lowBound=0, cat="Binary")

    production_lot_size = pulp.LpVariable.dicts("lot_size", (plants, products, periods), lowBound=0, cat="Continuous") Objective problem += ( pulp.lpSum([fixed_plant_cost[p] * z_plant[p] for p in plants])

    • pulp.lpSum( [ variable_production_cost[(p, pr)] * x[p][w][pr][t] for p in plants for w in warehouses for pr in products for t in periods ] )
    • pulp.lpSum( [ inbound_cost[(p, w, pr)] * x[p][w][pr][t] for p in plants for w in warehouses for pr in products for t in periods ] )
    • pulp.lpSum( [ outbound_cost[(w, c, pr)] * y[w][c][pr][t] for w in warehouses for c in customers for pr in products for t in periods ] )
    • pulp.lpSum([fixed_warehouse_cost[w] * z_warehouse[w] for w in warehouses])
    • pulp.lpSum( [ binary[p][pr][t] * setup_cost[(p, pr)] for p in plants for pr in products for t in periods ] )
    • pulp.lpSum( [ holding_cost[pr] * inventory[pr][t] for pr in products for t in periods ] ) )

    Constraints for p in plants: for pr in products: for t in periods: problem += production_lot_size[p][pr][t] >= binary[p][pr][t] for pr in products: for t in periods: for w in warehouses: problem += pulp.lpSum([x[p][w][pr][t] for p in plants]) == pulp.lpSum([y[w][c][pr][t] for c in customers]) + inventory[pr][t]

    for c in customers: for pr in products: for t in periods: problem += pulp.lpSum([y[w][c][pr][t] for w in warehouses]) >= demand.get((c, t, pr), 0)

    for p in plants: for w in warehouses: for pr in products: for t in periods: problem += x[p][w][pr][t] <= plant_capacity.get((p, pr), 0) * z_plant[p]

    for w in warehouses: for pr in products: for t in periods: problem += pulp.lpSum([y[w][c][pr][t] for c in customers]) <= warehouse_capacity.get((w, pr), 0) * z_warehouse[w]

    Solve the model with a time limit of 60 seconds time_limit_in_seconds = 60*2

    problem.solve(pulp.PULP_CBC_CMD(msg=1, timeLimit=time_limit_in_seconds))

    Binary output:

    for p in plants: for pr in products: for t in periods: if binary[p][pr][t].varValue > 0: print(binary[p][pr][t].varValue)

  2. Assistant

    Please make sure to properly format the Python code to avoid any indentation errors. I have formatted the code for you below. I have also added a condition in the binary constraint to set binary[p][pr][t] to 1 when the production_lot_size[p][pr][t] is greater than 0. Please try using the following modified code:

    import pandas as pd
    import pulp
    from pulp import LpVariable, LpProblem, lpSum, LpMinimize, LpInteger, LpBinary
    
    filename = "Multi Period Multi Commodity SCNWD.xlsx"
    
    # Read data from the file
    demand_df = pd.read_excel(filename, sheet_name="Demand")
    plant_capacity_df = pd.read_excel(filename, sheet_name="Plant Capacity")
    warehouse_capacity_df = pd.read_excel(filename, sheet_name="Warehouse Capacity per Product")
    fixed_plant_cost_df = pd.read_excel(filename, sheet_name="Fixed Plant Cost")
    variable_production_cost_df = pd.read_excel(filename, sheet_name="Variable Production Cost")
    inbound_cost_df = pd.read_excel(filename, sheet_name="Inbound Cost")
    outbound_cost_df = pd.read_excel(filename, sheet_name="Outbound Cost")
    fixed_warehouse_cost_df = pd.read_excel(filename, sheet_name="Fixed Warehouse Cost")
    holding_cost_df = pd.read_excel(filename, sheet_name="Holding Cost")
    setup_cost_df = pd.read_excel(filename, sheet_name="Setup Cost")
    
    # Create dictionaries
    demand = demand_df.set_index(["customer", "period", "product"])["demand"].to_dict()
    plant_capacity = plant_capacity_df.set_index(["plant", "product"])["capacity"].to_dict()
    warehouse_capacity = warehouse_capacity_df.set_index(["warehouse", "product"])["capacity"].to_dict()
    fixed_plant_cost = fixed_plant_cost_df.set_index("plant")["cost"].to_dict()
    variable_production_cost ...
    
    # Define Variables
    customers = demand_df['customer'].unique().tolist()
    plants = plant_capacity_df['plant'].unique().tolist()
    warehouses= warehouse_capacity_df['warehouse'].unique().tolist()
    products = demand_df['product'].unique().tolist()
    periods = demand_df['period'].unique().tolist()
    
    # Problem definition
    problem = pulp.LpProblem("Multi-Commodity_Flow_Problem", pulp.LpMinimize)
    
    # Decision variables
    x = pulp.LpVariable.dicts("x", (plants, warehouses, products, periods), lowBound=0, cat="Continuous")
    y = pulp.LpVariable.dicts("y", (warehouses, customers, products, periods), lowBound=0, cat="Continuous")
    z_plant = pulp.LpVariable.dicts("z_plant", plants, cat="Binary")
    z_warehouse = pulp.LpVariable.dicts("z_warehouse", warehouses, cat="Binary")
    inventory = pulp.LpVariable.dicts("inv", (products, range(0, 13)), lowBound=0, cat="Continuous")
    
    binary = pulp.LpVariable.dicts("binary", (plants, products, periods), lowBound=0, cat="Binary")
    
    production_lot_size = pulp.LpVariable.dicts("lot_size", (plants, products, periods), lowBound=0, cat="Continuous")
    
    # Objective
    problem += (
        pulp.lpSum([fixed_plant_cost[p] * z_plant[p] for p in plants])
        + pulp.lpSum(
            [
                variable_production_cost[(p, pr)] * x[p][w][pr][t]
                for p in plants
                for w in warehouses
                for pr in products
                for t in periods
            ]
        )
        + pulp.lpSum(
            [
                inbound_cost[(p, w, pr)] * x[p][w][pr][t]
                for p in plants
                for w in warehouses
                for pr in products
                for t in periods
            ]
        )
        + pulp.lpSum(
            [
                outbound_cost[(w, c, pr)] * y[w][c][pr][t]
                for w in warehouses
                for c in customers.tolist()
                for pr in products
                for t in periods
            ]
        )
        + pulp.lpSum([fixed_warehouse_cost[w] * z_warehouse[w] for w in warehouses])
        + pulp.lpSum(
            [
                binary[p][pr][t] * setup_cost[(p, pr)]
                for p in plants
    ...
            ]
        )
    )
    
    # Constraints
    for p in plants:
        for pr in products:
            for t in periods:
                problem += production_lot_size[p][pr][t] >= binary[p][pr][t]
                problem += production_lot_size[p][pr][t] <= binary[p][pr][t] * plant_capacity.get((p, pr), 0)
    
    for pr in products:
        for t in periods:
            for w in warehouses:
                problem += pulp.lpSum([x[p][w][pr][t] for p in plants]) == pulp.lpSum([y[w][c][pr][t] for c in customers]) + inventory[pr][t]
    
    for c in customers:
        for pr in products:
            for t in periods:
                problem += pulp.lpSum([y[w][c][pr][t] for w in warehouses]) >= demand.get((c, t, pr), 0)
    
    for p in plants:
        for w in warehouses:
            for pr in products:
                for t in periods:
                    problem += x[p][w][pr][t] <= plant_capacity.get((p, pr), 0) * z_plant[p]
    
    for w in warehouses:
        for pr in products:
            for t in periods:
                problem += pulp.lpSum([y[w][c][pr][t] for c in customers]) <= warehouse_capacity.get((w, pr), 0) * z_warehouse[w]
    
    # Solve the model with a time limit of 60 seconds
    time_limit_in_seconds = 60*2
    
    problem.solve(pulp.PULP_CBC_CMD(msg=1, timeLimit=time_limit_in_seconds))
    
    # Binary output:
    for p in plants:
        for pr in products:
            for t in periods:
                if binary[p][pr][t].varValue > 0:
                    print(binary[p][pr][t].varValue)
    

    Please make sure the file path for 'filename' is correct and test the code. If there are still any issues or errors, let me know so I can further help you debug it.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.