real and integer variables

5 Ansichten (letzte 30 Tage)
dal
dal am 5 Feb. 2025
Kommentiert: dal am 6 Feb. 2025
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
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
ga - Find minimum of function using genetic algorithm This MATLAB function finds a local unconstrained minimum, x, to the objective function, fun. Syntax x = ga(fun,nvars) x = ga(fun,nvars,A,b) x = ga(fun,nvars,A,b,Aeq,beq) x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon) x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options) x = ga(problem) [x,fval] = ga(___) [x,fval,exitflag,output] = ga(___) [x,fval,exitflag,output,population,scores] = ga(___) Input Arguments fun - Objective function function handle | function name nvars - Number of variables positive integer A - Linear inequality constraints real matrix b - Linear inequality constraints real vector Aeq - Linear equality constraints real matrix beq - Linear equality constraints real vector lb - Lower bounds [] (default) | real vector or array ub - Upper bounds [] (default) | real vector or array nonlcon - Nonlinear constraints function handle | function name options - Optimization options output of optimoptions | structure intcon - Integer variables vector of positive integers problem - Problem description structure Output Arguments x - Solution real vector fval - Objective function value at the solution real number exitflag - Reason ga stopped integer output - Information about the optimization process structure population - Final population matrix scores - Final scores column vector Examples openExample('globaloptim/OptimizeANonsmoothFunctionUsingGaExample') openExample('globaloptim/MinimizeANonsmoothFunctionWithLinearConstraintsExample') openExample('globaloptim/MinimizeNonsmoothFunctionLinearEqualityInequalityExample') openExample('globaloptim/OptimizeWithLinearConstraintsAndBoundsExample') openExample('globaloptim/OptimizeWithNonlinearConstraintsUsingGaExample') openExample('globaloptim/MinimizeWithNondefaultOptionsExample') openExample('globaloptim/MinimizeANonlinearFunctionWithIntegerConstraintsExample') openExample('globaloptim/ObtainTheSolutionAndFunctionValueExample') openExample('globaloptim/ObtainDiagnosticInformationExample') openExample('globaloptim/ObtainFinalPopulationAndScoresExample') See also gamultiobj, optimoptions, particleswarm, patternsearch, Optimize Introduced in Global Optimization Toolbox before R2006a Documentation for ga doc 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)
ga stopped because the average change in the penalty function value is less than options.FunctionTolerance and the constraint violation is less than options.ConstraintTolerance.
xval = -3
fval = 0.5750
exitflag = 1
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.

Community Treasure Hunt

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

Start Hunting!

Translated by