I have model with 2 BigM cosntraints
i use pyomo for generate model and solve it with cplex software solver/gmas cplex&gurobi
Pyomo 6.7.0
cplex 22.1.0
gams 25.1.2.0
i define some variables as below
model.x = Var(model.N * model.N, domain=Binary, initialize=0)
model.z = Var(model.N, domain=NonNegativeReals, initialize=0)
model.b = Var(model.N, domain=NonNegativeReals, initialize=0)
model.t = Var(model.N, domain=Reals, initialize=0)
model.w = Var(model.N * model.N, domain=Reals, initialize=0)
i set Bigm equal to 100.
here is my two bigM constraints
# c1_3
def C1_3(model: AbstractModel, i, k):
return model.w[i,k] <= model.BigM * model.x[i,k]
# Add the rule to the model
model.C1_3 = Constraint(model.N3, model.N, rule=C1_3)
# c3_3
def C3_3(model: AbstractModel, k):
return model.w[2,k] <= model.BigM * model.x[2,k]
# Add the rule to the model
model.C3_3 = Constraint(model.N, rule=C3_3)
first question
here is solution for gmas cplex solver
27 Declarations: N N2 N3 Pi BigM x_index x z b t w_index w OBJ C1_2 C1_3_index C1_3 C1 C2 C3_2 C3_3 C3 C4 C5 C6 C7 C8 C9
Problem:
- Name: tmp_znc2rla
Lower bound: 155.06617009579196
Upper bound: 155.06617009579196
Number of objectives: 1
Number of constraints: 114
Number of variables: 142
Number of nonzeros: 491
Sense: minimize
Solver:
- Status: ok
User time: 15.72
Termination condition: optimal
Termination message: MIP - Integer optimal solutionx3a Objective = 1.5506617010e+02
Statistics:
Branch and bound:
Number of bounded subproblems: 8981
Number of created subproblems: 8981
Error rc: 0
Time: 15.961628437042236
Solution:
- number of solutions: 0
number of solutions displayed: 0
Variable x
(1, 1)
(1, 2)= 1.0
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(1, 7)
(1, 8)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(2, 5)
(2, 6)= 1.00000000023
(2, 7)
(2, 8)= 4347825727.215808
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(3, 5)
(3, 6)
(3, 7)
(3, 8)= 1.0
(4, 1)
(4, 2)
(4, 3)
(4, 4)
(4, 5)= 1.0
(4, 6)
(4, 7)= 3817208288.843674
(4, 8)= -3817208288.843674
(5, 1)= 1.0
(5, 2)
(5, 3)
(5, 4)
(5, 5)
(5, 6)
(5, 7)= -128612821486.68263
(5, 8)= 128612821486.68263
(6, 1)
(6, 2)
(6, 3)
(6, 4)
(6, 5)= -4347825728.215811
(6, 6)= 4347825728.215811
(6, 7)= 1.0
(6, 8)
(7, 1)
(7, 2)
(7, 3)
(7, 4)= 1.0
(7, 5)
(7, 6)
(7, 7)= -41328694784.23347
(7, 8)= 41328694784.233444
(8, 1)
(8, 2)
(8, 3)= 1.0
(8, 4)
(8, 5)= 4347825728.215811
(8, 6)
(8, 7)= -4347825728.215811
(8, 8)
Variable z
1= 0.23
2= 0.05048780488559286
3= 0.033704909829425395
4= 0.0232999889531211
5= 0.01719167679563222
6= 0.014376797429941019
7= 0.011119273568327412
8= 0.006448859860163252
Variable b
1= 0.23
2= 0.27000000003630464
3= 0.65
4= 0.46999999999416203
5= 0.5000000000001962
6= 0.6099999998651174
7= 0.3900000000003713
8= 0.24999999999298045
Variable t
3= 0.06468750001238438
4= 0.1013941221380149
5= 0.07547621322907092
6= 0.06557717894832199
7= 0.08780527428077939
8= 0.049073944029608595
Variable w
(2, 1)
(2, 2)
(2, 3)= -999974253.6153661
(2, 4)
(2, 5)
(2, 6)= 0.22999999999999987
(2, 7)
(2, 8)= 999976918.0205219
(3, 1)
(3, 2)
(3, 3)
(3, 4)= -252549729.38354373
(3, 5)
(3, 6)
(3, 7)
(3, 8)= 0.06468749999581547
(4, 1)
(4, 2)
(4, 3)
(4, 4)
(4, 5)= 0.10139412209569595
(4, 6)
(4, 7)= 183486671.41322446
(4, 8)= -104533519.72574876
(5, 1)= 0.07547621323350022
(5, 2)
(5, 3)
(5, 4)
(5, 5)
(5, 6)
(5, 7)
(5, 8)= 1286128214.8668263
(6, 1)
(6, 2)
(6, 3)
(6, 4)
(6, 5)= -1545736679.8193102
(6, 6)= 1545468104.1245408
(6, 7)= 0.06557717894832248
(6, 8)
(7, 1)
(7, 2)
(7, 3)
(7, 4)= 0.08780527429914714
(7, 5)
(7, 6)
(7, 7)= -26863651609.75159
(7, 8)= 413286947.84233445
(8, 1)
(8, 2)
(8, 3)= 0.049073944035091306
(8, 4)
(8, 5)= 2826156297.618622
(8, 6)
(8, 7)= 520075495.81888485
(8, 8)
obj_val: 155.06617009579196
(8, 1)
(8, 2)
(8, 3)= 0.049073944035091306
(8, 4)
(8, 5)= 2826156297.618622
(8, 6)
(8, 7)= 520075495.81888485
(8, 8)
obj_val: 155.06617009579196
Start time: Wed Jul 24 11:31:14 2024
End time: Wed Jul 24 11:31:30 2024
Duration (seconds): 16.03162407875061
why does in this solution that slolved by cplex solver domian of x,w isn’t correct and x get value other than 0,1?
second question
here is solution for gmas cplex solver
Problem:
- Name: Cx3aUsersXxLonelyAppDataLocalTemptmp2ybm850cmodel.gms
Lower bound: 139.5641843005433
Upper bound: 155.0661701337002
Number of objectives: 1
Number of constraints: 115.0
Number of variables: 143.0
Number of binary variables: None
Number of integer variables: 64.0
Number of continuous variables: 79.0
Number of nonzeros: 493.0
Sense: minimize
Solver:
- Name: GAMS (25, 1, 2, 0)
Status: ok
Return code: 0
Message: None
User time: 2.463000244461
System time: None
Wallclock time: None
Termination condition: optimal
Termination message: None
Solution:
- number of solutions: 0
number of solutions displayed: 0
Variable x
(1, 1)
(1, 2)= 1.0
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(1, 7)
(1, 8)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(2, 5)
(2, 6)= 1.0
(2, 7)
(2, 8)
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(3, 5)
(3, 6)
(3, 7)
(3, 8)= 1.0
(4, 1)
(4, 2)
(4, 3)
(4, 4)
(4, 5)= 1.0
(4, 6)
(4, 7)
(4, 8)
(5, 1)= 1.0
(5, 2)
(5, 3)
(5, 4)
(5, 5)
(5, 6)
(5, 7)
(5, 8)
(6, 1)
(6, 2)
(6, 3)
(6, 4)
(6, 5)
(6, 6)
(6, 7)= 1.0
(6, 8)
(7, 1)
(7, 2)
(7, 3)
(7, 4)= 1.0
(7, 5)
(7, 6)
(7, 7)
(7, 8)
(8, 1)
(8, 2)
(8, 3)= 1.0
(8, 4)
(8, 5)
(8, 6)
(8, 7)
(8, 8)
Variable z
1= 0.22999999999999993
2= 0.05048780487804879
3= 0.033704909819639285
4= 0.023299988946612143
5= 0.01719167679098487
6= 0.014376797426483129
7= 0.011119273565761674
8= 0.006448859858586732
Variable b
1= 0.22999999999999993
2= 0.26999999999999996
3= 0.6500000000000001
4= 0.47000000000000003
5= 0.5
6= 0.61
7= 0.39
8= 0.25
Variable t
3= 0.06468750000000001
4= 0.10139412207987947
5= 0.07547621320984417
6= 0.06557717893226127
7= 0.08780527427302752
8= 0.049073944019921836
Variable w
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(2, 5)
(2, 6)= 0.22999999999999993
(2, 7)
(2, 8)
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(3, 5)
(3, 6)
(3, 7)
(3, 8)= 0.06468750000000001
(4, 1)
(4, 2)
(4, 3)
(4, 4)
(4, 5)= 0.10139412207987947
(4, 6)
(4, 7)
(4, 8)
(5, 1)= 0.07547621320984417
(5, 2)
(5, 3)
(5, 4)
(5, 5)
(5, 6)
(5, 7)
(5, 8)
(6, 1)
(6, 2)
(6, 3)
(6, 4)
(6, 5)
(6, 6)
(6, 7)= 0.06557717893226127
(6, 8)
(7, 1)
(7, 2)
(7, 3)
(7, 4)= 0.08780527427302752
(7, 5)
(7, 6)
(7, 7)
(7, 8)
(8, 1)
(8, 2)
(8, 3)= 0.049073944019921836
(8, 4)
(8, 5)
(8, 6)
(8, 7)
(8, 8)
obj_val: 155.0661701337002
Start time: Mon Jul 25 11:38:49 1994
End time: Mon Jul 25 11:38:51 1994
Duration (seconds): 2.817117691040039
as u can there is a feasible optimal solution also this optimality of this result is confirmed by other calculation
as u can see my 2 cosntraints i only compare x and w and w get value less than 1
now if i put BigM to 10 in order to reduce computation time i get infeasible soulotion why does this happen?
here is result for BigM = 10
Problem:
- Name: Cx3aUsersXxLonelyAppDataLocalTemptmpy4z93meamodel.gms
Lower bound: -inf
Upper bound: nan
Number of objectives: 1
Number of constraints: 115.0
Number of variables: 143.0
Number of binary variables: None
Number of integer variables: 64.0
Number of continuous variables: 79.0
Number of nonzeros: 493.0
Sense: minimize
Solver:
- Name: GAMS (25, 1, 2, 0)
Status: ok
Return code: 0
Message: None
User time: 0.030000088736415
System time: None
Wallclock time: None
Termination condition: infeasible
Termination message: None
Solution:
- number of solutions: 0
number of solutions displayed: 0
Variable x
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(1, 7)
(1, 8)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(2, 5)
(2, 6)
(2, 7)
(2, 8)
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(3, 5)
(3, 6)
(3, 7)
(3, 8)
(4, 1)
(4, 2)
(4, 3)
(4, 4)
(4, 5)
(4, 6)
(4, 7)
(4, 8)
(5, 1)
(5, 2)
(5, 3)
(5, 4)
(5, 5)
(5, 6)
(5, 7)
(5, 8)
(6, 1)
(6, 2)
(6, 3)
(6, 4)
(6, 5)
(6, 6)
(6, 7)
(6, 8)
(7, 1)
(7, 2)
(7, 3)
(7, 4)
(7, 5)
(7, 6)
(7, 7)
(7, 8)
(8, 1)
(8, 2)
(8, 3)
(8, 4)
(8, 5)
(8, 6)
(8, 7)
(8, 8)
Variable z
1
2
3
4
5
6
7
8
Variable b
1
2
3
4
5
6
7
8
Variable t
3
4
5
6
7
8
Variable w
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(2, 5)
(2, 6)
(2, 7)
(2, 8)
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(3, 5)
(3, 6)
(3, 7)
(3, 8)
(4, 1)
(4, 2)
(4, 3)
(4, 4)
(4, 5)
(4, 6)
(4, 7)
(4, 8)
(5, 1)
(5, 2)
(5, 3)
(5, 4)
(5, 5)
(5, 6)
(5, 7)
(5, 8)
(6, 1)
(6, 2)
(6, 3)
(6, 4)
(6, 5)
(6, 6)
(6, 7)
(6, 8)
(7, 1)
(7, 2)
(7, 3)
(7, 4)
(7, 5)
(7, 6)
(7, 7)
(7, 8)
(8, 1)
(8, 2)
(8, 3)
(8, 4)
(8, 5)
(8, 6)
(8, 7)
(8, 8)
obj_val: 0.0
Start time: Mon Jul 25 11:45:29 1994
End time: Mon Jul 25 11:45:29 1994
Duration (seconds): 0.29376542568206787
3th question
why does cplex and gams cplex solver solution are diffrent and why does when i use other configuration that both them give me same result cplex solving time is more than gams cplex signifently
gams = 3 cplex =16
in small meduim problem
4th question
does really pyomo make solving time dramatically slower that make me use cplex or gams instead of pyomo for lage sacale model?
u want to point that this run time is also high for this scale of model
here is my full code
from pyomo.environ import *
import time
# AbstractModel, Set, Param, Var, Objective, Constraint, summation
# Create model
model = AbstractModel()
# define Sets
# numbre of cities
model.N = Set()
model.N2 = Set()
model.N3 = Set()
model.Pi = Param(model.N)
model.BigM = Param()
#
model.x = Var(model.N * model.N, domain=Binary)
model.z = Var(model.N, domain=NonNegativeReals)
model.b = Var(model.N, domain=NonNegativeReals)
model.t = Var(model.N3, domain=NonNegativeReals)
model.w = Var(model.N2 * model.N, domain=NonNegativeReals)
# define Objective Funvtion
# Objective Funvtion
def obj_expression(model: AbstractModel):
return model.z[8]
model.OBJ = Objective(rule=obj_expression)
# c1_2
def C1_2(model: AbstractModel, i):
return sum(model.w[i,k] for k in model.N) == model.t[i]
# Add the rule to the model
model.C1_2 = Constraint(model.N3, rule=C1_2)
# c1_3
def C1_3(model: AbstractModel, i, k):
return model.w[i,k] <= model.BigM * model.x[i,k]
# Add the rule to the model
model.C1_3 = Constraint(model.N3, model.N, rule=C1_3)
# c1
def C1(model: AbstractModel, i):
return model.z[i] == model.b[i] + sum(model.w[i,k]/model.Pi[k] for k in model.N) + model.z[i-2]
# Add the rule to the model
model.C1 = Constraint(model.N3, rule=C1)
# c2
def C2(model: AbstractModel, i):
return model.t[i] == model.z[i-1] - model.z[i-2]
# Add the rule to the model
model.C2 = Constraint(model.N3, rule=C2)
# c3_2
def C3_2(model: AbstractModel):
return sum(model.w[2,k] for k in model.N) == model.z[1]
# Add the rule to the model
model.C3_2 = Constraint(rule=C3_2)
# c3_3
def C3_3(model: AbstractModel, k):
return model.w[2,k] <= model.BigM * model.x[2,k]
# Add the rule to the model
model.C3_3 = Constraint(model.N, rule=C3_3)
# c3
def C3(model: AbstractModel):
return model.z[2] == model.b[2] + sum(model.w[2,k]/model.Pi[k] for k in model.N)
# Add the rule to the model
model.C3 = Constraint(rule=C3)
# c4
def C4(model: AbstractModel):
return model.z[1] == model.b[1]
# Add the rule to the model
model.C4 = Constraint(rule=C4)
# c5
def C5(model: AbstractModel, i):
return model.b[i] == sum(model.x[i,k] / model.Pi[k] for k in model.N)
# Add the rule to the model
model.C5 = Constraint(model.N, rule=C5)
# c6
def C6(model: AbstractModel, i):
return sum(model.x[i,k] for k in model.N) == 1
# Add the rule to the model
model.C6 = Constraint(model.N, rule=C6)
# c7
def C7(model: AbstractModel, k):
return sum(model.x[i,k] for i in model.N) == 1
# Add the rule to the model
model.C7 = Constraint(model.N, rule=C7)
# c8
def C8(model: AbstractModel, i):
if i < 3:
return Constraint.Skip
return model.t[i] >= 0
# Add the rule to the model
model.C8 = Constraint(model.N, rule=C8)
# c9
def C9(model: AbstractModel, i):
if i == 1:
return Constraint.Skip
return model.z[i-1] <= model.z[i]
# Add the rule to the model
model.C9 = Constraint(model.N, rule=C9)
# create instance
instance = model.create_instance("data.dat")
# instance.display()
instance.pprint()
# Record the start time
start_time = time.time()
# solve model
# cplex
opt = SolverFactory('cplex')
opt.options['mip_display'] = 5
results = opt.solve(instance, tee=False)
# # gams
# solvername = 'gams'
# opt = SolverFactory(solvername)
# results = opt.solve(
# instance, solver='cplex')
# Record the end time
end_time = time.time()
duration = end_time - start_time
print(results)
for v in instance.component_objects(Var):
print("Variable",v)
for index in v:
try:
# print (f" {index}= {value(v[index])}")
# if value(v[index]) != 0:
print (f" {index}= {1/value(v[index])}")
except:
print (f" {index}")
continue
print(f"obj_val: {value(instance.OBJ)}")
print("Start time:", time.ctime(start_time))
print("End time:", time.ctime(end_time))
print("Duration (seconds):", duration)
my data,dat file
set N := 1 2 3 4 5 6 7 8;
set N2 := 2 3 4 5 6 7 8;
set N3 := 3 4 5 6 7 8;
param Pi :=
1 0.5
2 0.23
3 0.25
4 0.39
5 0.47
6 0.27
7 0.61
8 0.65
;
param BigM := 100;
i search in internet i saw pyomo was slow in prevoius version but that was for long time ago and they solved this problem by intrducing solverfactory
i also find solver_io=’python’ but coludn’t use it
i tried biger BigM=1000 and they both give me same optimal result
No name is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.