i dont know why my code is not taking a constraint into account
Answeredfrom gurobipy import Model, GRB
m = Model()
# Define decision variables p[i, j] and pjv2v
p = {}
pjv2v = []
piv2i=[]
for O in range(5):
for j in range(10):
p[O+1, j+1] = m.addVar(lb=0, vtype=GRB.BINARY, name="p[{},{}]".format(O+1,j+1))
for i in range(5):
piv2i.append(m.addVar(lb=0, ub=23, name="piv2i[{}]".format(i+1)))
for j in range(10):
pjv2v.append(m.addVar(lb=0, ub=23, name="pjv2v[{}]".format(j+1)))
Gain_final_V2I=[-1556.969928074958, 115088.91771640752, 177694.47911317323, 130573.06734283901, -2659.099484648859]
Gain_final_V2V=[-16257.085357645332, -10851.836660602074, -3690.126292616347, -10974.11894473536, -5420.915168815722, -12177.244964192028, -27627.77141131063, -16700.777958813524, -14158.645323548015, -9364.610894011512]
for k in range(5):
down[k]= sum(p[k+1,j+1]*pjv2v[j]*Gain_final_V2V[j] for j in range(10))+noise
up[k]=piv2i[k]*Gain_final_V2I[k]
auxiliary_variable=[]
SINR=[]
w=[]
LOG2_=[]
for i in range(5):
auxiliary_variable.append(m.addVar(lb=-GRB.INFINITY ,ub =-1, vtype=GRB.CONTINUOUS, name="Auxiliary_Variable{0}".format(i+1)))
SINR.append(m.addVar(lb=0,ub=GRB.INFINITY,vtype=GRB.CONTINUOUS, name="SINR{0}".format(i+1)))
w.append(m.addVar(lb=1,ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, name="1+SINR{0}".format(i+1)))
LOG2_.append(m.addVar(lb=-GRB.INFINITY,ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, name="log2(1+SINR){0}".format(i+1)))
con3=m.addConstrs(auxiliary_variable[i]==down[i] for i in range(5))
con4=m.addConstrs((auxiliary_variable[i]*SINR[i]==up[i]) for i in range(5))
con5= [m.addGenConstrLogA(w[i], LOG2_[i], 2) for i in range(5)]
con1=m.addConstrs(sum(p[i+1,j+1] for i in range(5))<=1 for j in range(10))
con2=m.addConstrs(sum(p[i+1,j+1] for j in range(10))<=3 for i in range(5))
constraint = gurobipy.LinExpr()
for i in range(5):
constraint += LOG2_[i] * (1.5e6)
con6=m.addConstr(constraint, gurobipy.GRB.GREATER_EQUAL, 1.5e6)
m.setObjective(constraint,GRB.MAXIMIZE)
m.setParam("NonConvex",2)
m.optimize()
vars = m.getVars()
# Print the optimal value of each variable
for v in vars:
print("Optimal value of", v.varName, ":", v.X)
now whenever i am running the code:

which means it is not taking account of con1
p[i,j] should be once or twice or thrice be 1
which is not occouring
please let me know where i am going wrong
if there any problem in boundary setting or something
how this code can take account of the constraint i provided
-
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 Sujana,
I would recommend you undertake the following steps:
1) Before optimizing your model, try to write it to your drive, for example as an .lp file:
m.write("model.lp")and then compare the constraints you get with the mathematical formulation you are implementing. Such a comparison will tell you exactly, which constraints are implemented incorrectly.
Perhaps you will need to use a smaller dataset to keep the size of the .lp file manageable.
2) Once you know which constraints are culprits, you will usually also have an idea of how to fix them.
Hope this helps. In case you have specific questions pertaining to the implementation of your model, we will gladly help you.
Best regards
JonaszPS From afar, it seems to me that you may need at least one constraint of the form \( \Sigma x \geq 1 \) to get the effect you are after. All of the constraints are of " \( \leq \) " or " \( = \) " sense for now...
0
Post is closed for comments.
Comments
2 comments