Possible bug with coneprog?
3 次查看(过去 30 天)
显示 更早的评论
I am wondering if this is a bug in coneprog or expected behavior. The example used to reproduce this is from https://www.mathworks.com/help/optim/ug/discretized-optimal-trajectory-cone-programming.html . When the code below is run as is then the expected result is outputted. However, when a "dummy" constraint is added (by uncommenting the two lines in the setupproblem function) that has nothing to do with the original it changes the answer signicantly.
Thanks in advance for any responses.
clc,clear,close all
p0 = [0 0 0];
pF = [5 10 3];
trajprob = setupproblem(20);
opts = optimoptions('coneprog','Display','iter');
[sol,fval,eflag,output] = solve(trajprob,options=opts)
plottrajandaccel(sol,p0,pF)
function trajectoryprob = setupproblem(T,air)
if nargin == 1
air = false;
end
N = 50;
g = [0 0 -9.81];
p0 = [0 0 0];
pF = [5 10 3];
Amax = 25;
t = T/N;
p = optimvar("p",N,3);
v = optimvar("v",N,3);
a = optimvar("a",N-1,3,"LowerBound",-Amax,"UpperBound",Amax);
trajectoryprob = optimproblem;
s = optimvar("s",N-1,"LowerBound",0,"UpperBound",3*Amax);
trajectoryprob.Objective = sum(s)*t;
scons = optimconstr(N-1);
for i = 1:(N-1)
scons(i) = norm(a(i,:)) <= s(i);
end
acons = optimconstr(N-1);
for i = 1:(N-1)
acons(i) = norm(a(i,:)) <= Amax;
end
vcons = optimconstr(N+1,3);
vcons(1,:) = v(1,:) == [0 0 0];
if air
vcons(2:N,:) = v(2:N,:) == v(1:(N-1),:)*exp(-t) + t*(a(1:(N-1),:) + repmat(g,N-1,1));
else
vcons(2:N,:) = v(2:N,:) == v(1:(N-1),:) + t*(a(1:(N-1),:) + repmat(g,N-1,1));
end
vcons(N+1,:) = v(N,:) == [0 0 0];
pcons = optimconstr(N+1,3);
pcons(1,:) = p(1,:) == p0;
if air
pcons(2:N,:) = p(2:N,:) == p(1:(N-1),:) + t*(1+exp(-t))/2*v(1:(N-1),:) + t^2/2*(a(1:(N-1),:) + repmat(g,N-1,1));
else
pcons(2:N,:) = p(2:N,:) == p(1:(N-1),:) + t*v(1:(N-1),:) + t^2/2*(a(1:(N-1),:) + repmat(g,N-1,1));
end
pcons((N+1),:) = p(N,:) == pF;
trajectoryprob.Constraints.acons = acons;
trajectoryprob.Constraints.scons = scons;
trajectoryprob.Constraints.vcons = vcons;
trajectoryprob.Constraints.pcons = pcons;
%%%%%%%%% NEW DUMMY VARIABLE AND CONSTRAINT %%%%%%%%%
% test_var = optimvar("test_var",N,1);
% trajectoryprob.Constraints.test_cons = test_var >= 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
function plottrajandaccel(sol,p0,pF)
figure
psol = sol.p;
plot3(psol(:,1),psol(:,2),psol(:,3),'rx')
hold on
plot3(p0(1),p0(2),p0(3),'ks')
plot3(pF(1),pF(2),pF(3),'bo')
hold off
view([18 -10])
xlabel("x")
ylabel("y")
zlabel("z")
legend("Steps","Initial Point","Final Point")
figure
asolm = sol.a;
nasolm = sqrt(sum(asolm.^2,2));
plot(nasolm,"rx")
xlabel("Time step")
ylabel("Norm(acceleration)")
end
0 个评论
采纳的回答
Alan Weiss
2022-8-5
I think that you have identified a problem with the default options for trajectory problems: the optimality tolerance should be chosen smaller than default. If you run the problem with a tighter optimality tolerance, ther results look better, and look pretty much the same with or without your dummy variables.
p0 = [0 0 0];
pF = [5 10 3];
trajprob = setupproblem(20);
opts = optimoptions('coneprog','Display','iter','OptimalityTolerance',1e-10);
[sol,fval,eflag,output] = solve(trajprob,options=opts)
plottrajandaccel(sol,p0,pF)
function trajectoryprob = setupproblem(T,air)
if nargin == 1
air = false;
end
N = 50;
g = [0 0 -9.81];
p0 = [0 0 0];
pF = [5 10 3];
Amax = 25;
t = T/N;
p = optimvar("p",N,3);
v = optimvar("v",N,3);
a = optimvar("a",N-1,3,"LowerBound",-Amax,"UpperBound",Amax);
trajectoryprob = optimproblem;
s = optimvar("s",N-1,"LowerBound",0,"UpperBound",3*Amax);
trajectoryprob.Objective = sum(s)*t;
scons = optimconstr(N-1);
for i = 1:(N-1)
scons(i) = norm(a(i,:)) <= s(i);
end
acons = optimconstr(N-1);
for i = 1:(N-1)
acons(i) = norm(a(i,:)) <= Amax;
end
vcons = optimconstr(N+1,3);
vcons(1,:) = v(1,:) == [0 0 0];
if air
vcons(2:N,:) = v(2:N,:) == v(1:(N-1),:)*exp(-t) + t*(a(1:(N-1),:) + repmat(g,N-1,1));
else
vcons(2:N,:) = v(2:N,:) == v(1:(N-1),:) + t*(a(1:(N-1),:) + repmat(g,N-1,1));
end
vcons(N+1,:) = v(N,:) == [0 0 0];
pcons = optimconstr(N+1,3);
pcons(1,:) = p(1,:) == p0;
if air
pcons(2:N,:) = p(2:N,:) == p(1:(N-1),:) + t*(1+exp(-t))/2*v(1:(N-1),:) + t^2/2*(a(1:(N-1),:) + repmat(g,N-1,1));
else
pcons(2:N,:) = p(2:N,:) == p(1:(N-1),:) + t*v(1:(N-1),:) + t^2/2*(a(1:(N-1),:) + repmat(g,N-1,1));
end
pcons((N+1),:) = p(N,:) == pF;
trajectoryprob.Constraints.acons = acons;
trajectoryprob.Constraints.scons = scons;
trajectoryprob.Constraints.vcons = vcons;
trajectoryprob.Constraints.pcons = pcons;
%%%%%%%%% NEW DUMMY VARIABLE AND CONSTRAINT %%%%%%%%%
test_var = optimvar("test_var",N,1,"LowerBound",-1,"UpperBound",1);
trajectoryprob.Constraints.test_cons = test_var >= 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
function plottrajandaccel(sol,p0,pF)
figure
psol = sol.p;
plot3(psol(:,1),psol(:,2),psol(:,3),'rx')
hold on
plot3(p0(1),p0(2),p0(3),'ks')
plot3(pF(1),pF(2),pF(3),'bo')
hold off
view([18 -10])
xlabel("x")
ylabel("y")
zlabel("z")
legend("Steps","Initial Point","Final Point")
figure
asolm = sol.a;
nasolm = sqrt(sum(asolm.^2,2));
plot(nasolm,"rx")
xlabel("Time step")
ylabel("Norm(acceleration)")
end
I will update the example in the documentation.
Alan Weiss
MATLAB mathematical toolbox documentation
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!