Skip to content

Ability to create sub classes #2057

Open
@staadecker

Description

@staadecker

Summary

I would love to be able to create subclasses of Expression, Set, Param, and Var. Specifically I'd love to be able to override __init__() to handle some custom arguments. Unfortunately, the way many of the classes are setup at the moment make this impossible. For example, __new__() in Set prevents properly creating an instance of a subclass of set.

Rationale

Being able to extend Pyomo would allow greater flexibility for users.

Example

import pyomo.environ as pyo


class CustomSet(pyo.Set):
    def __init__(self, *args, custom_param=None, **kwargs):
        self.custom_param = custom_param
        super().__init__(*args, **kwargs)


# Works fine
m1 = pyo.AbstractModel()
m1.FirstSet = pyo.Set(dimen=2)
m1.DependantSet = pyo.Set(m1.FirstSet)

# Doesn't work, raises "can't apply a Set operator to an indexed Set component"
m2 = pyo.AbstractModel()
m2.FirstSet = CustomSet(dimen=2, custom_param="some_value")
m2.DependantSet = pyo.Set(m2.FirstSet)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions