real and integer variables
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
How can I perform an optimization with the ga function using both real and integer variables, and what mean banality, how to use it
can anyone help me to solve this sample : optimize y with simple GA and plot it using real une integer variable decision and penality.
y=(1.002)-((1-exp(-0.00003*x+(0.000675)))/(0.00003*(x-21)+(0.00063*(1-exp(-0.00003*x+(0.000675))))))
lb=24 ub=720
Antworten (1)
John D'Errico
am 5 Feb. 2025
Bearbeitet: John D'Errico
am 5 Feb. 2025
No. We won't do what is surely your homework assignment. But why not TRY IT!
First, plotting it is trivial. LEARN TO USE a tool like fplot. So, first, learn to write it as a function!
Read the help about function handles, and anonymous functions.
For example, here is a function handle. For a different function. No, I won't do your homework. That is your job, not ours.
Fun = @(x) 0.1*x.^2 + 2 - 0.025*x.^3 + x;
Decide where it lives. I'll pick the interval from -5 to +8.
fplot(Fun,[-5,8])
However, the rest of your question makes little sense. There is only one variable. You want to optimize it with one integer variable, and penalty? What does a penalty have to do with anything? GA is not a solver that uses a penalty function in any form anyway, since it allows bound constraints. And since there is only one variable, ALL of the one variables are integer in your question.
Regardless, READ THE HELP DOCS FOR ga. In there, you will find examples of how to use ga. If you want to specify some of a set of variables, look at the input argument intcon, as it allows you to designate which variabes are integer, and which ones are allowed to be continuous.
You can use the links I gave by clicking on ga above for the complete documentation on GA. You can also read the help for ga here:
help ga
Which gives an abbreviated documentaiton.
So to minimize Fun, on the interval [-5 to 8, with x an integer, we would do
lb = -5;
ub = 8;
nvars = 1; % ony one variable, x.
intcon = 1; % ALL of the variables are integer.
[xval,fval,exitflag] = ga(Fun,nvars,[],[],[],[],lb,ub,[],intcon)
On the interval we chose, with x an integer, that is the minimum. You can see that from the plot.
In a more complicated case with multiple variables, where some variables are integer and some are continuous, you would tell GA which ones were integer, by listing the index of only the integer variables in intcon.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!