Skip to content

Commit

Permalink
* Further housekeeping, #61
Browse files Browse the repository at this point in the history
  • Loading branch information
MBaranskiEBC committed Jul 31, 2019
1 parent 708fee7 commit c624e27
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 41 deletions.
9 changes: 4 additions & 5 deletions pyDMPC/ControlFramework/Experiment.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import System
import time
import Time
import Init

def main():

# Generate a system for the experiment
Time.Time.contr_sys_type = "Modelica"
sys = System.Bexmoc()
sys = System.Bexmoc(interp = False)

System.Bexmoc.prep_cont_sys()

# Minimum sample interval in seconds
min_samp_inter = 10
min_samp_inter = Init.time_incr

sys.initialize()

try:
try:
for cur_time in range(0, 86400 , min_samp_inter):
start_time = time.time()
sys.execute()
#time.sleep(max(0, min_samp_inter - time.time() + start_time))
print(f"Time: {cur_time}")

System.Bexmoc.close_mod()
System.Bexmoc.close_cont_sys()
Expand Down
8 changes: 4 additions & 4 deletions pyDMPC/ControlFramework/Init.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@
stop.append(3600)
incr.append(10.)
opt_time.append(600)
samp_time.append(60)
samp_time.append(time_incr)
lib_paths.append(glob_lib_paths)
res_path.append(glob_res_path + "\\" + name_wkdir)
dym_path.append(glob_dym_path)
mod_path.append(f'{glob_res_path}\\heater')
command_names.append(["valveHeater"])
command_variables.append(["decisionVariables.table[1,2]"])
commands.append([[0,0], [10,10], [30,30], [60,60], [80,80], [100,100]])
commands.append([[i,i] for i in range(0,100,5)])
traj_points.append([])
traj_var.append([])
cost_fac.append([0.1, 0.0, 1.0])
Expand All @@ -109,14 +109,14 @@
stop.append(3600.)
incr.append(10.)
opt_time.append(600)
samp_time.append(60)
samp_time.append(time_incr)
lib_paths.append(glob_lib_paths)
res_path.append(glob_res_path + "\\" + name_wkdir)
dym_path.append(glob_dym_path)
mod_path.append(f'{glob_res_path}\\cooler')
command_names.append(["valveCooler"])
command_variables.append(["decisionVariables.table[1,2]"])
commands.append([[0,0], [2,2], [4,4], [6,6], [8,8], [10,10], [12,12], [14,14], [16,16], [18,18], [20,20], [22,22]])
commands.append([[i,i] for i in range(0,100,5)])
traj_points.append([])
traj_var.append([])
cost_fac.append([0.0, 1.0, 0])
1 change: 0 additions & 1 deletion pyDMPC/ControlFramework/Modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ def load_mod(self):

def write_inputs(self):
import numpy as np

commands_1 = self.states.commands[0]*np.ones(10)
commands_2 = self.states.commands[1]*np.ones(50)
commands = commands_1.tolist() + commands_2.tolist()
Expand Down
15 changes: 9 additions & 6 deletions pyDMPC/ControlFramework/Subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,11 @@ def interp_minimize(self, interp):

if len(inputs) >= 2:
if interp:
interp_com = []
self.coup_vars_send = opt_outputs
self.command_send = it.interp1d(inputs, opt_command,
for com in opt_command:
interp_com.append(com[0])
self.command_send = it.interp1d(inputs, interp_com,
fill_value = (100,100), bounds_error = False)
else:
self.coup_vars_send = opt_outputs
Expand All @@ -227,9 +230,6 @@ def interp_minimize(self, interp):
self.coup_vars_send = opt_outputs[0]
self.command_send = opt_command[0]

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
Expand Down Expand Up @@ -310,8 +310,11 @@ def send_commands(self):
if (cur_time - self.last_write) > self.model.times.samp_time:
self.last_write = cur_time

print(self.fin_command[0])
if type(self.fin_command) is list:
com = self.fin_command[0]
else:
com = self.fin_command

if self.model.states.command_names is not None:
for nam in self.model.states.command_names:
System.Bexmoc.write_cont_sys(nam, self.fin_command[0])
System.Bexmoc.write_cont_sys(nam, com)
33 changes: 8 additions & 25 deletions pyDMPC/ControlFramework/System.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,39 +211,35 @@ def broadcast(self, sys_list = None):

if self.subsystems[i].downs_neigh is not None:
for k in self.subsystems[i].downs_neigh:
#print(k)
#print(self.subsystems[k])
self.subsystems[k].coup_vars_rec = self.subsystems[i].coup_vars_send
#print(f"{self.subsystems[k].name}: #{self.subsystems[k].coup_vars_rec}")

self.subsystems[k].coup_vars_rec = (
self.subsystems[i].coup_vars_send)

i += j



class Bexmoc(System):

def __init__(self):
def __init__(self, interp):
super().__init__()
self.interp = interp

def initialize(self):
for i,sub in enumerate(self.subsystems):
if Bexmoc.contr_sys_typ == "Modelica":
sub.get_inputs()


def execute(self):

for i in range(len(self.subsystems)):
self.subsystems[i].optimize(interp=False)
self.subsystems[i].optimize(interp=self.interp)
if self.subsystems[i].par_neigh is not None:
for j in self.subsystems[i].par_neigh:
self.subsystems[j].optimize(interp=False)
self.subsystems[j].optimize(interp=self.interp)
self.broadcast([i] + self.subsystems[i].par_neigh)
else:
#print(f"Broadcasting: {i}")
self.broadcast([i])


for i,sub in enumerate(self.subsystems):
sub.get_inputs()
sub.interp(iter_real = "real")
Expand All @@ -256,7 +252,6 @@ def execute(self):
Time.Time.set_time()

def iterate(self):
import time
for i,sub in enumerate(self.subsystems):
print(sub.name)

Expand All @@ -269,26 +264,19 @@ def iterate(self):
sub.inputs = [inputs[0], sub.model.states.set_points[0]]

sub.inputs.sort()
#print(sub.inputs)
#time.sleep(2)
sub.optimize(interp = True)
#sub.interp(iter_real = "iter")
self.broadcast([i])


for ino in range(0,8,1):
for i,sub in enumerate(self.subsystems):
if i == 1:
sub.get_inputs()
#print(f"Measurement: {sub.model.states.inputs[0]}")
#time.sleep(2)
inputs = sub.model.states.inputs[0]
sub.inputs = [inputs[0] - 0.1, inputs[0] + 0.1]

else:
#print(sub.coup_vars_rec)
if sub.coup_vars_rec != []:
#print(f"Coupling: {sub.coup_vars_rec[0]}")
if (self.subsystems[1].inputs[0] <
self.subsystems[1].model.states.set_points[0]):
sub.inputs = [min(sub.coup_vars_rec[0],
Expand All @@ -303,24 +291,19 @@ def iterate(self):
print(f"{sub.name}: {sub.inputs}")

sub.optimize(interp = True)
#sub.interp(iter_real = "iter")
#print(f"{sub.name}: {sub.command_send}")

self.broadcast([i])


for i,sub in enumerate(self.subsystems):
sub.interp(iter_real = "iter")
sub.send_commands()
#print(f"{sub.name}: {sub.fin_command}")
#time.sleep(2)

if Bexmoc.contr_sys_typ == "Modelica":
cur_time = Time.Time.get_time()

Bexmoc.proceed(cur_time, Time.Time.time_incr)
tim = Time.Time.set_time()
#print("Time: " + str(tim))
Time.Time.set_time()

def terminate(self):
Bexmoc.close_cont_sys()

0 comments on commit c624e27

Please sign in to comment.