Commit d2955f07 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

Revert "optimizer: remove hash code in initializer"

This reverts commit eeea6927.
parent e84577ba
......@@ -92,6 +92,7 @@ private:
const bit_array& x,
const double value,
const double duration,
const std::size_t hash,
const long int loop,
const int remaining_constraints) noexcept
{
......@@ -100,6 +101,7 @@ private:
m_data[id].x = x;
m_data[id].value = value;
m_data[id].duration = duration;
m_data[id].hash = hash;
m_data[id].loop = loop;
m_data[id].remaining_constraints = remaining_constraints;
}
......@@ -166,6 +168,7 @@ public:
}
for (int i = 0, e = m_size; i != e; ++i) {
m_data[i].make_hash();
m_data[i].value = costs.results(m_data[i].x, cost_constant_);
m_data[i].remaining_constraints = 0;
......@@ -192,13 +195,15 @@ public:
info(ctx, " Population {}:\n", m_indices.size());
for (int i = 0; i != m_size; ++i)
info(ctx,
" - {}: value {} constraints {}\n",
" - {}: value {} constraints {} hash {}\n",
m_indices[i],
m_data[m_indices[i]].value,
m_data[m_indices[i]].remaining_constraints);
m_data[m_indices[i]].remaining_constraints,
m_data[m_indices[i]].hash);
}
void insert(const bit_array& x,
const std::size_t hash,
const int remaining_constraints,
const double duration,
const long int loop) noexcept
......@@ -207,8 +212,9 @@ public:
to_log(stdout,
5u,
"- insert advance {} ({}s in {} loops\n",
"- insert advance {} (hash: {}) {}s in {} loops\n",
remaining_constraints,
hash,
duration,
loop);
......@@ -222,6 +228,7 @@ public:
x,
costs.results(x, cost_constant),
duration,
hash,
loop,
remaining_constraints);
......@@ -229,6 +236,7 @@ public:
}
void insert(const bit_array& x,
const std::size_t hash,
const double value,
const double duration,
const long int loop) noexcept
......@@ -237,8 +245,9 @@ public:
to_log(stdout,
5u,
"- insert solution {} {}s in {} loops\n",
"- insert solution {} (hash: {}) {}s in {} loops\n",
value,
hash,
duration,
loop);
......@@ -248,12 +257,12 @@ public:
bound.last,
m_data[bound.last].value);
replace_result(bound.last, x, value, duration, loop, 0);
replace_result(bound.last, x, value, duration, hash, loop, 0);
sort();
}
bool can_be_inserted(const bit_array& x, const int constraints) const noexcept
bool can_be_inserted(const std::size_t hash, const int constraints) const noexcept
{
{
m_indices_reader lock(m_indices_mutex);
......@@ -268,14 +277,15 @@ public:
m_data_reader lock_data(m_data_mutex[i]);
if (m_data[i].remaining_constraints == constraints
&& m_data[i].x == x)
&& m_data[i].hash == hash)
return false;
}
return true;
}
bool can_be_inserted(const bit_array& x, const double value) const noexcept
bool can_be_inserted([[maybe_unused]] const std::size_t hash,
const double value) const noexcept
{
{
m_indices_reader lock(m_indices_mutex);
......@@ -291,7 +301,7 @@ public:
m_data_reader lock_data(m_data_mutex[i]);
if (m_data[i].remaining_constraints == 0 && m_data[i].value == value
&& m_data[i].x == x)
&& m_data[i].hash == hash)
return false;
}
......@@ -443,11 +453,12 @@ private:
for (int i = 0; i != m_size; ++i) {
to_log(stdout,
5u,
"- {} id {} value {} constraint {}\n",
"- {} id {} value {} constraint {} hash {}\n",
i,
m_indices[i],
m_data[m_indices[i]].value,
m_data[m_indices[i]].remaining_constraints);
m_data[m_indices[i]].remaining_constraints,
m_data[m_indices[i]].hash);
}
#endif
}
......@@ -622,11 +633,14 @@ struct best_solution_recorder
const int remaining_constraints,
const long int loop)
{
if (m_storage.can_be_inserted(solution, remaining_constraints)) {
auto hash = bit_array_hash()(solution);
if (m_storage.can_be_inserted(hash, remaining_constraints)) {
const auto end = std::chrono::steady_clock::now();
const auto duration = compute_duration(m_start, end);
m_storage.insert(solution, remaining_constraints, duration, loop);
m_storage.insert(
solution, hash, remaining_constraints, duration, loop);
}
}
......@@ -634,11 +648,13 @@ struct best_solution_recorder
const double value,
const long int loop)
{
if (m_storage.can_be_inserted(solution, value)) {
auto hash = bit_array_hash()(solution);
if (m_storage.can_be_inserted(hash, value)) {
const auto end = std::chrono::steady_clock::now();
const auto duration = compute_duration(m_start, end);
m_storage.insert(solution, value, duration, loop);
m_storage.insert(solution, hash, value, duration, loop);
}
}
......
......@@ -68,9 +68,15 @@ struct raw_result
bit_array x;
double value = itm::bad_value<Mode, double>();
double duration = 0.0;
std::size_t hash = 0;
long int loop = 0;
index remaining_constraints = std::numeric_limits<index>::max();
void make_hash() noexcept
{
hash = bit_array_hash()(x);
}
bool is_solution() const noexcept
{
return remaining_constraints == 0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment