-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.m
110 lines (84 loc) · 3.77 KB
/
main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function [result, scenario] = main(varargin)
% MAIN main function for graph-based receeding horizon control
if verLessThan('matlab', '9.12')
warning("Code is developed in MATLAB 2022a, prepare for backward incompatibilities.")
end
% check if Config object is given as input
options = read_object_from_input(varargin, 'Config');
% check if Scenario object is given as input
scenario = read_object_from_input(varargin, 'Scenario');
% If scenario/options are not given, determine from UI
if isempty(scenario)
if isempty(options)
try
options = start_options();
catch ME
warning(ME.message);
return
end
end
plant = PlantFactory.get_experiment_interface(options.environment);
% create scenario
random_seed = RandStream('mt19937ar');
scenario = create_scenario(options, random_seed, plant);
else
plant = PlantFactory.get_experiment_interface(scenario.options.environment);
end
% write scenario to disk if distributed (for lab or local debugging with main_distributed())
if scenario.options.is_prioritized == true
save('scenario.mat', 'scenario');
end
is_prioritized_parallel_in_lab = (scenario.options.is_prioritized && scenario.options.environment == Environment.CpmLab && scenario.options.compute_in_parallel);
if is_prioritized_parallel_in_lab
disp('Scenario was written to disk. Select main_distributed(vehicle_id) in LCC next.')
if exist("commun/cust1/matlab_msg_gen", 'dir')
try
rmdir("commun/cust1/matlab_msg_gen", 's');
catch
warning("Unable to delete commun/cust1/matlab_msg_gen. Please delete manually");
end
end
if exist("commun/cust2/matlab_msg_gen", "dir")
try
rmdir("commun/cust2/matlab_msg_gen", 's');
catch
warning("Unable to delete commun/cust2/matlab_msg_gen. Please delete manually");
end
end
else
hlc_factory = HLCFactory();
hlc_factory.set_scenario(scenario);
dry_run = (scenario.options.environment == Environment.CpmLab); % TODO: dry run also for unified lab api?
if scenario.options.use_cpp
optimizer(Function.CheckMexFunction);
end
if scenario.options.is_prioritized == true && scenario.options.compute_in_parallel
%% simulate distribution locally using the Parallel Computing Toolbox
get_parallel_pool(scenario.options.amount);
do_plot = scenario.options.options_plot_online.is_active;
can_handle_parallel_plot = isa(plant, 'SimLab');
if do_plot
if can_handle_parallel_plot
visualization_data_queue = plant.set_visualization_data_queue;
% create central plotter - used by all workers via data queue
plotter = PlotterOnline(hlc_factory.scenario);
afterEach(visualization_data_queue, @plotter.data_queue_callback);
else
warning('The currently selected environment cannot handle plotting of a parallel execution!');
end
end
spmd (scenario.options.amount)
hlc = hlc_factory.get_hlc(scenario.options.veh_ids(labindex), dry_run, plant);
[result, scenario] = hlc.run();
end
if do_plot
plotter.close_figure();
end
result = {result{:}};
scenario = {scenario{:}};
else
hlc = hlc_factory.get_hlc(scenario.options.veh_ids, dry_run, plant);
[result, scenario] = hlc.run();
end
end
end