メインコンテンツへスキップ

gurobipy.GurobiError: Range constraint requires a list [lb,ub]

回答済み

コメント

1件のコメント

  • Jaromił Najman
    • Gurobi Staff

    Hi Marco,

    The issue lies within the constraint

    model.addConstr(value2 == (value0, value1))

    What you are trying to formulate is \(v_2 = (v_0,v_1)\). While this might be correct from a theoretical formulation point of view, it is not how you would formulate such an equality constraint.

    In practice, you have to define a 2-dimensional optimization variable

    value2 = model.addVar(2, lb=-30, ub=30,vtype=gb.GRB.INTEGER name="value2")

    You can then define the equality constraints

    model.addConstr(value2[0] == value0)
    model.addConstr(value2[1] == value1)

    But here comes another issue. You would like to use \(v_0,v_1\) as indices for other list objects. This is not possible without a correct formulation because \(v_0,v_1\) are optimization variables and don't have any value during model construction. However, the compiler (or interpreter) requires a value to access the index of the respective list object.

    It is possible to formulate variables as indices as described in the post use a decision variable as an index. However, this formulation is quite complex and I would not recommend it if there is a way to avoid it.

    In your case you maybe could avoid having to formulate variables as indices. To achieve this, you can define a binary \(v\) variable for every \(v_0,v_1\) combination. In your case, this would be 3600 additional variables. You could then drop the \(v_0,v_1\) variables. 

    value = {}
    for i in range(-30,31):
    for j in range(-30,31):
    value[i,j] = model.addVar(vtype=gb.GRB.BINARY, name="value[%d,%d]"%(i,j))

    Exactly one of these new variable has to be \(1\) at any time.

    model.addConstr(gb.quicksum(value[i,j] for i in range(-30,31) for j in range(-30,31)) == 1)

    You can now formulate your objective by summing over all list values and multiplying each of them with the binary value variable

    model.setObjective(gb.quicksum( alpha * time_reloc[i,j] * value[i,j] 
                                + beta * node_dist_to_reloc[i,j] * value[i,j] 
                                  + gamma * routeCoefficient_reloc[i,j] * value[i,j]
                                  for i in range(-30,31)
                                  for j in range(-30,31)), gb.GRB.MINIMIZE)

    Depending on your level of gurobipy knowledge, it might make sense to have a look at our Python introduction series and maybe our MIP introduction series.

    Best regards, 
    Jaromił

    1

サインインしてコメントを残してください。