71 { 0, 28, 57, 72, 81, 85, 80, 113, 89, 80 },
72 { 28, 0, 28, 45, 54, 57, 63, 85, 63, 63 },
73 { 57, 28, 0, 20, 30, 28, 57, 57, 40, 57 },
74 { 72, 45, 20, 0, 10, 20, 72, 45, 20, 45 },
75 { 81, 54, 30, 10, 0, 22, 81, 41, 10, 41 },
76 { 85, 57, 28, 20, 22, 0, 63, 28, 28, 63 },
77 { 80, 63, 57, 72, 81, 63, 0, 80, 89, 113 },
78 { 113, 85, 57, 45, 41, 28, 80, 0, 40, 80 },
79 { 89, 63, 40, 20, 10, 28, 89, 40, 0, 40 },
80 { 80, 63, 57, 45, 41, 63, 113, 80, 40, 0 }
128int main(
int argc,
char ** argv ) {
133 boost::graph_traits<Graph>::vertex_iterator v, v_end;
134 for( boost::tie(v,v_end) = boost::vertices(g); v != v_end; ++v )
135 boost::put(boost::vertex_color_t(), g, *v, ( boost::format(
"City_%1%" ) % *v ).str() );
137 WeightMap weightMap = boost::get( boost::edge_weight, g );
138 ColorMap colorMap = boost::get( boost::edge_color, g );
142 bool inserted =
false;
144 RealMatrix costMatrix( 10, 10 );
145 for( std::size_t i = 0; i < costMatrix.size1(); i++ ) {
146 for( std::size_t j = 0; j < costMatrix.size1(); j++ ) {
148 if( i == j )
continue;
150 costMatrix(i,j) =
cities[i][j];
153 boost::tie( e, inserted ) = boost::add_edge( i, j, g );
156 weightMap[ e ] =
cities[i][j];
158 colorMap[ e ] =
"blue";
168 TspTourLength ttl( costMatrix );
172 const std::size_t mu = 100;
174 const std::size_t lambda = 100;
183 std::iota( t.begin(),t.end(), 0 );
186 for( std::size_t i = 0; i < parents.size(); i++ ) {
187 parents[i].searchPoint() = t;
191 parents[i].penalizedFitness() = parents[i].unpenalizedFitness() = ttl( parents[i].
searchPoint() );
195 while( ttl.evaluationCounter() < 10000 ) {
196 RealVector selectionProbabilities(parents.size());
197 for(std::size_t i = 0; i != parents.size(); ++i){
198 selectionProbabilities(i) = parents[i].unpenalizedFitness();
200 selectionProbabilities/= sum(selectionProbabilities);
202 for( std::size_t i = 0; i < offspring.size() - 1; i+=2 ) {
205 offspring[ i ] = *rws(
random::globalRng, parents.begin(), parents.end(), selectionProbabilities );
206 offspring[ i+1 ] = *rws(
random::globalRng, parents.begin(), parents.end(), selectionProbabilities );
210 offspring[ i ].penalizedFitness() =
211 offspring[ i ].unpenalizedFitness() = ttl( offspring[ i ].
searchPoint() );
213 offspring[ i+1 ].penalizedFitness() =
214 offspring[ i+1 ].unpenalizedFitness() = ttl( offspring[ i+1 ].
searchPoint() );
218 std::swap( parents, offspring );
225 Tour final = parents.front().searchPoint();
228 bool extracted =
false;
229 for( std::size_t i = 0; i <
final.size() - 1; i++ ) {
230 boost::tie( e, extracted ) = boost::edge(
Vertex(
final( i ) ),
Vertex(
final( i + 1 ) ), g );
233 colorMap[ e ] =
"green";
238 std::vector< Vertex > approxTour;
239 boost::metric_tsp_approx( g, boost::make_tsp_tour_len_visitor( g, std::back_inserter( approxTour ) , len, weightMap ) );
241 for( std::size_t i = 0; i < approxTour.size() - 1; i++ ) {
242 boost::tie( e, extracted ) = boost::edge( approxTour[ i ], approxTour[ i+1 ], g );
245 colorMap[ e ] =
"red";
249 std::ofstream outGraphviz(
"graph.dot" );
250 boost::dynamic_properties dp;
251 dp.property(
"node_id", boost::get( boost::vertex_color, g ) );
252 dp.property(
"weight", boost::get( boost::edge_weight, g ) );
253 dp.property(
"label", boost::get( boost::edge_weight, g ) );
254 dp.property(
"color", boost::get( boost::edge_color, g ) );
255 boost::write_graphviz_dp( outGraphviz, g, dp );
258 std::cout << parents.front().searchPoint() <<
" -> " << parents.front().unpenalizedFitness() << std::endl;
260 std::cout <<
"Approx: " << len <<
" vs. GA: " << parents.front().unpenalizedFitness() << std::endl;