2022-04-21 20:00:42 +02:00
|
|
|
#include "wave_form_collapse.h"
|
|
|
|
|
2022-04-20 02:39:35 +02:00
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace {
|
2022-04-20 03:24:50 +02:00
|
|
|
// Normalize a vector so the sum of its elements is equal to 1.0f
|
2022-04-21 16:31:03 +02:00
|
|
|
Vector<double> &normalize(Vector<double> &v) {
|
2022-04-20 03:05:34 +02:00
|
|
|
double sum_weights = 0.0;
|
2022-04-21 16:31:03 +02:00
|
|
|
int size = v.size();
|
2022-04-21 20:00:42 +02:00
|
|
|
const double *vpr = v.ptr();
|
2022-04-21 16:31:03 +02:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
sum_weights += vpr[i];
|
2022-04-20 03:05:34 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 20:00:42 +02:00
|
|
|
double *vpw = v.ptrw();
|
2022-04-20 03:05:34 +02:00
|
|
|
double inv_sum_weights = 1.0 / sum_weights;
|
2022-04-21 16:31:03 +02:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
vpw[i] *= inv_sum_weights;
|
2022-04-20 03:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
2022-04-20 02:39:35 +02:00
|
|
|
}
|
2022-04-20 03:05:34 +02:00
|
|
|
} //namespace
|
2022-04-20 02:39:35 +02:00
|
|
|
|
2022-04-21 20:00:42 +02:00
|
|
|
Array2D<uint32_t> WaveFormCollapse::wave_to_output() const {
|
2022-04-21 16:43:04 +02:00
|
|
|
Array2D<uint32_t> output_patterns(wave.height, wave.width);
|
2022-04-21 16:31:03 +02:00
|
|
|
|
2022-04-21 16:43:04 +02:00
|
|
|
for (uint32_t i = 0; i < wave.size; i++) {
|
|
|
|
for (uint32_t k = 0; k < nb_patterns; k++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
if (wave.get(i, k)) {
|
2022-04-21 16:31:03 +02:00
|
|
|
output_patterns.data.write[i] = k;
|
2022-04-20 03:05:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output_patterns;
|
2022-04-20 02:39:35 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 20:00:42 +02:00
|
|
|
//WaveFormCollapse::WaveFormCollapse() {
|
|
|
|
//}
|
|
|
|
|
|
|
|
WaveFormCollapse::WaveFormCollapse(bool periodic_output, int seed,
|
2022-04-21 16:31:03 +02:00
|
|
|
Vector<double> patterns_frequencies,
|
2022-04-21 17:45:30 +02:00
|
|
|
Vector<Propagator::PropagatorEntry> propagator, uint32_t wave_height, uint32_t wave_width) :
|
2022-04-21 20:00:42 +02:00
|
|
|
gen(seed), patterns_frequencies(normalize(patterns_frequencies)), wave(wave_height, wave_width, patterns_frequencies), nb_patterns(propagator.size()), propagator(wave.height, wave.width, periodic_output, propagator) {
|
|
|
|
}
|
2022-04-20 02:39:35 +02:00
|
|
|
|
2022-04-21 20:00:42 +02:00
|
|
|
Array2D<uint32_t> WaveFormCollapse::run() {
|
2022-04-20 03:05:34 +02:00
|
|
|
while (true) {
|
|
|
|
// Define the value of an undefined cell.
|
|
|
|
ObserveStatus result = observe();
|
|
|
|
|
|
|
|
// Check if the algorithm has terminated.
|
2022-04-21 20:00:42 +02:00
|
|
|
if (result == OBSERVE_STATUS_FAILURE) {
|
2022-04-21 16:43:04 +02:00
|
|
|
return Array2D<uint32_t>(0, 0);
|
2022-04-21 20:00:42 +02:00
|
|
|
} else if (result == OBSERVE_STATUS_FAILURE) {
|
2022-04-20 03:05:34 +02:00
|
|
|
return wave_to_output();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Propagate the information.
|
|
|
|
propagator.propagate(wave);
|
|
|
|
}
|
2022-04-20 02:39:35 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 20:00:42 +02:00
|
|
|
WaveFormCollapse::ObserveStatus WaveFormCollapse::observe() {
|
2022-04-20 03:05:34 +02:00
|
|
|
// Get the cell with lowest entropy.
|
|
|
|
int argmin = wave.get_min_entropy(gen);
|
|
|
|
|
|
|
|
// If there is a contradiction, the algorithm has failed.
|
|
|
|
if (argmin == -2) {
|
2022-04-21 20:00:42 +02:00
|
|
|
return OBSERVE_STATUS_FAILURE;
|
2022-04-20 03:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the lowest entropy is 0, then the algorithm has succeeded and
|
|
|
|
// finished.
|
|
|
|
if (argmin == -1) {
|
|
|
|
wave_to_output();
|
2022-04-21 20:00:42 +02:00
|
|
|
return OBSERVE_STATUS_SUCCESS;
|
2022-04-20 03:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Choose an element according to the pattern distribution
|
|
|
|
double s = 0;
|
2022-04-21 16:43:04 +02:00
|
|
|
for (uint32_t k = 0; k < nb_patterns; k++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
s += wave.get(argmin, k) ? patterns_frequencies[k] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uniform_real_distribution<> dis(0, s);
|
|
|
|
double random_value = dis(gen);
|
|
|
|
size_t chosen_value = nb_patterns - 1;
|
|
|
|
|
2022-04-21 16:43:04 +02:00
|
|
|
for (uint32_t k = 0; k < nb_patterns; k++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
random_value -= wave.get(argmin, k) ? patterns_frequencies[k] : 0;
|
|
|
|
if (random_value <= 0) {
|
|
|
|
chosen_value = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And define the cell with the pattern.
|
2022-04-21 16:43:04 +02:00
|
|
|
for (uint32_t k = 0; k < nb_patterns; k++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
if (wave.get(argmin, k) != (k == chosen_value)) {
|
2022-04-21 14:28:04 +02:00
|
|
|
propagator.add_to_propagator(argmin / wave.width, argmin % wave.width, k);
|
2022-04-20 03:05:34 +02:00
|
|
|
wave.set(argmin, k, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 20:00:42 +02:00
|
|
|
return OBSERVE_STATUS_TO_CONTINUE;
|
2022-04-20 03:05:34 +02:00
|
|
|
}
|
2022-04-21 20:00:42 +02:00
|
|
|
|
|
|
|
void WaveFormCollapse::bind_methods() {
|
|
|
|
}
|