'How can I take the solution values from first calculation and use it in another calculation in python using docplex
I calculated a machine scheduling problem using docplex in python. I obtained one of the decision variable as:
| yib | solution |
|---|---|
| y_0_1 | 1 |
| y_1_2 | 1 |
| y_2_1 | 1 |
| y_3_1 | 1 |
| y_4_1 | 1 |
Since I wanted to use this values in another calculation I used this code:
ySol = [y[i,b].solution_value for i in range(0,J) for b in range(1,B)]
Then, I tried to use ySol in my constraints.
***my first question is, this code is true code to take the decision variable?
after I added ySol in the second calculation I took this error:
"TypeError: list indices must be integers or slices, not tuple"
I tried some alternative ways but I've not solved the tuple problem yet.
***my second question is, how can I solve this error?
thank you in advance!
Solution 1:[1]
you can either use warmstart
from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)
warmstart=mdl.new_solution()
warmstart.add_var_value(nbbus40,8)
warmstart.add_var_value(nbbus30,0)
mdl.add_mip_start(warmstart)
sol=mdl.solve(log_output=True)
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
or fixed start
from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)
#Fixed start nbBus40 should be 5
nbbus40.lb=5
nbbus40.ub=5
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Fleischer |
