Change optimized variables when using fminsearch

10 ビュー (過去 30 日間)
Gregory McFadden
Gregory McFadden 2017 年 1 月 5 日
コメント済み: John D'Errico 2017 年 1 月 6 日
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.

製品

Community Treasure Hunt

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

Start Hunting!

Translated by