且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用匿名函数在MATLAB中优化约束整数表达式?

更新时间:2023-11-23 21:32:58

因此,看来fmincon并不是从事这项工作的唯一工具.实际上,我什至无法运行它.在下面,fmincon被卡住"在IC上,并且拒绝执行任何操作……为什么……这是另一篇文章!使用相同的布局和公式,fminbnd找到正确的解决方案.据我所知,唯一的区别是前者使用的是有条件的.但是我的条件是没有花哨,而且真的是不需要的.因此,它必须与算法有关.我想这就是使用黑匣子"时得到的.无论如何,经过长期的,痛苦的,痛苦的学习经历,这是一个解决方案:

So it seems fmincon is not the only tool for this job. In fact I couldn't even get it to work. Below, fmincon gets "stuck" on the IC and refuses to do anything...why...that's for a different post! Using the same layout and formulation, fminbnd finds the correct solution. The only difference, as far as I know, is that the former was using a conditional. But my conditional is nothing fancy, and really unneeded. So it's got to have something to do with the algorithm. I guess that's what you get when using a "black box". Anyway, after a long, drawn out, painful, learning experience, here is a solution:

options = optimset('Display','iter','MaxIter',500,'OutputFcn',@outfun);

% Conditional
index = @(L) min(find(abs([dpdx(range(range<=L),a_of_L(L)),inf] - 1) - tol > 0,1,'first'),length(range));

% Optimize
%xsol = fmincon(@(L) -range(index(L)),IC,[],[],[],[],LB,UB,confun,options);
xsol = fminbnd(@(L) -range(index(L)),LB,UB,options);

我要特别感谢@AndrasDeak的所有支持.如果没有帮助,我将无法解决!

I would like to especially thank @AndrasDeak for all their support. I wouldn't have figured it out without the assistance!