0

I am not really sure about how to define a decision variable by using integer_var_list(). Supposing I have a set of scalar decision variables X = [[x1,x2,x3,x4,x5,x6]], then I defined them as:

'''

x = np.empty((Total_T,1), dtype= object)
for i in range(Total_T):
      x[i] = mdl.integer_var(lb= 0, ub= inf, name='x' + str(i+1))
Then I will get X as follows (each x is scalar).
[[x1]
 [x2]
 [x3]
 [x4]
 [x5]
 [x6]]

'''

However when I tried following code:

'''

x = np.empty((Total_T,1), dtype= object)
for i in range(Total_T):
      x = mdl.integer_var_list(Total_T,lb= 0, ub= inf, name='x' + str(i+1))
I got an error notice below.

[docplex.mp.Var(type=I,name='x6_0'), docplex.mp.Var(type=I,name='x6_1'), 
docplex.mp.Var(type=I,name='x6_2'), docplex.mp.Var(type=I,name='x6_3'), 
 docplex.mp.Var(type=I,name='x6_4'), docplex.mp.Var(type=I,name='x6_5')]
Traceback (most recent call last):
  File "C:/Users/pknu/Documents/Research/project/MILP Case 1 19 July 2022.py", line 78, in 
   <module>
    x_in = get_index_value(1) #number 1 for transition in
  File "C:/Users/pknu/Documents/Research/project/MILP Case 1 19 July 2022.py", line 73, in 
  get_index_value
    get_value = x[ind_x]
TypeError: list indices must be integers or slices, not tuple

'''

Can anyone let me know:

  1. What are the keys in mdl.integer_var_list(keys, lb = None, ub=None, name =<class'str'>, key_format=None) and how to use it?
  2. Can I define X as mdl.integer_var_list but contains x[i] as mdl.integer_var for i from 0 to Total_T=6?

Thank you.

Best regards,

Nicholas

1 Answers1

0

Small integer_var_list example

from docplex.mp.model import Model 

Buses=[
    (40,500),
    (30,400)
    ]

nbKids=300
nbSizes=len(Buses)

# Indexes

busSize=0;
busCost=1;

for b in Buses:
    print("buses with ",b[busSize]," seats cost ",b[busCost])

mdl = Model(name='buses')

#decision variables

mdl.nbBus = mdl.integer_var_list(nbSizes,0,1000,name="nbBus")


# Constraint
mdl.add(sum(mdl.nbBus[b]*Buses[b][busSize] for b in range(0,nbSizes)) >= nbKids)

# Objective
mdl.minimize(sum(mdl.nbBus[b]*Buses[b][busCost] for b in range(0,nbSizes)))

msol=mdl.solve()

# Dislay solution
for b  in range(0,nbSizes):
    print(msol[mdl.nbBus[b]]," buses with ",Buses[b][busSize]," seats")
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15