how to build a histogram from an output with 2 vectors

I have a program that does the best selection from a file and choose the best trains to fit the weight=600 Tons and the highest nr of passengers. I want to execute the code multiple times and save output to create a histogram.
%display results:
if selection == zeros(chromosome_length, 1)
message = sprintf('GA CANNOT FIND VALID SELECTION WITH GIVEN CONSTRAINTS');
disp(message)
else
message = sprintf('OPTIMAL SELECTION OF ITEMS: [');
for i = 1:chromosome_length
if selection(i) == 1
message = sprintf('%s \n\t- %s', message, string(trains_table.Manufacturer(i)));
end
end
fprintf('%s\n ]\n', message);
fprintf('TOTAL weight OF RAILCARS: %d Tons\n', selection * trains_table.weight);
fprintf('TOTAL DAILY PASSENGERS: %d\n', selection * trains_table.daily_passengers);
end
Output is this:
Best fitness for this run = 13400
****GA Finished****
OPTIMAL SELECTION OF ITEMS: [
- Paynes Trains Limited Co.
- Motor Company of Maine
- Thomas The Engine Inc.
]
TOTAL weight OF RAILCARS: 600 Tons
TOTAL DAILY PASSENGERS: 13400

Réponses (1)

Athul Prakash
Athul Prakash le 16 Oct 2020

0 votes

Hi Heg,
It seems that you haven't posted the code where the 'ga' algorithm is running and the results are obtained.
In general, I would recommend that you save the entire code to a file as a simple function which returns these outputs.
You can call the function multiple times in a loop to run 'ga' and populate a vector with the output of each run. Once you have a vector of all the outputs, the hist() function should help you create the desired histogram.
Hope it helps!

5 commentaires

Hi Athul.Thank you for reaching me out .This is the other part of the code from begining:
clc;
clear;
% set weight_limit, read input file into variable trains_table:
weight_limit=600;
input_file= 'Train_info.csv';
trains_table=readtable(input_file);
fprintf('******** READING ITEMS FROM %s ********\n',input_file);
fprintf('WEIGHT LIMIT IS SET TO %d\n',weight_limit);
% define chromosome length and fitness function:
chromosome_length= height(trains_table);
fit_func=@(chromosome)-(chromosome*trains_table.daily_passengers);
% define A, b, Lb, Ub, int_indices:
% we need a matrice to play a role of masks so one manufacturer can be
% selected once and for this we buid a diagonal ones matrix which would be
% the same in size with chromosome length
A=vertcat(trains_table.weight',eye(chromosome_length));
b=[weight_limit, ones(1,chromosome_length)];
Lb=zeros(1,chromosome_length);
Ub=ones(1,chromosome_length);
int_indices=1:chromosome_length;
% run ga:
disp('****GA STARTING*****');
options = optimoptions('ga','display','off');
[selection, selection_fitness] = ga(fit_func,chromosome_length,A,b,...
[],[],Lb,Ub,[],int_indices);
fprintf('Best fitness for this run = %d\n', abs(selection_fitness));
disp('****GA Finished****');
Now I want to know how can I save all this functions in one.I am pretty new on genetic algorithms.I would appreciate your help.
As far as I can understand, all the input data and parameters for calling ga() seem to be static and won't change across different runs of the algorithm.
You may just loop over the call to ga, for example:
N = 10 % Number of times to run
results_selection = zeros(1,N);
results_selection_fitness = zeros(1,N);
for i=1:N
% This line from your code is put in a loop.
[selection, selection_fitness] = ga(fit_func,chromosome_length,A,b,...
[],[],Lb,Ub,[],int_indices);
results_selection = selection;
results_selection_fitness(i) = selection_fitness;
end
If you're new to Matlab or want to brush up on the basics of the language, such as functions, loops, vectors etc, I would suggest going through the Matlab OnRamp course online. It's a great resource and may get you up to speed in short time.
Thank you so much!

Connectez-vous pour commenter.

Question posée :

le 13 Oct 2020

Commenté :

le 18 Oct 2020

Community Treasure Hunt

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

Start Hunting!

Translated by