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

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

If you do not care about sign but want as close to zero as possible,then minimize the square of the function.
Matt J
Matt J 2019년 3월 31일
편집: Matt J 2019년 3월 31일
We need a clearer picture of what the difficulty is. Presumably you are using fmincon. Why isn't it a simple matter of specifying lb,ub?
@Walter Thank you for the suggestion. I tried this and came up with similar objective function values. These are the results using interior point algorithm:
"Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.993427e-17.
Objective function value: 1.8175355256292112E-27"
What does the first line mean?
However, the values returned for my variables do seem more reasonable than what it was previously giving. If I try to use other algorithm's it gives me errors or unusable values as before.
@Matt I am using fmincon. I suppose I don't clearly see an effective way of applying the lb and ub to A itself. If you would explain I would appreciate it.
Sorry if these are dumb questions guys but I do apreciate your help.
Sorry, I just coppied and pasted that objective function value and missed the sign of the value. That is a perfectly small value and acceptable solution.
@Walter Thank you for that solution. So simple and effective. Can't believe I didn't think about it at all or see anyone else say anything in another post (unless I just missed it). May have been staring at it too long haha.
Could you guys still maybe explain the statement about which matrix it is refering to being singular?
@Matt I would still appreciate if you wouldn't mind showing a way to bound the objective function using lb an ub please.
Thank you again.
Can you provide everything we would need to run the code?
Sugs
Sugs 2019년 3월 31일
편집: Sugs 2019년 3월 31일
Sure. I have attached the files. I am running into an issue with my fifth joint (using A5,B5 etc.) It seems that it cannot converge. I raise my function evaluation limit to only produce the same results. The end values don't satisfy my constraint of x(9)+x(10)+x(11) = 9. It ends up being just a hair short. Any ideas on what may cause this after you take a look? The others seem to be good. I am going to plug my values for now into my simulation to see if it produces satisfactory results.
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 2019년 3월 31일
편집: Sugs 2019년 4월 1일
@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?

댓글을 달려면 로그인하십시오.

 채택된 답변

Sugs
Sugs 2019년 4월 4일
편집: Sugs 2019년 4월 4일

0 개 추천

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.

추가 답변 (1개)

Matt J
Matt J 2019년 3월 31일
편집: Matt J 2019년 3월 31일
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

Thank you I will be changing those.
@Matt I would still appreciate if you wouldn't mind showing a way to bound the objective function using lb an ub please.
Maybe I am making it more difficult than it needs to be, but I don't see how this helps my situation. I have read this before and it seems to me that it is just placing bounds on the variables within my function (which I have done in an incorrect way as above). I don't see how to use this in my case to limit the output of my objective function near plus or minus zero.
Matt J
Matt J 2019년 3월 31일
편집: Matt J 2019년 3월 31일
Is it the objective function that must be bounded near zero? Or is it the unknown parameters?
The objective function.
You need to use the nonlcon argument for that
function [c,ceq]=nonlcon(x, lb,ub)
A=Objective(x);
c(2)=A-ub;
c(1)=lb-A;
ceq=[];
end
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 2019년 4월 1일
편집: Matt J 2019년 4월 1일
It should depend only on how close to 0 you want A to be.

댓글을 달려면 로그인하십시오.

질문:

2019년 3월 31일

편집:

2019년 4월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by