How can I make patternsearch optimize using additional values?

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);
function RSS = do(param)
load y_highpass_TK_short y_highpass_TK_short
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);

 Accepted Answer

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:
function RSS = do(params,extradata) % extradata are the values in y_highpass_TK_short y_highpass_TK_short
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);
% Your code here
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

8 Comments

Thank you for your comments. I changed my code to pass data instead of loading in the objective function. However, my problem will not be solved by your suggestion. n_1, n_19, n_27, n_29, and n_47 are themselves output values from another function (Page test).
Input of the objective function "do" and Page test
  • T0
  • T1
  • T2
  • alpha
Output of Page test
  • n_1
  • n_19
  • n_27
  • n_29
  • n_47
Output of the objective function "do"
  • RSS
I want to set constraint on the output of the Page test without controlling them directly. I want to control only T0, T1, T2, and alpha so that RSS becomes the smallest if n_1, n_19, n_27, n_29, and n_47 are also some appropriate values. Is it possible?
In that case, I believe that you should calculate the parameters
  • n_1
  • n_19
  • n_27
  • n_29
  • n_47
in the objective AND nonlinear constraint functions. The nonlinear constraint function could be, assuming that the constraint is that all of these parameters are larger than 30,
function [c,ceq] = nlcon(param)
pagetestoutput = page(param); % the function that computes the values
c = pagetestoutput; % I assume pagetestoutput is a vector of 5 values
ceq = [];
end
It is important that your objective and constraint function both call page. Something like:
function RSS = do(param)
pagetestoutput = page(param); % the function that computes the values
n_1_groundtruth = pagetestoutput(1);
n_19_groundtruth = pagetestoutput(2);
n_27_groundtruth = pagetestoutput(3);
n_29_groundtruth = pagetestoutput(4);
n_47_groundtruth = pagetestoutput(5);
% Now do the remaining calculations
end
You might object at this point that you are calculating page(param) more often than necessary. That is true; for more efficiency, see Objective and Nonlinear Constraints in the Same Function.
Alan Weiss
MATLAB mathematical toolbox documentation
I think I could improve my code to achieve what I wanted to do by the nonlinear constrain function technique. Thank you so much for your help.
Oops, I goofed. The constraint obviously should be
c = 30 - pagetestoutput; % I assume pagetestoutput is a vector of 5 values
This constrains pagetestoutput to be larger than 30.
Alan Weiss
MATLAB mathematical toolbox documentation
Thank you for the correction.
Actually, I had also set my "c" exactly the same.
I became more confident that my code is written correctly.
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.
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
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.

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!