contrib/mul/mmn/mmn_csp_solver.h
Go to the documentation of this file.
00001 #ifndef mmn_csp_solver_h_
00002 #define mmn_csp_solver_h_
00003 //:
00004 // \file
00005 // \brief See if the Constraint Satisfaction Problem is satisfiable
00006 // \author Martin Roberts
00007 
00008 #include <vcl_vector.h>
00009 #include <vcl_set.h>
00010 #include <mmn/mmn_arc.h>
00011 #include <mmn/mmn_dependancy.h>
00012 #include <mmn/mmn_graph_rep1.h>
00013 #include <mbl/mbl_stl_pred.h>
00014 
00015 //: Given (sparse?) graph eliminate invalid arcs and nodes until a kernel of arc-consistent nodes and arcs remains
00016 //  See Werner 2007 paper in IEEE Trans on Pattern Recognition and Machine Intelligence
00017 //  Can be used to see if Max Sum problem has been reduced to "trivial" form
00018 //  i.e. if the maximal nodes are joined up via maximal arcs
00019 //  We successively delete
00020 //  a) nodes not linked with some neighbour by any edge
00021 //  b) edges lacking an end node
00022 
00023 class mmn_csp_solver
00024 {
00025  public:
00026     //: Subset of labels present for each node
00027     typedef vcl_set<unsigned>  label_subset_t;
00028 
00029     //: Define the subset of labels linked
00030     // For each original arc (outer vector), the inner set gives all the
00031     // corresponding node labels actually linked
00032     // Note the first in the pair corresponds always to the lower node ID in the arc (i.e. as for arc pair costs
00033     typedef vcl_set<vcl_pair<unsigned ,unsigned >, mbl_stl_pred_pair_order<unsigned ,unsigned > >  arc_labels_subset_t;
00034     //Similar but multiset with partial ordering by first label
00035     typedef vcl_multiset<vcl_pair<unsigned ,unsigned >,
00036         mbl_stl_pred_pair_key_order<vcl_pair<unsigned ,unsigned > > >  arc_labels_subset_t1;
00037     //Similar but multiset with partial ordering by second label
00038     typedef vcl_multiset<vcl_pair<unsigned ,unsigned >,
00039         mbl_stl_pred_pair_value_order<vcl_pair<unsigned ,unsigned > > >  arc_labels_subset_t2;
00040 
00041  private:
00042     unsigned nnodes_;
00043 
00044     bool verbose_;
00045     //:Vector of nodes, defining which labels are present for each node
00046     //Note some sets may become empty
00047     vcl_vector<label_subset_t > node_labels_present_;
00048 
00049     //: Define the subset of labels linked
00050     // For each original arc (outer vector), the inner set gives all the
00051     // corresponding node labels actually linked
00052     // Note the first in the pair corresponds always to the lower node ID in the arc (i.e. as for arc pair costs
00053     vcl_vector<arc_labels_subset_t1 > arc_labels_linked1_;
00054     vcl_vector<arc_labels_subset_t2 > arc_labels_linked2_;
00055     //:Store in graph form (so each node's neighbours are conveniently to hand)
00056     mmn_graph_rep1 graph_;
00057 
00058     //: The arcs from which graph was generated
00059     vcl_vector<mmn_arc> arcs_;
00060 
00061     //: delete any node labels not linked by any current arcs
00062     //Return true if any deletions occur
00063     bool check_for_node_deletions();
00064 
00065     //: delete any arcs with either target node label not present
00066     //Return true if any deletions occur
00067     bool check_for_arc_deletions();
00068 
00069     void initialise_arc_labels_linked(const vcl_vector<mmn_csp_solver:: arc_labels_subset_t >& links_subset);
00070 
00071     void init();
00072  public:
00073     //: Default constructor
00074     mmn_csp_solver();
00075 
00076     //: Construct with arcs
00077     mmn_csp_solver(unsigned num_nodes,const vcl_vector<mmn_arc>& arcs);
00078 
00079     //: Input the arcs that define the graph
00080     void set_arcs(unsigned num_nodes,const vcl_vector<mmn_arc>& arcs);
00081 
00082     bool operator()(const vcl_vector<mmn_csp_solver::label_subset_t >& node_labels_subset,
00083                     const vcl_vector<mmn_csp_solver::arc_labels_subset_t >& links_subset);
00084 
00085     void set_verbose(bool verbose) {verbose_=verbose;}
00086     const vcl_vector<label_subset_t >& kernel_node_labels() const {return node_labels_present_;}
00087 };
00088 
00089 #endif // mmn_csp_solver_h_