Add number of iterations of the solution as well
This commit is contained in:
parent
859b34308c
commit
70f0125d13
6
sa.cpp
6
sa.cpp
@ -13,8 +13,11 @@ auto sa::solution::simulated_annealing(int capacity, const std::vector<long long
|
|||||||
std::default_random_engine eng(rdevice());
|
std::default_random_engine eng(rdevice());
|
||||||
std::uniform_real_distribution<> rand(0, 1);
|
std::uniform_real_distribution<> rand(0, 1);
|
||||||
|
|
||||||
|
int iteration = 0;
|
||||||
|
|
||||||
while (temp > temp_min) {
|
while (temp > temp_min) {
|
||||||
sa::solution neighbor(prev);
|
iteration++;
|
||||||
|
sa::solution neighbor(prev, iteration);
|
||||||
neighbor.setneighbor();
|
neighbor.setneighbor();
|
||||||
|
|
||||||
int diff = prev.fitness - neighbor.fitness;
|
int diff = prev.fitness - neighbor.fitness;
|
||||||
@ -29,5 +32,6 @@ auto sa::solution::simulated_annealing(int capacity, const std::vector<long long
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
best.setiterations(iteration);
|
||||||
return best;
|
return best;
|
||||||
}
|
}
|
||||||
|
|||||||
15
sa.hpp
15
sa.hpp
@ -14,6 +14,8 @@ class solution {
|
|||||||
std::default_random_engine gen;
|
std::default_random_engine gen;
|
||||||
int capacity;
|
int capacity;
|
||||||
int fitness;
|
int fitness;
|
||||||
|
int iterations;
|
||||||
|
int iteration;
|
||||||
|
|
||||||
auto calculate_boxes()->int {
|
auto calculate_boxes()->int {
|
||||||
int count = 1;
|
int count = 1;
|
||||||
@ -43,9 +45,13 @@ class solution {
|
|||||||
public:
|
public:
|
||||||
solution() = default;
|
solution() = default;
|
||||||
|
|
||||||
|
solution(const solution &other, int itr): items(other.items), gen(other.gen),
|
||||||
|
capacity(other.capacity), fitness(other.fitness),
|
||||||
|
iteration(itr) {}
|
||||||
|
|
||||||
solution(const std::vector<long long> &items, int capacity): // Gera a solução inicial
|
solution(const std::vector<long long> &items, int capacity): // Gera a solução inicial
|
||||||
items(items), gen(std::random_device()()), capacity(capacity),
|
items(items), gen(std::random_device()()), capacity(capacity),
|
||||||
fitness(calculate_boxes()) {}
|
fitness(calculate_boxes()), iteration(0) {}
|
||||||
|
|
||||||
void setneighbor() { // Gera um vizinho da solução
|
void setneighbor() { // Gera um vizinho da solução
|
||||||
std::uniform_int_distribution<> dist(0, items.size() - 1);
|
std::uniform_int_distribution<> dist(0, items.size() - 1);
|
||||||
@ -68,9 +74,12 @@ class solution {
|
|||||||
std::swap(items, other.items);
|
std::swap(items, other.items);
|
||||||
std::swap(capacity, other.capacity);
|
std::swap(capacity, other.capacity);
|
||||||
std::swap(gen, other.gen);
|
std::swap(gen, other.gen);
|
||||||
|
std::swap(iteration, other.iteration);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_sol() const {
|
void print_sol() const {
|
||||||
|
std::cout << "Iteração da solução: " << iteration << '\n';
|
||||||
|
std::cout << "Número de iterações calculadas: " << iterations << '\n';
|
||||||
std::cout << "Número de caixas: " << fitness << '\n';
|
std::cout << "Número de caixas: " << fitness << '\n';
|
||||||
|
|
||||||
int box_now = 1;
|
int box_now = 1;
|
||||||
@ -92,6 +101,10 @@ class solution {
|
|||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setiterations(int itr) {
|
||||||
|
iterations = itr;
|
||||||
|
}
|
||||||
|
|
||||||
static auto simulated_annealing(int capacity, const std::vector<long long> &items,
|
static auto simulated_annealing(int capacity, const std::vector<long long> &items,
|
||||||
double alpha, double temp,
|
double alpha, double temp,
|
||||||
double temp_min)->solution;
|
double temp_min)->solution;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user