Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readd terminal arcs heuristic #622

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/adiar/internal/algorithms/quantify.h
Original file line number Diff line number Diff line change
Expand Up @@ -1306,28 +1306,47 @@ namespace adiar::internal
const predicate<typename Policy::label_type>& pred)
{
// Extract meta data constants about the DAG
const size_t size = dd.size();
const size_t width = dd.width();
const size_t size = dd.size();
const size_t width = dd.width();
const size_t false_terminals = dd.number_of_terminals(false);
const size_t true_terminals = dd.number_of_terminals(true);

// ---------------------------------------------------------------------------------------------
// Terminal Count Heuristics

const size_t false_weight =
1 + Policy::collapse_to_terminal(typename Policy::pointer_type(false));

const size_t true_weight =
1 + Policy::collapse_to_terminal(typename Policy::pointer_type(true));

const double total_arcs = 2.0 * size;
const double weighted_terminals = false_weight * false_terminals + true_weight * true_terminals;
const size_t exponent = 16.0 * (weighted_terminals / total_arcs) + 0.5;

const typename Policy::label_type terminal_threshold =
(1u << std::min<size_t>(exponent, log2(2*Policy::max_label))) - 1u;

// ---------------------------------------------------------------------------------------------
// Shallow Variables Heuristic

// Keep track of the number of nodes on the top-most levels in relation to the total size
size_t seen_nodes = 0u;

// Threshold to stop after the first n/3 nodes.
const size_t max_nodes = size / 3;

// Final result
//
// TODO: Turn `result` into a double and decrease the weight of to-be quantified variables
// depending on `seen_nodes`, the number of levels of width `width`, and/or in general a
// (quadratic?) decay in the weight.
typename Policy::label_type result = 0u;
// TODO: Turn `shallow_variables` into a double and decrease the weight of to-be quantified
// variables depending on `seen_nodes`, the number of levels of width `width`, and/or in
// general using a (quadratic?) decay.
typename Policy::label_type shallow_variables = 0u;

level_info_stream<false /*top-down*/> lis(dd);

while (lis.can_pull()) {
const level_info li = lis.pull();

if (pred(li.label()) == Policy::quantify_onset) { result++; }
if (pred(li.label()) == Policy::quantify_onset) { shallow_variables++; }

// Stop at the (first) widest level
if (li.width() == width) { break; }
Expand All @@ -1337,8 +1356,10 @@ namespace adiar::internal
if (max_nodes < seen_nodes) { break; }
}

adiar_assert(result < Policy::max_label);
return result;
adiar_assert(shallow_variables <= Policy::max_label);

// ---------------------------------------------------------------------------------------------
return std::min<size_t>(terminal_threshold, shallow_variables);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Loading