I am trying to write a simple optimization problem by using DOCPLEX. I am pretty new on python+DO and What I have tried to do is:
# Set and parameters
I = ["i1"];
J = ["j1"];
Parameters form = {("i1", "j1"): number}
# model
mdl = Model(name='name')
# Declare variables
idx_x = [(i,j) for i in I for j in J]
x = mdl.binary_var_dict(idx_x, name="x");
# The optimization model
objective function & model constraints
# output
mdl.print_information()
m = mdl.solve(log_output=True)
s = m.display()
It seems the above template is fine and the problem is solved without any issue. The problem I have faced is, when I want to invoke the variable's value, I cannot find a convenient way to get whose values and applying them again in other parts of the model. Let's say the following methods display the solution, but they are not what I am looking for.
The first method:
for v in mdl.iter_binary_vars():
print(v," = ",v.solution_value)
The second method:
print(mdl.solution.get_value_dict(x))
I want to write an equation as $Gamma_{i} = 1 - \sum_{i} \sum_{j} x_{i,j}$ in wich $x_{i,j}$ would be the variable's value. I also tried a method as follows, but it produces an error:
for i in range(len(I)):
for j in range(len(J)):
print(m.x[i,j].solution_value)
==========================================
AttributeError: 'SolveSolution' object has no attribute 'x'
I was wondering if, how we can fix that?