Constraint 'feasibility_cut[1]' does not have a proper value. Found 'True'

Question:

I’m new to python and pyomo, so I would kindly appreciate your help,

I’m currently having trouble trying to add a constraint to my mathematical model in Pyomo, the problem is while I try to add the "feasibility_cut", it says "Constraint ‘feasibility_cut[1]’ does not have a proper value. Found ‘True’ ", what I understand from this is that, pyomo sees this constraint as a logical comparative constraint, which is I don’t know why!

Here is a part of the code that I think is necessary to see:

RMP = ConcreteModel()
RMP.ymp = Var(SND.E, within=Integers)
RMP.z = Var(within = Reals)

S1 = (len(SND.A), len(SND.K))
S2 = (len(SND.A), len(SND.A))
uBar= np.zeros(S1)
vBar=np.zeros(S2)

RMP.optimality_cut = ConstraintList()
RMP.feasibility_cut = ConstraintList()

expr2 = (sum(SND.Fixed_Cost[i,j]*RMP.ymp[i,j] for i,j in SND.E) + RMP.z)
RMP.Obj_RMP = pe.Objective(expr = expr2, sense = minimize)

iteration=0
epsilon = 0.01
while (UB-LB)>epsilon :
    iteration = iteration +1
    DSPsolution = Solver.solve(DSP)
    
    for i in SND.A:
        for k in SND.K:
            uBar[i-1,k-1] = value(DSP.u[i,k])
    for i,j in SND.E:
        vBar[i-1,j-1] = value(DSP.v[i,j])
        
    if value(DSP.Obj_DSP) == DSPinf:
        RMP.feasCut.add()
    else:
        RMP.optimCut.add()

    RMPsolution = solver.solve(RMP)
    
    UB=min(UB,)
    LB=max(LB,value(RMP.Obj_RMP))

    if value(DSP.Obj_DSP) == DSPinf:
        RMP.feasibility_cut.add( 0>= sum(-SND.Capacity[i,j]*vBar[i-1,j-1]*RMP.ymp[i,j] for i,j in 
        SND.E) + sum(uBar[i-1,k-1]*SND.New_Demand[k,i] for i in SND.A for k in SND.K if (k,i) in 
        SND.New_Demand) ) 
    else:
        RMP.optimality_cut.add( RMP.z >= sum(SND.Fixed_Cost[i,j]*RMP.ymp[i,j] for i,j in SND.E) + 
        sum(uBar[i-1,k-1]*SND.New_Demand[k,i] for i in SND.A for k in SND.K) - 
        sum(SND.Capacity[i,j]*vBar[i-1,j-1]*RMP.ymp[i,j] for i,j in SND.E) )
    
Asked By: ehsan mirzaei

||

Answers:

Welcome to the site.

A couple preliminaries… When you post code that generates an error, it is customary (and easier for those to help) if you post the entire code necessary to reproduce the error and the stack trace, or at least identify what line is causing the error.

So when you use a constraint list in pyomo, everything you add to it must be a legal expression in terms of the model variables, parameters, and other constants etc. You are likely getting the error because you are adding an expression that evaluates to True. So it is likely that an expression you are adding does not depend on a model variable. See the example below.

Also, you need to be careful mingling numpy and pyomo models, numpy arrays etc. can cause some confusion and odd errors. I’d recommend putting all data into the model or using pure python data types (lists, sets, dictionaries).

Here are 2 errors. You have an empty add() in your code too, which will throw an error.

In [1]: from pyomo.environ import *

In [2]: m = ConcreteModel()

In [3]: m.my_constraints = ConstraintList()

In [4]: m.X = Var()

In [5]: m.my_constraints.add(m.X >= 5)   # good!
Out[5]: <pyomo.core.base.constraint._GeneralConstraintData at 0x7f8778cfa880>

In [6]: m.my_constraints.add()    # error!
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-cf466911f3a1> in <module>
----> 1 m.my_constraints.add()

TypeError: add() missing 1 required positional argument: 'expr'

In [7]: m.my_constraints.add(3 <= 4)  # error:  trivial evaluation!
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-a0bec84404b0> in <module>
----> 1 m.my_constraints.add(3 <= 4)

...

ValueError: Invalid constraint expression. The constraint expression resolved to a trivial Boolean (True) instead of a Pyomo object. Please modify your rule to return Constraint.Feasible instead of True.

Error thrown for Constraint 'my_constraints[2]'

In [8]: 
Answered By: AirSquid