2

I am new to optimisation and have a fairly basic query.

I have a model with two decision variables x and y that vary in time. I'd like to add a conditional constraint on y at time t depending upon x[t-1], such that I've implemented the following code:

for t in model.timesteps:
        if t>1:
            if model.x[t-1] <= 1:
                model.addConstr(model.y[t] >= 100)
            elif model.x[t-1] <= 0.5:
                model.addConstr(model.y[t] >= 50)
            elif model.x[t-1] <= 0.3:
                model.addConstr(model.y[t] >= 20)

However, the above code produces the error:

File "tempconstr.pxi", line 44, in gurobipy.TempConstr.bool gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)

Having done a little reading on previous related queries on this page, I believe I might need to use a binary indicator variable in order to implement the above. However, I'm not certain as to whether this would solve the above issue.

Could anyone point me in the right direction here please?

Many thanks in advance!

2
  • Is x[t-1] ever > 1? Are you worried about the edge cases like x[t-1] = 0.50001? Commented Oct 8, 2020 at 17:02
  • Hi, thanks for the response! No, x[t-1] is between [0,1] and edge cases are not an issue. Commented Oct 8, 2020 at 20:20

2 Answers 2

1

First, I assume your order of operations is incorrect; you intended that the right-hand side is 20 for 0 ≤ x[t-1] ≤ 0.3, 50 for 0.3 < x[t-1] ≤ 0.5 and 100 for 0.5 < x[t-1] ≤ 1.0.

The bigger issue is that you were mixing Python programming with MIP modeling. What you need is to convert that logic into a MIP model. There are several ways to do this. One is to use a piecewise linear constraint to represent the right-side values of the y[t] constraints. However, I prefer to model this explicitly. There are a few similar options; here is one I think is easy to understand: add binary variables z[0], z[1] and z[2] to represent the 3 ranges of x[t-1]; this gives the following code:

for t in model.timesteps:
  if t>1:
    z = model.addVars(3, vtype='B', name="z_%s" % str(t))
    model.addConstr(x[t-1] <= z.prod([0.3, 0.5, 1.0]))
    model.addConstr(y[t] >= z.prod([20, 50, 100]))
    model.addConstr(z.sum() == 1)
Sign up to request clarification or add additional context in comments.

Comments

0

Gurobi doesn't support to add a model constraint by defining first its value.

Instead of model.addConstr(model.y[t] >= 100)

Try model.addConstr(100 <= model.y[t])

Source: https://support.gurobi.com/hc/en-us/articles/360039628832-Constraint-has-no-bool-value-are-you-trying-lb-expr-ub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.