-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
153 lines (124 loc) · 4.96 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <fstream>
#include <numeric>
#include <algorithm>
#include <cmath> // std::sqrt()
#include "ttp/ttpga.hpp"
#include "ttp/ttpindividual.hpp"
#include "ttp/ttpinstance.hpp"
#include "utils/individualrecorder.hpp"
#include "utils/geneticutils.hpp"
enum ErrorTypes
{
NUM_ARGS = -1,
EXCEPTION = -2
};
void printHelp()
{
std::cout << "Usage: GA <TTP-instance-file>"
<< " <TTPGA-config-file>"
<< " <number-of-executions>"
<< " [<log-output-directory> [<standard-output-file>]]" << std::endl;
}
int main( int argc, char *argv[] )
{
if( argc < 4 )
{
printHelp();
return ErrorTypes::NUM_ARGS;
}
std::streambuf* stcout = std::cout.rdbuf();
std::ofstream coutFile;
if( argc >= 6 )
{
try
{
coutFile = std::ofstream( argv[ 5 ], std::ios::app );
std::cout.rdbuf( coutFile.rdbuf() );
}
catch( std::exception& e )
{
std::cerr << e.what() << std::endl;
return ErrorTypes::EXCEPTION;
}
}
unsigned numExecs = 0;
try
{
numExecs = std::stoi( argv[ 3 ] );
}
catch( std::exception& e )
{
std::cerr << e.what() << std::endl;
return ErrorTypes::EXCEPTION;
}
std::cout << "\n\n===============================" << std::endl
<< "-------- GA INITIATED ---------" << std::endl;
TTPGA ga;
std::vector< TTPIndividual > bestIndividuals;
bestIndividuals.reserve( numExecs );
for( unsigned i = 1; i <= numExecs; i++ )
{
std::cout << "\n===============================" << std::endl
<< "----- EXECUTION " << i << " STARTED -----" << std::endl;
std::cout << "Date and time: " << IndividualRecorder::getSuffixDateTime() << std::endl;
try
{
TTPGAConfig gaC = TTPGAConfig::readTTPGAConfigFromFile( argv[ 2 ] );
ga.gaConfig = gaC;
TTPInstance problem( argv[ 1 ] );
std::cout << "Problem: " << problem.probFileName << std::endl;
std::cout << gaC.toString() << std::endl;
if( argc >= 5 )
{
ga.outputDirectory = argv[ 4 ];
}
ga.problem = problem;
ga.run();
TTPIndividual ind = ga.getBestNIndividuals( 1 )[ 0 ];
bestIndividuals.push_back( ind );
std::cout << "Best individual: " << ind.toString() << std::endl;
std::cout << "Total execution time: " << std::chrono::duration_cast< std::chrono::milliseconds >
( ga.executionTime ).count()
<< " milliseconds" << std::endl;
std::cout << "Number of processed generations: " << ga.numProcGens << std::endl;
}
catch( std::exception& e )
{
std::cerr << e.what() << std::endl;
return ErrorTypes::EXCEPTION;
}
std::cout << "----- EXECUTION " << i << " DONE ------" << std::endl
<< "===============================" << std::endl;
}
std::vector< double > fitnessOfTheBests;
fitnessOfTheBests.reserve( bestIndividuals.size() );
for( TTPIndividual& ind : bestIndividuals )
{
fitnessOfTheBests.push_back( ind.fitness );
}
double sumFits = std::accumulate( std::begin( fitnessOfTheBests ), std::end( fitnessOfTheBests ), 0.0 );
double meanFits = sumFits / fitnessOfTheBests.size();
double accum = 0.0;
std::for_each( std::begin( fitnessOfTheBests ), std::end( fitnessOfTheBests ), [ & ]( const double d )
{
accum += ( d - meanFits ) * ( d - meanFits );
});
double stdevFits = std::sqrt( accum / ( fitnessOfTheBests.size() - 1 ) );
std::cout << "\nFitness of the best individuals: " << std::endl;
for( std::size_t i = 1; i <= bestIndividuals.size(); i++ )
{
std::cout << " Execution " << i << ": " << bestIndividuals[ i - 1 ].fitness << std::endl;
}
std::cout << "\nFitness of the best individual of all executions: " << GeneticUtils::getBestNIndividuals( bestIndividuals, 1 )[ 0 ].fitness << std::endl;
std::cout << "Fitness of the worst individual of all executions: " << GeneticUtils::getWorstNIndividuals( bestIndividuals, 1 )[ 0 ].fitness << std::endl;
std::cout << "Average fitness: " << meanFits << std::endl;
std::cout << "Sample standard deviation: " << stdevFits << std::endl;
std::cout << "\n---------- GA DONE ----------" << std::endl
<< "===============================" << std::endl;
std::cout.rdbuf( stcout );
return 0;
}