3

I am working on a Mixed Integer Programming Model in Pyomo and hope to set initial values for my decision variables (which is a matrix in my case). How can I do that?

To be more specific, the following can succeed in setting a initial guess of 2 on the (scalar) decision variable y.

model.y = Var(initialize=2)

However, what if model.x is a 3-by-2 matrix and I want to make an initial guess for each of its elements? For instance, I tried something like the following without success:

model.I = RangeSet(3)
model.J = RangeSet(2)
model.K = Set(within=model.I *model.J)
model.x = Var(model.K, initialize=np.randomize(3,2))

Could someone help with this? Thanks much.

Wilson
  • 125
  • 8
  • I think you get this error "module 'numpy' has no attribute 'randomize'". Numpy has random module. – kur ag Sep 14 '20 at 08:32

1 Answers1

1

It depends on what you would like to initialize the values of each element to. For example, if you want every element in model.x to be initialized to the same value, such as 1, you can do the following:

from pyomo.environ import Var, RangeSet, ConcreteModel

model = ConcreteModel()

model.I = RangeSet(3)
model.J = RangeSet(2)
model.x = Var(model.I, model.J, initialize=1)

Alternatively, if you have specific values in mind for each element, you can construct a dictionary of indexed keys to values:

model = ConcreteModel()

model.I = RangeSet(1)
model.J = RangeSet(2)
init_dict = {(1,1):2,
             (1,2):4}
model.x = Var(model.I, model.J, initialize=init_dict)

For larger sets, you can use something like itertools.product to generate all of the keys and np.random.randomint to generate a random value with list comprehension. This would let you add optional arguments like upper/lower random value bounds. Use model.x.pprint() to see the initialized values.