contrib/mul/mmn/mmn_solver.h
Go to the documentation of this file.
00001 #ifndef  mmn_solver_h_
00002 #define mmn_solver_h_
00003 //:
00004 // \file
00005 // \brief Base for classes which solve Markov Random Field problems
00006 // \author Tim Cootes
00007 
00008 #include <mmn/mmn_arc.h>
00009 #include <vnl/vnl_fwd.h>
00010 
00011 #include <vsl/vsl_binary_io.h>
00012 #include <vcl_vector.h>
00013 #include <vcl_string.h>
00014 #include <vcl_cassert.h>
00015 #include <vcl_memory.h>
00016 #include <vcl_iosfwd.h>
00017 
00018 //: Base for classes which solve Markov Random Field problems.
00019 class mmn_solver
00020 {
00021 protected:
00022 
00023 public:
00024 
00025     //: Dflt ctor
00026   mmn_solver();
00027 
00028     //: Destructor
00029   virtual ~mmn_solver();
00030 
00031     //: Input the arcs that define the graph
00032   virtual void set_arcs(unsigned num_nodes,
00033                         const vcl_vector<mmn_arc>& arcs) =0;
00034 
00035   //: Find values for each node with minimise the total cost
00036   //  \param node_cost: node_cost[i][j] is cost of selecting value j for node i
00037   //  \param pair_cost: pair_cost[a](i,j) is cost of selecting values (i,j) for nodes at end of arc a.
00038   //  \param x: On exit, x[i] gives choice for node i
00039   // NOTE: If arc a connects nodes v1,v2, the associated pair_cost is ordered
00040   // with the node with the lowest index being the first parameter.  Thus if
00041   // v1 has value i1, v2 has value i2, then the cost of this choice is
00042   // (v1<v2?pair_cost(i1,i2):pair_cost(i2,i1))
00043   // Returns the minimum cost
00044   virtual double solve(
00045                  const vcl_vector<vnl_vector<double> >& node_cost,
00046                  const vcl_vector<vnl_matrix<double> >& pair_cost,
00047                  vcl_vector<unsigned>& x) =0;
00048 
00049   //: Initialise from a text stream
00050   virtual bool set_from_stream(vcl_istream &is);
00051 
00052     //: Version number for I/O
00053   short version_no() const;
00054 
00055     //: Name of the class
00056   virtual vcl_string is_a() const;
00057 
00058     //: Create a copy on the heap and return base class pointer
00059   virtual mmn_solver* clone() const = 0;
00060 
00061     //: Print class to os
00062   virtual void print_summary(vcl_ostream& os) const =0;
00063 
00064     //: Save class to binary file stream
00065   virtual void b_write(vsl_b_ostream& bfs) const =0;
00066 
00067     //: Load class from binary file stream
00068   virtual void b_read(vsl_b_istream& bfs) =0;
00069 
00070   static vcl_auto_ptr<mmn_solver>
00071     create_from_stream(vcl_istream &is);
00072 };
00073 
00074   //: Allows derived class to be loaded by base-class pointer
00075 void vsl_add_to_binary_loader(const mmn_solver& b);
00076 
00077   //: Binary file stream output operator for class reference
00078 void vsl_b_write(vsl_b_ostream& bfs, const mmn_solver& b);
00079 
00080   //: Binary file stream input operator for class reference
00081 void vsl_b_read(vsl_b_istream& bfs, mmn_solver& b);
00082 
00083   //: Stream output operator for class reference
00084 vcl_ostream& operator<<(vcl_ostream& os,const mmn_solver& b);
00085 
00086   //: Stream output operator for class pointer
00087 vcl_ostream& operator<<(vcl_ostream& os,const mmn_solver* b);
00088 
00089 #endif
00090 
00091