Skip to content

Commit 21ffc35

Browse files
committed
fix kp example and introduce sref_copy macro
1 parent 2b691ab commit 21ffc35

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

Examples/KP/build/.gitignore

-2
This file was deleted.

Examples/KP/src/mainKP.cpp

+59-40
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ int main(int argc, char** argv) {
3030
// Problem Context
3131
////////////////////////////////////////////////////////////////////////////
3232

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
3435

3536
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
3840
StopCriteria<EvaluationKP> sosc;
3941

4042
ConstructiveRandom c1(p, ev); // construtor de vizinhanças aleatórias
@@ -46,38 +48,46 @@ int main(int argc, char** argv) {
4648
////////////////////////////////////////////////////////////////////////////
4749
cout << "\nWill generate random solution" << endl;
4850

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???)
5154
ESolutionKP& se = *opse;
5255
std::cout << "solution: " << opse->first << std::endl;
5356
ev.reevaluate(se);
5457
se.second.print();
5558
cout << "GUD\n\n"; // qq é isso Igor?
5659

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!
5862

5963
/*
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
6266
check.add(c1); // carrega o construtivo para testes
6367
check.add(ns1); // carrega a vizinhança para testes
6468
check.run(100, 10); // executa testes com 10 iterações
6569
*/
6670

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);
6873
/*
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
7280
};
7381
7482
auto soscSA { StopCriteria(specificStopBy) };
7583
*/
7684

7785
/*
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
8191
}
8292
};
8393
*/
@@ -86,20 +96,24 @@ int main(int argc, char** argv) {
8696
// Simulated Annealing
8797
////////////////////////////////////////////////////////////////////////////
8898

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);
91101
sref<NS<ESolutionKP>> _ns1 = new NSSeqBitFlip(p, rg);
102+
sref<NSSeq<ESolutionKP>> _nsseq1 = new NSSeqBitFlip(p, rg);
92103
sref<NS<ESolutionKP>> _neighbors = _ns1;
93104
double _alpha = 0.98;
94105
int _SAmax = 100;
95106
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);
97109

98110
/*
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
103117
}
104118
);
105119
*/
@@ -111,7 +125,7 @@ int main(int argc, char** argv) {
111125
SearchOutput<ESolutionKP> sout = sa.search(sosc); // Faz a busca, de fato
112126
op<ESolutionKP> r = *sout.best;
113127

114-
//r->first.print();
128+
// r->first.print();
115129
std::cout << r->first << std::endl;
116130
r->second.print();
117131

@@ -120,40 +134,45 @@ int main(int argc, char** argv) {
120134
////////////////////////////////////////////////////////////////////////////
121135
std::cout << "\nHill Climbing Search:";
122136

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);
128142
//
129-
//pair<SolutionKP, Evaluation<>> se(s, e);
143+
// pair<SolutionKP, Evaluation<>> se(s, e);
130144
std::cout << "Best Improvement Search: ";
131145
sd.lsearch(se, sosc).second.print(); // executa e imprime HC + BI
132146
std::cout << "First Improvement Search: ";
133147
pm.lsearch(se, sosc).second.print(); // executa e imprime HC + FI
134148
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
136151

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);
142159

143160
////////////////////////////////////////////////////////////////////////////
144161
// BRKGA
145162
////////////////////////////////////////////////////////////////////////////
146163
std::cout << "\nBRKGA Search";
147164

148165
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);
150168

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);
153171

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);
155174

156-
//pair<CopySolution<random_keys>, Evaluation<>>* r2 = brkga.search(sosc);
175+
// pair<CopySolution<random_keys>, Evaluation<>>* r2 = brkga.search(sosc);
157176
SearchOutput<ESolutionKP> sout2 = brkga.search(sosc);
158177

159178
std::optional<pair<SolutionKP, EvaluationKP>> r2 = sout2.best;

include/OptFrame/Concepts/MyConcepts.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
// ========== "not null shared pointer" shortcuts
3434
template <class R>
3535
using sref = nnptr::NNShared<R>;
36+
// NOLINTNEXTLINE
37+
#define sref_copy(X) nnptr::copy(X)
3638
//
3739
template <class R>
3840
using vsref = std::vector<sref<R>>;

0 commit comments

Comments
 (0)