# How can I make patternsearch optimize using additional values?

10 views (last 30 days)
Kei Manabe on 4 Jun 2020
Commented: Kei Manabe on 3 Jul 2020
I am trying to optimize the Page test parameters. In the last two lines, as the cost function of patternsearch, "RSS" is calculated. Basically, I would like to find the parameters to minimize RSS. However, I would like to make patternsearch optimize considering other values: n_1, n_19, n_27, n_29, n_47. These values should be larger than 30. Is there any way or idea to have additional constraints for patternsearch? Thank you so much in advance.
rand1 = rand(1)*1000; rand2 = rand(1)*1000; rand3 = rand(1); rand4 = rand(1)/100;
param0 = [round(max(rand1,rand2)), round(min(rand1,rand2)), round(rand3, 2), round(rand4, 5)];
options = optimoptions('patternsearch', 'PlotFcn', 'psplotbestx', 'MeshTolerance', 1, 'ScaleMesh', false, 'InitialMeshSize',10);
A = [];
b = [];
Aeq = [];
beq = [];
nlcon = [];
lb = [1 1 0.1 1/500000];
ub = [100000 100000 10 1/25000];
fun = @do;
[param, RSS, exitflag, ~] = patternsearch(fun, param0, A, b, Aeq, beq, lb, ub, nlcon, options);
n_1_groundtruth = 46;
n_19_groundtruth = 43;
n_27_groundtruth = 38;
n_29_groundtruth = 35;
n_47_groundtruth = 40;
T0 = param(1);
T1 = param(2);
T2 = param(3);
alpha = param(4);
[Vn_1, ~, loc_maxima_1, N_1] = doPagetest_loop(y_highpass_TK_short{1}, T0, T1, T2, alpha);
n_1 = length(Vn_1(loc_maxima_1==1));
[Vn_19, ~, loc_maxima_19, N_19] = doPagetest_loop(y_highpass_TK_short{2}, T0, T1, T2, alpha);
n_19 = length(Vn_19(loc_maxima_19==1));
[Vn_27, ~, loc_maxima_27, N_27] = doPagetest_loop(y_highpass_TK_short{3}, T0, T1, T2, alpha);
n_27 = length(Vn_27(loc_maxima_27==1));
[Vn_29, ~, loc_maxima_29, N_29] = doPagetest_loop(y_highpass_TK_short{4}, T0, T1, T2, alpha);
n_29 = length(Vn_29(loc_maxima_29==1));
[Vn_47, ~, loc_maxima_47, N_47] = doPagetest_loop(y_highpass_TK_short{5}, T0, T1, T2, alpha);
n_47 = length(Vn_47(loc_maxima_47==1));
RSS = sqrt((n_1-n_1_groundtruth)^2 + (n_19-n_19_groundtruth)^2 + (n_27-n_27_groundtruth)^2 ...
+ (n_29-n_29_groundtruth)^2 + (n_47-n_47_groundtruth)^2);

Alan Weiss on 5 Jun 2020
Before I get to your specific question, allow me an observation: it is very inefficient to call a load statement in an objective function. I think that you will have much better luck passing in fixed data using a parameterization technique.
Now for your specific question. If you also want to optimize over the variables n_1, n_19, n_27, n_29, and n_47, then I suggest that you make these variables part of your optimization. Append them to your params vector as follows:
T0 = params(1);
T1 = params(2);
T2 = params(3);
alpha = params(4);
n_1 = params(5);
n_19 = params(6);
n_27 = params(7);
n_29 = params(8);
n_47 = params(9);
end
To keep params(5:9) above 30 during the optimization, set lower bounds:
lb = 30*ones(1,9);
lb(1:4) = [1 1 0.1 1/500000];
Extend the ub vector to be of length 9 as well.
Alan Weiss
MATLAB mathematical toolbox documentation

Kei Manabe on 2 Jul 2020
Could I ask another question regarding the nonlinear constraint here?
I am trying to find optimal answers using the nonlinear constraint but I am getting error saying "Optimization terminated: no feasible point found". I wonder why the optimization tool doesn't find the closest answer which satisfies the nonlinear constraint. I mean it looks trying optimal answer first and then judging if it satisfies the nonlinear constraint or not.
Is my understanding correct? If so, is there any way to make MATLAB not to find optimal answer which is yet unknown if it satisfies the nonlinear constraint or not?
Thank you so much in advance.
Alan Weiss on 2 Jul 2020
Your understanding is not quite right. Solvers attempt to find a feasible solution, not first a solution and then feasibility. In fact, you might want to check whether any feasible solution exists. Try the suggestions in Converged to an Infeasible Point.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Kei Manabe on 3 Jul 2020
Thank you so much.
I am relieved to know the MATLAB optimization doesn't apply the nonlinear constraint after optimal answer is found.
I will try to understand more with the linked document.