Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def Param(self, value=None, name=None):
"""GK parameters can become MVs and FVs. Since GEKKO defines
MVs and FVs directly, there's not much use for parameters. Parameters
are effectively constants unless the resulting .spm model is used later
and the parameters can be set as MVs or FVs. """
if name is not None:
name = re.sub(r'\W+', '_', name).lower()
else:
name = 'p' + str(len(self._parameters) + 1)
parameter = GKParameter(name, value)
self._parameters.append(parameter)
return parameter
becomes an alias for the first.
var1 = Variable 1 name
var2 = Variable 2 name (default=None)
pos1 = Step position in the collocation horizon for var1 (default=None)
pos2 = Step position in the collocation horizon for var2 (default=None)
node1 = Node within the pos1 step (default='end')
node2 = Node within the pos2 step (default='end')
'''
# TODO: add checks for types
#e.g. if connecting a variable position (pos1 not None) to another variable,
# it must be an FV
# make string versions of var1 and var2
if pos1 is not None:
#make sure var1 is a GEKKO param or var
if isinstance(var1,(GKVariable,GKParameter)):
var1_str = 'p(' + str(pos1) + ').n(' + str(node1) + ').' + var1.name
else:
raise TypeError('Variable 1 must be GEKKO Param or Var to use position')
else:
if var1 is not None:
var1_str = str(var1)
else:
raise Exception('Error: var1 must not be None')
if pos2 is not None:
# make sure var1 is a GEKKO param or var
if isinstance(var2,(GKVariable,GKParameter)):
var2_str = 'p(' + str(pos2) + ').n(' + str(node2) + ').' + var2.name
elif (var2=='fixed'):
var2 = 'fixed'
elif (var2=='calculated'):
raise TypeError
except TypeError:
raise AttributeError(str(name)+" is an output property")
#no other properties allowed
else:
raise AttributeError(str(name)+" is not a property of this variable")
#for initializing model
else:
self.__dict__[name] = value
class GK_FV(GKParameter):
"""Fixed Variable. Inherits GKParameter."""
def __init__(self, name='', value=0, lb=None, ub=None, gk_model=None, model_path=None, integer=False):
# prevents the __setattr__ function from sending options to the server
# until the __init__ function has completed since they should only be
# sent if changed from their defaults
self.__dict__['_initialized'] = False
if not hasattr(self,'type'): #don't overwrite MV
self.type = 'FV'
self.model_name = gk_model
#self.path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.model_name) OLD from when model file were in the same directory; now using temp files
self.path = model_path #use the same path as the model
# FV options
self.gekko_data['vars']['variables'].append(d)
if main_dict[var].name + '.tr' in self.results:
d = var_dict.copy()
d['name'] = var + '(Tr)'
self.gekko_data['vars']['variables'].append(d)
if main_dict[var].name + '.bcv' in self.results:
d1 = var_dict.copy()
d1['name'] = var + '_hist(bias)'
self.gekko_data['vars']['variables'].append(d1)
d2 = var_dict.copy()
d2['name'] = var + '_hist(nobias)'
self.gekko_data['vars']['variables'].append(d2)
d3 = var_dict.copy()
d3['name'] = var + '(nobias)'
self.gekko_data['vars']['variables'].append(d3)
elif isinstance(main_dict[var], GKParameter):
self.gekko_data['vars']['parameters'].append(var_dict)
if isinstance(main_dict[var], (GK_MV, GK_FV)):
d = var_dict.copy()
d['name'] = var + '_hist'
self.gekko_data['vars']['parameters'].append(d)
elif isinstance(main_dict[var], GK_Intermediate):
self.gekko_data['vars']['intermediates'].append(var_dict)
# Update the variable if the data has been loaded before
data = False
if isinstance(main_dict[var], GKVariable):
self.get_variable_fron_main(var)
return
elif isinstance(main_dict[var], GKParameter):
self.get_parameter_from_main(var)
return
self.LOWER = None
self.LSTVAL = None
self.MEAS = None
self.NEWVAL = None
self.PSTATUS = None
self.STATUS = None
if ub is not None:
self.UPPER = ub
else:
self.UPPER = None
self.VDVL = None
self.VLACTION = None
self.VLHI = None
self.VLLO = None
GKParameter.__init__(self, name=name, value=value, integer=integer)