-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic.cpp
More file actions
151 lines (118 loc) · 3.68 KB
/
genetic.cpp
File metadata and controls
151 lines (118 loc) · 3.68 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
#include <iostream>
#include <string>
#include "genetic.h"
Genetic::Genetic(int cities,int toursgen,int genrun,double percentmut)
{
rowlimit=cities-1;
tourspergen=toursgen;
gentorun=genrun;
numbermut = (percentmut/100)*toursgen;
numberperm =toursgen-numbermut-2;// this is the number of permutations which will be toursgen-numbermut-2
numberelite=2 ;// this is 2
//fill up vector with first set of permutations.
Permutation object(rowlimit);
p=object;
//p.initS(length);
//
int* ptr =p.mkcp(rowlimit);
for(int i = 0; i < toursgen; i++)
{
// p.printS();
myvector.insert(myvector.begin(),ptr);
p.perm1();
ptr=p.mkcp(rowlimit);
}
}
double Genetic::geneticalg(double s[][20])
{
//intiially check all of the permutations in our vector
//keep track of the two best orders
scanforelite(s);
int* copy1 = myvector.at(firstindex);
int* copy2 =myvector.at(secondindex);
myvector.clear(); // we clear the vector because we have our two best tours from the first gen
int* ptr =p.mkcp(rowlimit);
for(int i = 0; i < gentorun; i++)
{
// insert two elites
myvector.insert(myvector.begin(),copy1);
myvector.insert(myvector.begin(),copy2);
// j look adds the additional number of permutations to generation
for(int j=0; j< numberperm; j++)
{
// p.printS();
myvector.insert(myvector.begin(),ptr);
p.perm1();
ptr=p.mkcp(rowlimit);
}
//this will add numbermut mutations to the vector and the utations are generated with the elites..
mutateadd(s,copy1,copy2, numbermut);
// now after everything is added again to the generation we can scan it and pick out are two elites and repeat the process
scanforelite(s);
copy1=myvector.at(firstindex);
copy2=myvector.at(secondindex);
myvector.clear();
}
return solution;
//now firstindex and secondindex are the the two elite orders and we will perform mutations on these
}
void Genetic::scanforelite(double s[][20])
{
int firstindex , secondindex;
int* ptr;
double newcost=0, mincost1,mincost2;
for(int i=0; i < myvector.size();i++)
{
ptr=myvector.at(i);
newcost=s[0][ptr[0]];
for(int j=1; j<=rowlimit ;j++)
{
int prev = ptr[j-1],next=ptr[j];
if(j==rowlimit)
{next=0;}
newcost=newcost+s[prev][next];
}
if(mincost1>newcost)
{mincost1=newcost;
firstindex=i;
solution=mincost1;
}
else
{ if(mincost2>newcost)
{mincost2=newcost;
secondindex=i;
}
}
//if its the first iteration initialize mincost to the first
if(i==0)
{mincost1=newcost;
mincost2=newcost;
firstindex=i;
secondindex=i;
}
}
this->firstindex=firstindex;
this->secondindex=secondindex;
}
void Genetic::mutateadd(double s[][20], int* elite1 , int* elite2 , int amount)
{
//this function swaps and adds basically making mutations ...
//so this needs to be adjusted to get better approixmations at larger numbers of cities
for(int i=0 ; i<amount/2 ; i++)
{
myvector.insert(myvector.begin(),swapcopy(i%rowlimit,(3*i)%rowlimit,elite1));
myvector.insert(myvector.begin(),swapcopy(i%rowlimit,(3*i)%rowlimit,elite2));
}
}
int* Genetic::swapcopy(int i, int j, int* elite)
{
int* temp=new int[20];
for(int i=0; i<rowlimit ; i++)
{
temp[i]=elite[i];
}
int c = temp[i];
temp[i] = temp[j];
temp[j] = c;
return temp;
}