How can I integrate into a fitness function another code.

6 visualizzazioni (ultimi 30 giorni)
am trying to use the GA on my existing project(simulator). So I have created the algorithm and I am trying to integrate it into my existing code. So what I am trying to do is instead of using the "y" function, I am trying to use my project code. But it seems that it does not like it when I am commenting out the Y, or even when I am using y=@Project_code;
clear all;
car_range_max=[98,52];
car_range_min=[-98,-52];
opts=gaoptimset('PlotFcns',@gaplotbestf,'PopulationSize',10,'PopInitRange',[car_range_max;car_range_min],'Generations',20,'CrossoverFraction',0.7,'TolFun',0.0001,'MutationFcn',@mutationadaptfeasible);
global z
z = [];
nvars = 2;
[bestx, minfval_M]=ga(@untitled2,nvars,[],[],[],[],car_range_max,car_range_min,[],opts);
bestx
fval
%/*********************** another file ***************************************/
function y= untitled2(x)
global z
max_speed=x(1);
acc_speed=x(2);
[y,car_energy,car_travel_time]= Project_code(max_speed,acc_speed); % Project_code is expecting the values from max_speed and acc_speed
z=[z;max_speed,acc_speed,car_energy,car_travel_time];
end
%Bellow is redundant as I will use the function Project_code and not the y
%funtion
%function [y, car_energy,car_travel_time] = Project_code(max_speed, acc_speed)
% y = max_speed+acc_speed+4*max_speed+4*acc_speed+8;
% max_speed = randi(10); acc_speed = randi(10);
%end

Risposte (1)

Walter Roberson
Walter Roberson il 21 Feb 2022
global z
z = [];
nvars = 2;
[bestx, fval] = ga(@untitled2, nvars)
Optimization terminated: maximum number of generations exceeded.
bestx = 1×2
-2.0049 -1.9989
fval = 2.5557e-05
z
z = 9453×4
5.5635 -2.5558 7.0000 6.0000 9.7908 1.5695 2.0000 8.0000 4.6551 0.7655 10.0000 10.0000 -1.8040 -9.4902 8.0000 3.0000 0.4168 -9.8843 6.0000 10.0000 0.1650 5.2319 9.0000 3.0000 -1.0891 2.2510 6.0000 5.0000 -8.3005 6.3059 5.0000 8.0000 -9.9609 -1.9774 1.0000 2.0000 2.5086 9.1399 9.0000 9.0000
function y = untitled2(x)
global z
max_speed=x(1);
acc_speed=x(2);
[y, a, b] = Project_code(max_speed, acc_speed);
z=[z;max_speed,acc_speed,a,b]; % store the data into a table
end
function [y, a, b] = Project_code(max_speed, acc_speed)
y = max_speed^2+acc_speed^2+4*max_speed+4*acc_speed+8;
a = randi(10); b = randi(10);
end
  5 Commenti
Walter Roberson
Walter Roberson il 21 Feb 2022
Well that's a problem.
If Project_Code is currently a script that expects to receive values in certain variables and make assignments to particular variables... then it is really recommended that you convert it to a be a function. Calling a script from a function can lead to subtle problems.
I Bos
I Bos il 21 Feb 2022
Yes, you are correct, the Project_Code expect some values ie. max_speed and acc_speed. When I was trying to call that script Project_Code. I was geeting the result with some errors as well.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by