minimizing with genetic algorithm

1 Ansicht (letzte 30 Tage)
dal
dal am 25 Dez. 2024
Kommentiert: dal am 30 Dez. 2024
Good morming
I would like to minimize my function with GA
function y=obj1(x)
y=(1.002)-((1-exp(-0.00003*x))/(0.00003*(x+1.5)+(0.00063*(1-exp(-0.00003*x)))));
for x=24:720
the résule in Matlab whene I use :
[x,y]=ga(@obj1,1,24,720)
Optimization terminated: average change in the fitness value less than options.TolFun.
x =
-1.499097579634137
y =
-3.566513164743854e+004
negative results.

Antworten (2)

Walter Roberson
Walter Roberson am 25 Dez. 2024
y = @(x) (1.002)-((1-exp(-0.00003*x))./(0.00003*(x+1.5)+(0.00063*(1-exp(-0.00003*x)))));
fplot(y, [-2, 10])
fplot(y, [-1.5 -1.498])
fplot(y, [5 1000])
syms x
[N,D] = numden(y(x))
N = 
D = 
vpasolve(D)
ans = 
So near -1.49906 is indeed a minimum, since it is a critical point of the function. ga() was correct to find it.
On the positive side, the minimum is somewhere near 300.
If you want to constraint to positive values, then use a lb parameter to ga:
A = [];
b = [];
Aeq = [];
beq = [];
lb = 24;
ub = 720;
[x,y]=ga(y, 1, A, b, Aeq, beq, lb, ub)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
x = 315.7285
y = 0.0120
Your bug was in providing lb and ub in the slots reserved for A and b.
  1 Kommentar
dal
dal am 30 Dez. 2024
Thank you for your help, you've really helped me

Melden Sie sich an, um zu kommentieren.


Sam Chak
Sam Chak am 25 Dez. 2024
Hey @dal
For time-invariant real-valued univariate functions, it is recommended to plot the graph to estimate the approximate location of the minimum point. From the graph, we can visually estimate that the local minimum lies between 200 and 400 with a high degree of confidence. Narrowing the search range can sometimes enhance the algorithm's efficiency. If you can provide the rate of change of the function, some algorithms can search even more effectively
In the example below, I used 'fminbnd()' (I call it f min bound) instead of the genetic algorithm, 'ga()'. Please ensure that you enter the intended inputs correctly and in the correct sequence. In your case, the function did not flag an error because there was no error from the perspective of program execution. This can often be challenging for beginners, as they may not understand what went wrong, even though they are certain that the result is incorrect.
lb = 24; % lower bound of plot
ub = 720; % upper bound of plot
ss = 0.01; % step size
%% the function
f = @(x) 1.002 - ((1 - exp(- 0.00003*x))./(0.00003*(x + 1.5) + (0.00063*(1 - exp(- 0.00003*x)))));
x = linspace(lb, ub, (ub - lb)/ss + 1);
%% the plot
plot(x, f(x)), grid on, grid minor
xlabel('x'), ylabel('f(x)')
lb = 200; % lower bound of search
ub = 400; % upper bound of search
%% location of the local minimum value
xmin = fminbnd(f, lb, ub)
xmin = 315.7286
%% the minimum value in the given range
fmin = f(xmin)
fmin = 0.0120
  1 Kommentar
dal
dal am 30 Dez. 2024
Thank you for your help, you've really helped me

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by