Decision Optimization

 View Only
Expand all | Collapse all

SyntaxError: invalid syntax while adding a constraint multiplying two varaibles

  • 1.  SyntaxError: invalid syntax while adding a constraint multiplying two varaibles

    Posted Tue December 27, 2022 09:31 AM
    Hi
    I'm new to Decision optimization using Watson studio. I need to add a constraint by multiplying a Boolean variable & input data. I used following statement to create a binary decision variable
    mdl.WareHouseOpenClose = mdl.binary_var_dict(Transport, lb=0, name='WareHouseOpenClose')​

    I need to create following constraint
    Sum of TransportFlow from warehouse-OpenClose*FixedCost, OpenClose is a binary variable

    So I Used following steps to read input & add constraint by multiplying it with WarehouseOpenClose binary decision variable
    from docplex.mp.model import Model
    mdl = Model(name='Transport')
    
    df_FixedCost=inputs['FixedCost']
    df_Supply = inputs['Supply']
    df_TransportCost = inputs['TransportationCost']
    df_FixedCost=inputs['FixedCost']
    
    df_Supply['StockLevel'] = df_Supply['StockLevel'].astype(float)
    df_FixedCost['FixedCost']=df_FixedCost['FixedCost'].astype(float)
    
    
    
    WareHouseStatus = list(df_FixedCost.itertuples(name='WareHouseStatus', index=False))
    Supply = list(df_Supply.itertuples(name = 'Supply', index=False))
    Transport = list(df_TransportCost.itertuples(name='Transport', index=False))
    
    mdl.WareHouseOpenClose = mdl.binary_var_dict(Transport, lb=0, name='WareHouseOpenClose')
    mdl.TransportFlow = mdl.continuous_var_dict(Transport, lb=0, name='TransportFlow')
    
    for s in Supply:
        for w in WareHouseStatus:
             mdl.add_constraint(mdl.sum(mdl.TransportFlow[a] for a in Transport if a.Warehouse == w.Warehouse)-(mdl.WareHouseOpenClose[a] for a in WareHouseStatus if a.Warehouse == w.Warehouse)*(w.FixedCost if a.Warehouse == w.Warehouse)<= s.StockLevel, 'ct_fixeCost_{0}'.format(w.Warehouse))
    ​

    I'm getting Invalid syntax error  for add constraint. Can you please help me understand what wrong I'm doing?

    ------------------------------
    Niladri Chakraborty
    ------------------------------

    #DecisionOptimization


  • 2.  RE: SyntaxError: invalid syntax while adding a constraint multiplying two varaibles

    Posted Wed December 28, 2022 05:15 AM
    Dear Niladri,

    Your program, when executing, raises 

    File "../syntaxerror.py", line 23
    mdl.add_constraint(mdl.sum(mdl.TransportFlow[a] for a in Transport if a.Warehouse == w.Warehouse)-(mdl.WareHouseOpenClose[a] for a in WareHouseStatus if a.Warehouse == w.Warehouse)*(w.FixedCost if a.Warehouse == w.Warehouse)<= s.StockLevel, 'ct_fixeCost_{0}'.format(w.Warehouse)) 
    


    the python comprehension cannot be parsed because:

    (w.FixedCost if a.Warehouse == w.Warehouse)

    is not valid python syntax.
    I hope this helps.



    ------------------------------
    Renaud Dumeur
    ------------------------------