Siddhesh
Siddhesh

Reputation: 482

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

I am using following objective function for optimization:

function Length_Sum = objective_function( l1,l2,l3 )

Length_Sum = l1 + l2 + l3; 

end

With constraints function given below, the constrint function uses another function for calculating values of thetas,

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

Function called by constraint 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?

Upvotes: 0

Views: 372

Answers (1)

PawelK
PawelK

Reputation: 396

ga accepts constraint function with input in form of one vector with number of elements corresponding to number of constrainded variables. You should change

function [c, ceq] = simple_constraint(l1,l2,l3)

to

function [c, ceq] = simple_constraint(input)
l1 = input(1);
l2 = input(2);
l3 = input(3);

Next time, I suggest you try File->Generate Code... option from the mentioned toolbox. Then you can debug more easily from Matlab window.

There is also another problem in your program. Try running inverse_kinematics(20,20,20). It fails on line 29, but I won't go into details here, because this is not part of the question.

Upvotes: 1

Related Questions