non zero variable
AnsweredHi
I have a variable that is in the interval of 1 and 1 but it cannot be zero. I would be appreciate if you help me to define the constraints properly.
m = gp.Model()
phi_0 = m.addVar(vtype=gp.GRB.CONTINUOUS,lb=1,ub=1,name=("phi_0"))
phi_1 = m.addVar(vtype=gp.GRB.CONTINUOUS,lb=1,ub=1,name=("phi_1"))
m.addConstr(phi_0 >= 1e3 or phi_0 <= 1e3 )
m.addConstr(phi_0 >= 1e3 or phi_0 <= 1e3 )
gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)

Hi Zahra,
To define your variable
phi_0
such that it can never be zero but lies within the interval [−1,1], you can utilize auxiliary variables. In your case, you want to enforce thatphi_0
is either greater than a small positive threshold or less than a small negative threshold.Here's how you can modify your code to implement this constraint correctly:
import gurobipy as gp
m = gp.Model()
phi_0 = m.addVar(vtype=gp.GRB.CONTINUOUS, lb=1, ub=1, name="phi_0")
# Create a helper variable to manage the or constraint
phi_0_positive = m.addVar(vtype=gp.GRB.BINARY, name="phi_0_positive")
phi_0_negative = m.addVar(vtype=gp.GRB.BINARY, name="phi_0_negative")
# Add constraints that link these binary variables with phi_0 ranges
m.addConstr(phi_0 >= 1e3  (1  phi_0_positive), name="phi_0_pos_constr")
m.addConstr(phi_0 <= 1e3 + (1  phi_0_negative), name="phi_0_neg_constr")
# Ensure that one of the binary variables must be true, so phi_0 cannot be zero
m.addConstr(phi_0_positive + phi_0_negative == 1, name="phi_0_or_constr")
# Now phi_0 is constrained to be either > 1e3 or < 1e3This method uses binary helper variables (
phi_0_positive
andphi_0_negative
) to enforce thatphi_0
must be strictly positive or strictly negative, hence not zero. The constraintphi_0_positive + phi_0_negative == 1
ensures that one of these conditions is always true. Bot
0
Please sign in to leave a comment.
Comments
1 comment