-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
108 lines (95 loc) · 2.76 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
#include <iostream>
#include "graph.h"
#include "representation.h"
#include "population.h"
#include "randomgenerator.h"
#include "graphEvaluator.h"
#include "csvbuilder.h"
using namespace std;
void logPopulation(ofstream &fout,string instanceName,CPopulation &pop,Graph &graph,time_t start,int run)
{
GraphEvaluator evaluator;
int bestFitIdx=0;
float bestFit=evaluator.ComputeFitness(graph,*pop.Get(0));
float meanFit=evaluator.ComputeFitness(graph,*pop.Get(0));
int worstFitIdx=0;
float worstFit=evaluator.ComputeFitness(graph,*pop.Get(0));
float sum=0;
for(int i=0;i<pop.GetPopSize();i++)
{
auto el=pop.Get(i);
auto fit=evaluator.ComputeFitness(graph,*el);
sum+=fit;
if(bestFit>fit)
{
bestFitIdx=i;
bestFit=fit;
}
if(worstFit<fit)
{
worstFitIdx=i;
worstFit=fit;
}
}
meanFit=sum/pop.GetCount();
fout<<run<<","<<instanceName<<","<<pop.GetGeneration()<<","<<bestFit<<","<<meanFit<<","<<worstFit<<","<<time(NULL)-start<<"\n";
///cout<<bestFitIdx<<"\n";
///list<int> decode= CRepresentation::Decode(*pop.Get(bestFitIdx));
///for (int i=0;i<pop.Get(bestFitIdx)->GetCount();i++)
///{
/// cout<<pop.Get(bestFitIdx)->Get(i)<<" ";
///}
///cout<<"\n";
///for(auto el:decode)
///{
/// cout<<el<<" ";
///}
///cout<<"\n";
}
int main(int argc,char* argv[])
{
srand(time(NULL));
string path = "./ALL_tsp/"+string(argv[1]);
int popSize=20;
int MaxGenerations=10000;
float m_rate=0.01;
float c_rate=1;
time_t start=time(NULL);
Graph graph(path);
graph.printGraph();
GraphEvaluator evaluator;
string name=string(argv[1]);
name.pop_back();
name.pop_back();
name.pop_back();
name.pop_back();
ofstream fout(name+".csv");
fout<<"run,name, generation, min, mean, max, time ,\n";
float best=999999;
for(int i=0;i<MaxGenerations;i++)
{
CRepresentation c(true,graph.number_of_vertexes_);
if(evaluator.ComputeFitness(graph,c)<best)
{
cout<<evaluator.ComputeFitness(graph,c)<<"\n";
best=evaluator.ComputeFitness(graph,c);
}
}
cout<<"Random search best: "<<best<<"\n";
cin>>best;
for(int run=1;run<=1;run++)
{
///std::cout <<"run: "<< run << '\n';
CPopulation pop(popSize,graph.number_of_vertexes_,graph,evaluator,true,0.05,0.02);
for(int i=0;i<=MaxGenerations;i++)
{
if(i%100==0)
{
std::cout<<" Generation: "<<pop.GetGeneration()<<"\n";
logPopulation(fout, name, pop,graph,start,run);
}
pop.Evolve(false);
}
}
return 0;
}