1#ifndef INFRARED_CLUSTER_TREE_HPP
2#define INFRARED_CLUSTER_TREE_HPP
24#include "feature_network.hpp"
54 template<
class FunValue=
double,
class EvaluationPolicy=StdEvaluationPolicy<FunValue>>
115 : fn_( domains_from_domsizes( domsizes ) ) {
125 : fn_(num_vars, domain) {
201 tree_[node].cluster.add_function( fn_.
add_function(x) );
227 return evaluate() != evaluation_policy_t::zero();
273 bool evaluated_ =
false;
275 bool single_empty_rooted_ =
false;
279 std::optional<std::set<var_idx_t>> trace_variables;
286 auto domains_from_domsizes( std::vector<int> &domsizes ) {
288 for (
auto x: domsizes) {
300 for(
auto &e: tree_.adj_edges(v)) {
302 dfs_evaluate(e.target());
307 const auto &parent = tree_[ e.source() ].cluster;
308 const auto &child = tree_[ e.target() ].cluster;
310 auto sep = child.sep_vars(parent);
311 auto diff = child.diff_vars(parent);
315 sep_diff.insert(sep_diff.end(),diff.begin(),diff.end());
317 auto message = std::make_unique<message_t>(sep, fn_);
321 auto it = a.make_iterator
332 it.register_finish_stage2_hook
334 [&message,&a,&x] () {
336 x = evaluation_policy_t::zero();
339 for(; ! it.finished() ; ++it ) {
340 x = evaluation_policy_t::plus( x, it.value() );
344 auto msg = fn_.add_function(std::move(message));
346 tree_[ e.source() ].cluster.add_function(msg);
363 for(
const auto &e: tree_.adj_edges(v)) {
365 const auto &parent = tree_[ e.source() ].cluster;
366 const auto &child = tree_[ e.target() ].cluster;
368 auto diff = child.diff_vars(parent);
369 bool diff_changed =
false;
371 if (trace_variables) {
373 auto new_end = remove_if(diff.begin(), diff.end(),
375 return trace_variables->find(i) == trace_variables->end();
377 diff_changed = new_end != diff.end();
378 diff.erase(new_end, diff.end());
381 if (! diff.empty()) {
385 assert(a.eval_determined(child.constraints(),
386 StdEvaluationPolicy<bool>()));
388 auto it = a.make_iterator(diff, fn_,
391 a.eval_determined(child.functions(),
397 auto value = evaluation_policy_t::zero();
400 for( ; ! it.finished(); ++it ) {
401 value = evaluation_policy_t::plus( value, it.value() );
407 value = (*e.message)(a);
410 auto selector =
typename evaluation_policy_t::selector(value);
415 auto x = evaluation_policy_t::zero();
416 for( ; ! it.finished(); ++it ) {
417 x = evaluation_policy_t::plus( x, it.value() );
419 if (selector.select(x)) {
425 dfs_traceback(e.target(), a);
435 template<
class FunValue,
class EvaluationPolicy>
437 ClusterTree<FunValue,EvaluationPolicy>::single_empty_root() {
438 if (single_empty_rooted_) {
return root_;}
442 std::set<vertex_descriptor_t> old_roots;
444 for (
int i=0; i < int(tree_.size()); ++i) {
448 for (
int i=0; i < int(tree_.size()); ++i) {
449 for (
auto &e: tree_.adj_edges(i)) {
450 old_roots.erase( e.target() );
454 if ( old_roots.size()==1 && tree_[ *old_roots.begin() ].cluster.empty() ) {
455 root_ = *old_roots.begin();
458 root_ = tree_.add_vertex();
460 for (
auto old_root : old_roots) {
461 tree_.add_edge(root_, old_root);
465 single_empty_rooted_ =
true;
470 template<
class FunValue,
class EvaluationPolicy>
472 ClusterTree<FunValue,EvaluationPolicy>
474 auto root = single_empty_root();
480 evaluation_result_ = a.eval_determined(tree_[root].cluster.functions(),
evaluation_policy_t());
484 return evaluation_result_;
487 template<
class FunValue,
class EvaluationPolicy>
496 dfs_traceback(single_empty_root(), a);
501 template<
class FunValue,
class EvaluationPolicy>
510 trace_variables = variables;
514 dfs_traceback(single_empty_root(), a);
516 trace_variables.reset();
A tree of clusters (=variables, functions, constraints)
Definition cluster_tree.hpp:55
ClusterTree(int num_vars, const FiniteDomain &domain)
Construct with uniform domains.
Definition cluster_tree.hpp:124
FeatureNetwork< FunValue, EvaluationPolicy > feature_network_t
Definition cluster_tree.hpp:58
typename tree_t::vertex_descriptor_t vertex_descriptor_t
type of identifiers of vertices (typically 'long int')
Definition cluster_tree.hpp:91
bool is_consistent()
Check consistency.
Definition cluster_tree.hpp:226
ClusterTree(std::vector< int > domsizes)
construct from vector of upper bounds
Definition cluster_tree.hpp:114
~ClusterTree()
Definition cluster_tree.hpp:138
auto traceback()
Generate a traceback.
Definition cluster_tree.hpp:489
auto evaluate()
Evaluate the cluster tree (by DP)
Definition cluster_tree.hpp:473
ClusterTree(int num_vars, int domsize)
Construct with uniform domains.
Definition cluster_tree.hpp:134
auto restricted_traceback(const std::set< var_idx_t > &variables, const assignment_t &assignment)
Generate a restricted traceback.
Definition cluster_tree.hpp:504
typename feature_network_t::constraint_t constraint_t
Definition cluster_tree.hpp:67
auto add_root_cluster(const std::vector< var_idx_t > &vars)
add new root cluster to the tree
Definition cluster_tree.hpp:156
ClusterTree(const FiniteDomainVector &domains)
Construct with variable domains.
Definition cluster_tree.hpp:106
graph::adjacency_list< vertex_info_t, edge_info_t > tree_t
Definition cluster_tree.hpp:88
typename feature_network_t::cluster_t cluster_t
Definition cluster_tree.hpp:64
typename feature_network_t::var_idx_t var_idx_t
Definition cluster_tree.hpp:63
const auto & feature_network() const
read access to constraint network
Definition cluster_tree.hpp:141
typename feature_network_t::evaluation_policy_t evaluation_policy_t
evaluation policy type
Definition cluster_tree.hpp:61
typename feature_network_t::function_t function_t
Definition cluster_tree.hpp:68
void add_constraint(vertex_descriptor_t node, const std::shared_ptr< constraint_t > &x)
add new constraint to cluster
Definition cluster_tree.hpp:187
ClusterTree()
construct empty
Definition cluster_tree.hpp:96
auto add_child_cluster(vertex_descriptor_t parent, const std::vector< int > &vars)
add new child cluster to the tree
Definition cluster_tree.hpp:172
void add_function(vertex_descriptor_t node, const std::shared_ptr< function_t > &x)
add new function to cluster
Definition cluster_tree.hpp:200
typename feature_network_t::fun_value_t fun_value_t
Definition cluster_tree.hpp:66
typename feature_network_t::assignment_t assignment_t
Definition cluster_tree.hpp:65
the feature network
Definition feature_network.hpp:208
FunValue fun_value_t
Definition feature_network.hpp:211
auto add_function(const std::shared_ptr< function_t > &x)
add function
Definition feature_network.hpp:298
int var_idx_t
Definition feature_network.hpp:209
Function< FunValue > function_t
Definition feature_network.hpp:214
EvaluationPolicy evaluation_policy_t
Definition feature_network.hpp:217
auto add_constraint(const std::shared_ptr< constraint_t > &x)
add constraint
Definition feature_network.hpp:269
Cluster< fun_value_t > cluster_t
suitable cluster type for the constraint network
Definition feature_network.hpp:220
Assignment assignment_t
Definition feature_network.hpp:213
Constraint constraint_t
Definition feature_network.hpp:215
Definition finite_domain.hpp:29
A materialized function.
Definition functions.hpp:233
size_t vertex_descriptor_t
Definition graph.hpp:27
auto & add_edge(vertex_descriptor_t source, vertex_descriptor_t target, const edge_info_t &e)
add egde
Definition graph.hpp:78
vertex_descriptor_t add_vertex(const vertex_t &v)
add vertex
Definition graph.hpp:63
Definition assignment.hpp:29
std::vector< FiniteDomain > FiniteDomainVector
Definition finite_domain.hpp:148
information at an edge of the tree
Definition cluster_tree.hpp:82
edge_info_t(function_t *message=nullptr)
Definition cluster_tree.hpp:83
function_t * message
Definition cluster_tree.hpp:85
information at a vertex (=cluster/bag) of the tree
Definition cluster_tree.hpp:73
vertex_info_t(const cluster_t &cluster)
Definition cluster_tree.hpp:76
vertex_info_t()
Definition cluster_tree.hpp:74
cluster_t cluster
Definition cluster_tree.hpp:78