Writing ga output to text file

5 views (last 30 days)
Shreenath Krishnamurthy
Shreenath Krishnamurthy on 4 Oct 2018
Commented: Alan Weiss on 16 Oct 2018
Hi, I am running genetic algorithm in MATLAB 2018b, the code is given below.I have some issues in writing the output to the text file. I would like to have my DV3.txt to store the decision variables, the objectives and the calculated values P1,P2 for all evaluations. At the moment it is not possible to do it this way as the output file is only appended for a short number of evaluations (less than 50). Any suggestions on why this is happening ? Let me know if my question is not clear. Thanks.
% MAINFILE
global iteration
iteration=0;
t1 = tic;
NumVar=7;
Xlow=[10 0.1 10 1 0.1 0.01 0.01];
Xup =[30 2 500 100 0.5 0.05 30];
parpool(8);
options = optimoptions('gamultiobj','UseParallel', true,...
'PopulationSize',15*NumVar,'Generations',40, 'CrossoverFraction',0.5,'CrossoverFcn',@crossoverintermediate ,'MutationFcn',...
@mutationadaptfeasible,'ParetoFraction',0.35);
[X,f,exitflag,output,final_pop]=gamultiobj(@funcfile,NumVar,[],[],[],[],...
Xlow,Xup,options);
delete(gcp('nocreate'))
time = toc(t1);
In the funcfile I am writing the following to store the output in text file
fid = fopen('DV3.txt', 'at');
fprintf(fid, '%f %f %f %f %f %f %f %f %f %f %f\r\n',...
X(1),X(2),X(3),X(4),X(5),X(6),X(7),F1,F2,P1,P2);
fclose(fid);
Where X(1) TO X(8) are my decision variables. F1 F2 are objectives and P1 and P2 are values calculated for a given set of X
  3 Comments
KSSV
KSSV on 4 Oct 2018
At the moment it is not possible to do it this way as the output file is only appended for a short number of evaluations (less than 50).
why what is happening if iterations are more? You getting errors?
Shreenath Krishnamurthy
Shreenath Krishnamurthy on 4 Oct 2018
To clarify, the main file is the one where I call GA. The function file is written to solve a set of odes and X(1) to X(8) are input values to the ode function. The problem i face is this. I start a ga yesterday evening, the DV3.txt file is last appended around 7 pm yesterday after which there has been no change to the file. The only error message that i get is the ode solver failing. Sorry I will not be able to attach the code due to specific reasons.

Sign in to comment.

Answers (1)

Alan Weiss
Alan Weiss on 4 Oct 2018
I suspect that the issue is having multiple workers trying to write to the same output file at the same time. See "Factors that Affect Results" in Improving Performance in Parallel Computing.
You might have better luck using an output function to record the history that you want.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Shreenath Krishnamurthy
Shreenath Krishnamurthy on 16 Oct 2018
Edited: Shreenath Krishnamurthy on 16 Oct 2018
Hi Alan, I have tried the following 1.Use tempname to write temporary files 2. Spmd to write txt files for each worker. In the first case, i do not see files being written after 5 hours i.e around 100 evaluations. The second case does not create 8 files if i use 8 workers. Any comments on where the issue could be ? This is how my code is written
function[f]=objectivefun(X)
input data
X(1),X(2), X(3).....
try
while switch=0
[time,YALL]=ode15s(@fun1, 0:delt:tspan,Y,options);
[time,YALL]=ode15s(@fun2, 0:delt:tspan,Y,options);
[time,YALL]=ode15s(@fun3, 0:delt:tspan,Y,options);
[time,YALL]=ode15s(@fun4, 0:delt:tspan,Y,options);
[time,YALL]=ode15s(@fun5, 0:delt:tspan,Y,options);
[time,YALL]=ode15s(@fun6, 0:delt:tspan,Y,options);
if n==30
swtch=1;
end
end %(End while)
%%Nett calculations
Calculate P1 P2 P3 P4
spmd
fh = fopen(sprintf('dfile_%d.txt', labindex), 'at');
fprintf(fh, '%f %f %f %f %f %f %f %f %f %f %f\n',X(1),X(2),X(3),X(4),X(5),X(6),X(7),F1,F2,P1,P2);
fclose(fh)
end
f=zeros(2,1);
f(1)= P3
f(2)= -P4
catch
f = [2000000;2000000];
end
ode functions
end %end objective function
Alan Weiss
Alan Weiss on 16 Oct 2018
I am not sure what is wrong. It is possible that ga starts to sample points that cause the ODE solver to fail, and that this halts the process. Have you tried running in serial to see what occurs? Have you tried running the ODE solver at the points where ga seems to run into trouble?
Sorry, I am not an expert at debugging parallel programs, so I might not have the best suggestions.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!