Output of a Function as a Constraint in Genetic Algorithm

1 次查看(过去 30 天)
Hello,
I am trying to use the genetic algorithm to find the minimum value to a function.
My cost function is
function y=FitnessFcn(x)
y=abs(x(1));
end
My Constraint Function:
function [c, c_eq] =Constraints_CW(x)
format long;
c=[
abs(Compute(x(1))); ];
c_eq=[];
end
Where Compute
function X=Compute(x)
r0=[100; 100; 100;];
v0=[0;0;0];
%Initial Condition Vector
IC=[r0; v0];
%ODE 45 parameters
tol=1e-6;
options=odeset('RelTol',tol,'AbsTol',[tol tol tol tol tol tol]);
N=1;
M=10000;
time1=linspace(0,N,1000);
time2=linspace(N,M,9000);
time3=linspace(0,10000,10000);
%Integrator to solve the 2BP
[t,x1]=ode45(@(t, CW) CW_Thrust(t, CW, x ), time1, IC, options);
IC2=x1(1000,:)';
[t,x2]=ode45('CW', time2, IC2, options);
[t,x3]=ode45('CW', time3, IC2, options);
X=vertcat(x1,x2);
X=X(10000,2);
end
The idea is to iterate through values of x(1), pass them into the Compute function and return the last element of X which is then computed in the cost function.
Nothing seems to be happeniing when I run it.
Thanks!

采纳的回答

Walter Roberson
Walter Roberson 2021-3-19
We do not have your CW function or your CW_Thrust function to examine.
return the last element of X
Since you only need the last value of X, you should avoid computing any other point if it is not needed for that last value.
[t,x1]=ode45(@(t, CW) CW_Thrust(t, CW, x ), time1, IC, options);
[t,x2]=ode45('CW', time2, IC2, options);
X=vertcat(x1,x2);
X=X(10000,2);
So the entry extracted from X -- the entry that is returned -- is from the last run for time2.
[t,x3]=ode45('CW', time3, IC2, options);
You ignore the t, t3 generated there is creating your output, so you might as well not run for time3.
Also, your time3 starts at 0 but you are using the same IC2 as was the case for when you ran time2 where time2 starts with N=1 not from 0, so you need to think more carefully about the initial conditions for running time3 if you decide to keep the run.
You are running the last entry for x2, but you do not do any calculations for any of the other entries, so you do not really care how many are generated or what they are. That being the case, you might as well just use a vector of length 3 specifying the times.
time1 = linspace(0,N,3);
time2 = linspace(N,M,3);
[t,x1] = ode45(@(t, CW) CW_Thrust(t, CW, x ), time1, IC, options);
IC2 = x1(end,:)';
[t,x2] = ode45(@CW, time2, IC2, options);
X = x2(end,2);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by