Search code examples
pythonsetpyomo

Use indexed sets in pyomo


I have a flow calculation problem that is from the pyomo website. I altered some of the sets, because I need to test something for a different model. However, the altered version is not running and I cannot figure out why. The code is as follows:

from pyomo.environ import AbstractModel, Param, minimize, Var, NonNegativeReals,\
    Objective, Constraint, SolverFactory, Set, summation, RangeSet

model = AbstractModel()

## Connections
# pipes
model.Pipes = Set()

## Nodes
# all nodes
model.Nodes = Set()
# Nodes or pipes that going out, e.g. from node a going out to b, c, d
model.NodesOut = Set(model.Nodes)
# Nodes or pipes that going in, e.g. in node a going e, f
model.NodesIn = Set(model.Nodes)

model.Flow = Var(model.Pipes, domain=NonNegativeReals)
model.FlowCost = Param(model.Pipes)

model.Demand = Param(model.Nodes)
model.Supply = Param(model.Nodes)

def Obj_rule(m):
    return summation(m.FlowCost, m.Flow)
model.Obj = Objective(rule=Obj_rule, sense=minimize)

def FlowBalance_rule(m, node):
    return m.Supply[node] \
        + sum(m.Flow[m.NodesIn[node][pipe]] for pipe in m.NodesIn[node]) \
        - m.Demand[node] \
        - sum(m.Flow[m.NodesIn[node][pipe]] for pipe in m.NodesOut[node]) \
        == 0
model.FlowBalance = Constraint(model.Nodes, rule=FlowBalance_rule)

dict_data = {
    None: {
        'Pipes': {None: ['AB', 'AC', 'CB']},
        'Nodes': {
            None: ['CityA', 'CityB', 'CityC']
        },
        'NodesIn': {
            'CityA': [],
            'CityB': ['AB', 'CB'],
            'CityC': ['AC',]
        },
        'NodesOut': {
            'CityA': ['AB', 'AC'],
            'CityB': [],
            'CityC': ['CB',]
        },
        'Demand': {
            'CityA': 0,
            'CityB': 1,
            'CityC': 1,
        },
        'FlowCost': {
            'AB': 1.4,
            'AC': 2.7,
            'CB': 1.6,
        },
        'Supply': {
            'CityA': 2,
            'CityB': 0,
            'CityC': 0,
        }
    }
}

I get the following error: IndexError: NodesIn[CityA] indices must be integers, not str. I understand the error, but I do not understand why a string cannot be used. Pyomo solves this pipe flow problem as follows:

from pyomo.environ import AbstractModel, Param, minimize, Var, NonNegativeReals,\
    Objective, Constraint, SolverFactory, Set, summation, RangeSet

model = AbstractModel()

## Connections
# # all connections
model.Arcs = Set(dimen=2)

## Nodes
# all nodes
model.Nodes = Set()
# Nodes or pipes that going out, e.g. from node a going out to b, c, d
model.NodesOut = Set(model.Nodes)
# Nodes or pipes that going in, e.g. in node a going e, f
model.NodesIn = Set(model.Nodes)

model.Flow = Var(model.Arcs, domain=NonNegativeReals)
model.FlowCost = Param(model.Arcs)

model.Demand = Param(model.Nodes)
model.Supply = Param(model.Nodes)

def Obj_rule(m):
    return summation(m.FlowCost, m.Flow)
model.Obj = Objective(rule=Obj_rule, sense=minimize)

def FlowBalance_rule(m, node):
    return m.Supply[node] \
        + sum(m.Flow[i, node] for i in m.NodesIn[node]) \
        - m.Demand[node] \
        - sum(m.Flow[node, j] for j in m.NodesOut[node]) \
        == 0
model.FlowBalance = Constraint(model.Nodes, rule=FlowBalance_rule)

dict_data = {
    None: {
        'Arcs': {None: [('CityA', 'CityB'), ('CityA', 'CityC'), ('CityC', 'CityB')]},
        'Nodes': {
            None: ['CityA', 'CityB', 'CityC']
        },
        'NodesIn': {
            'CityA': [],
            'CityB': ['CityA', 'CityC'],
            'CityC': ['CityA',]
        },
        'NodesOut': {
            'CityA': ['CityB', 'CityC'],
            'CityB': [],
            'CityC': ['CityB',]
        },
        'Demand': {
            'CityA': 0,
            'CityB': 1,
            'CityC': 1,
        },
        'FlowCost': {
            ('CityA', 'CityB'): 1.4,
            ('CityA', 'CityC'): 2.7,
            ('CityC', 'CityB'): 1.6,
        },
        'Supply': {
            'CityA': 2,
            'CityB': 0,
            'CityC': 0,
        }
    }
}

For my more complex problem, I would need to use the first version of the code (using the indexed set m.NodesIn).


Solution

  • I have solved the problem for code I. The FlowBalance_rule has to be reformulated as follows:

    def FlowBalance_rule(m, node):
        return m.Supply[node] \
            + sum(m.Flow[pipe] for pipe in m.NodesIn[node]) \
            - m.Demand[node] \
            - sum(m.Flow[pipe] for pipe in m.NodesOut[node]) \
            == 0
    model.FlowBalance = Constraint(model.Nodes, rule=FlowBalance_rule)