Skip to content

Commit

Permalink
* Adapt the interpolation for trajectory integration, #61
Browse files Browse the repository at this point in the history
  • Loading branch information
MBaranskiEBC committed Jul 27, 2019
1 parent f26d5fc commit 759348e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
10 changes: 5 additions & 5 deletions pyDMPC/ControlFramework/Init.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
par_neigh.append(None)
input_names.append(["coolerTemperature.T"])
input_variables.append([r"variation.table[1,2]"])
inputs.append([i for i in range(280,325,5)])
inputs.append([i for i in range(280,325,1)])
output_names.append(["supplyAirTemperature.T"])
set_points.append([303])
state_var_names.append(["heaterInitials[1].y"])
Expand All @@ -90,7 +90,7 @@
commands.append([[0,0], [10,10], [30,30], [60,60], [80,80], [100,100]])
traj_points.append([])
traj_var.append([])
cost_fac.append([0.5, 0.0, 1.0])
cost_fac.append([0.1, 0.0, 1.0])

sys_id.append(1)
name.append("Cooler")
Expand All @@ -100,7 +100,7 @@
par_neigh.append(None)
input_names.append(["preHeaterTemperature.T"])
input_variables.append([r"variation.table[1,2]"])
inputs.append([i for i in range(280,325,5)])
inputs.append([i for i in range(280,325,1)])
output_names.append(["supplyAirTemperature.T"])
set_points.append([303])
state_var_names.append(["coolerInitials[1].y"])
Expand All @@ -116,7 +116,7 @@
mod_path.append(f'{glob_res_path}\\cooler')
command_names.append(["valveCooler"])
command_variables.append(["decisionVariables.table[1,2]"])
commands.append([[0,0], [10,10], [30,30], [60,60], [80,80], [100,100]])
commands.append([[0,0], [2,2], [4,4], [6,6], [8,8], [10,10], [12,12], [14,14], [16,16], [18,18]])
traj_points.append([])
traj_var.append([])
cost_fac.append([0.5, 1.0, 0])
cost_fac.append([0.0, 1.0, 0])
16 changes: 11 additions & 5 deletions pyDMPC/ControlFramework/Subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def optimize(self, interp):
def interp_minimize(self, interp):

from scipy import interpolate as it
import time

opt_costs = []
opt_outputs = []
Expand Down Expand Up @@ -239,12 +240,13 @@ def interp_minimize(self, interp):
self.coup_vars_send = opt_outputs[0]
self.command_send = opt_command[0]

#print(f"{self.name}: {self.command_send}")
#time.sleep(2)
#print(f"Cost: {self.cost_send}")
print(f"self.cost_send: {self.cost_send}")
time.sleep(2)
print(f"Command: {self.command_send}")

def calc_cost(self, command, outputs):
import scipy.interpolate
import numpy as np

cost = self.cost_fac[0] * sum(command)
#print(f"Cost after the 1st step: {cost}")
Expand All @@ -257,7 +259,8 @@ def calc_cost(self, command, outputs):
cost += self.cost_fac[1] * c(outputs)
#print(f"Interp. cost: {c(outputs)}")
elif type(c) is list:
cost += self.cost_fac[1] * c[0]
idx = self.find_nearest(np.asarray(self.inputs), outputs)
cost += self.cost_fac[1] * c[idx]
else:
cost += self.cost_fac[1] * c
#print(f"Cost after the 2nd step: {cost}")
Expand Down Expand Up @@ -286,6 +289,8 @@ def interp(self, iter_real):
inp = self.model.states.inputs

idx = self.find_nearest(np.asarray(self.inputs), inp[0])
#print(f"self.inputs): {self.inputs}")
#print(f"inp[0]: {inp[0]}")

if self.command_send != []:
if (type(self.command_send) is scipy.interpolate.interpolate.interp1d):
Expand All @@ -298,7 +303,8 @@ def interp(self, iter_real):
self.fin_coup_vars = self.coup_vars_send(inp[0])
else:
self.fin_coup_vars = self.coup_vars_send[idx]

print(f"self.fin_command: {self.fin_command}")
print(f"idx: {idx}")

def get_inputs(self):

Expand Down
8 changes: 4 additions & 4 deletions pyDMPC/Utilities/mlp_train_modelica.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def main():

module = "heater"
module = "cooler"
command = [] # The manipulated variable in the model
T_cur = [] # The current inflow temperature

Expand All @@ -28,7 +28,7 @@ def main():
sync_rate = 60 # Synchronisation rate of the FMU

# Load exisiting FMU
model = load_fmu(f"C:\TEMP\Dymola\{module}.fmu")
model = load_fmu(f"D:\dymola\{module}.fmu")

""" Initialize the FMU """
model.set('valveOpening',0)
Expand All @@ -39,12 +39,12 @@ def main():

""" Actual training sequence """
for k in range(40):
for t in range(12000):
for t in range(1200):
"""Write random values to the controlled variables"""
if t < 100:
command.append(0)
elif t%120 == 0:
command.append(random.uniform(0.0,100.0))
command.append(random.uniform(0.0,10.0))
else:
command.append(command[-1])

Expand Down

0 comments on commit 759348e

Please sign in to comment.