繁体   English   中英

我可以在约束函数中使用if语句在MATLAB中使用ga进行优化吗?

[英]Can I use if statement in constraint function for optimization using ga in MATLAB?

我有一个约束函数,该函数调用另一个函数(inverse_kinematics)来计算一些约束,如果不满足c(1)和c(2),则inverse_kinematics()会出错,如何确保c(1)和c (2)在调用inverse_kinematics()函数之前是否满意? 我可以在约束函数中使用if条件来解决它吗? 下面是我的优化函数的代码,

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); % Gives error when c(1) and c(2) are not satisfied

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 

提前致谢 !

您可以使用try and catch检查约束

例如

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;
    try
        thetas = inverse_kinematics(l1,l2,l3);
    catch
        warning('C1 and C2 is wrong'); % Or you can change any parameter here
    end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM