Search code examples
pythonpyomo

Pyomo: TypeError: unhashable type: 'OrderedScalarSet'


Dears,
I am new on Python and Pyomo as well.

I'm creating an abstract model, below there are the sets, parameters and variables related to my error:

SETS:
SCUC.UP = Set()
SCUC.zone = Set()
from .dat file
"set UP := G1 G2 G3 G4 G5 G6;
set zone := Z1 Z2 Z3;"

PARAMETERS: SCUC.Pmi = Param(SCUC.UP)
SCUC.zonal_UP = Param(SCUC.UP, SCUC.zone)
from .dat file
"param Pmi :=
G1 300.0
G2 200.0
G3 350.0
G4 210.0
G5 200.0
G6 240.0;
param zonal_UP :=
G1 Z1 1
G2 Z1 1
G3 Z1 0
G4 Z1 0
G5 Z1 0
G6 Z1 0
G1 Z2 0
G2 Z2 0
G3 Z2 1
G4 Z2 1
G5 Z2 0
G6 Z2 0
G1 Z3 0
G2 Z3 0
G3 Z3 0 G4 Z3 0
G5 Z3 1
G6 Z3 1;"

VARIABLES:
SCUC.UP_gz = Var(SCUC.zone)
SCUC.DPg_g = Var(SCUC.UP, within = Reals)

I'm trying to type three constraints where I need to multiply a vector of variables/parameters with a vector of parameters to obtain a scalar value. Here the constraint formulation:

def zonal_gen_rule(SCUC,z):  
    return SCUC.UP_gz[z]  ==  SCUC.Pmi[SCUC.UP] * SCUC.zonal_UP[SCUC.UP,z] + SCUC.zonal_UP[SCUC.UP,z] * SCUC.DPg_g[SCUC.UP]
SCUC.zonal_gen = Constraint(SCUC.zone, rule=zonal_gen_rule)

But when I lunch the instance construction it appears the following error:

TypeError: unhashable type: 'OrderedScalarSet'

It also tried to transpose the vectors with numpy but it didn't work.

I hope someone could help me.


Solution

  • You are getting that error because you are passing the entire set into your expression, instead of an element of the set. And as the whole set is "unhashable" you are getting the error

    SCUC.UP_gz[z]  ==  SCUC.Pmi[SCUC.UP] * ...
                                   ^
                                this is illegal... it is the whole set
    

    It isn't clear what type of constraint you are trying to make from the context of your question... Meaning it isn't clear if you intend to sum across the members of SCUC.UP or make a constraint for each pair... So the example below shows "both ways" of doing this.

    Also, a little standardization in use of caps will help you troubleshoot. Convention is that all variables are lower case, however, convention in set notation sometimes has caps for the set name and lower for members.... either way, consistency is key to troubleshooting.

    from pyomo.environ import *
    
    SCUC = ConcreteModel('example')
    
    # SETS
    SCUC.ups = Set(initialize=['G1', 'G2', 'G3'])
    SCUC.zones = Set(initialize=['Z1', 'Z2'])
    
    # PARAMS
    SCUC.pmi = Param(SCUC.ups, initialize=2)  # junk initialization...
    SCUC.zonal_up = Param(SCUC.ups, SCUC.zones, initialize=3)
    
    # VARIABLES
    SCUC.u = Var(SCUC.zones)
    SCUC.d = Var(SCUC.ups)
    
    # CONSTRAINTS
    # "for each" (up, zone) pair...
    def zonal_gen_rule(SCUC, zone, up):
        return SCUC.u[zone] == SCUC.pmi[up] * SCUC.zonal_up[up, zone] \
                               + SCUC.zonal_up[up, zone] * SCUC.d[up]
    SCUC.zonal_gen_1 = Constraint(SCUC.zones, SCUC.ups, rule=zonal_gen_rule)
    
    # "for each zone, sum the ups"
    def zonal_gen_rule_2(SCUC, zone):
        return SCUC.u[zone] == sum(SCUC.pmi[up] * SCUC.zonal_up[up, zone] \
                               + SCUC.zonal_up[up, zone] * SCUC.d[up] for up in SCUC.ups)
    SCUC.zonal_gen_2 = Constraint(SCUC.zones, rule=zonal_gen_rule_2)
    
    SCUC.pprint()
    

    Yields:

    ...
    
    2 Constraint Declarations
        zonal_gen_1 : Size=6, Index=zonal_gen_1_index, Active=True
            Key          : Lower : Body                  : Upper : Active
            ('Z1', 'G1') :   0.0 : u[Z1] - (6 + 3*d[G1]) :   0.0 :   True
            ('Z1', 'G2') :   0.0 : u[Z1] - (6 + 3*d[G2]) :   0.0 :   True
            ('Z1', 'G3') :   0.0 : u[Z1] - (6 + 3*d[G3]) :   0.0 :   True
            ('Z2', 'G1') :   0.0 : u[Z2] - (6 + 3*d[G1]) :   0.0 :   True
            ('Z2', 'G2') :   0.0 : u[Z2] - (6 + 3*d[G2]) :   0.0 :   True
            ('Z2', 'G3') :   0.0 : u[Z2] - (6 + 3*d[G3]) :   0.0 :   True
        zonal_gen_2 : Size=2, Index=zones, Active=True
            Key : Lower : Body                                              : Upper : Active
             Z1 :   0.0 : u[Z1] - (6 + 3*d[G1] + 6 + 3*d[G2] + 6 + 3*d[G3]) :   0.0 :   True
             Z2 :   0.0 : u[Z2] - (6 + 3*d[G1] + 6 + 3*d[G2] + 6 + 3*d[G3]) :   0.0 :   True