help using matlab solve ga function for optimization of non linear expression

8 visualizaciones (últimos 30 días)
Hello , i have this non linear function called "h" shown in the code bellow.
I want to optimize k1, k2 ,a ,b parameters so this function will get values between 0.55 and 0.45 over the x axes.
I have tried to use the following manual and save the script file as optim.m
i tried to use use solve ga to get my function be get values between 0.55 and 0.45 over the x axes
i get an error shown bellow.
Where did i go wrong?
"Error using optim
Constraints must be an OptimizationConstraint or a struct containing OptimizationConstraints."
Thanks.
h=@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
cam=h(a,b,k1,k2);
prob = optimproblem("Objective",cam);
prob.Constraints.cons1=a<=1;
prob.Constraints.cons2=a>=-1;
prob.Constraints.cons3=b<=1;
prob.Constraints.cons4=b>=-1;
prob.Constraints.cons5=h<=0.55;
prob.Constraints.cons5=h>=0.45;
[sol,fval] = solve(prob,"Solver","ga");

Respuestas (1)

Matt J
Matt J el 4 de Nov. de 2022
Editada: Matt J el 4 de Nov. de 2022
See, Optimize with Nonlinear Constraints Using GA. It should look something like this:
x=linspace(0,100,10000);
vars = ga(fun,4,A,b,Aeq,beq,lb,ub, @(vars)nonlcon(vars,x), options);
function [c,ceq]=nonlcon(vars,x)
a=vars(1); b=vars(2);
k1=vars(3); k2=vars(4);
h =abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
c(1)=0.45-min(h);
c(2)=max(h)-0.55;
ceq=[];
end
  7 comentarios
fima v
fima v el 5 de Nov. de 2022
Editada: fima v el 5 de Nov. de 2022
Hello, it gives me the following error.
Why you created two functions of the same type.
|you have changed the original code.could you please use one "h" function?
i only want a simple example to run
Error using .*
Optimization variables cannot be combined with complex data.
Error in *
Error in solve>@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x)) (line 5)
h=@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
Error in fcn2optimexpr>createFunctionExpression (line 261)
[evalOut{:}] = func(evalInputs{:});
Error in fcn2optimexpr (line 120)
[varargout{1:nout}] = createFunctionExpression(func, ...
Error in solve (line 8)
cam=fcn2optimexpr(h,a,b,k1,k2);
Caused by:
Function evaluation failed while attempting to determine output size. The function might contain an
error, or might not be well-defined at the automatically-chosen point. To specify output size without
function evaluation, use 'OutputSize'.
>>
a=optimvar('a','Lower',-1,'Upper',+1);
b=optimvar('b','Lower',-1,'Upper',+1);
k1=optimvar('k1','LowerBound',-1,'UpperBound',1);
k2=optimvar('k1','LowerBound',-1,'UpperBound',1);
h=@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
hmax=@(a,b,k1,k2) max(h(a,b,k1,k2));
hmin=@(a,b,k1,k2) min(h(a,b,k1,k2));
cam=fcn2optimexpr( h, a,b,k1,k2);
camMax=fcn2optimexpr( hmax, a,b,k1,k2);
camMin=fcn2optimexpr( hmin, a,b,k1,k2);
prob = optimproblem("Objective",cam);
prob.Constraints.camUpper=camMax<=0.55;
prob.Constraints.camLower=camMin>=0.45;
[sol,fval] = solve(prob,"Solver","ga");
Matt J
Matt J el 5 de Nov. de 2022
Editada: Matt J el 5 de Nov. de 2022
Optimization variables cannot be combined with complex data.
This ran to completion for me:
a=optimvar('a','Lower',-1,'Upper',+1);
b=optimvar('b','Lower',-1,'Upper',+1);
k1=optimvar('k1','LowerBound',-1,'UpperBound',1);
k2=optimvar('k2','LowerBound',-1,'UpperBound',1);
x=linspace(0,10,1000);%<---- I made this up
h=@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
hmax=@(a,b,k1,k2) max(h(a,b,k1,k2));
hmin=@(a,b,k1,k2) min(h(a,b,k1,k2));
cam=fcn2optimexpr( h, a,b,k1,k2);
camMax=fcn2optimexpr( hmax, a,b,k1,k2);
camMin=fcn2optimexpr( hmin, a,b,k1,k2);
prob = optimproblem("Objective",camMax);
prob.Constraints.camUpper=camMax<=0.55;
prob.Constraints.camLower=camMin>=0.45;
[sol,fval] = solve(prob,"Solver","ga");
Solving problem using ga. Optimization terminated: average change in the fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
you have changed the original code.could you please use one "h" function?
No, you need multiple functions. However, here is a slightly different formulation which avoids hmin.
a=optimvar('a','Lower',-1,'Upper',+1);
b=optimvar('b','Lower',-1,'Upper',+1);
k1=optimvar('k1','LowerBound',-1,'UpperBound',1);
k2=optimvar('k2','LowerBound',-1,'UpperBound',1);
x=linspace(0,10,1000);%<---- I made this up
h=@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
hmax=@(a,b,k1,k2) max(h(a,b,k1,k2));
cam=fcn2optimexpr( h, a,b,k1,k2);
camMax=fcn2optimexpr( hmax, a,b,k1,k2);
prob = optimproblem("Objective",camMax);
prob.Constraints.camUpper=camMax<=0.55;
prob.Constraints.camLower=cam>=0.45;
[sol,fval] = solve(prob,"Solver","ga");

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by