Compare commits

..

No commits in common. "f536ca37fef620aec817cf0e05a8e63356808b33" and "52327fd62eba150a52233428d49be221dcd4b073" have entirely different histories.

4 changed files with 10 additions and 113 deletions

View File

@ -1,29 +0,0 @@
#include <iostream>
#include <vector>
#include "sa.hpp"
#ifndef TEMP
#define TEMP 1000
#endif
#ifndef ALPHA
#define ALPHA 0.99
#endif
int main(int argc, char *argv[])
{
int number_of_items = 0;
int capacity = 0;
std::cin >> number_of_items >> capacity;
std::vector<long long> items(number_of_items);
for (auto &i : items) {
std::cin >> i;
}
sa::solution act = sa::solution::simulated_annealing(capacity, items,
TEMP, ALPHA);
act.print_sol();
}

7
run.sh
View File

@ -1,7 +0,0 @@
#!/usr/bin/env bash
IFS=$'\n'
for file in $(find test -type f); do
build/main.out < $file
done

33
sa.cpp
View File

@ -1,33 +0,0 @@
#include "sa.hpp"
#include <cmath>
auto sa::solution::simulated_annealing(int capacity, const std::vector<long long> &items,
const double alpha, double temp)->sa::solution
{
sa::solution best(items, capacity);
best.randomize();
sa::solution prev = best;
std::random_device rdevice;
std::default_random_engine eng(rdevice());
std::uniform_real_distribution<> rand(0, 1);
const double temp_min = 0.30;
while (temp < temp_min) {
sa::solution neighbor(prev);
neighbor.setneighbor();
int diff = prev.fitness - neighbor.fitness;
if (diff <= 0 || std::exp(-diff / temp) > rand(eng)) {
prev.swap(neighbor);
}
temp *= alpha;
if (prev.fitness < best.fitness) {
best = prev;
}
}
return best;
}

54
sa.hpp
View File

@ -1,11 +1,9 @@
#ifndef SIMULATED_ANNEALING_HEADER_12647_H
#define SIMULATED_ANNEALING_HEADER_12647_H
#include <algorithm>
#include <iostream>
#include <queue>
#include <random>
#include <vector>
#include <algorithm>
#include <random>
namespace sa {
@ -16,7 +14,7 @@ class solution {
int fitness;
auto calculate_boxes()->int {
int count = 1;
int count = 0;
long long now = 0;
for (auto i : items) {
@ -31,23 +29,16 @@ class solution {
return count;
}
static void print_box(int box, std::queue<long long> &stored) {
std::cout << "Caixa " << box << ":";
while (!stored.empty()) {
std::cout << ' ' << stored.front();
stored.pop();
}
std::cout << '\n';
}
public:
solution() = default;
solution(const std::vector<long long> &items, int capacity): // Gera a solução inicial
items(items), gen(std::random_device()()), capacity(capacity),
fitness(calculate_boxes()) {}
items(items), capacity(capacity), fitness(calculate_boxes()) {
std::random_device rdevice;
gen.seed(rdevice());
}
solution(const solution &sol):
items(sol.items), gen(sol.gen), capacity(sol.capacity) { // Gera um vizinho da solução
void setneighbor() { // Gera um vizinho da solução
std::uniform_int_distribution<> dist(0, items.size() - 1);
int first = dist(gen);
int second = 0;
@ -69,31 +60,6 @@ class solution {
std::swap(capacity, other.capacity);
std::swap(gen, other.gen);
}
void print_sol() const {
std::cout << "Número de caixas: " << fitness << '\n';
int box_now = 1;
long long now = 0;
std::queue<long long> items_stored;
for (auto i : items) {
if (now + i > capacity) {
print_box(box_now, items_stored);
box_now++;
now = i;
} else {
now += i;
}
items_stored.push(i);
}
print_box(box_now, items_stored);
std::cout << '\n';
}
static auto simulated_annealing(int capacity, const std::vector<long long> &items,
double alpha, double temp)->solution;
};
} // namespace sa