-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSPusingGAwithRouletteWhellSelection.cpp
More file actions
323 lines (323 loc) · 9.08 KB
/
TSPusingGAwithRouletteWhellSelection.cpp
File metadata and controls
323 lines (323 loc) · 9.08 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include<bits/stdc++.h>
#define random (rand()/(double)RAND_MAX)
#define ll int64_t
#define Add push_back
using namespace std;
//=======================================================================//
//=====================Class Definition of Graph=========================//
//=======================================================================//
class Graph{
ll TotalVertices;
vector<pair<ll,ll> >Vertices; //pair of vertices between which path exists
vector<pair<ll,ll> >Edges; //first one is weight and second one is index corresponding to the vertex pair
vector<ll>OptimalPath;
ll TotalCost;
public:
Graph(ll TotalVertices){
TotalCost=0;
this->TotalVertices=TotalVertices;
}
Graph(){
this->TotalVertices=5;
addEdge(1,2,7);
addEdge(1,3,50);
addEdge(1,4,6);
addEdge(1,5,4);
addEdge(2,3,11);
addEdge(2,4,5);
addEdge(2,5,15);
addEdge(3,4,8);
addEdge(3,5,2);
addEdge(4,5,27);
}
void addEdge(ll v1,ll v2,ll weight); //given verices and edge weight betwene them, add a new entry into graph
ll getCost(vector<ll>Path); //give a path funciton will reutrn the travel cost or 0 in case path is not valid
ll getCost(); //in case the optimal path is set then return the cost of that path
ll getTotalVertices(); //return total number of vertices in the Graph
bool isPathValid(vector<ll>Path); //for checking is path is valid
void setPath(vector<ll>OptimalPath); //set the current optimal path as received from the calling function
ll getDecesionVariables();
};
//===========================================================//
//===================Class definition of Individual==========//
//===========================================================//
class Individual{
vector<ll>Gene;
ll DecesionVariables;
Graph Properties;
public:
Individual(){
Properties=Graph();
DecesionVariables=Properties.getDecesionVariables();
Gene.resize(DecesionVariables,0);
do{
Gene.clear();
Gene.resize(DecesionVariables,0);
for(ll i=0;i<Gene.size();i++){
ll vertex=(random*DecesionVariables)+1;
while(find(Gene.begin(),Gene.end(),vertex)!=Gene.end()){
vertex=(random*DecesionVariables)+1;
}
Gene[i]=vertex; }
}while(!Properties.isPathValid(Gene));
Properties.setPath(Gene);
this->Fitness=Properties.getCost();
}
ll Fitness;
ll getFitness();
vector<ll>getGene();
bool isGeneValid(vector<ll>Array);
void setGene(vector<ll>Gene);
void displayIndividual();
ll getDecesionVariables();
};
bool compare(const Individual & l,const Individual & r)
{
return l.Fitness < r.Fitness;
}
//===========================================================//
//===================Class definition of SimulateGA==========//
//===========================================================//
class SimulateGA{
vector<Individual>Population;
ll PopulationSize;
public:
SimulateGA(int PopulationSize){
this->PopulationSize=PopulationSize;
Population.resize(PopulationSize);
}
void doCrossover();
vector<ll> doMutation(vector<ll>Array);
void displayPopulation();
void displayFittest();
vector<pair<ll,ll> > RouletteWheelSelection();
};
//===========================================================//
//==============Member functions for SimulateGA Class========//
//===========================================================//
void SimulateGA::displayPopulation(){
for(int i=0;i<Population.size();i++){
cout<<endl;
Population[i].displayIndividual();
}
}
void SimulateGA::displayFittest(){
(*min_element(Population.begin(),Population.end(),compare)).displayIndividual();
}
vector<pair<ll,ll> > SimulateGA::RouletteWheelSelection(){
vector<pair<ll,ll> >MatingPair;
ll SumFitness=0;
for(ll i=0;i<PopulationSize;i++){
SumFitness+=Population[i].getFitness();
}
for(ll i=0;i<PopulationSize/2;i++){
ll r=random*SumFitness;
ll Sum=0;
ll j=0;
while(Sum<r){
Sum+=Population[j].getFitness();
j++;
}
ll pair1=--j;
r=random*SumFitness;
Sum=0;
j=0;
while(Sum<r){
Sum+=Population[j].getFitness();
j++;
}
ll pair2=--j;
MatingPair.Add(make_pair(pair1,pair2));
}
return MatingPair;
}
void SimulateGA::doCrossover(){
vector<Individual>NewPopulation;
ll range=Population[0].getDecesionVariables();
vector<pair<ll,ll> >MatingPair;
MatingPair=RouletteWheelSelection(); //call Roulette Wheel Selection function to select the mating pairs
for(ll index=0;index<MatingPair.size();index++){
Individual Parent1=Population[MatingPair[index].first];
Individual Parent2=Population[MatingPair[index].second];
vector<ll>Gene1=Parent1.getGene();
vector<ll>Gene2=Parent2.getGene();
vector<ll>Points;
Points.Add(random*Gene1.size());
Points.Add(random*Gene2.size());
sort(Points.begin(),Points.end());
vector<ll>ChildGene1(range);
vector<ll>ChildGene2(range);
for(ll i=Points[0];i<Points[1];i++){
ChildGene1[i]=Gene1[i];
ChildGene2[i]=Gene2[i];
}
for(ll i=0;i<range;i++){
if(ChildGene1[i]==0){
ll id=0;
ll flag=0;
do{
if(find(ChildGene1.begin(),ChildGene1.end(),Gene2[id])==ChildGene1.end()){
ChildGene1[i]=Gene1[id];
flag=1;
}
id++;
}while(flag==0);
}
if(ChildGene2[i]==0){
ll id=0;
ll flag=0;
do{
if(find(ChildGene2.begin(),ChildGene2.end(),Gene1[id])==ChildGene2.end()){
ChildGene1[i]=Gene1[id];
flag=1;
}
id++;
}while(flag==0);
}
}
ChildGene1=doMutation(ChildGene1);
ChildGene2=doMutation(ChildGene2);
vector<Individual>Family;
Family.Add(Parent1);
Family.Add(Parent2);
Individual Child1;
Individual Child2;
if(Child1.isGeneValid(ChildGene1)){
Child1.setGene(ChildGene1);
Family.Add(Child1);
}
if(Child1.isGeneValid(ChildGene2)){
Child1.setGene(ChildGene2);
Family.Add(Child2);
}
sort(Family.begin(),Family.end(),compare);
NewPopulation.Add(Family[0]);
NewPopulation.Add(Family[1]);
}
Population.clear();
Population=NewPopulation;
}
vector<ll> SimulateGA::doMutation(vector<ll>Array){
Individual Person;
ll r1=random*Array.size();
ll r2=random*Array.size();
vector<ll>Temp=Array;
Temp=Array;
ll t=Temp[r1];
Temp[r1]=Temp[r2];
Temp[r2]=t;
return Temp;
}
//===========================================================//
//==============Member functions for Individual Class========//
//===========================================================//
ll Individual::getFitness(){
return Fitness;
}
vector<ll> Individual::getGene(){
return Gene;
}
bool Individual::isGeneValid(vector<ll>Array){
if(Properties.isPathValid(Array)){
return true;
}
return false;
}
void Individual::setGene(vector<ll>Gene){
this->Gene=Gene;
Properties.setPath(Gene);
this->Fitness=Properties.getCost();
}
void Individual::displayIndividual(){
cout<<"Path: ";
for(int i=0;i<Gene.size();i++){
cout<<Gene[i]<<" ";
}
cout<<Gene[0]<<" "; //since last and first vertex has to be same
cout<<"Fitness: "<<Fitness;
}
ll Individual::getDecesionVariables(){
return this->DecesionVariables;
}
//===========================================================//
//================Member functions for Graph Class===========//
//===========================================================//
bool Graph::isPathValid(vector<ll>Path){
if(getCost(Path)==0){
return false;
}
return true;
}
void Graph::addEdge(ll v1,ll v2,ll weight){
Vertices.Add(make_pair(v1,v2));
Edges.Add(make_pair(weight,Vertices.size()-1));
}
ll Graph::getTotalVertices(){
return TotalVertices;
}
void Graph::setPath(vector<ll>OptimalPath){
this->OptimalPath=OptimalPath;
TotalCost=getCost(OptimalPath);
}
ll Graph::getCost(){
return TotalCost;
}
ll Graph::getCost(vector<ll>Path){
ll cost=0;
vector<ll>visited;
for(ll i=0;i<Path.size()-1;i++){
ll v1=Path[i];
ll v2=Path[i+1];
if(find(visited.begin(),visited.end(),v1)==visited.end()){
visited.Add(v1);
}
else{
return 0;
}
if(i==Path.size()-2){
if(find(visited.begin(),visited.end(),v2)==visited.end()){
visited.Add(v2);
}
else{
return 0;
}
}
if(find(Vertices.begin(),Vertices.end(),make_pair(v1,v2))<Vertices.end()){
cost+=Edges[(find(Vertices.begin(),Vertices.end(),make_pair(v1,v2))-Vertices.begin())].first;
}
else if(find(Vertices.begin(),Vertices.end(),make_pair(v2,v1))<Vertices.end()){
cost+=Edges[(find(Vertices.begin(),Vertices.end(),make_pair(v2,v1))-Vertices.begin())].first;
}
else{
//cout<<"\nNo path found! ";
return 0;
}
}
if(find(Vertices.begin(),Vertices.end(),make_pair(Path[Path.size()-1],Path[0]))<Vertices.end()){
cost+=Edges[(find(Vertices.begin(),Vertices.end(),make_pair(Path[Path.size()-1],Path[0]))-Vertices.begin())].first;
}
else if(find(Vertices.begin(),Vertices.end(),make_pair(Path[0],Path[Path.size()-1]))<Vertices.end()){
cost+=Edges[(find(Vertices.begin(),Vertices.end(),make_pair(Path[0],Path[Path.size()-1]))-Vertices.begin())].first;
}
else{
return 0;
}
return cost;
}
ll Graph::getDecesionVariables(){
return TotalVertices;
}
int main(){
SimulateGA g=SimulateGA(100);
cout<<"\n==================Initial Population==============\n";
g.displayPopulation();
g.displayFittest();
/*for(int i=0;i<10;i++){
cout<<"\n==================Generation "<<i+1<<"==============\n";
g.doCrossover();
g.displayFittest();
}*/
//g.displayPopulation();
g.displayFittest();
return 0;
}