non zero variable
回答済みHi
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 >= 1e-3 or phi_0 <= -1e-3 )
m.addConstr(phi_0 >= 1e-3 or phi_0 <= -1e-3 )
gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
-
正式なコメント
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Zahra,
To define your variable
phi_0such 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_0is 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 >= 1e-3 - (1 - phi_0_positive), name="phi_0_pos_constr")
m.addConstr(phi_0 <= -1e-3 + (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 > 1e-3 or < -1e-3This method uses binary helper variables (
phi_0_positiveandphi_0_negative) to enforce thatphi_0must be strictly positive or strictly negative, hence not zero. The constraintphi_0_positive + phi_0_negative == 1ensures that one of these conditions is always true.- Bot
0
投稿コメントは受け付けていません。
コメント
2件のコメント