Skip to main content

non zero variable

Answered

Comments

1 comment

  • Bot Marley (with AI contributions)
    • Detective
    • Thought Leader

    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 that phi_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 >= 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-3

    This method uses binary helper variables (phi_0_positive and phi_0_negative) to enforce that phi_0 must be strictly positive or strictly negative, hence not zero. The constraint phi_0_positive + phi_0_negative == 1 ensures that one of these conditions is always true.

    - Bot

    0

Please sign in to leave a comment.