Using gurobi optimize variable
AnsweredPlease help, In the below code, i want to optimize the C such that maximum bw_initial[i][k] is selected. This code does not produce the correct value for all i..when i run i got this
for i=1, C[1,1] 0 C[1,2] 1 C[1,3] 0 C[1,4] 0 when bw[1,1] = 17.845765 bw[1,2] = 47.036043 bw[1,3] = 17.62816 bw[1,4] = 45.454545
it's correct as maximum value is for index(col) 2
for i=2, C[2,1] 1 C[2,2] 0 C[2,3] 0 C[2,4] 0 when bw[2,1] = 39.528471 bw[2,2] = 60.633906
bw[2,3] = 29.961076 bw[2,4] = 70.710678
it's not correct because maximum value is for index 2 (col),
for i=3, C[3,1] 1 C[3,2] 0 C[3,3] 0 C[3,4] 0 when bw[3,1] = 19.07272 bw[3,2] = 30.700278
bw[3,3] = 89.442719 bw[3,4] = 22.134948
it's not correct because maximum value is for index(col) 3
C[4,1] 0 C[4,2] 0 C[4,3] 1 C[4,4] 0 bw[4,1] = 19.869293 bw[4,2] = 24.189616 bw[4,3] = 164.39898 bw[4,4] = 19.400111
it's correct as maximum value is for index (col) 3
C[5,1] 0 C[5,2] 0 C[5,3] 0 C[5,4] 1 correct
C[6,1] 0 C[6,2] 0 C[6,3] 1 C[6,4] 0 correct
C[7,1] 0 C[7,2] 0 C[7,3] 1 C[7,4] 0 correct
C[8,1] 0 C[8,2] 0 C[8,3] 1 C[8,4] 0 correct
C[9,1] 0 C[9,2] 1 C[9,3] 0 C[9,4] 0 correct
C[10,1] 0 C[10,2] 0 C[10,3] 0 C[10,4] 1 correct
C[11,1] 1 C[11,2] 0 C[11,3] 0 C[11,4] 0 it's not correct as index 2 has maximum bw[11,1] 23.2119 bw[11,2] 250 bw[11,3] 30.6282 bw[11,4] 52.7046
C[12,1] 0 C[12,2] 0 C[12,3] 1 C[12,4] 0 correct
import gurobipy as gp
from gurobipy import GRB
import numpy.random as nmp
import numpy as nmp1
M=4
R=2
P=12
r_d=nmp1.array([0,2784.1197313700336,1732.8079780363323,1838.9621555991528,2658.1153759429553,2614.766454499971,2851.0224151217917, 1926.7146058957019,2782.155110131377,2593.0183386247504,2337.4051758710193,1813.66195993706,2195.782753056987])
#action_data
x = nmp.uniform(0,0,size=(P+1))
a_d=nmp1.array(x)
for i in range(P+1):
a_d[i]=r_d[i]/5
d=nmp1.array([0,0.9402464272132742,0.7448813804681473,0.6565893304701793,0.6008024612896197,0.5146076482342433,0.582672879367322,0.8669282654741378,0.8590528679956628,0.946149432999205,0.604588896353693,0.9652070673409696,0.9263662096190788])
CU=nmp1.array([0,633.0078669901113,883.1634714339139,805.3492477945315,550.7580377592622,874.7768423200384,774.8169342185408,758.161700861176,615.0165436142821,574.9698857827192,534.8853850841147, 512.4025723207523,898.920298549464 ])
x = nmp.uniform(0,0,size=(P+1,M+1))
bw_initial=nmp1.array([[0,0,0,0,0],[0,17.845765, 47.036043, 17.62816, 45.454545],[0, 39.528471,60.633906, 29.961076, 70.710678],[0,19.07272, 30.700278, 89.442719, 22.134948],[ 0, 19.869293, 24.189616, 164.398987, 19.400111],[0,50.188561, 36.155076, 21.276596, 64.415663],[ 0, 14.382907, 28.194194, 29.210312, 19.725746
],[0, 19.748813, 20.129242,70.359754, 17.200523],[0, 19.964097, 26.602896, 200, 20.61527],[0, 26.416599,200, 26.602896, 95.782629],[0, 24.992191,76.92307700000001, 21.295885, 164.398987],[0, 23.211917, 250, 30.628195, 52.704628],[0, 21.551524,36.060922,101.534617,25.342864]])
#create model m
m = gp.Model()
#ceate optimize and auxiliary variables
TL=m.addVars(range(1,P+1),vtype=GRB.CONTINUOUS,name="TL")
C=m.addVars(range(1,P+1),range(1,M+1),lb=0,ub=1,vtype=GRB.BINARY, name="C")
X = m.addVar(vtype=GRB.CONTINUOUS, name="X")
bw=m.addVars(range(1,P+1),range(1,M+1),vtype=GRB.CONTINUOUS,name="bw")
m.setObjective(X, GRB.MINIMIZE) # objective to minimize the GFT
m.addConstr(X== gp.max_(TL[i] for i in range(1,P+1)), name="GFT")
for i in range(1,P+1):
for j in range(1,M+1):
m.addConstr(bw[i,j]==bw_initial[i][j])
for i in range(1,12+1):
m.addConstr(TL[i]==(r_d[i]*d[i]/CU[i] + a_d[i]*sum(1/bw_initial[i][k]*C[i,k] for k in range(1,M+1))),"TLc") #optimize
for i in range(1,P+1):
m.addConstr(sum(C[i,k] for k in range(1,M+1))==1) #optimize model 2
m.optimize()
for v in m.getVars():
print('%s %g' % (v.VarName, v.X))
print('Obj: %g' % m.ObjVal)
optimal_value=m.ObjVal
m.write("mymodel_gft_optimize.lp")
-
To verify whether your model implementation is correct, you could fix the values of the variable of which you think are incorrect.
m.addConstr(C[x,y] == 1)
[...]and see whether your model is feasible. If it is feasible and the optimal solution is indeed better than the one found by Gurobi without the fixings, then you should first try setting MIPGap = 0. If this does not help, then you might want to try to understand why the solution found by Gurobi has a better optimal solution value.
Please also note that it is possible that your model has multiple optimal solution, i.e., there exist different points with the same optimal solution value. For more information, please refer to the Knowledge Base article How do I find additional solutions to a model?
If the fixed model is infeasible, then please proceed as described in How do I determine why my model is infeasible? to find the source of infeasibility.
0
Please sign in to leave a comment.
Comments
1 comment