Skip to content

Commit f26d5fc

Browse files
committed
* Small corrections for command trajectories, #61
1 parent b6c35bf commit f26d5fc

File tree

4 files changed

+28
-263
lines changed

4 files changed

+28
-263
lines changed

pyDMPC/ControlFramework/Init.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,58 +65,58 @@
6565
# Subsystems
6666
sys_id.append(0)
6767
name.append("Heater")
68-
model_type.append("Modelica")
68+
model_type.append("Scikit")
6969
ups_neigh.append(1)
7070
downs_neigh.append(None)
7171
par_neigh.append(None)
7272
input_names.append(["coolerTemperature.T"])
7373
input_variables.append([r"variation.table[1,2]"])
74-
inputs.append(range(280,325,5))
74+
inputs.append([i for i in range(280,325,5)])
7575
output_names.append(["supplyAirTemperature.T"])
7676
set_points.append([303])
7777
state_var_names.append(["heaterInitials[1].y"])
7878
model_state_var_names.append(["mas1.k"])
7979
start.append(0.)
8080
stop.append(3600)
8181
incr.append(10.)
82-
opt_time.append(0)
83-
samp_time.append(10)
82+
opt_time.append(600)
83+
samp_time.append(60)
8484
lib_paths.append(glob_lib_paths)
8585
res_path.append(glob_res_path + "\\" + name_wkdir)
8686
dym_path.append(glob_dym_path)
87-
mod_path.append('ModelicaModels.SubsystemModels.DetailedModels.Heater')
87+
mod_path.append(f'{glob_res_path}\\heater')
8888
command_names.append(["valveHeater"])
8989
command_variables.append(["decisionVariables.table[1,2]"])
90-
commands.append([[i] for i in range(0,105,5)])
90+
commands.append([[0,0], [10,10], [30,30], [60,60], [80,80], [100,100]])
9191
traj_points.append([])
9292
traj_var.append([])
9393
cost_fac.append([0.5, 0.0, 1.0])
9494

9595
sys_id.append(1)
9696
name.append("Cooler")
97-
model_type.append("Modelica")
97+
model_type.append("Scikit")
9898
ups_neigh.append(None)
9999
downs_neigh.append([0])
100100
par_neigh.append(None)
101101
input_names.append(["preHeaterTemperature.T"])
102102
input_variables.append([r"variation.table[1,2]"])
103-
inputs.append(range(280,325,5))
103+
inputs.append([i for i in range(280,325,5)])
104104
output_names.append(["supplyAirTemperature.T"])
105105
set_points.append([303])
106106
state_var_names.append(["coolerInitials[1].y"])
107107
model_state_var_names.append(["hex.ele[1].mas.T"])
108108
start.append(0.)
109109
stop.append(3600.)
110110
incr.append(10.)
111-
opt_time.append(0)
112-
samp_time.append(10)
111+
opt_time.append(600)
112+
samp_time.append(60)
113113
lib_paths.append(glob_lib_paths)
114114
res_path.append(glob_res_path + "\\" + name_wkdir)
115115
dym_path.append(glob_dym_path)
116-
mod_path.append(r'ModelicaModels.SubsystemModels.DetailedModels.Cooler')
116+
mod_path.append(f'{glob_res_path}\\cooler')
117117
command_names.append(["valveCooler"])
118118
command_variables.append(["decisionVariables.table[1,2]"])
119-
commands.append([[i] for i in range(0,105,5)])
119+
commands.append([[0,0], [10,10], [30,30], [60,60], [80,80], [100,100]])
120120
traj_points.append([])
121121
traj_var.append([])
122122
cost_fac.append([0.5, 1.0, 0])

pyDMPC/ControlFramework/Modeling.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,13 @@ def load_mod(self):
276276
def write_inputs(self):
277277
import numpy as np
278278

279-
commands = [com*np.ones(60) for com in self.states.commands]
280-
inputs = [inp*np.ones(60) for inp in self.states.inputs]
279+
commands_1 = self.states.commands[0]*np.ones(10)
280+
commands_2 = self.states.commands[1]*np.ones(50)
281+
commands = commands_1.tolist() + commands_2.tolist()
282+
inputs = self.states.inputs[0]*np.ones(60)
283+
inputs = inputs.tolist()
281284

282-
inputs = np.stack((commands + inputs), axis=1)
285+
inputs = np.stack(([commands] + [inputs]), axis=1)
283286

284287
self.scal_inputs = self.scaler.transform(inputs)
285288

pyDMPC/ControlFramework/Subsystem.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,32 +273,32 @@ def find_nearest(self, a, a0):
273273
import numpy as np
274274
"Element in nd array `a` closest to the scalar value `a0`"
275275
idx = np.abs(a - a0).argmin()
276-
return a.flat[idx]
276+
277+
return idx
277278

278279
def interp(self, iter_real):
279280
import scipy.interpolate
281+
import numpy as np
280282

281283
if iter_real == "iter" and self.coup_vars_rec != []:
282284
inp = self.coup_vars_rec
283285
else:
284286
inp = self.model.states.inputs
285287

286-
#print(f"{self.name}: {inp}")
288+
idx = self.find_nearest(np.asarray(self.inputs), inp[0])
287289

288290
if self.command_send != []:
289291
if (type(self.command_send) is scipy.interpolate.interpolate.interp1d):
290292
self.fin_command = self.command_send(inp[0])
291293
else:
292-
self.fin_command = self.command_send[self.find_nearest(self.command_send, inp[0])]
294+
self.fin_command = self.command_send[idx]
293295

294296
if self.coup_vars_send != []:
295297
if type(self.coup_vars_send) is scipy.interpolate.interpolate.interp1d:
296298
self.fin_coup_vars = self.coup_vars_send(inp[0])
297299
else:
298-
self.fin_coup_vars = self.coup_vars_send[self.find_nearest(self.command_send, inp[0])]
300+
self.fin_coup_vars = self.coup_vars_send[idx]
299301

300-
#print(f"{self.name}: {self.fin_command}")
301-
#time.sleep(2)
302302

303303
def get_inputs(self):
304304

@@ -329,6 +329,8 @@ def send_commands(self):
329329

330330
if (cur_time - self.last_write) > self.model.times.samp_time:
331331
self.last_write = cur_time
332+
333+
print(self.fin_command[0])
332334

333335
if self.model.states.command_names is not None:
334336
for nam in self.model.states.command_names:

0 commit comments

Comments
 (0)