diff --git a/random.cpp b/random.cpp new file mode 100644 index 0000000..482c406 --- /dev/null +++ b/random.cpp @@ -0,0 +1,4 @@ +#include "random.hpp" + +std::random_device rdevice; +std::mt19937_64 rng::rng(rdevice()); diff --git a/random.hpp b/random.hpp new file mode 100644 index 0000000..03fe810 --- /dev/null +++ b/random.hpp @@ -0,0 +1,24 @@ +#ifndef RANDOM_HPP_RANDOMNESS_WHATEVER_HEADER_238947837827_H +#define RANDOM_HPP_RANDOMNESS_WHATEVER_HEADER_238947837827_H + +#include + +namespace rng { + extern std::mt19937_64 rng; + + template + auto random_int() -> int + { + static std::uniform_int_distribution<> rint(start, end); + return rint(rng); + } + + template + auto random_double() -> double + { + static std::uniform_real_distribution<> rdouble(start, end); + return rdouble(rng); + } +} // namespace rng + +#endif diff --git a/sa.cpp b/sa.cpp index 371ae41..203b515 100644 --- a/sa.cpp +++ b/sa.cpp @@ -6,11 +6,7 @@ auto sa::solution::simulated_annealing(int capacity, const std::vectorsa::solution { sa::solution best(items, capacity); - sa::solution prev = best; - std::random_device rdevice; - std::default_random_engine eng(rdevice()); - std::uniform_real_distribution<> rand(0, 1); int iteration = 0; @@ -20,7 +16,7 @@ auto sa::solution::simulated_annealing(int capacity, const std::vector() / temp < 0.8) { swap(prev, neighbor); } diff --git a/sa.hpp b/sa.hpp index 4e7c45f..62ccf7e 100644 --- a/sa.hpp +++ b/sa.hpp @@ -6,11 +6,13 @@ #include #include +#include #include #include #include #include -#include + +#include "random.hpp" namespace sa { @@ -94,7 +96,6 @@ class box { class solution { std::vector boxes; - std::mt19937_64 gen; long long fitness; int capacity; int iterations; @@ -104,7 +105,6 @@ class solution { using std::swap; swap(one.boxes, two.boxes); - swap(one.gen, two.gen); swap(one.capacity, two.capacity); swap(one.fitness, two.fitness); swap(one.iterations, two.iterations); @@ -122,8 +122,7 @@ class solution { &sequence11 }; - std::uniform_real_distribution<> rand(0, 1); - int now = rand(gen) > 0.3 ? 0 : 1; + int now = rng::random_double<0, 1>() > 0.3 ? 0 : 1; if (sequences[now]->empty()) { now ^= 1; @@ -133,19 +132,17 @@ class solution { } std::uniform_int_distribution<> dist(0, (int)sequences[now]->size() - 1); - (boxes[choice].*swaps[now])(boxes[(*sequences[now])[dist(gen)]]); + (boxes[choice].*swaps[now])(boxes[(*sequences[now])[dist(rng::rng)]]); } public: solution() = default; - solution(const solution &other, int itr): boxes(other.boxes), gen(other.gen), - fitness(other.fitness), capacity(other.capacity), - iteration(itr) {} + solution(const solution &other, int itr): boxes(other.boxes), fitness(other.fitness), + capacity(other.capacity), iteration(itr) {} solution(const std::vector &items, int capacity): // Gera a solução inicial - gen(std::random_device()()), capacity(capacity), - iteration(0) { + capacity(capacity), iteration(0) { std::multiset its; for (auto i : items) { its.insert(i); @@ -177,7 +174,7 @@ class solution { } void setneighbor() { // Gera um vizinho da solução - int choice = std::uniform_int_distribution<>(0, (int)boxes.size() - 1)(gen); + int choice = std::uniform_int_distribution<>(0, (int)boxes.size() - 1)(rng::rng); std::vector sequence10; std::vector sequence11; @@ -209,10 +206,6 @@ class solution { } } - void randomize() { - std::shuffle(boxes.begin(), boxes.end(), gen); - } - void print_sol(char flags = 0) { if(!(flags & SIMULATED_ANNEALING_DISABLE_SHOWITRNUM)){ std::cout << "Iteração da solução: " << iteration << '\n';