Search code examples
pythonoptimizationscipy-optimizescipy-optimize-minimize

Scipy pythonoptimization error:ValueError: operands could not be broadcast together with shapes (9,) (6,) (6,)


I am try to solve maximization problem using python library called SciPy.I am very need to python and SCipy. I code the given problem using looking at the youtube video at urlhttps://www.youtube.com/watch?v=M7ZA9fq2zCE&t=191s. I am solving price optimization problem. I took our product should be at the margin of the competitor price range.Manufacturing cost is added to cost function.Shipping cost was also added to cost function defined as "objective_fcn".Shipping cost is consist of serveral values.Packing costs,Delivary to port cost,tax,terminal cost and carriage cost.(see picture enter image description here.And finally I add customer discount cost to the cost function.SO cost function has main three costs; manufacturing cost, customer discounts and shipping cost.My problem is I coded my optimization problem as it is in the given youtube video.But I got error saying "ValueError: operands could not be broadcast together with shapes (9,) (6,) (6,)".I don't know what is means and I don't know how to solve this issue. Please help me. Code is there.I comment importnat details in there.

import numpy as np
from scipy.optimize import minimize
import math

# company descitions and inputs
Newyorkquantity = 60 / 100;
Felixstorequantity = 40 / 100;

DelivaryToPortLowerBoundry = 4 / 100;
DelivaryToPortHigherBoundry = 5 / 100;
PackingCostLowerBoundry = 1 / 10;
PackingCostHigheBoundry = 12 / 10;

Bronze_DiscountUpperBound = 20;
Bronze_DiscountLowerBound = 0;

Silver_DiscountUpperBound = 30;
Silver_DiscountLowerBound = 21;

Gold_DiscountUpperBound = 50;
Gold_DiscountLowerBound = 35;

Platinum_DiscountUpperBound = 71;
Platinum_DiscountLowerBound = 51;

NumberofchocolatesContainsInaContainer = 1000000;

# constant values
taxeForAcontainer = 30000;
terminalchargesForAcontainer = 40500;
STORAGEMAXIMUMCHOCOLATE=850000000;

# model predicted values
BronzeCustomers = 2500;
SilverCustomers = 1800;
GoldCustomers = 700;
PlatinumCustomers = 350;

ShippingrOneUnitContainerCost_NY = 80000;
ShippingrOneUnitContainerCost_Felixtow = 70000;
Manufacturing = 25;
CompetitorLowerBoundryprice = 250;
CompetitorHigherBoundryprice = 340;


def objective_fcn(x):
    price = x[0]
    quantity = x[1]
    bronze_Discount = x[2]
    silver_Discount = x[3]
    Gold_Discount = x[4]
    Platinum_Discount = x[5]
    DelivaryToPort = x[6]
    PackingCostOneUnit = x[7]
    transporttcircles = x[8]

    bronzecost = BronzeCustomers * (price - price * bronze_Discount)
    Silvercost = SilverCustomers * (price - price * silver_Discount)
    Goldcost = GoldCustomers * (price - price * Gold_Discount)
    Platinumcost = PlatinumCustomers * (price - price * Platinum_Discount)
    customercost = bronzecost + Silvercost + Goldcost + Platinumcost
    print("hello")
    packigcostAllunits = PackingCostOneUnit * quantity;
    delivarytoporCosttAll = DelivaryToPort * transporttcircles;
    ShippingCarringcharge_NewYK = (math.ceil((quantity * Newyorkquantity / NumberofchocolatesContainsInaContainer))) * ShippingrOneUnitContainerCost_NY;
    ShippingCarringcharge_FelixStore = (math.ceil((quantity * Felixstorequantity / NumberofchocolatesContainsInaContainer))) * ShippingrOneUnitContainerCost_Felixtow;
    Total_containers = (math.ceil((quantity * Newyorkquantity / NumberofchocolatesContainsInaContainer))) + (math.ceil((quantity * Felixstorequantity / NumberofchocolatesContainsInaContainer)));

    shippingTotalCost = packigcostAllunits + delivarytoporCosttAll + ShippingCarringcharge_NewYK + ShippingCarringcharge_FelixStore + taxeForAcontainer*Total_containers +terminalchargesForAcontainer;

    return -(price * quantity - (customercost + Manufacturing * quantity + shippingTotalCost))



bounds_price = (CompetitorLowerBoundryprice, CompetitorHigherBoundryprice)
bound_quantity = (10000,STORAGEMAXIMUMCHOCOLATE)
bounds_bronze_DS = (Bronze_DiscountLowerBound, Bronze_DiscountUpperBound)
bounds_silver_DS = (Silver_DiscountLowerBound, Silver_DiscountUpperBound)
bounds_Gold_DS = (Gold_DiscountLowerBound, Gold_DiscountUpperBound)
bounds_Platinum_DS = (Platinum_DiscountLowerBound, Platinum_DiscountUpperBound)

bounds_DelivaryToPor = (DelivaryToPortLowerBoundry,DelivaryToPortHigherBoundry)
bounds_PackingCostLowerBoundry = (PackingCostLowerBoundry,PackingCostHigheBoundry)




boundsss = [bounds_price, bound_quantity, bounds_bronze_DS, bounds_silver_DS, bounds_Gold_DS, bounds_Platinum_DS]


x0 = [251, 100000, 1, 22, 36, 52,DelivaryToPortLowerBoundry,PackingCostLowerBoundry,3]

