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

inverse relation shadow variable NameError: name XXX is not defined

MinamiHuang opened this issue · comments

@planning_entity
class Device:
    def __init__(self, id, node, controller):
        self.id = id
        self.node = node
        self.controller = controller

    @planning_id
    def get_id(self):
        return self.id

    def get_node():
        return node

    @planning_variable(Controller, ["controller_range"])
    def get_controller():
        return controller

    def set_controller(new_ctr):
        self.controller = new_ctr

@planning_entity
class Controller:
    def __init__(self, id, node, dev_list=None):
        self.id = id
        self.node = node
        if dev_list is None:
            self.dev_list = []
        else:
            self.dev_list = dev_list

    @planning_id
    def get_id(self):
        return self.id

    def get_node():
        return node

    @inverse_relation_shadow_variable(Controller, source_variable_name = "controller")
    def get_dev_list():
        return dev_list

    def set_dev_list(new_list):
        self.dev_list = new_list

If I pass the 'Controller', I get the NameError. If I put the class Controller before Device and omit the param source_type, I receive the message "missing 1 required positional argument: 'source_type' "

Please help me construct the Bi-directional variable, thanks very much.

@ge0ffrey
@Christopher-Chianelli

I recommend you look at the inverse relation test:

@optapy.planning_entity
class InverseRelationEntity:
def __init__(self, code, value=None):
self.code = code
self.value = value
@optapy.planning_variable(object, ['value_range'])
def get_value(self):
return self.value
def set_value(self, value):
self.value = value
@optapy.planning_entity
class InverseRelationValue:
def __init__(self, code, entities=None):
self.code = code
if entities is None:
self.entities = []
else:
self.entities = entities
@optapy.inverse_relation_shadow_variable(InverseRelationEntity, source_variable_name='value')
def get_entities(self):
return self.entities
def set_entities(self, entities):
self.entities = entities

In particular, you would use object for get_controller, and Device for get_dev_list.