Optimization problem with patternsearch
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I'm trying to use the pattern search in my optimization problem, but i'm having trouble implementing it.
Here is the code:
-------------------------------------------------------------------------------------------------------------------
rng(1953,'twister');
D = 30; %dimension
Run = 51;
bounds = [-100, 100];
range = [0.01, 0.02, 0.03, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
MaxFES = 1e04*D;
opt = optimset('Display', 'final', 'MaxFunEvals', MaxFES, 'TolFun', 1e-08);
%
x0 = zeros(D,1); % inicial points
x = zeros(D,Run); % Solutions
fval = zeros(Run,1); % Results
error_range = zeros(size(range,2), Run); % error convergence
A = [];
b = [];
Aeq = [];
beq = [];
nonlcon = [];
f=str2func('cec17_func');
for i = 1:Run,
x0(:,i) = rand(D,1).*(bounds(2) - bounds(1)) + bounds(1);
options = optimoptions('patternsearch','Display','iter','PlotFcn',@psplotbestf);
% Algorithm function
[x(:,i), fval(i,1),exitflag{i}, output{i}] = ...
patternsearch(f,x0(:,i),A,b,Aeq,beq,bounds(1),bounds(2),nonlcon,options);
range_run = range.*size(output{i}.fvals,1);
error_range(:,i) = output{i}.fvals(range_run);
end
[fv, index] = sort(fval);
tab_1 = table(fv(1), mean(fv), fv(end), std(fv),...
'VariableNames', {'Best', 'Mean', 'Worst', 'Std'},...
'RowNames', {'F1'});
disp(tab_1);
plot(range_run, error_range(:,index(end)), range_run, error_range(:,index(1)));
xlabel('Number of Function Evaluations');
ylabel('Error (f(x)-f(x)*)');
legend('Worst Result', 'Best Result');
h = gca;
h.YScale = 'log';
I get the following error:
Error using cec17_func
example: f= cec17_func([3.3253000e+000, -1.2835000e+000]', 1);
Error in funevaluate (line 40)
f = feval(FUN,reshapeinput(Xin,X),varargin{:});
Error in poptimfcnchk (line 19)
[y,count] = funevaluate(FUN,Xin,X,'init',[],[],objFcnArg{:});
Error in patternsearch (line 361)
[Iterate,OUTPUT.funccount] = poptimfcnchk(FUN,nonlcon,initialX,Iterate, ...
Error in Untitled2 (line 98)
[x(:,i), fval(i,1),exitflag{i}, output{i}] = ...
Caused by:
Failure in initial user-supplied objective function evaluation. PATTERNSEARCH cannot continue.
--------------------------------------------------------------------------------------------------------------------
this error occurs because the cec17_func have more then 1 function in it, but i don't know how can I specify which function inside the cec17_func I want to optimize using the patternsearch
Does anyone know what a solution would be to solve this problem ?
Best regards.
0 Commenti
Risposte (1)
Walter Roberson
il 23 Gen 2018
"this error occurs because the cec17_func have more then 1 function in it, but i don't know how can I specify which function inside the cec17_func I want to optimize using the patternsearch"
That is not possible. Given the name of a .m file, the only function in it that can be accessed directly is the function that has the same name as the .m file. In order to be able to execute any other function in the file, you need to (somehow) have obtained a function handle to the function.
If you need to call a function inside cec17_func.m and it is not cec17_func itself, then you should be splitting cec17_func into multiple files.
2 Commenti
Walter Roberson
il 29 Gen 2018
str2func('cec17_func') can only create a function handle to the first function in cec17_func.m, not to any other function inside cec17_func.m
It is legal, and sometimes useful, to create something along the lines of
function handles = cec17_func
handles = {@cec17_part1, @cec17_part2, @cec17_part3};
end
function y = cec17_part1(x)
...
end
function y = cec17_part2(x)
...
end
function y = cecl17_part3(x)
...
end
Then calling cec17_func would return a cell array of function handles to functions inside cec17_func.m . The cell array can then be indexed at need.
If you change the cell array to a struct...
function handles = cec17_func
handles = struct('init', @cec17_part1, 'reader', @cec17_part2, 'writer', @cec17_part3);
end
(rest the same)
then instead of indexing, the returned struct could be accessed at named fields to give the desired function. This is then fairly close to creating an "object", which is more or less a struct with a set of named data fields and a set of named functions that know how to operate on that data.
Vedere anche
Categorie
Scopri di più su Direct Search in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!