4

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?

A.Omidi
  • 8,832
  • 2
  • 13
  • 49

1 Answers1

4

The key is a tuple of strings and not a tuple of numbers.

from docplex.mp.model import Model

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()

for v in mdl.iter_binary_vars(): print(v," = ",v.solution_value)

for i in I: for j in J: print("i=",i," and j=",j," gives ",x[(i,j)].solution_value)

gives

solution for: name
x_i1_j1  =  0
i= i1  and j= j1  gives  0

and with regards to the constraint you could try

gamma=0
mdl.add(gamma==1-sum(x[(i,j)] for i in I for j in J))
Alex Fleischer
  • 4,000
  • 5
  • 11
  • Dear Alex, Many thanks for your explanation. I can do that. It is very strange that with the definition of the set as $["i"]$ it is represented as a range and it works fine and does not need to use the range syntax!!! Also, is it possible to elaborate a bit more on the second part of the question? – A.Omidi Dec 27 '21 at 04:58
  • 1
    @A.Omidi You defined your set I as a list with one string element in it (["i1"]) and then you used that to define your variable x. So, your variable's key is the tuple of ('i1', 'j1') and when you want to access the variable, you need to say x['i1', 'j1'] (which is what Alex did in his answer). – EhsanK Dec 27 '21 at 17:48
  • @EhsanK, many thanks for your explanation. – A.Omidi Dec 27 '21 at 18:45