@@ -30,11 +30,13 @@ int main(int argc, char** argv) {
30
30
// Problem Context
31
31
// //////////////////////////////////////////////////////////////////////////
32
32
33
- sref<RandGen> rg = RandGenMersenneTwister (); // declara um bom gerador de números aleatórios
33
+ sref<RandGen> rg =
34
+ RandGenMersenneTwister (); // declara um bom gerador de números aleatórios
34
35
35
36
Scanner scanner (File (" instance-4.txt" )); // carrega arquivo no disco
36
- ProblemInstance p (scanner); // cria um problema-teste usando o arquivo carregado
37
- MyEvaluator ev (p); // cria o avaliador baseado no problema-teste
37
+ ProblemInstance p (
38
+ scanner); // cria um problema-teste usando o arquivo carregado
39
+ MyEvaluator ev (p); // cria o avaliador baseado no problema-teste
38
40
StopCriteria<EvaluationKP> sosc;
39
41
40
42
ConstructiveRandom c1 (p, ev); // construtor de vizinhanças aleatórias
@@ -46,38 +48,46 @@ int main(int argc, char** argv) {
46
48
// //////////////////////////////////////////////////////////////////////////
47
49
cout << " \n Will generate random solution" << endl;
48
50
49
- // SolutionKP s = *c1.generateSolution(10); // timelimit (10???)
50
- op<ESolutionKP> opse = c1.initialSearch (StopCriteria<EvaluationKP>(10 )).first ; // timelimit (10???)
51
+ // SolutionKP s = *c1.generateSolution(10); // timelimit (10???)
52
+ op<ESolutionKP> opse = c1.initialSearch (StopCriteria<EvaluationKP>(10 ))
53
+ .first ; // timelimit (10???)
51
54
ESolutionKP& se = *opse;
52
55
std::cout << " solution: " << opse->first << std::endl;
53
56
ev.reevaluate (se);
54
57
se.second .print ();
55
58
cout << " GUD\n\n " ; // qq é isso Igor?
56
59
57
- // CheckCommand not working anymore... must CLEAN it!! not accepting R, ADS, ... simplify it, please!
60
+ // CheckCommand not working anymore... must CLEAN it!! not accepting R, ADS,
61
+ // ... simplify it, please!
58
62
59
63
/*
60
- CheckCommand<RepKP, MY_ADS, SolutionKP> check; // cria o módulo de testes (opcional)
61
- check.add(ev1); // carrega a avaliação para testes
64
+ CheckCommand<RepKP, MY_ADS, SolutionKP> check; // cria o módulo de testes
65
+ (opcional) check.add(ev1); // carrega a avaliação para testes
62
66
check.add(c1); // carrega o construtivo para testes
63
67
check.add(ns1); // carrega a vizinhança para testes
64
68
check.run(100, 10); // executa testes com 10 iterações
65
69
*/
66
70
67
- // BasicSimulatedAnnealing<ESolutionKP> sa(ev, c1, *nsseq_bit, 0.98, 100, 900.0, rg);
71
+ // BasicSimulatedAnnealing<ESolutionKP> sa(ev, c1, *nsseq_bit, 0.98, 100,
72
+ // 900.0, rg);
68
73
/*
69
- std::function<bool(const op<ESolutionKP>&, BasicSimulatedAnnealing<ESolutionKP, EvaluationKP, Component>*)> specificStopBy =
70
- [](const op<ESolutionKP>& se, BasicSimulatedAnnealing<ESolutionKP, EvaluationKP, Component>* m) -> bool {
71
- return ((m->getT() > 0.001) && (m->getTimer().now() < 120)); // 120 seconds and freezing 0.001
74
+ std::function<bool(const op<ESolutionKP>&,
75
+ BasicSimulatedAnnealing<ESolutionKP, EvaluationKP, Component>*)>
76
+ specificStopBy =
77
+ [](const op<ESolutionKP>& se, BasicSimulatedAnnealing<ESolutionKP,
78
+ EvaluationKP, Component>* m) -> bool { return ((m->getT() > 0.001) &&
79
+ (m->getTimer().now() < 120)); // 120 seconds and freezing 0.001
72
80
};
73
81
74
82
auto soscSA { StopCriteria(specificStopBy) };
75
83
*/
76
84
77
85
/*
78
- SpecificMethodStop<ESolutionKP, EvaluationKP, BasicSimulatedAnnealing<ESolutionKP>> spc {
79
- [&](const ESolutionKP& best, const StopCriteria<EvaluationKP>& sosc, BasicSimulatedAnnealing<ESolutionKP>* me) -> bool {
80
- return ((me->getT() > 0.001) && (me->getTimer().now() < 120)); // 120 seconds and freezing 0.001
86
+ SpecificMethodStop<ESolutionKP, EvaluationKP,
87
+ BasicSimulatedAnnealing<ESolutionKP>> spc {
88
+ [&](const ESolutionKP& best, const StopCriteria<EvaluationKP>& sosc,
89
+ BasicSimulatedAnnealing<ESolutionKP>* me) -> bool { return ((me->getT() >
90
+ 0.001) && (me->getTimer().now() < 120)); // 120 seconds and freezing 0.001
81
91
}
82
92
};
83
93
*/
@@ -86,20 +96,24 @@ int main(int argc, char** argv) {
86
96
// Simulated Annealing
87
97
// //////////////////////////////////////////////////////////////////////////
88
98
89
- sref<GeneralEvaluator<ESolutionKP>> _evaluator = ev ;
90
- sref<InitialSearch<ESolutionKP>> _constructive = c1 ;
99
+ sref<GeneralEvaluator<ESolutionKP>> _evaluator = sref_copy (ev) ;
100
+ sref<InitialSearch<ESolutionKP>> _constructive = sref_copy (c1) ;
91
101
sref<NS<ESolutionKP>> _ns1 = new NSSeqBitFlip(p, rg);
102
+ sref<NSSeq<ESolutionKP>> _nsseq1 = new NSSeqBitFlip(p, rg);
92
103
sref<NS<ESolutionKP>> _neighbors = _ns1;
93
104
double _alpha = 0.98 ;
94
105
int _SAmax = 100 ;
95
106
double _Ti = 900.0 ;
96
- BasicSimulatedAnnealing<ESolutionKP> sa (_evaluator, _constructive, _neighbors, _alpha, _SAmax, _Ti, rg);
107
+ BasicSimulatedAnnealing<ESolutionKP> sa (_evaluator, _constructive, _neighbors,
108
+ _alpha, _SAmax, _Ti, rg);
97
109
98
110
/*
99
- sa.specificStopBy =
100
- SpecificMethodStop<ESolutionKP, EvaluationKP, BasicSimulatedAnnealing<ESolutionKP>>(
101
- [&](const ESolutionKP& best, const StopCriteria<EvaluationKP>& sosc, BasicSimulatedAnnealing<ESolutionKP>* me) -> bool {
102
- return ((me->getT() > 0.001) && (me->getTimer().now() < 120)); // 120 seconds and freezing 0.001
111
+ sa.specificStopBy =
112
+ SpecificMethodStop<ESolutionKP, EvaluationKP,
113
+ BasicSimulatedAnnealing<ESolutionKP>>(
114
+ [&](const ESolutionKP& best, const StopCriteria<EvaluationKP>& sosc,
115
+ BasicSimulatedAnnealing<ESolutionKP>* me) -> bool { return ((me->getT() >
116
+ 0.001) && (me->getTimer().now() < 120)); // 120 seconds and freezing 0.001
103
117
}
104
118
);
105
119
*/
@@ -111,7 +125,7 @@ int main(int argc, char** argv) {
111
125
SearchOutput<ESolutionKP> sout = sa.search (sosc); // Faz a busca, de fato
112
126
op<ESolutionKP> r = *sout.best ;
113
127
114
- // r->first.print();
128
+ // r->first.print();
115
129
std::cout << r->first << std::endl;
116
130
r->second .print ();
117
131
@@ -120,40 +134,45 @@ int main(int argc, char** argv) {
120
134
// //////////////////////////////////////////////////////////////////////////
121
135
std::cout << " \n Hill Climbing Search:" ;
122
136
123
- BestImprovement<ESolutionKP> bi (ev, ns1 );
124
- FirstImprovement<ESolutionKP> fi (ev, ns1 );
125
- HillClimbing<ESolutionKP> sd (ev, bi );
126
- HillClimbing<ESolutionKP> pm (ev, fi );
127
- RandomDescentMethod<ESolutionKP> rdm (ev, ns1 , 10 );
137
+ BestImprovement<ESolutionKP> bi (_evaluator, _nsseq1 );
138
+ FirstImprovement<ESolutionKP> fi (_evaluator, _nsseq1 );
139
+ HillClimbing<ESolutionKP> sd (_evaluator, sref_copy (bi) );
140
+ HillClimbing<ESolutionKP> pm (_evaluator, sref_copy (fi) );
141
+ RandomDescentMethod<ESolutionKP> rdm (_evaluator, _ns1 , 10 );
128
142
//
129
- // pair<SolutionKP, Evaluation<>> se(s, e);
143
+ // pair<SolutionKP, Evaluation<>> se(s, e);
130
144
std::cout << " Best Improvement Search: " ;
131
145
sd.lsearch (se, sosc).second .print (); // executa e imprime HC + BI
132
146
std::cout << " First Improvement Search: " ;
133
147
pm.lsearch (se, sosc).second .print (); // executa e imprime HC + FI
134
148
std::cout << " Random Descent Search: " ;
135
- rdm.lsearch (se, sosc).second .print (); // executa e imprime RDM com 10 iterações
149
+ rdm.lsearch (se, sosc)
150
+ .second .print (); // executa e imprime RDM com 10 iterações
136
151
137
- // EvaluatorSubsetRandomKeys<EvaluationKP, double, ESolutionKP> eprk(ev1, 0, p.N - 1);
138
- // EvaluatorSubsetRandomKeys<SolutionKP> eprk(ev1, 0, p.N - 1);
139
- // Evaluator<std::vector<double>, EvaluationKP, ESolutionKP>& _eprk = eprk;
140
- // sref<DecoderRandomKeys<SolutionKP, EvaluationKP, double>> _decoder =
141
- // *new DecoderRandomKeysEvaluator<SolutionKP, EvaluationKP, double, ESolutionKP>(_eprk);
152
+ // EvaluatorSubsetRandomKeys<EvaluationKP, double, ESolutionKP> eprk(ev1, 0,
153
+ // p.N - 1);
154
+ // EvaluatorSubsetRandomKeys<SolutionKP> eprk(ev1, 0, p.N - 1);
155
+ // Evaluator<std::vector<double>, EvaluationKP, ESolutionKP>& _eprk = eprk;
156
+ // sref<DecoderRandomKeys<SolutionKP, EvaluationKP, double>> _decoder =
157
+ // *new DecoderRandomKeysEvaluator<SolutionKP, EvaluationKP, double,
158
+ // ESolutionKP>(_eprk);
142
159
143
160
// //////////////////////////////////////////////////////////////////////////
144
161
// BRKGA
145
162
// //////////////////////////////////////////////////////////////////////////
146
163
std::cout << " \n BRKGA Search" ;
147
164
148
165
sref<DecoderRandomKeys<ESolutionKP, double >> _decoder =
149
- new EvaluatorSubsetRandomKeys<EvaluationKP, double , ESolutionKP>(ev, 0 , p.N - 1 );
166
+ new EvaluatorSubsetRandomKeys<EvaluationKP, double , ESolutionKP>(ev, 0 ,
167
+ p.N - 1 );
150
168
151
- sref<InitialEPopulation<std::pair<std::vector<double >, Evaluation<>>>> _genKeys =
152
- new RandomKeysInitEPop<EvaluationKP, double >(p.N , rg);
169
+ sref<InitialEPopulation<std::pair<std::vector<double >, Evaluation<>>>>
170
+ _genKeys = new RandomKeysInitEPop<EvaluationKP, double >(p.N , rg);
153
171
154
- BRKGA<ESolutionKP, double > brkga (_decoder, _genKeys, 1000 , 30 , 0.4 , 0.3 , 0.6 , rg);
172
+ BRKGA<ESolutionKP, double > brkga (_decoder, _genKeys, 1000 , 30 , 0.4 , 0.3 , 0.6 ,
173
+ rg);
155
174
156
- // pair<CopySolution<random_keys>, Evaluation<>>* r2 = brkga.search(sosc);
175
+ // pair<CopySolution<random_keys>, Evaluation<>>* r2 = brkga.search(sosc);
157
176
SearchOutput<ESolutionKP> sout2 = brkga.search (sosc);
158
177
159
178
std::optional<pair<SolutionKP, EvaluationKP>> r2 = sout2.best ;
0 commit comments