MATLAB Optimization - how to move on to next possible solution if a condition is not satisfied?

时间:2017-06-15 09:28:18

标签: matlab function optimization

I am using the fmincon optimization to find parameters that fit constraints. It is currently set up like:

constraints = [1 2 3];    
interval = 100;

time_start = [2500];
time_boundary = [500 5000];
param2_start = [0.5];
param2_boundary = [0 1];

opts = optimset(optimset('fmincon'),'TolFun',1e-4,'TolX',1e-4,'display','iter');
[x,fval] = fmincon(@(X) func(X,constraints,interval),[time_start param2_start],[],[],[],[],[time_boundary(1) param2_boundary(1)],[time_boundary(2) param2_boundary(2)],[],opts)

where a simplified func is:

function out = func(X,constraints,interval)

  time = X(1);
  param2 = X(2);

  % Check inputs before calculations
  if (mod(time,interval) > 0)
      out = 999;
  else
      % Perform calculations based on parameters, constraints and interval.
      % Simplified to:
      out = time/interval;
  end

end

However, the function (func) can fail if time is not integer-divisible by interval.

Is there a way to catch this within the function so that fmincon moves onto another solution without failing?

Or, is there a better way to use fmincon (or another optimization tool) to avoid this issue?

1 个答案:

答案 0 :(得分:2)

是的,将您的功能修改为(伪代码,因为您没有提供功能的内容)

function(X, constraints, interval)
try
   <<code that can fail when function is being optimized>>
   <<time/interval when interval == 0 for example>>
catch
   return <<penalty value>>
end
end

基本上,我们的想法是当函数失败时,你会返回一个惩罚值,这样最小化器就会远离传递给你函数的值。

编辑:根据您的编辑,该功能将是:

function out = func(X, constraints, interval)
try
    out = X(1)/interval;
catch
    out = 1e10;
end
end

但是,你永远不会在块的catch部分结束,因为即使intervalNaNInf或{{1},除法也不会导致错误}}。我只是张贴这个,因为你问我。我对函数“失败”的理解特别是它会抛出错误。如果它只是产生不正确的结果,那么上面的函数是正确的。我只是将失败案例中的0修改为高于您获得的任何值,否则将训练最小化器离开此点。