result = minimize(fun=objective_fcn, x0=x0, bounds=boundsss, method='SLSQP')

if result.success:
    fitted_params = result.x
    print(fitted_params)
else:
    raise ValueError(result.message)

Solution

  • Error is caused by the fact you provided 9 dimensions in x0 and 6 dimensions in bounds parameters. I made-up some bounds and fixed your problem:

    import numpy as np
    from scipy.optimize import minimize
    import math
    
    # company descitions and inputs
    Newyorkquantity = 60 / 100;
    Felixstorequantity = 40 / 100;
    
    DelivaryToPortLowerBoundry = 4 / 100;
    DelivaryToPortHigherBoundry = 5 / 100;
    PackingCostLowerBoundry = 1 / 10;
    PackingCostHigheBoundry = 12 / 10;
    
    Bronze_DiscountUpperBound = 20;
    Bronze_DiscountLowerBound = 0;
    
    Silver_DiscountUpperBound = 30;
    Silver_DiscountLowerBound = 21;
    
    Gold_DiscountUpperBound = 50;
    Gold_DiscountLowerBound = 35;
    
    Platinum_DiscountUpperBound = 71;
    Platinum_DiscountLowerBound = 51;
    
    NumberofchocolatesContainsInaContainer = 1000000;
    
    # constant values
    taxeForAcontainer = 30000;
    terminalchargesForAcontainer = 40500;
    STORAGEMAXIMUMCHOCOLATE=850000000;
    
    # model predicted values
    BronzeCustomers = 2500;
    SilverCustomers = 1800;
    GoldCustomers = 700;
    PlatinumCustomers = 350;
    
    ShippingrOneUnitContainerCost_NY = 80000;
    ShippingrOneUnitContainerCost_Felixtow = 70000;
    Manufacturing = 25;
    CompetitorLowerBoundryprice = 250;
    CompetitorHigherBoundryprice = 340;
    
    
    def objective_fcn(x):
        price = x[0]
        quantity = x[1]
        bronze_Discount = x[2]
        silver_Discount = x[3]
        Gold_Discount = x[4]
        Platinum_Discount = x[5]
        DelivaryToPort = x[6]
        PackingCostOneUnit = x[7]
        transporttcircles = x[8]
    
        bronzecost = BronzeCustomers * (price - price * bronze_Discount)
        Silvercost = SilverCustomers * (price - price * silver_Discount)
        Goldcost = GoldCustomers * (price - price * Gold_Discount)
        Platinumcost = PlatinumCustomers * (price - price * Platinum_Discount)
        customercost = bronzecost + Silvercost + Goldcost + Platinumcost
        print("hello")
        packigcostAllunits = PackingCostOneUnit * quantity;
        delivarytoporCosttAll = DelivaryToPort * transporttcircles;
        ShippingCarringcharge_NewYK = (math.ceil((quantity * Newyorkquantity / NumberofchocolatesContainsInaContainer))) * ShippingrOneUnitContainerCost_NY;
        ShippingCarringcharge_FelixStore = (math.ceil((quantity * Felixstorequantity / NumberofchocolatesContainsInaContainer))) * ShippingrOneUnitContainerCost_Felixtow;
        Total_containers = (math.ceil((quantity * Newyorkquantity / NumberofchocolatesContainsInaContainer))) + (math.ceil((quantity * Felixstorequantity / NumberofchocolatesContainsInaContainer)));
    
        shippingTotalCost = packigcostAllunits + delivarytoporCosttAll + ShippingCarringcharge_NewYK + ShippingCarringcharge_FelixStore + taxeForAcontainer*Total_containers +terminalchargesForAcontainer;
    
        return -(price * quantity - (customercost + Manufacturing * quantity + shippingTotalCost))
    
    
    
    bounds_price = (CompetitorLowerBoundryprice, CompetitorHigherBoundryprice)
    bound_quantity = (10000,STORAGEMAXIMUMCHOCOLATE)
    bounds_bronze_DS = (Bronze_DiscountLowerBound, Bronze_DiscountUpperBound)
    bounds_silver_DS = (Silver_DiscountLowerBound, Silver_DiscountUpperBound)
    bounds_Gold_DS = (Gold_DiscountLowerBound, Gold_DiscountUpperBound)
    bounds_Platinum_DS = (Platinum_DiscountLowerBound, Platinum_DiscountUpperBound)
    
    bounds_DelivaryToPor = (DelivaryToPortLowerBoundry,DelivaryToPortHigherBoundry)
    bounds_PackingCostLowerBoundry = (PackingCostLowerBoundry,PackingCostHigheBoundry)
    
    ##### FIX HERE #####
    fake_bound1 = [DelivaryToPortLowerBoundry - 1, DelivaryToPortLowerBoundry + 1]
    fake_bound2 = [PackingCostLowerBoundry - 1, PackingCostLowerBoundry + 1]
    fake_bound3 = (2, 4)
    boundsss = [bounds_price, bound_quantity, bounds_bronze_DS, bounds_silver_DS, bounds_Gold_DS, bounds_Platinum_DS,
                fake_bound1, fake_bound2, fake_bound3]
    #### END OF FIX ####
    
    x0 = [251, 100000, 1, 22, 36, 52, DelivaryToPortLowerBoundry,PackingCostLowerBoundry,3]
    
    result = minimize(fun=objective_fcn, x0=x0, bounds=boundsss, method='SLSQP')
    
    if result.success:
        fitted_params = result.x
        print(fitted_params)
    else:
        raise ValueError(result.message)