Search code examples
pythonoptimizationgurobi

How to implement this idea into objective fucntion with Gurobi PY


I am working on an optimization project here and my objective is to come out with the optimal amount of each item to buy at certain vendors that can minimize the cost while meeting the demand(already figured out the demand constraints). Here is a little background for the case below, there are 2 Vendor, 2 distribution centers(DC), 2 items(UPC), 2 Microfulfillment centers (MFC) and the chart also contain the price, weight, and cube of each items.

The freight cost of each vendor(v1, v2) is also below where v1 charges a freight fee based on the total weight and cube of all ordered items from one DC to one MFC; v2 charges a freight fee based on the total order amount(in $) from one DC to one MFC.

Everything else for this model is done except for this complicated freight cost in the objective function. I am stuck on this part and would really appreciate some help on how to implement this freight fee idea. I do have an idea which is to first calculate the total the weight and cube of all ordered items from one DC to one MFC(using gp.quicksum()), then use multiple if-else statements to compare with the cube and weight range and use the corresponding freight fee. The same idea applies to v2 where it is based on the total order amount in dollars.

Would really want some advice on whether my idea will work OR if anyone can share an easier way to implement this idea would be greatly appreciated!

enter image description here

enter image description here

enter image description here


Solution

  • After a decent research, I found that the tiered order amount can be implemented easily by using "Model.addGenConstrPWL()" from gurobi which is a general constraints for Piecewise function.

    Below is a link for anyone that wants to take a look!

    https://www.gurobi.com/documentation/9.1/refman/py_model_agc_pwl.html

    Also, another useful link is below if your piecewise function input is the sum of other variables:

    https://support.gurobi.com/hc/en-us/community/posts/360077610571-A-weird-output-is-it-a-bug-of-Gurobi-8-1-1-