Infeasibility due to modelling the constraints
Answeredimport numpy as np
import cvxpy as cp
import gurobipy as gp
from gurobipy import GRB
n = 5
U_lower_bar_a1 = np.zeros((n, n))
U_upper_bar_a1 = np.zeros((n, n))
s = 0.03
U_lower_bar_a1[0][0] = -s
U_lower_bar_a1[0][1] = -s
U_lower_bar_a1[0][n-2] = -s
for row in range(1, n-4):
U_lower_bar_a1[row][row-1] = -s
U_lower_bar_a1[row][row] = -s
U_lower_bar_a1[row][row+1] = -s
U_lower_bar_a1[row][row+2] = -s
U_lower_bar_a1[row][n-2] = -s
U_lower_bar_a1[n-4][n-5] = -s
U_lower_bar_a1[n-4][n-4] = -s
U_lower_bar_a1[n-4][n-3] = -s
U_lower_bar_a1[n-4][n-2] = -s
U_lower_bar_a1[n-3][n-4] = -s
U_lower_bar_a1[n-3][n-3] = -s
U_lower_bar_a1[n-3][n-1] = -s
U_lower_bar_a1[n-2][0] = -0.6
U_lower_bar_a1[n-2][n-2] = 0
U_lower_bar_a1[n-2][n-1] = 0
U_lower_bar_a1[n-1][n-1] = -s
U_lower_bar_a1[n-1][n-2] = -s
U_lower_bar_a1[n-1][n-3] = -s
###################################################
U_upper_bar_a1[0][0] = s
U_upper_bar_a1[0][1] = s
U_upper_bar_a1[0][n-2] = s
for row in range(1, n-4):
U_upper_bar_a1[row][row-1] = s
U_upper_bar_a1[row][row] = s
U_upper_bar_a1[row][row+1] = s
U_upper_bar_a1[row][row+2] = s
U_upper_bar_a1[row][n-2] = s
U_upper_bar_a1[n-4][n-5] = s
U_upper_bar_a1[n-4][n-4] = s
U_upper_bar_a1[n-4][n-3] = s
U_upper_bar_a1[n-4][n-2] = s
U_upper_bar_a1[n-3][n-4] = s
U_upper_bar_a1[n-3][n-3] = s
U_upper_bar_a1[n-3][n-1] = s
U_upper_bar_a1[n-2][0] = -0.2
U_upper_bar_a1[n-2][n-2] = 0.3
U_upper_bar_a1[n-2][n-1] = 0.3
U_upper_bar_a1[n-1][n-1] = s
U_upper_bar_a1[n-1][n-2] = s
U_upper_bar_a1[n-1][n-3] = s
###################################################
mdl = gp.Model('X')
U_a1 = mdl.addMVar((n, n), vtype=GRB.CONTINUOUS, name='U_a1')
mdl.setObjective(1, sense=GRB.MINIMIZE)
#mdl.addConstrs( U_a1[i,j] >= U_lower_bar_a1[i,j] for i in range(0,n) for j in range(0,n))
mdl.addConstrs( U_a1[i,j] <= U_upper_bar_a1[i,j] for i in range(0,n) for j in range(0,n))
mdl.optimize()
For the above code, I am getting an infeasible model because the upper bound (U_upper_bar_a1) has a negative component. Is there a better way to model it, since only one component is negative ?
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Priya,
Variables in Gurobi, by default, take a lower bound of zero. If you expect a variable to take a negative value, please set its lower bound to an appropriate negative value.
Best regards,
Simran0 -
Thank you for the response.I uncommented the following also :
mdl.addConstrs( U_a1[i,j] >= U_lower_bar_a1[i,j] for i in range(0,n) for j in range(0,n))
But I am getting an infeasible model. Could you please help?
0 -
Hi Priya,
Uncommenting the following constraint will not help since the variable lower bound is still set to 0 by default.
mdl.addConstrs( U_a1[i,j] >= U_lower_bar_a1[i,j] for i in range(0,n) for j in range(0,n))
You can define the lower bound and upper bound of your MVar variable U_a1 to the arrays U_lower_bar_a1 and U_upper_bar_a1 as follows:
U_a1 = mdl.addMVar((n, n), lb=U_lower_bar_a1, ub=U_upper_bar_a1, vtype=GRB.CONTINUOUS, name='U_a1')
I hope this helps.Cheers,
Simran0
Post is closed for comments.
Comments
4 comments