Optimize an objective function for values plus or minus near zero.

14 views (last 30 days)
I am trying to optimize a function that I have defined. The problem is that the function defines acceleration at a revolute joint and of course must take on negative and positive values. I am having a hard time coming up with a way to constrain my objective function so that I can minimize it near zero. Basically, I don’t care how large the value is, but I want the output to be as close to zero as it possibly can, given my constraints. Please forgive if I have implemented any bad practices but I have attached my objective function (using the ub,lb, and while loop addition was my attempt at limiting the objective) and constraints. All recommendations are welcome. Please let me know if any more information is needed.
First is my objective function. Also, note that I have cut the A = .... way down since it may not be necesary, but if you need to see it all let me know.
function A = Objective(x)
%Objective Function
% Objective function to minimize accelerations for 6DOF arm
th1 = x(1);
th2 = x(2);
th3 = x(3);
th4 = x(4);
thdot1 = x(5);
thddot1 = x(6);
thdot4 = x(7);
thddot4 = x(8);
tau1f = x(9);
tau2f = x(10);
tau3f = x(11);
T = x(12);
ub = x(13); % Upper bounds of objective function
lb = x(14); % Lower bounds of objective function
A = 0;
flag = 0;
while (A >= lb) || (A <= ub)
if ((T >= 0) && (T <= tau1f)) % First segment of Acceleration.
A = thddot1+(T^2*1.0/tau1f^4*(tau2f^3*tau3f*th1*1.8e1-tau2f^3*tau3f*th2*1.8e1+tau1f^2*tau
elseif ((T > tau1f) && (T <= tau1f+tau2f)) % Second segment of Acceleration.
T = T - tau1f;
A = (tau1f*tau2f^2*th3*1.2e1-tau1f*tau3f^2*th2*6.0+tau2f*tau3f^2*th1*1.2e1+tau2f^2*tau3f*
else
T = T - (tau1f + tau2f); % Third segment of Acceleration.
A = -(tau1f*tau2f^2*th3*2.4e1+tau1f^2*tau2f*th3*1.2e1-tau1f^2*tau3f*th2*6.0+tau2f^2*tau3f
end
flag = flag + 1;
if flag == 1000
break
end
end
end
And for my constraints:
% x = [th1;th2;th3;th4;thdot1;thddot1;thdot4;thddot4;tau1f;tau2f;tau3f;T;ub;lb];
A1eq = [1 0 0 0 0 0 0 0 0 0 0 0 0 0;... % Linear equality
0 0 0 1 0 0 0 0 0 0 0 0 0 0;...
0 0 0 0 1 0 0 0 0 0 0 0 0 0;...
0 0 0 0 0 1 0 0 0 0 0 0 0 0;...
0 0 0 0 0 0 1 0 0 0 0 0 0 0;...
0 0 0 0 0 0 0 1 0 0 0 0 0 0;...
0 0 0 0 0 0 0 0 0 0 0 1 0 0;...
0 0 0 0 0 0 0 0 1 1 1 0 0 0;...
0 0 0 0 0 0 0 0 0 0 0 0 1 0;...
0 0 0 0 0 0 0 0 0 0 0 0 0 1];
b1eq = [0;85;0;0;0;0;9;9;50;-50]; % Linear equality
A1 = [0 -1 0 0 0 0 0 0 0 0 0 0 0 0;... % Linear inequality
0 1 0 0 0 0 0 0 0 0 0 0 0 0;...
0 0 -1 0 0 0 0 0 0 0 0 0 0 0;...
0 0 1 0 0 0 0 0 0 0 0 0 0 0;...
0 0 0 0 0 0 0 0 -1 0 0 0 0 0;...
0 0 0 0 0 0 0 0 1 1 1 0 0 0;...
0 0 0 0 0 0 0 0 0 -1 0 0 0 0;...
0 0 0 0 0 0 0 0 0 0 -1 0 0 0;...
0 0 0 0 0 0 0 0 -1 -1 -1 0 0 0];
b1 = [0;85;0;85;0;9;0;0;0]; % Linear inequality
x10 = [0;40;40;85;0;0;0;0;2;2;2;9;50;-50]; % Start point
Thanks in advance for any help
  8 Comments
Matt J
Matt J on 31 Mar 2019
The solver will never produce a result that satisfies the inequality constraints exactly, but they should satisfy them within the ConstraintTolerance input parameter.
Sugs
Sugs on 31 Mar 2019
Edited: Sugs on 1 Apr 2019
@Matt That makes sense to me if it were an inequality constraint. Since it is an equality constraint why would it not satisfy it? Also, why would it not converge? Is it possibly "hovering around a flat spot" in the function? Would there be a more suitable solver, or is fmincon best for this case?

Sign in to comment.

Accepted Answer

Sugs
Sugs on 4 Apr 2019
Edited: Sugs on 4 Apr 2019
Optimizing the squared value ( or abs value) of my objective function solved the issue. However, as pointed out from Matlab help, "squaring smooths out the result in the neighborhood of zero and can be easier on the solver since there would not be any undefined first derivatives", which gave me better results.
Thanks for all the other suggestions.

More Answers (1)

Matt J
Matt J on 31 Mar 2019
Edited: Matt J on 31 Mar 2019
Using linear inequalities like these
A1 = [0 -1 0 0 0 0 0 0 0 0 0 0 0 0;... % Linear inequality
0 1 0 0 0 0 0 0 0 0 0 0 0 0;...
to implement simple bounds is less effective than using the lb,ub input arguments to fmincon.
  8 Comments
Sugs
Sugs on 31 Mar 2019
I was thinking about trying this before. What would be the best way to pick my ub and lb to be since it may change from situation to situation. Meaning that the range of my x(i)'s will not always be the same and produce different results (the original posted example was only one of six components for the original issue, and if I define a new trajectory all six will continuously change).
Matt J
Matt J on 1 Apr 2019
Edited: Matt J on 1 Apr 2019
It should depend only on how close to 0 you want A to be.

Sign in to comment.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!