Diagnose Infeasibility of Simple 3-DoF Trajectory Optimization
2 次查看(过去 30 天)
显示 更早的评论
I am trying to write a simple optimization program which finds the best initial launch angle and velocity to arrive at a target location. I have written - using this mathworks tutorial as a guide - a program which I believe ought to do this.
First, I define and simulate for a randomly chosen initial guess the trajectory of a 3-DoF aircraft model (and define some constants).
% Constants.
g = 9.81; % gravity
dt = 1e-1;
tspan = 0:dt:5;
e = [50,0]; % target loc.
degs = pi/180; % conversion.
a = 30*degs; % a is angle of launch in rads.
s = 50; % velocity of vehicle at launch.
x = 0;
h = 0;
y0 = [s,a,x,h]';
[~,Y] = ode45(@(t,y) dfu(t,y),tspan,y0);
figure
hold on
plot(Y(:,3),Y(:,4),'k')
plot(e(1),e(2),'bx')
Next, I set up the optimization program and determine the solution.
% Setup Optimization Problem
r = optimvar('r',1,'LowerBound',0,'UpperBound',90*degs);
v = optimvar('v',1,'LowerBound',0,'UpperBound',100);
f = fcn2optimexpr(@RtoODE,r,v,tspan,[v,r,x,h]);
obj = 0;
prob = optimproblem("Objective",obj);
prob.Constraints.terminalx = f(end,3) == e(1);
prob.Constraints.terminaly = f(end,4) == e(2);
r0.r = a;
r0.v = s;
options = optimoptions(@fmincon,'Display','iter');
[rsol,~] = solve(prob,r0,'Options',options);
y0 = [rsol.v,rsol.r,x,h]';
% Model Production.
[~,Y] = ode45(@(t,y) dfu(t,y),tspan,y0);
plot(Y(:,3),Y(:,4),'r')
% Output check.
angle = rsol.r/degs
velocity = rsol.v
downrange = Y(end,3)
altitude = Y(end,4)
targetx = e(1)
targety = e(2)
Obviously, this is determining the problem to be infeasible. But I do not suspect this is the case. Just manually tuning some values returns successful solutions to within the required parameters.
Here are the additional functions required to run this program:
function dydt = dfu(~,y)
g = 9.81;
dydt = zeros(4,1);
dydt(1) = -g*sin(y(2));
dydt(2) = -(g*cos(y(2)))/y(1);
dydt(3) = y(1)*cos(y(2));
dydt(4) = y(1)*sin(y(2));
end
function solpts = RtoODE(r,v,tspan,y0)
sol = ode45(@(t,y)dfu(t,y),tspan,y0);
solpts = deval(sol,tspan);
solpts = solpts(3:4,:);
end
I was wondering if perhaps anyone can find my mistake, or offer general advice on simulating and solving optimization problems for this purpose.
Thank you.
3 个评论
Benjamin Thompson
2022-6-13
Possibly due to the choice of states here. Try using x, y, x-dot, and y-dot as the state variables. Then calculate the initial launch angle and velocity from the results of the optimization.
回答(1 个)
Bjorn Gustavsson
2022-6-13
Why not write the equation of motion component-by-component instead:
function dydtd2ydt2 = dfu(~,y)
g = 9.81;
dydtd2ydt2 = zeros(4,1);
dydy = y(3:4);
d2ydt2 = [0;-g];
dydtd2ydt2 = [dydt(:);d2ydt2];
end
Then you look at the ballode-example for how to catch the ground-impact with event-detection. That will definitely give you the postition of the particle as y(2) (presumably vertical) is equal to zero. From that you can determine how far from your target you landed and that is your function to fit. From there fmincon should be able to do the job. Have look at the help, documentation and code for/of ballode.
HTH
7 个评论
Alan Weiss
2022-6-14
I corrected a few minor errors in your syntax. I also changed sin to sind and cos to cosd in the dfu function; was that the right thing to do? I still get no feasible solution, and I'm not sure why.
function [sol,fval,eflag,output] = DiscreteOptimization(T)
N = 50;
g = 9.81;
degs = pi/180;
x0 = [50 15*degs 0 0]';
xN = [0 0 50 0]';
% t = ceil(T/N); % This looks like a mistake to me
t = T/N;
x = optimvar('x',4,N); % v, g, x, h.
ulb = repmat([-2;-5*degs],1,N-1); % Bound for u
uub = -ulb;
u = optimvar('u',2,N-1,'LowerBound',ulb,'UpperBound',uub); % a and r.
% Define problem.
prob = optimproblem;
% Convert nonlinear functions to optimizable.
% integ = fcn2optimexpr(@RK,x,u,t,'OutputSize',[4,1]); % Ignore for now.
dydt = fcn2optimexpr(@dfu,x,u,'OutputSize',[4,1]);
% % Control constraints. % I simply put bounds, which is more efficient
% ucons = optimconstr(2,N-1);
% for i = 1:N-1
% ucons(1,i) = norm(u(1,i)) <= 2;
% ucons(2,i) = norm(u(2,i)) <= 5*degs;
% end
% State constraints.
xcons = optimconstr(4,N+1);
xcons(:,1) = x(:,1) == x0;
xcons(:,N+1) = x(:,N) == xN;
% Dynamic constraints.
for i = 2:N
xcons(:,i) = x(:,i) == x(:,i-1) + dydt*t;
end
% Optimization:
% prob.Constraints.ucons = ucons;
prob.Constraints.xcons = xcons;
starti = zeros(4,N); % Linear interpolation for initial point
for i = 1:4
starti(i,:) = linspace(x0(i),xN(i),N); % linear interpolation
end
start.x = starti;
start.u = zeros(size(u));
opts = optimoptions("fmincon","MaxFunctionEvaluations",1000*N);
[sol,fval,eflag,output] = solve(prob,start,"Options",opts);
end
function dydt = dfu(y,u)
g = 9.81;
dydt = zeros(4,1); % Needed because the output size is 4-by-1
dydt(1) = -g*sind(y(2)) + u(1);
dydt(2) = -(g*cosd(y(2)))/y(1) + u(2);
dydt(3) = y(1)*cosd(y(2));
dydt(4) = y(1)*sind(y(2));
end
Alan Weiss
MATLAB mathematical toolbox documentation
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!