Optimal control of a system in a changing environment
- Driver:
Download script: changing_environment.m
The target of the study is show how to control a system that depends on environment parameters such as temperature or humidity. While the environment parameters can be measured, their influence on the system’s performance is often unknown.
As an example objective the 2d Rastrigin function
is considered. The environment parameter \(\phi\) acts as an additional phase offset to the first cosine function in the objective function
The phase shall slowly vary over time as
Please note, that this specific time dependent behaviour is not exploited and is assumed to be unknown.
Before being able to control the system in an optimal way depending on the environment, one has to learn for many environment values, where the global minimum is located. To this end, a standard Bayesian optimization is performed for 500 iterations that explores the parameter space. In a second phase, the target is to evaluate the system in an optimal way, i.e. an exploration of the parameter space is not desired. This behaviour is mainly enforced by choosing a small scaling value.
The control phase could have an arbitrary number of iterations and it would be problematic to add all new observations to the study. On the one hand, this slows down the computation time of a suggestion. Since the environment value changes during the computation, this can lead to less optimal evaluation points. On the other had, adding more and more data points close to each other leads to an ill conditioned Gaussian process surrogate. To avoid these drawbacks, data points are not added in the control phase if the study predicts a value with very small uncertainty, which means that the observation would not add significant information.
1client = jcmoptimizer.Client();
2
3%Rastrigin-like function depending on additional phase offset phi
4function val=rast(x1, x2, phi)
5 val = 10*2 + x1^2 + x2^2 - 10*cos(2*pi*x1 + phi) - 10*cos(2*pi*x2);
6end
7
8%time-dependent slowly varying phi
9function phi=current_phi()
10 t = now()*1e5;
11 phi = 2*pi*sin(t/180);
12end
13
14% Definition of the search domain
15design_space = { ...
16 struct('name', 'x1', 'type', 'continuous', 'domain', [-1.5,1.5]), ...
17 struct('name', 'x2', 'type', 'continuous', 'domain', [-1.5,1.5]) ...
18};
19
20% Definition of the environment variable "phi"
21environment = { ...
22 struct('name', 'phi', 'type', 'variable', 'domain', [-2*pi, 2*pi]) ...
23};
24
25 % Creation of the study object with study_id 'changing_environment'
26study = client.create_study( ...
27 'design_space', design_space, ...
28 'environment', environment, ...
29 'driver','ActiveLearning',...
30 'study_name','Optimal control of a system in a changing environment',...
31 'study_id', 'changing_environment');
32
33% In the initial training phase, the target is to explore the
34% parameter space to find the global minimim.
35study.configure( ...
36 ... train with 500 data points
37 'max_iter', 500, ...
38 ... Advanced sample computation is switched off since the environment
39 ... parameter phi can change significantly between computation
40 ... of the suggestion and evaluation of the objective function
41 'acquisition_optimizer', struct('compute_suggestion_in_advance', false) ...
42);
43
44% Evaluation of the black-box function for specified design parameters
45function observation = evaluate(study, sample)
46
47 pause(2); % make objective expensive
48 observation = study.new_observation();
49 % get current phi
50 phi = current_phi();
51 observation.add(rast(sample.x1, sample.x2, phi), 'environment_value', {phi});
52
53end
54
55% Run the minimization
56study.set_evaluator(@evaluate);
57study.run();
58
59
60% The target in the control phase is to evaluate the offet Rastrigin function only
61% at well performing (x1,x2)-point depending on the current value of the environment.
62MAX_ITER = 500; %evaluate for 500 additional iterations
63study.configure( ...
64 'max_iter', 500 + MAX_ITER, ...
65 ... The scaling is reduced to penalize parameters with large uncertainty
66 'scaling', 0.01, ...
67 ... The lower-confidence bound (LCB) strategy is chosen instead of the
68 ... default expected improvement (EI). LCB is easier to maximize at the
69 ... risk of less exploration of the parameter space, which is anyhow not
70 ... desired in the control phase.
71 'objectives', {...
72 struct('type', 'Minimizer', 'name', 'objective', 'strategy', 'LCB') ...
73 }, ...
74 'acquisition_optimizer', struct('compute_suggestion_in_advance', false) ...
75);
76
77% keep track of suggested design points and phis at request time and evaluation time
78design_points = {};
79phis_at_request = {};
80phis_at_eval = {};
81
82iter = 0;
83while not(study.is_done())
84 iter = iter + 1;
85 if iter > MAX_ITER
86 break
87 end
88
89 phi = current_phi();
90 suggestion = study.get_suggestion({phi});
91 phis_at_request{end + 1} = phi;
92 sample = suggestion.sample;
93 design_points{end + 1} = [sample.x1, sample.x2];
94 try
95 obs = evaluate(study, sample);
96 %update phi from observation
97 phi = obs.data.None{1}.env{1};
98 phis_at_eval{end + 1} = phi;
99
100 predictions = study.driver.predict({[sample.x1, sample.x2, phi]});
101 std = sqrt(predictions.variance(1, 1));
102
103 fprintf("Uncertainty of prediction %f\n", std);
104 % add data only if prediction has significant uncertainty
105 if std > 0.01
106 study.add_observation(obs, suggestion.id);
107 else
108 study.clear_suggestion( ...
109 suggestion.id, sprintf("Ignoring observation with uncertainty %f", std));
110 end
111
112 catch err
113 study.clear_suggestion( ...
114 suggestion.id, ...
115 sprintf("Evaluator function failed with error: %s", err.message) ...
116 )
117 rethrow(err);
118 end
119
120end
121
122
123fig = figure('Position', [0, 0, 1000, 500]);
124
125% all observed training samples
126observed = study.driver.get_observed_values();
127subplot(1, 2, 1)
128plot(observed.means,".")
129hold on;
130xline(500, '--');
131xlabel("training+control iteration")
132ylabel("observed value of Rastrigin function")
133
134%observed values during control phase
135observed_vals = [];
136
137%values that would have been observed at request time,
138%i.e. if there would be no time delay between request and
139%evaluation of suggestion
140observed_vals_at_request = [];
141
142for i = 1:length(design_points)
143 observed_vals(i) = rast( ...
144 design_points{i}(1), design_points{i}(2), phis_at_eval{i});
145 observed_vals_at_request(i) = rast( ...
146 design_points{i}(1), design_points{i}(2), phis_at_request{i});
147end
148
149%best value of x1-parameter depending on environment
150function val = best_x1(phi)
151 val = -phi/(2*pi);
152 if abs(phi) > pi
153 val = val + sign(phi);
154 end
155end
156
157% best possible values
158best_vals = [];
159for i = 1:length(phis_at_eval)
160 phi = phis_at_eval{i};
161 best_vals(i) = rast(best_x1(phi), 0, phi);
162end
163
164subplot(1, 2, 2)
165semilogy(observed_vals,".", 'DisplayName', 'observed values')
166hold on;
167semilogy(observed_vals_at_request,".", 'DisplayName', 'observed values if no time delay')
168semilogy(best_vals, 'DisplayName', 'best possible values')
169ylim([1e-4, 1e1])
170xlabel("control iteration")
171legend()
172saveas(fig, "training_and_control.png")
173
174client.shutdown_server();
Left: During the initial training phase in the first 500 iterations, the parameter space is explored leading to small and large objective values. In the control phase, only small objective values are observed. Right: The observed values (blue dots) agree well with the lowest achievable values (green line). Most of the deviations are due to the time offset between the request of a new suggestion for a given environment value \(\phi\) and the actual evaluation of the Rastrigin function about a second later. To see this, the values that would have been observed at the time of request are shown as orange dots.