【问题标题】:Can I use if statement in constraint function for optimization using ga in MATLAB?我可以在约束函数中使用 if 语句在 MATLAB 中使用 ga 进行优化吗?
【发布时间】:2017-11-03 18:53:45
【问题描述】:

我有一个约束函数,它调用另一个函数(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 

提前致谢!

【问题讨论】:

    标签: matlab optimization constraints genetic-algorithm


    【解决方案1】:

    您可以使用trycatch 来检查约束

    例如

    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
    

    【讨论】:

    • 我比较关心是否使用try和catch,会不会干扰遗传算法的流程?当 inverse_kinematics(l1,l2,l3) 无法计算时,要为从 c(3) 到 c(20) 的约束分配什么值?
    • 嗯,这取决于您正在实施的事情。您应该加入一些替代解决方案,以保证优化的收敛性。
    • 换句话说,你的问题不是编程问题,而是你所要求的概念问题。我认为,数学堆栈交换是解决这个问题的最佳方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2021-12-16
    相关资源
    最近更新 更多