From 4fd67250a5bdec5894a8c5ca85324dddf7150040 Mon Sep 17 00:00:00 2001 From: Marc Baranski Date: Wed, 8 May 2019 14:45:51 +0200 Subject: [PATCH] * Now possible to indicate in the Init if a subsystem has variation, #33 --- pyDMPC/ControlFramework/Init_Geo.py | 3 + pyDMPC/ControlFramework/Subsystem.py | 13 ++- pyDMPC/ControlFramework/System.py | 1 + pyDMPC/ControlFramework/algorithm/BExMoC.py | 115 +++++++++++--------- 4 files changed, 78 insertions(+), 54 deletions(-) diff --git a/pyDMPC/ControlFramework/Init_Geo.py b/pyDMPC/ControlFramework/Init_Geo.py index d2cd547..26b75ca 100644 --- a/pyDMPC/ControlFramework/Init_Geo.py +++ b/pyDMPC/ControlFramework/Init_Geo.py @@ -122,6 +122,7 @@ IDs_inputs = [] T_set = [] Q_set = [] +variation = [] """ Subsystems """ # Ground @@ -140,6 +141,7 @@ cost_par.append("decisionVariables.y[1]") T_set.append(285) Q_set.append(2200) +variation.append(True) # Building @@ -158,3 +160,4 @@ cost_par.append("decisionVariables.y[1]") T_set.append(295) Q_set.append(0) +variation.append(True) \ No newline at end of file diff --git a/pyDMPC/ControlFramework/Subsystem.py b/pyDMPC/ControlFramework/Subsystem.py index 3dae49b..660126a 100644 --- a/pyDMPC/ControlFramework/Subsystem.py +++ b/pyDMPC/ControlFramework/Subsystem.py @@ -19,7 +19,7 @@ def __init__(self, name, position, bounds_DVs,model_path, names_BCs, num_VarsOut, names_DVs, output_vars, initial_names, IDs_initial_values,IDs_inputs,cost_par, - T_set,Q_set, type_subSyst=None): + T_set,Q_set,variation, type_subSyst=None): self._name = name self._type_subSyst = type_subSyst self._num_DVs = num_DVs @@ -41,6 +41,7 @@ def __init__(self, name, position, self._IDs_inputs = IDs_inputs self.T_set = T_set self.Q_set = Q_set + self.variation = variation def GetNeighbour(self, neighbour_name): @@ -110,7 +111,10 @@ def CalcDVvalues(self, time_step, time_storage, iter, model): if time_step-time_storage < Init.optimization_interval and time_step != Init.sync_rate: # Interpolation try: - [commands, costs, outputs] = BExMoC.Interpolation(self.measurements, self.lookUpTables[1], self._bounds_DVs, self.lookUpTables[0], self.lookUpTables[2]) + [commands, costs, outputs] = BExMoC.Interpolation( + self.measurements, self.lookUpTables[1], + self._bounds_DVs, self.lookUpTables[0], + self.lookUpTables[2],self.variation) except: commands = [] @@ -135,7 +139,10 @@ def CalcDVvalues(self, time_step, time_storage, iter, model): sio.savemat((Init.path_res +'\\'+Init.name_wkdir + '\\' + self._name + '\\' + 'OptimizerTrack.mat' ), {'OptimizerTrackCounter11': res_grid}) try: - [commands, costs, outputs] = BExMoC.Interpolation(self.measurements, self.lookUpTables[1], self._bounds_DVs, self.lookUpTables[0], self.lookUpTables[2]) + [commands, costs, outputs] = BExMoC.Interpolation( + self.measurements, self.lookUpTables[1], + self._bounds_DVs, self.lookUpTables[0], + self.lookUpTables[2], self.variation) print('measurements: ' + str(self.measurements)) print('commands: ' +str(commands)) diff --git a/pyDMPC/ControlFramework/System.py b/pyDMPC/ControlFramework/System.py index c7bc98d..83de130 100644 --- a/pyDMPC/ControlFramework/System.py +++ b/pyDMPC/ControlFramework/System.py @@ -53,6 +53,7 @@ def GenerateSubSys(self): Init.cost_par[i], Init.T_set[i], Init.Q_set[i], + Init.variation[i], Init.type_subSyst[i]) ) subsystems.sort(key = lambda x: x.position) diff --git a/pyDMPC/ControlFramework/algorithm/BExMoC.py b/pyDMPC/ControlFramework/algorithm/BExMoC.py index a978f45..0993d04 100644 --- a/pyDMPC/ControlFramework/algorithm/BExMoC.py +++ b/pyDMPC/ControlFramework/algorithm/BExMoC.py @@ -191,7 +191,8 @@ def CalcLookUpTables(s, time_storage, init_conds): return [storage_cost, storage_DV, storage_out, exDestArr, storage_grid] -def Interpolation(measurements_SubSys, storage_DV, bounds_DVs, storage_cost, storage_out): +def Interpolation(measurements_SubSys, storage_DV, bounds_DVs, storage_cost, + storage_out, variation): """ Interpolate the values of the decision variables, costs and outputs @@ -211,57 +212,69 @@ def Interpolation(measurements_SubSys, storage_DV, bounds_DVs, storage_cost, sto Reformat the boundary conditions, decision variables, outputs and costs """ - cond_BC = [True if L bounds_DVs[i+1]: - commands[i] = bounds_DVs[i+1] - print('Val > higher Bound') - elif val >= bounds_DVs[i] and val <= bounds_DVs[i+1]: - commands[i] = val - # last case: invalid interpolation - else: - commands[i] = bounds_DVs[i] - print('interpolation failed!') - return [commands, costs, out] - - except: + if variation: + cond_BC = [True if L bounds_DVs[i+1]: + commands[i] = bounds_DVs[i+1] + print('Val > higher Bound') + elif val >= bounds_DVs[i] and val <= bounds_DVs[i+1]: + commands[i] = val + # last case: invalid interpolation + else: + commands[i] = bounds_DVs[i] + print('interpolation failed!') + + except: + commands = [] + costs = [] + out = [] + + for i in range(0,len(storage_DV)): + commands.append(storage_DV[0,0]) + costs.append(0) + out.append(0) + print('interpolation failed!') + + else: commands = [] costs = [] out = [] - + for i in range(0,len(storage_DV)): - commands.append(storage_DV[0,0]) - costs.append(0) - out.append(0) - print('interpolation failed!') + commands.append(storage_DV[0,2]) + costs.append(storage_cost[0,2]) + out.append(storage_out[0,2]) + + return [commands, costs, out]