Skip to content

Commit 759348e

Browse files
committed
* Adapt the interpolation for trajectory integration, #61
1 parent f26d5fc commit 759348e

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

pyDMPC/ControlFramework/Init.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
par_neigh.append(None)
7272
input_names.append(["coolerTemperature.T"])
7373
input_variables.append([r"variation.table[1,2]"])
74-
inputs.append([i for i in range(280,325,5)])
74+
inputs.append([i for i in range(280,325,1)])
7575
output_names.append(["supplyAirTemperature.T"])
7676
set_points.append([303])
7777
state_var_names.append(["heaterInitials[1].y"])
@@ -90,7 +90,7 @@
9090
commands.append([[0,0], [10,10], [30,30], [60,60], [80,80], [100,100]])
9191
traj_points.append([])
9292
traj_var.append([])
93-
cost_fac.append([0.5, 0.0, 1.0])
93+
cost_fac.append([0.1, 0.0, 1.0])
9494

9595
sys_id.append(1)
9696
name.append("Cooler")
@@ -100,7 +100,7 @@
100100
par_neigh.append(None)
101101
input_names.append(["preHeaterTemperature.T"])
102102
input_variables.append([r"variation.table[1,2]"])
103-
inputs.append([i for i in range(280,325,5)])
103+
inputs.append([i for i in range(280,325,1)])
104104
output_names.append(["supplyAirTemperature.T"])
105105
set_points.append([303])
106106
state_var_names.append(["coolerInitials[1].y"])
@@ -116,7 +116,7 @@
116116
mod_path.append(f'{glob_res_path}\\cooler')
117117
command_names.append(["valveCooler"])
118118
command_variables.append(["decisionVariables.table[1,2]"])
119-
commands.append([[0,0], [10,10], [30,30], [60,60], [80,80], [100,100]])
119+
commands.append([[0,0], [2,2], [4,4], [6,6], [8,8], [10,10], [12,12], [14,14], [16,16], [18,18]])
120120
traj_points.append([])
121121
traj_var.append([])
122-
cost_fac.append([0.5, 1.0, 0])
122+
cost_fac.append([0.0, 1.0, 0])

pyDMPC/ControlFramework/Subsystem.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def optimize(self, interp):
148148
def interp_minimize(self, interp):
149149

150150
from scipy import interpolate as it
151+
import time
151152

152153
opt_costs = []
153154
opt_outputs = []
@@ -239,12 +240,13 @@ def interp_minimize(self, interp):
239240
self.coup_vars_send = opt_outputs[0]
240241
self.command_send = opt_command[0]
241242

242-
#print(f"{self.name}: {self.command_send}")
243-
#time.sleep(2)
244-
#print(f"Cost: {self.cost_send}")
243+
print(f"self.cost_send: {self.cost_send}")
244+
time.sleep(2)
245+
print(f"Command: {self.command_send}")
245246

246247
def calc_cost(self, command, outputs):
247248
import scipy.interpolate
249+
import numpy as np
248250

249251
cost = self.cost_fac[0] * sum(command)
250252
#print(f"Cost after the 1st step: {cost}")
@@ -257,7 +259,8 @@ def calc_cost(self, command, outputs):
257259
cost += self.cost_fac[1] * c(outputs)
258260
#print(f"Interp. cost: {c(outputs)}")
259261
elif type(c) is list:
260-
cost += self.cost_fac[1] * c[0]
262+
idx = self.find_nearest(np.asarray(self.inputs), outputs)
263+
cost += self.cost_fac[1] * c[idx]
261264
else:
262265
cost += self.cost_fac[1] * c
263266
#print(f"Cost after the 2nd step: {cost}")
@@ -286,6 +289,8 @@ def interp(self, iter_real):
286289
inp = self.model.states.inputs
287290

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

290295
if self.command_send != []:
291296
if (type(self.command_send) is scipy.interpolate.interpolate.interp1d):
@@ -298,7 +303,8 @@ def interp(self, iter_real):
298303
self.fin_coup_vars = self.coup_vars_send(inp[0])
299304
else:
300305
self.fin_coup_vars = self.coup_vars_send[idx]
301-
306+
print(f"self.fin_command: {self.fin_command}")
307+
print(f"idx: {idx}")
302308

303309
def get_inputs(self):
304310

pyDMPC/Utilities/mlp_train_modelica.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def main():
1010

11-
module = "heater"
11+
module = "cooler"
1212
command = [] # The manipulated variable in the model
1313
T_cur = [] # The current inflow temperature
1414

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

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

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

4040
""" Actual training sequence """
4141
for k in range(40):
42-
for t in range(12000):
42+
for t in range(1200):
4343
"""Write random values to the controlled variables"""
4444
if t < 100:
4545
command.append(0)
4646
elif t%120 == 0:
47-
command.append(random.uniform(0.0,100.0))
47+
command.append(random.uniform(0.0,10.0))
4848
else:
4949
command.append(command[-1])
5050

0 commit comments

Comments
 (0)