Skip to content

Commit

Permalink
Minor refactor on optbase and its derivatives
Browse files Browse the repository at this point in the history
  • Loading branch information
jinningwang committed Dec 13, 2024
1 parent 6dcb027 commit 14b436e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 39 deletions.
6 changes: 6 additions & 0 deletions ams/opt/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def __init__(self,
self.dual = None
self.code = None

def get_idx(self):
raise NotImplementedError

def get_all_idxes(self):
raise NotImplementedError

@ensure_symbols
def parse(self):
"""
Expand Down
14 changes: 1 addition & 13 deletions ams/opt/exprcalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,11 @@ def __init__(self,
model: Optional[str] = None,
src: Optional[str] = None,
):
OptzBase.__init__(self, name=name, info=info, unit=unit)
OptzBase.__init__(self, name=name, info=info, unit=unit, model=model)
self.optz = None
self.e_str = e_str
self.code = None
self.model = model
self.owner = None
self.src = src
self.is_group = False

def get_idx(self):
if self.is_group:
return self.owner.get_idx()
elif self.owner is None:
logger.info(f'ExpressionCalc <{self.name}> has no owner.')
return None
else:
return self.owner.idx.v

@ensure_symbols
def parse(self):
Expand Down
14 changes: 1 addition & 13 deletions ams/opt/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,14 @@ def __init__(self,
vtype: Optional[str] = float,
horizon: Optional[str] = None,
):
OptzBase.__init__(self, name=name, info=info, unit=unit)
OptzBase.__init__(self, name=name, info=info, unit=unit, model=model)
self.tex_name = tex_name
self.e_str = e_str
self.optz = None
self.code = None
self.model = model
self.owner = None
self.src = src
self.is_group = False
self.horizon = horizon

def get_idx(self):
if self.is_group:
return self.owner.get_idx()
elif self.owner is None:
logger.info(f'ExpressionCalc <{self.name}> has no owner.')
return None
else:
return self.owner.idx.v

@ensure_symbols
def parse(self):
"""
Expand Down
13 changes: 13 additions & 0 deletions ams/opt/optbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def __init__(self,
name: Optional[str] = None,
info: Optional[str] = None,
unit: Optional[str] = None,
model: Optional[str] = None,
):
self.om = None
self.name = name
Expand All @@ -97,6 +98,9 @@ def __init__(self,
self.rtn = None
self.optz = None # corresponding optimization element
self.code = None
self.model = model # indicate if this element belongs to a model or group
self.owner = None # instance of the owner model or group
self.is_group = False

@ensure_symbols
def parse(self):
Expand Down Expand Up @@ -153,3 +157,12 @@ def size(self):

def __repr__(self):
return f'{self.__class__.__name__}: {self.name}'

def get_idx(self):
if self.is_group:
return self.owner.get_idx()
elif self.owner is None:
logger.info(f'{self.class_name} <{self.name}> has no owner.')
return None
else:
return self.owner.idx.v
14 changes: 1 addition & 13 deletions ams/opt/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,8 @@ def __init__(self,
self.tex_name = tex_name if tex_name else name
# variable internal index inside a model (assigned in run time)
self.id = None
OptzBase.__init__(self, name=name, info=info, unit=unit)
OptzBase.__init__(self, name=name, info=info, unit=unit, model=model)
self.src = src
self.is_group = False
self.model = model # indicate if this variable is a group variable
self.owner = None # instance of the owner model or group
self.v0 = v0
self.horizon = horizon
self._shape = shape
Expand Down Expand Up @@ -166,15 +163,6 @@ def v(self, value):
else:
self.optz.value = value

def get_idx(self):
if self.is_group:
return self.owner.get_idx()
elif self.owner is None:
logger.info(f'Variable <{self.name}> has no owner.')
return None
else:
return self.owner.idx.v

@ensure_symbols
def parse(self):
"""
Expand Down

0 comments on commit 14b436e

Please sign in to comment.