optapy / optapy

OptaPy is an AI constraint solver for Python to optimize planning and scheduling problems.

Home Page:https://www.optapy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

optapy - Support attribute level annotations/decorators

Christopher-Chianelli opened this issue · comments

Currently, annotations/decorators can only be specified on a getter
(i.e. like this:

@planning_entity
class Entity:
    value: int
    
    def __init__(self, value=None):
        self.value = value
    
    @planning_variable(int, value_range_provider_ref=['value_range'])
    def get_value(self):
        return self.value
    
    def set_value(self, value):
        self.value = value

)

This code is quite verbose, so we would like to support attribute level annotations/decorators.
However, unlike Java, you cannot put an annotation on an attribute (other than declaring what type an attribute is).

There are several options:

Do it on a class level (on @planning_entity/@planning_solution):

@planning_entity(
    planning_variables=[PlanningVariable('value', int, value_range_provider_ref=['value_range'])]
)
class Entity:
    value: int
    def __init__(self, value=None):
        self.value = value

Do it as a static/class attribute

@planning_entity
class Entity:
    value: int = PlanningVariable(int, value_range_provider_ref=['value_range'])
    def __init__(self, value=None):
        self.value = value

Do it with Annotated (https://docs.python.org/3/library/typing.html#typing.Annotated)

@planning_entity
class Entity:
    value: Annotated[int, PlanningVariable(value_range_provider_ref=['value_range'])]
    def __init__(self, value=None):
        self.value = value