Change optimized variables when using fminsearch

9 次查看(过去 30 天)
So I have the following line of code :
x = fminsearch(@(x) GUIOptValFcn(x(1),x(2),x(3)),[0,0,0]);
What I need to be able to do is to conditionally set any of the three optimize variables to a constant value, and optimize the remaining. I am looking for a more robust method than a case statement, as the 3 variables are likely to grow in the future, and creating a case statement for all possible combinations of the variables is unwieldy.
I have considered going into the value function and conditionally setting the values to constants based on an input vector containing the constant values and a second logical vector that determines if the constant value is allowed to be optimized. I could then overwrite the x vector values with the ones held constant after the optimization is complete. I am interested in thoughts on better ways to achieve this, if possible.

回答(2 个)

Star Strider
Star Strider 2017-1-5
If you want to selectively limit specific parameters, I would use the Optimization Toolbox fmincon function.
Example:
x = fmincon(fun,x0,A,b,[],[],lb,ub)
Then, set the ‘lb’ and ‘ub’ values to be the same (or varying by ±eps) for the parameters you do not want to change.
  2 个评论
Walter Roberson
Walter Roberson 2017-1-6
fmincon uses a quite different algorithm than fminsearch does. fmincon only ever escapes local minima by accident.
fmincon is more efficient and more accurate when searching for the bottom of a local minima, but fminsearch can often escape local minima.
John D'Errico
John D'Errico 2017-1-6
Setting the upper and lower bounds to be the same is often a bad idea for optimizers. It can easily cause numerical problems, depending on if the solver is able to resolve that set of bound constraints properly. (For example, does the solver check to see if the bounds are identical, and then just explicitly remove that variable from the optimization? I've not looked at how the bounds are dealt with internally, so I cannot be sure.)
Now, as it turns out, it looks like fmincon is able to handle setting the bound constraints equal, at least in the one test I ran just now. However, you may possibly run into problems, and this is one thing I'd look at first if you do have a problem.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-1-5
编辑:Walter Roberson 2017-1-5
function r = selectInputs(x, IsSelected, Constants, Fcn)
selected_x_cell = num2cell(x .* IsSelected + (~IsSelected).*Constants);
r = Fcn(selected_x_cell{:});
For example,
sel_12 = [true, true, false]; %true where the variable is to pass through
cons_12 = [0, 0, 7.84]; %use any finite value for the places where the variable is to pass through but 0 is easier to understand
x = fminsearch(@(x) selectInputs(x, sel_12, cons_12, @GUIOptValFcn), [0,0,0]);
Caution: this code is not adequate for the case where fminsearch drives an unselected input to infinity.

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by