-
Notifications
You must be signed in to change notification settings - Fork 0
/
routeSetMutation.h
232 lines (197 loc) · 6.3 KB
/
routeSetMutation.h
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
/*
* File: routeSetMutation.h
* Author: MAN
*
* Created on September 8, 2013, 9:02 PM
*/
#ifndef _ROUTESETMUTATION_H
#define _ROUTESETMUTATION_H
#define FRONT 0
#define BACK 1
//#include <eoOp.h>
//#include<utils/eoRNG.h>
#include "route.h"
#include "inputData.h"
template<class GenotypeT>
int rouletteWheelForRoute(GenotypeT & _genotype, eoEvalFuncPtr< Route<double> >& _eval)
{
std::vector <double> fits;
double total = 0;
for (int i = 0; i < _genotype.size(); i++)
{
_eval(_genotype[i]);
total += _genotype[i].fitness();
fits.push_back(_genotype[i].fitness());
}
return rng.roulette_wheel(fits, total);
}
int rouletteWheelForPath(int startIndex)
{
std::vector <double> fits;
double total = 0;
for (int i = 0; i < VERTICES_NO; i++)
{
if (startIndex == i)
{
fits.push_back(0);
continue;
}
total += DS[startIndex][i];
fits.push_back(DS[startIndex][i]);
}
return rng.roulette_wheel(fits, total);
}
int rouletteWheelForNode( int startIndex,std::vector<int> AdjList)
{
if(AdjList.size() == 1)
return 0;
std::vector <double> fits;
double total = 0;
double fit;
for(int i=0;i<AdjList.size();i++)
{
fit = 1/( 1+p[startIndex][AdjList[i]] );
fits.push_back(fit);
total += fit;
}
return rng.roulette_wheel(fits, total);
}
template<class GenotypeT>
class SmallMutation : public eoMonOp<GenotypeT>
{
public:
/**
* Ctor - no requirement
*/
// START eventually add or modify the anyVariable argument
SmallMutation(float _pDelete, eoEvalFuncPtr< Route<double> >& _eval) : pDelete(_pDelete), eval(_eval)
{
// START Code of Ctor of an SmallMutation object
// END Code of Ctor of an SmallMutation object
}
/// The class name. Used to display statistics
std::string className() const
{
return "SmallMutation";
}
/**
* modifies the parent
* @param _genotype The parent genotype (will be modified)
*/
bool operator()(GenotypeT & _genotype)
{
// START code for mutation of the _genotype object
int mutIndex = rouletteWheelForRoute(_genotype, eval);
Route<double>& mutRoute = _genotype[mutIndex];
// std::cout << mutRoute << std::endl;
std::list<int>::iterator it[2], nIt;
it[FRONT] = mutRoute.mutableR().begin();
it[BACK] = mutRoute.mutableR().end();
it[BACK]--; //points to last elem
int selectedEnd = rng.flip();
nIt = it[selectedEnd];
int selectedNode = *it[selectedEnd];
if (pDelete >= rng.uniform()) //delete a terminal
{
if (mutRoute.mutableR().size() < 3) //path is too small, no deletion just return
return false;
mutRoute.erase(it[selectedEnd]);
mutRoute.nodeList[selectedNode] = 0;
}
else //append a terminal
{
vector<int> AdjListForSelected = AdjList[selectedNode];
for (vector<int>::iterator iit = AdjListForSelected.begin(); iit != AdjListForSelected.end(); iit++)
{
if (mutRoute.nodeList[*iit] == 1)
{
AdjListForSelected.erase(iit);
--iit;
}
}
if (AdjListForSelected.size() == 0)
return false;
int neighbor;
if (selectedEnd == FRONT)
{
nIt++;
neighbor = *nIt;
}
else
{
nIt--;
neighbor = *nIt;
it[BACK]++; //again points to end
}
int toAddIndex;
// toAddIndex = rng.random(AdjListForSelected.size());
toAddIndex = rouletteWheelForNode(selectedNode,AdjListForSelected);
mutRoute.insert(it[selectedEnd], AdjListForSelected[toAddIndex]);
mutRoute.nodeList[AdjListForSelected[toAddIndex]] = 1;
}
mutRoute.invalidate();
return true;
// END code for mutation of the _genotype object
}
private:
// START Private data of an SmallMutation object
// varType anyVariable; // for example ...
// END Private data of an SmallMutation object
float pDelete;
eoEvalFuncPtr< Route<double> >& eval;
};
template<class GenotypeT>
class BigMutation : public eoMonOp<GenotypeT>
{
public:
/**
* Ctor - no requirement
*/
// START eventually add or modify the anyVariable argument
BigMutation(eoEvalFuncPtr< Route<double> >& _eval) : eval(_eval)
// BigMutation( varType _anyVariable) : anyVariable(_anyVariable)
// END eventually add or modify the anyVariable argument
{
// START Code of Ctor of an BigMutation object
// END Code of Ctor of an BigMutation object
}
/// The class name. Used to display statistics
string className() const
{
return "BigMutation";
}
/**
* modifies the parent
* @param _genotype The parent genotype (will be modified)
*/
bool operator()(GenotypeT & _genotype)
{
// START code for mutation of the _genotype object
int mutIndex = rouletteWheelForRoute(_genotype, eval);
Route<double>& mutRoute = _genotype[mutIndex];
//std::cout<<_genotype[mutIndex]<<std::endl;
int terminal[2];
terminal[0] = mutRoute.mutableR().front();
terminal[1] = mutRoute.mutableR().back();
int startNode = terminal[rng.flip()];
if(startNode > VERTICES_NO)
{
cout<<mutRoute<<endl;
}
int endNode = rouletteWheelForPath(startNode);
mutRoute.setR(sPath[startNode][endNode]);
vector<int> newNodeList(VERTICES_NO, 0);
for (list<int>::iterator lit = sPath[startNode][endNode].begin(); lit != sPath[startNode][endNode].end(); lit++)
{
newNodeList[*lit] = 1;
}
mutRoute.setNodeList(newNodeList);
mutRoute.invalidate();
return true;
// END code for mutation of the _genotype object
}
private:
// START Private data of an BigMutation object
eoEvalFuncPtr< Route<double> >& eval; // END Private data of an BigMutation object
};
#endif /* _ROUTESETMUTATION_H */