I would like to write the following simple model in GurobiPy, which is part of many terms that's belongs to the objective function, I assumed that I have three suppliers are selected and three warehouses are opened, where $QS_{swpt}$, $Q_{whpt}$ and $QU_{whpt}$.are CONTINUOUS decicison variables. $S_{st}$, $W_{wt}$, $G_{gt}$, $O_{ot}$ and $L_{lt}$ are BINARY decision variables, I have tried a toy instance by hand and on GurobiPy but, the results from the latter was wrong, I would like to know a rule of thumb to model such problems, or any resource that explain how to model similar and advanced problems.

$t$ is the time.

$s$ are the suppliers.

$w$ are the warehouses.

$p$ are the products.

$$COF= \sum_ {t}\sum_{s}FS_{st}*S_st+\sum_ {t}\sum_{w}FW_{wt}*W_{wt}+\sum_ {t}\sum_{g}FG_{gt}*G_{gt}$$ $$+\sum_ {t}\sum_{o}FO_{ot}*O_{ot}+\sum_ {t}\sum_{l}FL_{lt}*L_{lt}$$ $$ OCS= \sum_{t}\sum_{s}\sum_{w}\sum_{p}QS_{swpt}(MC_{spt}+TS_{swpt}) $$ $$PCS=\sum_{t}\sum_{s}\sum_{w}\sum_{p}KW_{wpt}*Y_{wt}+QS_{swpt}*c_{spt}$$ $$CIW=\sum_{t}\sum_{s}\sum_{w}\sum_{p}QW_{whpt}*[I_{wt}+HCW_{wt}+TW_{wpht}*(\boldsymbol{1-\bar{p}})]$$ $$SCH=\sum_{t}\sum_{w}\sum_{h}\sum_{p}QU_{whpt}*p_{ht}$$
$Minimize\; Z = COF+OCS+PCS+CIW+SCH$

#Contiuous Decision Variables
    QS_swpt= m.addVar(vtype=GRB.CONTINUOUS, name="QS_swpt", lb=0)
    QW_whpt= m.addVar(vtype=GRB.CONTINUOUS, name="QW_whpt", lb=0 )
    QW_gpt= m.addVar(vtype=GRB.CONTINUOUS, name= "QW_gpt", lb=0)
    QU_whpt=m.addVar(vtype=GRB.CONTINUOUS, name= "QU_whpt", lb=0)
#Binary Decision Variables
    Y_wt= m.addVar(vtype=GRB.BINARY, name="Y_wt")
    S_st=m.addVar(vtype=GRB.BINARY, name="S_st")
    W_wt= m.addVar(vtype=GRB.BINARY, name="W_wt")
    G_gt=m.addVar(vtype=GRB.BINARY, name="G_gt")
    O_ot=m.addVar(vtype=GRB.BINARY, name="O_ot")
    L_lt=m.addVar(vtype=GRB.BINARY, name="L_lt")
#Modelling the objective Function.
    COF=  quicksum(FS_st[s]*S_st for s in FS_st for sp in S for t in Time)\
            + quicksum(FW_wt[w]*W_wt for w in FW_wt for wr in W for t in Time)\
            + quicksum(FG_gt[g]*G_gt for g in FG_gt for gs in G  for t in Time)\
            + quicksum(FO_ot[o]*O_ot for o in FO_ot for om in O for t in Time)\
            + quicksum(FL_lt[l]*L_lt for l in FL_lt for lf in L for t in Time)
    OCS= quicksum(QS_swpt*(MC_spt[p]) for p in MC_spt for w in W for t in Time )\
            + quicksum(QS_swpt*(TS_swt[s]) for s in TS_swt for w in W for t in Time )
    PCS = quicksum(KW_wpt[k]* Y_wt for k in KW_wpt for w in W for t in Time)\
              + quicksum(c_st[c] for c in c_st for w in W for t in Time)
    CIW =  quicksum(QW_swpt[qs]*(I_wt[i]) for i in I_wt for s in S for qs in QW_swpt for t in Time)\
              + quicksum((QW_whpt[qs]*HCW_wt[hc]) for hc in HCW_wt for s in S for qs in QW_whpt for t in Time )\
              + quicksum(QW_whpt[qw]*(TW_whpt[ts]*(p_bc)) for ts in TW_whpt for w in W for qw in QW_whpt for t in Time)
    SCH= quicksum(QU_whpt[qu]*p_ht[ph] for ph in p_ht for u in W for qu in QU_whpt for t in Time)
  • 1
    $\begingroup$ Would you please, elaborate more on the problem specifications? If what you proposed is making your objective function, you can solve that by multi-objective techniques. What are really the model constraints and what you are exactly looking for? $\endgroup$
    – A.Omidi
    Mar 5, 2023 at 9:39
  • $\begingroup$ My problem is with modeling on Gurobi Python itself, I have added the objective function to make it clear. $\endgroup$
    – Abde
    Mar 5, 2023 at 11:16
  • $\begingroup$ I can add the model constraint, but it is too tedious, I am modelling the objective function first. $\endgroup$
    – Abde
    Mar 5, 2023 at 11:32
  • 1
    $\begingroup$ @Abde please post a some part of your code. Also ensure that COF, OCS etc used in objective have been all declared as variables and these relations as constraints. $\endgroup$ Mar 5, 2023 at 12:32
  • $\begingroup$ Ok sorry I thought COF, OCS are being used in others constrs. Then yes these are merely expressions as Omidi has shown. Better to post your code if you want and ensure constraints are accurately modelled. $\endgroup$ Mar 5, 2023 at 13:02

2 Answers 2


One possible way is by defining each part of the objective function separately and finally collecting them in an objective method. For example, suppose there are two different terms in the object as $\sum_{i} P_{i}x_{i}$ and $\sum_{i} \sum_{j} C_{i,j}y_{i,j}$ , then a simple template would be:

I = 10
J = 10
P = np.random.rand(I)
C = np.random.rand(I,J)

m = gp.Model()

x = m.addVars(I)
y = m.addVars(I,J)

part_1= gp.quicksum(P[i] * x[i] for i in range(I))   
part_2= gp.quicksum(C[i, j] * y[i, j] for i in range(I) for j in range(J))
objective= part_1 - part_2
m.setObjective(objective, GRB.MINIMIZE)

Sample variable declaration and use in expressions or constraints\

    QS_swpt =model addVars((S,W,P,T))# lb=o, vtype='c' is by default
    #similary for binary
    Y_wt= model.addVars((W,T),vtype='b') # S is set/list of suppliers,\
# T is list of time periods, W is list of warehouses. You can also make combinations like SWPT a list/set of tuples or tuplelist like [(s,w,p,t) for s in S for p in P for w in W]. In that case your variable declaration will be like mode.addVars(SWPT)
    # then expressions like OCS
    OCS= gp.quicksum(QS_swpt[s,w,p,t](MC_[s,p,t]+TS_swpt[s,w,p,t]) for p in products for w in warehouses for s in supplier for t in time)
  • $\begingroup$ I think I have to start modelling without time an multi-product,, then add them afterward to make clear for me, I found a model on gurobi website, I am trying to adopt it. $\endgroup$
    – Abde
    Mar 6, 2023 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.