"Error in running optimization. Not enough input arguments" while running ga

I am using following objective function:
function Length_Sum = objective_function( l1,l2,l3 )
Length_Sum = l1 + l2 + l3;
end
With constraints function given below:
function [c, ceq] = simple_constraint(l1,l2,l3)
c(1) = l3^2 + 200*l3*cos(30) + 10000 - (l1 + l2)^2;
c(2) = (100- l3*cos(30))^2 + (100*sin(30))^2 - (l1-l2)^2;
thetas = inverse_kinematics(l1,l2,l3);
c(3) = thetas(4,1) - 160;
c(4) = thetas(4,2) - 160;
c(5) = thetas(4,3) - 160;
c(6) = 20 - thetas(4,1);
c(7) = 20 - thetas(4,2);
c(8) = 20 - thetas(4,3);
c(9) = thetas(5,1) - 340;
c(10) = thetas(5,2) - 340;
c(11) = thetas(5,3) - 340;
c(12) = 200 - thetas(5,1);
c(13) = 200 - thetas(5,2);
c(14) = 200 - thetas(5,3);
c(15) = thetas(6,1) - 340;
c(16) = thetas(6,2) - 340;
c(17) = thetas(6,3) - 340;
c(18) = 200 - thetas(6,1);
c(19) = 200 - thetas(6,2);
c(20) = 200 - thetas(6,3);
ceq = [];
end
constraint function uses another function for generating values of thetas, the function is given below:
function thetas = inverse_kinematics(l1,l2,l3)
x = 100;
y = 0;
phi = 210*pi/180:60*pi/180:330*pi/180;
x1 = x - (l3*cos(phi));
y1 = y - (l3*sin(phi));
a = sqrt(x1.^2 + y1.^2);
y2 = -y1./a;
x2 = -x1./a;
gamma = atan2(y2,x2);
c = (- x1.^2 - y1.^2 - l1^2 + l2^2)./(2*l1*a);
d = acos(c);
theta1 = gamma + d;
if theta1 < 0
theta1 = theta1 + 2*pi;
end
theta4 = gamma - d;
if theta4 < 0
theta4 = theta4 + 2*pi;
end
e = (y1 - l1*sin(theta1))/l2;
f = (x1 - l1*cos(theta1))/l2;
theta2 = atan2(e,f) - theta1;
if theta2 < 0
theta2 = theta2 + 2*pi;
end
g = (y1 - l1*sin(theta4))/l2;
h = (x1 - l1*cos(theta4))/l2;
theta5 = atan2(g,h) - theta4;
if theta5 < 0
theta5 = theta5 + 2*pi;
end
theta3 = (phi)- (theta1 + theta2);
if theta3 < 0
theta3 = theta3 + 2*pi;
end
theta6 = (phi)- (theta4 + theta5);
if theta6 < 0
theta6 = theta6 + 2*pi;
end
thetas = [theta1;theta2;theta3;theta4;theta5;theta6].*180/pi;
end
After running this code using ga toolbox, with lower bounds [20 20 20] and upper bounds [100 100 100] and rest parameters set to default, I am getting "Error in running optimization. Not enough input arguments" error. Can someone help?

 採用された回答

Alan Weiss
Alan Weiss 2017 年 10 月 29 日
編集済み: Alan Weiss 2017 年 10 月 29 日
You need to have ONE variable as your decision variable. Something like
function Length_Sum = objective_function(x)
l1 = x(1);
l2 = x(2);
l3 = x(3);
Length_Sum = l1 + l2 + l3; % or just use sum(x)
Similarly, pass just one variable to simple_constraint, and extract l1, l2, and l3 inside the function.
Alan Weiss
MATLAB mathematical toolbox documentation

8 件のコメント

siddhesh rane
siddhesh rane 2017 年 10 月 30 日
@Alan Thanks. I am having problem with the constraint function if c(1) and c(2) are not satisfied inverse_kinematic() function gives an error. How can ensure that c(1) and c(2) are satisfied before running inverse_kinematics function? Is it allowed to use if condition in constraint function?
Alan Weiss
Alan Weiss 2017 年 10 月 30 日
Your constraint functions can have any statements you like, including if.
Alan Weiss
MATLAB mathematical toolbox documentation
siddhesh rane
siddhesh rane 2017 年 10 月 30 日
Do I need to set values of constraints c(3) to c(20) to very high positive value when c(1) and c(2) are not satisfied ? Otherwise wouldn't c(3) to c(20) have values from previous iteration which might mislead the GA and converge to non-feasible solution?
Alan Weiss
Alan Weiss 2017 年 10 月 30 日
I believe that, if inverse_kinematic() would return an error, then you don't have to run it at all. Just return NaN for each of the constraints that would be calculated by that function, or, if you like, set the returned values to an arbitrary large value, as you say.
No, ga would NOT take values from a previous iteration.
Allow me to make two comments on your code:
  1. You have a statement including cos(30). Are you expecting to calculate in degrees instead of radians? If so, use the cosd function, not cos.
  2. Generally, patternsearch is a faster and more robust optimizer than ga. I heartily suggest that you give it a try. If you want to start patternsearch from a variety of initial points to search for a global solution, try
x0 = lb + rand(size(lb)).*(ub - lb);
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
siddhesh rane
siddhesh rane 2017 年 10 月 31 日
@Alan: Thank you for the answer and pointing out the cos(30) error. I will try using pattern search.
siddhesh rane
siddhesh rane 2017 年 10 月 31 日
@Alan: I tried putting NaN values for constraints and using pattern search as well as ga, I am getting an error "Constraint function must return real value."
Alan Weiss
Alan Weiss 2017 年 10 月 31 日
Well, try using 1e100 instead as each constraint value when the constraint is not defined. I thought that patternsearch, at least, would do OK with NaN values, but I guess not. Sorry.
Alan Weiss
MATLAB mathematical toolbox documentation
siddhesh rane
siddhesh rane 2017 年 11 月 1 日
Thanks @Alan its working now

サインインしてコメントする。

その他の回答 (1 件)

Priya Venugopal
Priya Venugopal 2022 年 4 月 16 日

0 投票

how can we include the constraint Ax>=b as objective fuction....pl help

カテゴリ

質問済み:

2017 年 10 月 29 日

回答済み:

2022 年 4 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by