00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef IMPDOMINO_RESTRAINT_GRAPH_H
00010 #define IMPDOMINO_RESTRAINT_GRAPH_H
00011
00012 #include "domino_config.h"
00013 #include "JNode.h"
00014 #include "JEdge.h"
00015 #include "DiscreteSampler.h"
00016 #include "JunctionTree.h"
00017
00018 #include <IMP/Model.h>
00019 #include <IMP/ScoreState.h>
00020 #include <IMP/Restraint.h>
00021 #include "RestraintEvaluatorI.h"
00022
00023 #include <vector>
00024 #include <map>
00025 #include <sstream>
00026 #include <boost/graph/adjacency_list.hpp>
00027 #include <boost/graph/depth_first_search.hpp>
00028 IMPDOMINO_BEGIN_NAMESPACE
00029
00030
00031 #ifndef IMP_SWIG
00032
00033 IMPDOMINOEXPORT StringKey node_name_key();
00034 #endif
00035
00036 template < typename TimeMap > class dfs_time_visitor :
00037 public boost::default_dfs_visitor
00038 {
00039 typedef typename boost::property_traits <TimeMap >::value_type T;
00040 public:
00041 dfs_time_visitor(TimeMap dmap, TimeMap fmap, T & t)
00042 : m_dtimemap(dmap), m_ftimemap(fmap), m_time(t) {
00043 }
00044 template < typename Vertex, typename Graph >
00045 void discover_vertex(Vertex u, const Graph & g) const {
00046 put(m_dtimemap, u, m_time++);
00047 }
00048 template < typename Vertex, typename Graph >
00049 void finish_vertex(Vertex u, const Graph & g) const {
00050 put(m_ftimemap, u, m_time++);
00051 }
00052 TimeMap m_dtimemap;
00053 TimeMap m_ftimemap;
00054 T & m_time;
00055 };
00056
00057
00058
00059
00060
00061
00062
00063
00064 class IMPDOMINOEXPORT RestraintGraph
00065 {
00066 typedef std::pair<unsigned int, unsigned int> Pair;
00067 typedef boost::adjacency_list < boost::vecS, boost::vecS,
00068 boost::undirectedS, boost::no_property,
00069 boost::property<boost::edge_weight_t,
00070 boost::vecS> > Graph;
00071 public:
00072
00073
00074
00075
00076
00077
00078 RestraintGraph(const JunctionTree &jt,Model *mdl,
00079 RestraintEvaluatorI *r_eval);
00080
00081
00082
00083
00084 void initialize_graph(int number_of_nodes);
00085
00086 void move_to_global_minimum_configuration() const;
00087
00088 Float move_to_configuration(const CombState &comb) const;
00089 CombState * get_minimum_configuration() const;
00090
00091
00092
00093
00094
00095 void add_node(unsigned int node_index, Particles &particles,
00096 RestraintEvaluatorI *rstr_eval);
00097
00098
00099
00100
00101
00102 void add_edge(unsigned int node1_ind, unsigned int node2_ind);
00103 void set_sampling_space(DiscreteSampler &ds);
00104
00105
00106
00107
00108
00109
00110
00111 void initialize_potentials(Restraint *r, Particles *ps, Float weight);
00112 unsigned int number_of_nodes() const {
00113 return num_vertices(g_);
00114 }
00115 unsigned int number_of_edges() const {
00116 return num_edges(g_);
00117 }
00118
00119
00120
00121
00122 void infer(unsigned int num_of_solutions=1);
00123
00124
00125 void show(std::ostream& out = std::cout) const;
00126
00127
00128 void analyse(std::ostream& out = std::cout) const;
00129
00130
00131
00132
00133
00134 void show_sampling_space(std::ostream& out = std::cout) const;
00135
00136
00137
00138 Float get_minimum_score() const {
00139 std::stringstream err_msg;
00140 err_msg << "RestraintGraph::get_minimum_score the graph has not been"
00141 << " infered";
00142 IMP_INTERNAL_CHECK(infered_, err_msg.str());
00143 return (*(min_combs_->begin()))->get_total_score();
00144 }
00145
00146 void clear();
00147
00148
00149
00150
00151
00152
00153 const CombState *get_opt_combination(unsigned int i) const;
00154
00155
00156
00157
00158
00159
00160
00161
00162 JNode * get_node(const Particles &p);
00163 inline bool is_sampling_space_set() const {return sampling_space_set_;}
00164
00165
00166
00167
00168 Particles get_particles() const;
00169 protected:
00170
00171
00172
00173
00174
00175
00176
00177 void load_data(const JunctionTree &jt,Model *mdl,
00178 RestraintEvaluatorI *r_eval);
00179
00180
00181
00182
00183
00184
00185
00186 void dfs_order(unsigned int root_ind);
00187
00188 JEdge* get_edge(unsigned int n1, unsigned int n2) const {
00189 return edge_data_.find(get_edge_key(n1, n2))->second;
00190 }
00191
00192
00193
00194
00195
00196 unsigned int collect_evidence(unsigned int father_ind);
00197
00198
00199
00200
00201
00202 void distribute_evidence(unsigned int father_ind);
00203
00204
00205
00206
00207
00208
00209 void distribute_minimum(unsigned int father_ind, CombState *min_comb);
00210
00211
00212
00213
00214 void update(unsigned int w, unsigned int v);
00215 Pair get_edge_key(unsigned int node1_ind, unsigned int node2_ind) const;
00216 protected:
00217 typedef boost::graph_traits<Graph>::edge_descriptor Edge;
00218 typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
00219
00220 void clear_infered_data();
00221
00222 std::map<Pair, JEdge *> edge_data_;
00223 std::vector<JNode *> node_data_;
00224 Graph g_;
00225 std::map<int, int> particle2node;
00226 std::vector<int> node2particle;
00227
00228 std::vector<unsigned int> discover_time_;
00229 unsigned int root_;
00230 std::vector<CombState *> *min_combs_;
00231
00232 bool infered_;
00233 bool sampling_space_set_;
00234 };
00235
00236 IMPDOMINO_END_NAMESPACE
00237
00238 #endif