Skip to content

Commit

Permalink
* updated Control Framework, #33
Browse files Browse the repository at this point in the history
  • Loading branch information
LiMaDaMe committed Mar 12, 2019
1 parent d82371f commit 27bb3dc
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 8,166 deletions.
15 changes: 11 additions & 4 deletions pyDMPC/ControlFramework/Init.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

""" Settings for BExMoC algorithm """
# So far: For all subsystems the same settings
factors_BCs = [1.5, 0.03] # order: BC1, BC2, ...
center_vals_BCs = [30, 0.001]
amount_lower_vals = [9, 0]
amount_upper_vals = [9, 1]
factors_BCs = [5, 0.03] # order: BC1, BC2, ...
center_vals_BCs = [15, 0.001]
amount_lower_vals = [2, 0]
amount_upper_vals = [2, 1]
exp_BCs = [1, 1]
amount_vals_BCs = [1, 1]

Expand Down Expand Up @@ -120,6 +120,8 @@
IDs_initial_values= [] #for simulation
cost_par = [] #for MassFlowRate
IDs_inputs = []
T_set = []
Q_set = []

""" Subsystems """
# Ground
Expand All @@ -136,6 +138,9 @@
IDs_initial_values.append([])
IDs_inputs.append(["fieldTemperature_in","fieldMassflow_in"])
cost_par.append("decisionVariables.y[1]")
T_set.append(285)
Q_set.append(2200)


# Building
name.append('Building')
Expand All @@ -151,3 +156,5 @@
IDs_initial_values.append([])
IDs_inputs.append(["buildingMassflow_in","buildingTemperature_in"])
cost_par.append("decisionVariables.y[1]")
T_set.append(295)
Q_set.append(0)
9 changes: 8 additions & 1 deletion pyDMPC/ControlFramework/Init_Geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
IDs_initial_values= [] #for simulation
cost_par = [] #for MassFlowRate
IDs_inputs = []
T_set = []
Q_set = []

""" Subsystems """
# Ground
Expand All @@ -136,6 +138,9 @@
IDs_initial_values.append([])
IDs_inputs.append(["fieldTemperature_in","fieldMassflow_in"])
cost_par.append("decisionVariables.y[1]")
T_set.append(285)
Q_set.append(2200)


# Building
name.append('Building')
Expand All @@ -150,4 +155,6 @@
initial_names.append([])
IDs_initial_values.append([])
IDs_inputs.append(["buildingMassflow_in","buildingTemperature_in"])
cost_par.append("decisionVariables.y[1]")
cost_par.append("decisionVariables.y[1]")
T_set.append(295)
Q_set.append(0)
8 changes: 4 additions & 4 deletions pyDMPC/ControlFramework/Objective_Function.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ def Obj(values_DVs, BC, s):
global gl_output
gl_output = output_list


"""all other subsystems + costs of downstream system"""
k = 0
if s._type_subSyst != "consumer":
Expand All @@ -199,9 +198,10 @@ def Obj(values_DVs, BC, s):
for tout in output_traj[0]:
# Avoid nan by suppressing operations with small numbers
if values_DVs > 0.0001:
cost_total += Init.cost_factor*(values_DVs - Q_set)**2 + Init.cost_factor*(tout - T_set)**2 + costs_neighbor(20,tout-273)
cost_total += Init.cost_factor*(values_DVs - Init.Q_set)**2 + Init.cost_factor*(tout - Init.T_set)**2 + costs_neighbor(20,tout-273)
else:
cost_total += costs_neighbor(20,tout-273)
#cost_total += costs_neighbor(20,tout-273)
cost_total += 0
k += 1

cost_total = cost_total/len(output_traj[0])
Expand All @@ -210,7 +210,7 @@ def Obj(values_DVs, BC, s):
print("output: " + str(tout))
else:
for tout in output_traj[0]:
cost_total += Init.cost_factor*(values_DVs - Q_set)**2 + Init.cost_factor*(tout - T_set)**2 + costs_neighbor(20,tout-273)
cost_total += Init.cost_factor*(values_DVs - s.Q_set)**2 + Init.cost_factor*(tout - s.T_set)**2
#cost_total += max(0.01,cost_par[k])*Init.cost_factor + 100*(max(abs(tout-273-Init.set_point[0])-Init.tolerance,0))**2
k += 1

Expand Down
9 changes: 4 additions & 5 deletions pyDMPC/ControlFramework/Subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
type_subSyst=None):
T_set,Q_set, type_subSyst=None):
self._name = name
self._type_subSyst = type_subSyst
self._num_DVs = num_DVs
Expand All @@ -39,6 +39,8 @@ def __init__(self, name, position,
self._initial_names = initial_names
self._IDs_initial_values = IDs_initial_values
self._IDs_inputs = IDs_inputs
self.T_set = T_set
self.Q_set = Q_set


def GetNeighbour(self, neighbour_name):
Expand Down Expand Up @@ -74,10 +76,7 @@ def CalcDVvalues(self, time_step, time_storage, iter, model):
""" Get Measurements """
self.measurements = self.GetMeasurements(self._IDs_inputs, model)

if self._type_subSyst != "generator":
values = np.concatenate(([0.0], self.measurements[::-1]),axis=0)
else:
values = [0.0, self.measurements[1], self.measurements[0], self.measurements[3], self.measurements[2]]
values = np.concatenate(([0.0], self.measurements[::-1]),axis=0)
print(values)

# Save new 'CompleteInput.mat' File
Expand Down
2 changes: 2 additions & 0 deletions pyDMPC/ControlFramework/System.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def GenerateSubSys(self):
Init.IDs_initial_values[i],
Init.IDs_inputs[i],
Init.cost_par[i],
Init.T_set[i],
Init.Q_set[i],
Init.type_subSyst[i])
)
subsystems.sort(key = lambda x: x.position)
Expand Down
Binary file modified pyDMPC/ControlFramework/__pycache__/Init.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file modified pyDMPC/ControlFramework/__pycache__/Subsystem.cpython-36.pyc
Binary file not shown.
Binary file modified pyDMPC/ControlFramework/__pycache__/System.cpython-36.pyc
Binary file not shown.
8 changes: 3 additions & 5 deletions pyDMPC/ControlFramework/test_main_Geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import configparser
import matplotlib.pyplot as plt
import shutil
import time

def main():
"""Create a system and multiple subsystems"""
Expand Down Expand Up @@ -146,11 +145,10 @@ def f(s):
time.sleep(max(Init.sync_rate-time.time()+start,0))
start = time.time()
else:
length = len(Init.name)
for l,val in enumerate(command_all):
if Init.algorithm == 'BExMoC':
model.set(Init.names_DVs[4-l], val)
elif Init.algorithm== 'NC_DMPC':
model.set(Init.names_DVs[4-l], val)
if Init.names_DVs[length-l-1] != None:
model.set(Init.names_DVs[length-l-1], val)

print(val)

Expand Down
Loading

0 comments on commit 27bb3dc

Please sign in to comment.