• Gurobi Staff

Hi Vamsi,

Are you saying that the solution for the shown code and model is correct but for different input data you get the same solution? This sounds weird.

We recommend writing the formulated model to an LP file with model.write("model.lp") immediately before optimize(). Then, for small instances, you can manually check if the constraints are correctly built.

What seems strange to me is that x-variables are binary but they are used without some kind of distance coefficient in the distance constraints:

x[i, j] - x[i, j + 1] <= R

Note that the value of those variables can only be 0 or 1, so the left-hand side of those constraints can only be -1, 0, or 1. Maybe you need a different way to describe the distance.

Best regards,
Mario

Hi Mario,
Thanks for the suggestions.
Yes, wrong formulation of distance constraint.

I have defined one more continuous variable that tries to capture this. Here is the updated code.

import gurobipy as gp
from gurobipy import GRB

# Given dataM = [0, 4, 8, 15]R = 7m = 2# Create a Gurobi modelmodel = gp.Model('Point_Mapping')# Create decision variablesc = {}x = {}for i in range(1, len(M) - 1):    for j in range(m):        c[j] = model.addVar(lb=-GRB.INFINITY, vtype=GRB.CONTINUOUS, name=f'c_{j}')        x[i, j] = model.addVar(vtype=GRB.BINARY, name=f'x_{j}_{i}')# Set objective to maximize the sum of x_ijmodel.setObjective(gp.quicksum(x[i, j] for i in range(1, len(M) - 1) for j in range(m) ), sense=GRB.MAXIMIZE)# Add constraints at each node of M, the maximum points that can be placed is 1for i in range(1,len(M)-1):    model.addConstr(gp.quicksum(x[i, j] for j in range(m)) <= 1, name='Max_one_CS_cosntraint')# Add point constraintsmodel.addConstr(c[0] - M[0] >= 0.1, name='constraint_c1_m0')model.addConstr(c[0] - M[0] <= R, name='constraint_c1_m0_R')model.addConstr(M[-1] - c[m - 1] <= R, name='constraint_mnplus1_cm')model.addConstr(M[-1] - c[m - 1] >= 0.1, name='constraint_mnplus1_cm_R')for j in range(m - 1):    model.addConstr(c[j + 1] - c[j] <= R, name=f'constraint_c{j + 1}_cj')# Create additional binary variables for indicator constraintsz = {}for i in range(1, len(M) - 1):    for j in range(m):        z[i, j] = model.addVar(vtype=GRB.BINARY, name=f"z_{i}_{j}")# Indicator constraints for c[j] = M[i] implies z[i, j] = 1for i in range(1, len(M) - 1):    for j in range(m):        model.addConstr(c[j] - M[i] == 0 >> z[i, j] == 1, name=f"indicator_constraint_cj_mi_{i}_{j}")        # Indicator constraints for c[j] != M[i] implies z[i, j] = 0        model.addConstr(c[j] - M[i] != 0 >> z[i, j] == 0, name=f"indicator_constraint_cj_not_mi_{i}_{j}")# Link z variables to x variablesfor i in range(1, len(M) - 1):    for j in range(m):        model.addConstr(z[i, j] == x[i, j], name=f"link_z_to_x_{i}_{j}")# Update the modelmodel.update()model.write('model.lp')# Optimize the modelmodel.optimize()# Print the optimal solutionif model.status == GRB.OPTIMAL:    print('Optimal solution found:')    for j in range(m):        print(f'c_{j} = {c[j].X}')    for i in range(1, len(M) - 1):        for j in range(m):            print(f'x_{i}_{j} = {x[i, j].X}')else:    print('No solution found.')

I think I made some mistake in adding indicator constraint. I am getting the following error. Could you help?

line 48, in <module>    model.addConstr(c[j] - M[i] == 0 >> z[i, j] == 1, name=f"indicator_constraint_cj_mi_{i}_{j}")TypeError: unsupported operand type(s) for >>: 'int' and 'Var'
• Gurobi Staff

See the documentation of the indicator constraint: IF some binary variable is 1 (or 0) THEN some constraint must hold.
Your case is different: IF some constraint holds THEN a binary variable is 1.