Thank you so much for your time. I guessed it probably could be the reason. I have tried a big M constraint. However, it seems that the MAX constraint worked better overall. I will try to change it to an indicator constraint, however i am not 100% sure how to do this? Could this be the way:
#First variables
# Create variables for the temperature difference between layers. y
Delta_temp_layer = {}
for index in range(0,stop):
for n in range(1,layers-2):
Delta_temp_layer [index, n] = model.addVar( vtype=GRB.CONTINUOUS,
name='Delta_temp_layer_at_index_{0}_in_between_Layer_{1}_and_{2}'.format(index, n+1,n))
# Create variables for the temperature difference between layers. y
Delta_temp = {}
for index in range(0,stop):
for n in range(1,layers-2):
Delta_temp[index, n] = model.addVar(lb=-50, vtype=GRB.CONTINUOUS,
name='Delta_temp_at_index_{0}_in_between_Layer_{1}_and_{2}'.format(index, n+1,n))
# Create variable for indicator constraint
New_Binary_variable = model.addVars(range(0,stop), vtype=GRB.BINARY, name="New_Binary_variable")
#Second Constraint
for i in range(0,stop):
for lay in range(1,layers-2):
#Indicator constraint
model.addConstr(Delta_temp[i,lay] , GRB.EQUAL ,
(T[i,lay+1]-T[i,lay]) , name='Delta_temp_{2}_{1}_Constr_1_at_inx{0}'.format(i,lay,lay+1))
model.addGenConstrIndicator(New_Binary_variable, True, Delta_temp_layer[i,lay] - Delta_temp[i,lay], GRB.EQUAL, 0)
model.addGenConstrIndicator(New_Binary_variable, False, Delta_temp_layer[i,lay], GRB.EQUAL, 0)
#MAX Constraint
#model.addConstr(Delta_temp[i,lay] , GRB.EQUAL ,
(T[i,lay+1]-T[i,lay]),name='Delta_temp_{2}_{1}_Constr_1_at_inx{0}'.format(i,lay,lay+1))
#model.addGenConstrMax(Delta_temp_layer[i,lay], [Delta_temp[i,lay], 0], name="maxconstr_{2}_{1}_Constr_1_at_inx{0}".format(i,lay,lay+1))
##Big M constraint
##model.addConstr( T[i,lay+1]-T[i,lay], GRB.LESS_EQUAL , phi[i,lay]*M, name='Delta_temp_layer_{2}_{1}_Constr_1_at_inx{0}'.format(i,lay,lay+1))
##model.addConstr(-(T[i,lay+1]-T[i,lay]),GRB.LESS_EQUAL , (1-phi[i,lay])*M, name='Delta_temp_layer_{2}_{1}_Constr_2_at_inx{0}'.format(i,lay,lay+1))
##model.addConstr(Delta_temp_layer[i,lay], GRB.GREATER_EQUAL, (T[i,lay+1]-T[i,lay]), name='Delta_temp_layer_{2}_{1}_Constr_3_at_inx{0}'.format(i,lay,lay+1))
##model.addConstr(Delta_temp_layer[i,lay], GRB.LESS_EQUAL , (T[i,lay+1]-T[i,lay])+(1-phi[i,lay])*M,name='Delta_temp_layer_{2}_{1}_Constr_4_at_inx{0}'.format(i,lay,lay+1))
##model.addConstr(Delta_temp_layer[i,lay], GRB.LESS_EQUAL , phi[i,lay]*M, name='Delta_temp_layer_{2}_{1}_Constr_5_at_inx{0}'.format(i,lay,lay+1))
Best regards Morten
Btw i admire your work in "Constraint Integer Programming"