-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix optCtrl using tutorial in paper appendix
- Loading branch information
1 parent
af20dc6
commit e6b3e08
Showing
2 changed files
with
36 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,39 @@ | ||
function uOpt = optCtrl(obj, t, xs, deriv, uMode, ~) | ||
% uOpt = optCtrl(obj, t, deriv, uMode, dMode, MIEdims) | ||
|
||
position = xs{1}; | ||
velocity = xs{2}; | ||
|
||
%% Input processing | ||
if nargin < 5 | ||
uMode = 'min'; | ||
end | ||
|
||
if ~iscell(deriv) | ||
deriv = num2cell(deriv); | ||
end | ||
|
||
%% https://github.com/ZhiqingXiao/OpenAIGymSolution/blob/master/MountainCar-v0_close_form/mountaincar_v0_close_form.ipynb | ||
% position, velocity = observation | ||
% lb = min(-0.09 * (position + 0.25) ** 2 + 0.03, | ||
% 0.3 * (position + 0.9) ** 4 - 0.008) | ||
% ub = -0.07 * (position + 0.38) ** 2 + 0.07 | ||
% if lb < velocity < ub: | ||
% action = 2 # push right | ||
% else: | ||
% action = 0 # push left | ||
|
||
% TODO: Use deriv instead of hard-coded values above? | ||
% TODO: not sure this is working... | ||
|
||
%% Optimal control | ||
|
||
lb = min(-0.09 * (position + 0.25)^2 + 0.03, ... | ||
0.3 * (position + 0.9)^4 - 0.008); | ||
% https://arxiv.org/pdf/1709.07523.pdf | ||
% Appendix | ||
% need dot product of deriv and dx = f(x) | ||
% then argmin terms with action to find optimal control | ||
|
||
ub = -0.07 * (position + 0.38)^2 + 0.07; | ||
% we have | ||
% dx(1) = velocity; | ||
% u ~ { 0, 1, 2 } | ||
% dx(2) = (u - 1) * force + cos(3 * position) * (-gravity); | ||
|
||
% action = 0; % push left | ||
% if (lb < velocity) & (velocity < ub) | ||
% action = 2; % push right | ||
% end | ||
% TODO: handle uMode (always min for Backwards Reachability with goal) | ||
% u* = argmin_u (deriv{[0, 1]} * dx(2)) | ||
% = argmin_u (deriv{[0, 1]} (u - 1) * force) | ||
% = 0 when deriv{[0, 1]} >= 0, 2 when deriv{[0, 1]} < 0 | ||
|
||
%% Optimal control | ||
actions = zeros(size(xs{1})); | ||
actions((lb < velocity) & (velocity < ub)) = 2; | ||
uOpt = actions; | ||
if strcmp(uMode, 'max') | ||
uOpt = (deriv{1} >= 0) * 2 + (deriv{1} < 0) * 0; | ||
elseif strcmp(uMode, 'min') | ||
uOpt = (deriv{1} >= 0) * 0 + (deriv{1} < 0) * 2; | ||
else | ||
error('Unknown uMode!') | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters