[go: up one dir, main page]

Menu

[925ebb]: / Examples / mainTSP.cpp  Maximize  Restore  History

Download this file

207 lines (162 with data), 6.2 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
// OptFrame - Optimization Framework
// Copyright (C) 2009-2015
// http://optframe.sourceforge.net/
//
// This file is part of the OptFrame optimization framework. This framework
// is free software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License v3 as published by the
// Free Software Foundation.
// This framework is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License v3 for more details.
// You should have received a copy of the GNU Lesser General Public License v3
// along with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// ===================================
// Main.cpp file generated by OptFrame
// Project Traveling Salesman Problem
// ===================================
#include <stdlib.h>
#include <math.h>
#include <iostream>
using namespace std;
#include <set>
#include "../OptFrame/Solution.hpp"
//#include "../OptFrame/Util/TestSolution.hpp"
#include "../OptFrame/Loader.hpp"
#include "../OptFrame/Util/CheckCommand.hpp"
#include "../OptFrame/Util/BuildCommand.hpp"
#include "../OptFrame/Heuristics/EvolutionaryAlgorithms/BRKGA.hpp"
#include "TSP.h"
using namespace TSP;
using namespace scannerpp;
#include "../OptFrame/Util/PackTypes.hpp"
int main(int argc, char **argv)
{
Loader<RepTSP> optframe;
TSPProblemCommand tsp;
tsp.load("./TSP/tsplib/berlin52.txt", optframe.factory, optframe.dictionary, optframe.ldictionary);
/*
FILE* outf = fopen("berlin52.mtx","w");
fprintf(outf, "%d\n", tsp.p->n);
for(unsigned i=0; i<tsp.p->n; i++) {
for(unsigned j=0; j<tsp.p->n; j++)
fprintf(outf, "%d\t",int(100*(*tsp.p->dist)(i,j)));
fprintf(outf, "\n");
}
fclose(outf);
exit(1);
*/
bool check_verbose = false;
CheckCommand<RepTSP> check(check_verbose);
RandGenMersenneTwister rg(0);
RandomInitialSolutionTSP random(tsp.p, rg);
NearestNeighborConstructive cnn(tsp.p, rg);
ConstructiveBestInsertion cbi(tsp.p, rg);
TSPEvaluator eval(tsp.p);
NSEnumSwap enumswap(tsp.p, rg);
NSSeqTSP2Opt<int, OPTFRAME_DEFAULT_ADS, DeltaMoveTSP2Opt, ProblemInstance> nsseq_delta_2opt(tsp.p);
NSSeqTSP2Opt<int> tsp2opt;
NSSeqTSPOrOptk<int, OPTFRAME_DEFAULT_ADS, DeltaMoveTSPOrOptk, ProblemInstance> nsseq_delta_or1(1, tsp.p);
NSSeqTSPOrOptk<int> tspor1(1);
NSSeqTSPOrOptk<int> tspor2(2);
NSSeqTSPOrOptk<int> tspor3(3);
NSSeqTSPSwap<int> tspswap;
check.add(random);
check.add(cnn);
check.add(cbi);
check.add(eval);
check.add(enumswap);
check.add(nsseq_delta_2opt);
check.add(tsp2opt);
check.add(nsseq_delta_or1);
check.add(tspor1);
check.add(tspor2);
check.add(tspor3);
check.add(tspswap);
//check.run(100, 10);
cout << "will test BRKGA (n=" << tsp.p->n << ")" << endl;
EvaluatorPermutationRandomKeys eprk(eval, 0, tsp.p->n-1);
BRKGA<RepTSP> brkga(eprk, tsp.p->n, 10000, 10, 0.4, 0.3, 0.6);
pair<Solution<random_keys>&, Evaluation&>* r2 = brkga.search();
r2->first.print();
pair<Evaluation&, Solution<vector<int>>*> pd = eprk.decode(r2->first.getR());
pd.second->print();
if(eval.verify(pd.second->getR()))
cout << "CHECK: OK" << endl;
pd.first.print();
delete &pd.first;
delete pd.second;
r2->second.print();
delete &r2->first;
delete &r2->second;
delete r2;
cout << "end BRKGA tests" << endl;
BuildCommand<RepTSP> build;
for (unsigned i = 0; i <= 7; i++)
{
stringstream ss;
ss << "OptFrame:ComponentBuilder:LocalSearch:BI OptFrame:Evaluator 0 OptFrame:NS:NSSeq " << i;
string name = build.run(optframe.factory, optframe.dictionary, optframe.ldictionary, ss.str());
cout << "BUILT: '" << name << "'" << endl;
}
vector<LocalSearch<RepTSP>*> ns_list;
ns_list.push_back(new BestImprovement<RepTSP>(eval, tsp2opt));
ns_list.push_back(new BestImprovement<RepTSP>(eval, tspor1));
ns_list.push_back(new BestImprovement<RepTSP>(eval, tspor2));
ns_list.push_back(new BestImprovement<RepTSP>(eval, tspor3));
ns_list.push_back(new BestImprovement<RepTSP>(eval, tspswap));
for(unsigned i=0; i<ns_list.size(); i++)
ns_list[i]->setVerbose();
VariableNeighborhoodDescent<RepTSP> VND(eval, ns_list);
VND.setVerbose();
ILSLPerturbationLPlus2<RepTSP> pert(eval, tsp2opt, rg);
pert.add_ns(tspor1);
pert.add_ns(tspor2);
pert.add_ns(tspor3);
pert.add_ns(tspswap);
IteratedLocalSearchLevels<RepTSP> ils(eval, random, VND, pert, 3, 2);
//ils.setMessageLevel(4);
ils.setVerbose();
if (ils.information)
cout << "infomation is on for ILS" << endl;
cout << "will run ils" << endl;
Timer tim;
pair<Solution<RepTSP>&, Evaluation&>& psol = *ils.search(1000, 0, NULL, NULL);
cout << tim.now() << " secs" << endl;
psol.first.print();
psol.second.print();
// ===========
for(unsigned i=0; i<ns_list.size(); i++)
delete ns_list[i];
vector<NS<RepTSP>*> v_ns;
vector<NSSeq<RepTSP>*> v_nsseq;
v_nsseq.push_back(&tsp2opt);
v_nsseq.push_back(&tspor1);
v_nsseq.push_back(&tspor2);
v_nsseq.push_back(&tspor3);
v_nsseq.push_back(&tspswap);
for(unsigned i=0; i<v_nsseq.size(); i++)
v_ns.push_back(v_nsseq[i]);
BasicVNS<RepTSP> vns(eval, random, v_ns, v_nsseq);
vns.setMessageLevel(3); // INFORMATION
pair<Solution<RepTSP>&, Evaluation&>& psol2 = *vns.search(0, 8000, NULL, NULL);
psol2.first.print();
psol2.second.print();
// Remember the old times...
/*
echo building VND
define vnd_list [ OptFrame:LocalSearch: 0 , OptFrame:LocalSearch: 1, OptFrame:LocalSearch: 2, OptFrame:LocalSearch: 3 ]
component.create_list $vnd_list OptFrame:LocalSearch: comp_vnd_list
build OptFrame:LocalSearch:VND $Evaluator 0 $comp_vnd_list vnd
%component.list
echo building ILS
build OptFrame:ComponentBuilder:SingleObjSearch:ILS:ILSLevels $Evaluator 0 $Constructive 0 $vnd OptFrame:ILS:LevelPert:LPlus2 0 100 8 meu_ils
test 2 3 7000 7000 $Evaluator 0 $meu_ils output.txt solucao_saida
evaluate $Evaluator 0 $solucao_saida
*/
cout << "Program ended successfully" << endl;
return 0;
}