Is there a better function to minimize than fminsearch ?
29 ビュー (過去 30 日間)
古いコメントを表示
Hello, I would like to know if it exits a better function to minimize a function than fminsearch ? I have this line :
[X, fval, exitflag, output] = fminsearch(@func, X0, options, params)
I precise I have the optimization toolbox. Thank you for your help !
1 件のコメント
Walter Roberson
2017 年 10 月 18 日
Do you need a global optimization or a local optimization? Is the function differentiable? Is its Jacobian known? Is its Hessian known? If you were to pass symbolic variables into the function would it be able to return a symbolic formula in response ?
回答 (2 件)
Birdman
2017 年 10 月 18 日
There is a user-written function which contains Hooke-Jeeves algorithm. Maybe this will help you. The inputs and the outputs are clearly defined.
function [X,BestF,Iters] = hookejeeves(N, X, StepSize, MinStepSize, Eps_Fx, MaxIter, myFx)
% Function HOOKEJEEVS performs multivariate optimization using the
% Hooke-Jeeves search method.
%
% Input
%
% N - number of variables
% X - array of initial guesses
% StepSize - array of search step sizes
% MinStepSize - array of minimum step sizes
% Eps_Fx - tolerance for difference in successive function values
% MaxIter - maximum number of iterations
% myFx - name of the optimized function
%
% Output
%
% X - array of optimized variables
% BestF - function value at optimum
% Iters - number of iterations
%
Xnew = X;
BestF = feval(myFx, Xnew, N);
LastBestF = 100 * BestF + 100;
bGoOn = true;
Iters = 0;
while bGoOn
Iters = Iters + 1;
if Iters > MaxIter
break;
end
X = Xnew;
for i=1:N
bMoved(i) = 0;
bGoOn2 = true;
while bGoOn2
xx = Xnew(i);
Xnew(i) = xx + StepSize(i);
F = feval(myFx, Xnew, N);
if F < BestF
BestF = F;
bMoved(i) = 1;
else
Xnew(i) = xx - StepSize(i);
F = feval(myFx, Xnew, N);
if F < BestF
BestF = F;
bMoved(i) = 1;
else
Xnew(i) = xx;
bGoOn2 = false;
end
end
end
end
bMadeAnyMove = sum(bMoved);
if bMadeAnyMove > 0
DeltaX = Xnew - X;
lambda = 0.5;
lambda = linsearch(X, N, lambda, DeltaX, myFx);
Xnew = X + lambda * DeltaX;
end
BestF = feval(myFx, Xnew, N);
% reduce the step size for the dimensions that had no moves
for i=1:N
if bMoved(i) == 0
StepSize(i) = StepSize(i) / 2;
end
end
if abs(BestF - LastBestF) < Eps_Fx
break
end
LastBest = BestF;
bStop = true;
for i=1:N
if StepSize(i) >= MinStepSize(i)
bStop = false;
end
end
bGoOn = ~bStop;
end
function y = myFxEx(N, X, DeltaX, lambda, myFx)
X = X + lambda * DeltaX;
y = feval(myFx, X, N);
% end
function lambda = linsearch(X, N, lambda, D, myFx)
MaxIt = 100;
Toler = 0.000001;
iter = 0;
bGoOn = true;
while bGoOn
iter = iter + 1;
if iter > MaxIt
lambda = 0;
break
end
h = 0.01 * (1 + abs(lambda));
f0 = myFxEx(N, X, D, lambda, myFx);
fp = myFxEx(N, X, D, lambda+h, myFx);
fm = myFxEx(N, X, D, lambda-h, myFx);
deriv1 = (fp - fm) / 2 / h;
deriv2 = (fp - 2 * f0 + fm) / h ^ 2;
diff = deriv1 / deriv2;
lambda = lambda - diff;
if abs(diff) < Toler
bGoOn = false;
end
end
% end
17 件のコメント
Birdman
2017 年 10 月 20 日
編集済み: Birdman
2017 年 10 月 20 日
Firstly, enter the following informations for the *hookejeeves* function.
N=..; X=[a1 .. a11]; StepSize=[0.5 .. 0.5]; MinStepSize=[0.01 .. 0.01]; Eps_Fx=[];%let it be empty
Then, save the following function with the name hookejeeves
function [X,BestF,Iters] = hookejeeves(N, X, StepSize, MinStepSize, MaxIter, myFx) %Başlangıç atamalarının yapılması. BestF=x(k+1), LastBestF=x(k) gibi %düşünülebilir. Xnew = X; BestF = feval(myFx, Xnew, N); LastBestF = 100 * BestF + 100; %bGoOn değişkenine bağlı while döngüsü, maksimum iterasyon sayısına veya %verilen toleransa ulaşılınca biter. bGoOn = true; Iters = 0; %Civar aramasının gerçekleştiği while döngüsüdür. while bGoOn Iters = Iters + 1; if Iters > MaxIter break; end X = Xnew; %N=2 değişken için arama yapılmaktadır. for i=1:N bMoved(i) = 0; bGoOn2 = true; while bGoOn2 xx = Xnew(i); Xnew(i) = xx + StepSize(i); F = feval(myFx, Xnew, N); if F < BestF BestF = F; bMoved(i) = 1; else Xnew(i) = xx - StepSize(i); F = feval(myFx, Xnew, N); if F < BestF BestF = F; bMoved(i) = 1; else Xnew(i) = xx; bGoOn2 = false; end end end end for i=1:N bMadeAnyMove(i) = sum(bMoved(i));%Civar araması başarılıysa, bMadeAnyMove(i) değişkeni 0'dan farklı olur. if bMadeAnyMove(i) > 0 Xnew1(i) = 2*Xnew(i) - X(i);%if bloğunda yeni x(k+1) değeri yukarıda kullanılmak üzere elde edilir. end end BestF = feval(myFx, Xnew1, N); LastBestF = feval(myFx, Xnew, N); %Fonksiyonun değeri bir öncekinden daha küçükse, bir önceki değerlerin %yeni x değerini bulurken kullanılması. for i=1:N if BestF < LastBestF Xnew(i)=Xnew1(i); X(i)=Xnew(i); Xnew1(i) = 2*Xnew(i) - X(i); end end %Civar araması başarısızsa, adım sayısı yarıya düşürülür. for i=1:N if bMoved(i) == 0 StepSize(i) = StepSize(i) / 2; end end %Adım sayısı, verilen adım sayısından daha küçük olursa iterasyon sonlanır. bStop = true; for i=1:N if StepSize(i) >= MinStepSize(i) bStop = false; end end bGoOn = ~bStop; end
Don't worry about the comment lines, they are in turkish. Then enter the following code:
hookejeeves(N,X,StepSize,MinStepSize,Eps_Fx,@intrafunc)
This one should work. Let me know the outcome. If you give 11 variables, it might take a little longer to calculate but do not worry and wait for it to get it done.
Alan Weiss
2017 年 10 月 18 日
You might be interested in the Optimization Decision Table, which exists to help you choose the most appropriate solver.
Alan Weiss
MATLAB mathematical toolbox documentation
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Assumptions についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!