title: Pyomo | Creating simple model with indexed set
body: <p>I am having trouble creating a simple model in pyomo. I want to define the following abstract model:
  <img src="https://i.stack.imgur.com/kMnha.png" alt="image with model setup" /></p>
  <p><strong>An attempt at creating an abstract model</strong></p>
  <p>I define</p>
  <pre><code>m.V = pyo.Set()
  m.C = pyo.Set() # I first wanted to make this an indexed set in m.V, but this does not work as I cannot create variables with indexed sets (in next line)
  m.Components = pyo.Var(m.V*m.C, domain=Binary)  
  </code></pre>
  <p>Now I have no idea how to add the constraint. Just adding</p>
  <pre><code>Def constr(m,v):
      return sum([m.Components[v,c] for c in m.C]) == 2
  m.Constraint = Constraint(m.V, rule= constr)
  </code></pre>
  <p>will lead to the model also summing over components in <code>m.C</code> that should not fall under <code>m.V</code> (eg if I pass <code>m.V = ['Cars', 'Boats']</code>, and one of the <code>'Boats'</code> components I want to pass is <code>‘New sails’</code>; the above constraint will also put a constraint on <code>m.Components[‘Cars’,’New sails’]</code>, which does not make much sense.</p>
  <p><strong>Trying to work out a concrete example</strong></p>
  <p>Now if I try to work through this problem in a concrete way and follow e.g. <a href="https://stackoverflow.com/questions/59237082/variable-indexed-by-an-indexed-set-with-pyomo">Variable indexed by an indexed Set with Pyomo</a>, I still get an issue with the constraint. E.g. say I want to create a model that has this structure:</p>
  <pre><code>set_dict = {‘Car’:[ ‘New wheels’, ’New gearbox’, ’New seats’],’Boat’: [’New seats’, ‘New sail’, ‘New rudder‘]}
  </code></pre>
  <p>I then create these sets and variables:</p>
  <pre><code>m.V = pyo.Set(initialize=[‘Car’,’Boat’])
  m.C = pyo.Set(initialize=[‘New wheels’, ’New gearbox’, ’New seats’, ‘New sail’, ‘New rudder‘])
  m.VxC = pyo.Set(m.V*m.C, within = set_dict)
  m.Components = pyo.Var(m.VxC, domain=Binary)
  </code></pre>
  <p>But now I still dont see a way to add the constraint in a pyomo native way. I cannot define a function that sums just over <code>m.C</code> as then it will sum over values that are not allowed again (e.g. as above, ‘New sail’ for the ‘Cars’ vehicle type). It seems the only way to do this is to refer back to the set_dict and loop &amp; sum over that?</p>
  <p>I need to create an <strong>abstract</strong> model, so I want to be able to write out this model in a pyomo native way, not relying on additional dictionaries and other objects to pass the right dimensions/sets into the model.</p>
  <p>Any idea how I could do this?</p>