-1

Let us consider the following set:

m.sub_sit = pyomo.Set(
    initialize=[site],
    doc='site of sub problem')

in this case m.sub_sit is created by create_model() function with the argument 'Mid':

inst = create_model(site='Mid')

what I get by calling inst.sub_sit.pprint():

In:

inst.sub_sit.pprint()

Out:

sub_sit : site of sub problem
    Dim=0, Dimen=1, Size=1, Domain=None, Ordered=False, Bounds=None
    ['Mid']

What I really want to output is 'Mid', so that I can index other objects with 'Mid', and then I can use it in my code.

e.g.:

In1: PiZero
In2: PiZero[inst.sub_sit[1]]
In3: PiZero['Mid']
Out1:

 sub_sit
 Mid   -1.0
 Name: sub_costs, dtype: float64

Out2: -1.0

Out3: -1.0

Question: is there anyway to call this 'Mid' string from m.sub_sit pyomo set object, better than what I suggest?

btw m.sub_sit.value gives an output, almost what I needed: {'Mid'}

oakca
  • 1,408
  • 1
  • 18
  • 40
  • Why are you trying to index into a Pyomo Set using square brackets? If you want to index a variable by sub_sit you can do so with: m.PiZero = Var(m.sub_sit, initialize=-1) – Bethany Nicholson Dec 20 '17 at 17:38
  • it is a concrete model and PiZero is a dual variable which is not inside sub model, and is used for generating cuts for the master model... – oakca Dec 20 '17 at 21:16
  • @Engineero it was about pandas series because PiZero was pandas series object. TY for edit but also you are wrong. – oakca Dec 20 '17 at 21:17
  • You really need to give more context and show more of the model/code. Your question doesn't say anything about using Pandas objects. It's not clear to me what you are trying to do with sub_sit and PiZero. – Bethany Nicholson Dec 20 '17 at 22:11
  • question updated. – oakca Dec 20 '17 at 23:12

2 Answers2

1

You should never have to index into a Pyomo Set using square brackets. If you find yourself wanting to do this then you might want to rethink your motivation for using a Pyomo Set instead of a native Python list.

If your goal is to iterate through the values in the Pyomo Set and use those to index into Pandas objects then you can do something like:

for s in m.sub_sit:
    v = PiZero[s]
    # Do something with v

Alternatively you can cast the Pyomo Set to a list and work with the list instead:

s = list(m.sub_sit)

But again, if you're going to do this, do you really need the Pyomo Set?

If you want more examples of using Pandas and Pyomo together then take a look at this github repo for a sensor placement package. In particular you can see examples of creating Pyomo Sets using values from a Pandas DataFrame and also how to write constraints that use data in a DataFrame.

Bethany Nicholson
  • 2,723
  • 1
  • 11
  • 18
  • well it is a huge model and same model is used to create different models. Just one exception that the sub_sit is changing to an other string. And I don't need to use the for loop, since 'Mid' is the only element of it. Yes I could use a native python variable like: sub_sit = 'Mid' and then index it, but a drawback is I cannot reach sub_sit inside my constraint declerations, since the model given to the const_rules I can always get the value of m.sub_sit inside those const_rules. – oakca Dec 21 '17 at 17:35
  • here is an example: `def sub_costs_rule(m): return (pyomo.summation(m.costs) <= m.eta_res[m.sub_sit[1]] + m.Lambda * m.omegazero)` and yes I can always do `def sub_costs_rule(m, sub_sit)`, but now the `sub_cost_rule` has an other index, and it is breaking some of my functions. So better with first way. – oakca Dec 21 '17 at 17:42
  • Will sub_sit ever have more than one item? Another option is to attach your own data to a model e.g. `m.sub_sit = 'Mid'` or `m.sub_sit = ['Mid']` – Bethany Nicholson Dec 21 '17 at 19:32
  • You are right. One question though, do you think would it make a difference on computational speed if I declare it as a pyomo set, instead of `m.sub_sit = 'Mid'?` – oakca Dec 21 '17 at 22:13
  • Using a pyomo set will use more memory. I'm not sure if there is a significant difference in computational speed. – Bethany Nicholson Jan 02 '18 at 16:08
  • Hi Bethany. Is this still an issue? I'm currently debating whether I use Pyomo sets or simple lists to define certain constraints. Any updates on the memory/time efficiency of the alternatives? – David Bernal Jun 14 '19 at 23:37
0

Firstly ordered=True is added to m.sub_sit. Then 'Mid' was calliable with inst.sub_sit[1]. It should be ordered set otherwise it generates an error, that you cannot call the first object of unordered set.

m.sub_sit = pyomo.Set(
    initialize=[site],
    ordered=True,
    doc='site of sub problem')

In:

inst.sub_sit[1]

Out:

'Mid'
oakca
  • 1,408
  • 1
  • 18
  • 40