Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InteractionGraphBase.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/pack/interaction_graph/InteractionGraphBase.hh
11 /// @brief Interaction graph base class header
12 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
13 
14 #ifndef INCLUDED_core_pack_interaction_graph_InteractionGraphBase_hh
15 #define INCLUDED_core_pack_interaction_graph_InteractionGraphBase_hh
16 
17 // Unit Headers
19 
20 // Package Headers
21 
23 
24 // Project Headers
25 #include <core/types.hh>
26 
27 // Utility Headers
28 #include <utility/pointer/ReferenceCount.hh>
29 // AUTO-REMOVED #include <utility/vector1.hh>
30 
31 // STL Headers
32 #include <iosfwd>
33 // AUTO-REMOVED #include <vector>
34 #include <list>
35 // AUTO-REMOVED #include <assert.h>
36 
37 // ObjexxFCL Headers
38 #include <ObjexxFCL/FArray1D.hh>
39 #include <ObjexxFCL/FArray2D.hh>
40 
41 #include <utility/vector1_bool.hh>
42 
43 
44 namespace core {
45 namespace pack {
46 namespace interaction_graph {
47 
48 class NodeBase
49 {
50 public:
51  virtual ~NodeBase();
52  NodeBase( InteractionGraphBase*, int node_id, int num_states);
53  int get_num_states() const;
54  std::list< EdgeBase* >::iterator add_edge(EdgeBase* edge_ptr);
55  void drop_edge(std::list< EdgeBase* >::iterator edge_iterator);
56  void drop_all_edges();
57  EdgeBase* find_edge(int other_node_index) const;
58 
59  virtual void assign_zero_state() = 0;
60  virtual void prepare_for_simulated_annealing() = 0;
61  virtual void add_to_one_body_energies( ObjexxFCL::FArray1< core::PackerEnergy > & energy1b ) = 0;
62  virtual void add_to_one_body_energy( int state, core::PackerEnergy energy ) = 0;
63  virtual void update_one_body_energy( int state, core::PackerEnergy energy) = 0;
64  virtual void zero_one_body_energies() = 0;
65  virtual void print() const = 0;
66  virtual bool state_unassigned() const = 0;
67 
69 
70  virtual unsigned int count_static_memory() const = 0;
71  virtual unsigned int count_dynamic_memory() const;
72 
73  std::list< EdgeBase * >::const_iterator edge_list_begin();
74  std::list< EdgeBase * >::const_iterator edge_list_end();
75 
76 protected:
77  void update_edge_vector();
78 
79 public:
80 
81  //Read access to private data granted to derived classes
82  //inlined for efficiency
83  inline
84  int get_node_index() const
85  {
86  return node_index_;
87  }
88 
89  inline
91  {
92  return num_incident_edges_;
93  }
94 
95  inline
97  {
99  }
100 
101  inline
103  {
105  }
106 
107 public:
108 
109  /// These functions are public for the sake of writing good unit tests
110 
111  inline
112  EdgeBase const * get_incident_edge( int index ) const
113  {
114  assert( edge_vector_up_to_date_ );
115  return incident_edge_vector_[ index ];
116  }
117 
118  inline
120  {
121  assert( edge_vector_up_to_date_ );
122  return incident_edge_vector_[ index ];
123  }
124 
125  inline
126  int get_index_of_adjacent_node( int index ) const
127  {
128  assert( edge_vector_up_to_date_ );
129  return adjacent_node_ind_[ index ];
130  }
131 
132  inline
133  NodeBase const * get_adjacent_node( int index ) const
134  {
135  assert( edge_vector_up_to_date_ );
136  return adjacent_node_[ index ];
137  }
138 
139 
140  inline
142  {
143  assert( edge_vector_up_to_date_ );
144  return adjacent_node_[ index ];
145  }
146 
147 protected:
148  inline
150  {
152  }
153 
154  inline
156  {
157  return owner_;
158  }
159 
160  inline
162  {
163  return owner_;
164  }
165 
166 
167 private:
168 
174  std::list< EdgeBase* > incident_edge_list_;
175  std::vector< EdgeBase* > incident_edge_vector_;
176  std::vector< int > adjacent_node_ind_;
177  std::vector< NodeBase* > adjacent_node_;
180 
181  //no default constructor, uncopyable
182  NodeBase();
183  NodeBase( NodeBase const & );
185 };
186 
187 class EdgeBase
188 {
189 public:
190  virtual ~EdgeBase();
191  EdgeBase(InteractionGraphBase* owner, int first_node_ind, int second_node_ind);
192 
193  int get_other_ind(int callers_index) const;
194  NodeBase* get_other_node(int callers_index) const;
195  int get_first_node_ind() const;
196  int get_second_node_ind() const;
197  void set_pos_in_owners_list( std::list< EdgeBase* >::iterator edge_iterator);
198  void set_pos_in_node_edgevector(int callers_index, int position);
199 
200  bool same_edge(int node1, int node2) const;
201 
202  virtual void declare_energies_final() = 0;
203  virtual void prepare_for_simulated_annealing() = 0;
204 
205  virtual unsigned int count_static_memory() const = 0;
206  virtual unsigned int count_dynamic_memory() const;
207 
208  Real edge_weight() const {
209  return edge_weight_;
210  }
211 
212  virtual void set_edge_weight( Real weight ) = 0;
213 
214 protected:
215 
216  //Read access to private data granted to derived classes
217  inline
218  int get_node_index( int index ) const
219  {
220  assert( index == 0 || index == 1 );
221  return node_indices_[ index ];
222  }
223 
224  inline
225  int get_num_states_for_node( int index ) const
226  {
227  assert( index == 0 || index == 1 );
228  return num_node_states_[ index ];
229  }
230 
231  inline
232  NodeBase const *
233  get_node( int index ) const
234  {
235  assert( index == 0 || index == 1 );
236  return nodes_[ index ];
237  }
238 
239  inline
240  NodeBase *
241  get_node( int index )
242  {
243  assert( index == 0 || index == 1 );
244  return nodes_[ index ];
245  }
246 
247  inline
249  {
250  assert( index == 0 || index == 1 );
251  return pos_in_nodes_edge_vector_[ index ];
252  }
253 
254 public:
255  inline
257  {
258  return owner_;
259  }
260 
261  inline
263  {
264  return owner_;
265  }
266 
267 protected:
268 
269  /// @brief is a node the first or second node this edge is incident upon?
270  inline
271  int
272  which_node( int node_index ) const {
273  assert( node_index == node_indices_[ 0 ] || node_index == node_indices_[ 1 ] );
274  return ( node_index == node_indices_[ 0 ] ? 0 : 1 );
275  }
276 
277  /// @brief protected setter of the edge weight. To be called by derived
278  /// classes after they have completed the conversion from the previous edge weighting
279  /// to the new edge weighting.
280  void
281  edge_weight( Real );
282 
283 private:
288  std::list< EdgeBase* >::iterator pos_in_nodes_edge_list_[2];
289  std::list< EdgeBase* >::iterator pos_in_owners_edge_list_;
291 
292  /// Allow the arbitrary scaling of energies for each edge.
293  /// The derived classes have the responsibility of scaling each
294  /// energy as it is added to the edge, and also updating all of
295  /// the edge energies if the edge weight changes *after* all
296  /// the edge energies have been stored -- that is, by dividing each
297  /// energy by the old weight and multiplying by the new weight.
299 
300 
301  //no default constructor, uncopyable
302  EdgeBase();
303  EdgeBase( EdgeBase const & );
305 
306 };
307 
309 {
310 public:
311  virtual ~InteractionGraphBase();
312 
313  InteractionGraphBase(int num_nodes);
314 
315  virtual int get_num_nodes_v() const
316  {
317  return get_num_nodes();
318  }
319 
320  inline
321  int get_num_nodes() const
322  {
323  return num_ig_nodes_;
324  }
325 
326  virtual void initialize( rotamer_set::RotamerSetsBase const & rot_sets ) = 0;
327 
328  void set_num_states_for_node(int node, int num_states);
329  int get_num_states_for_node(int node) const;
331  virtual core::PackerEnergy get_one_body_energy_for_node_state( int node, int state) = 0;
332  void add_edge( int node1, int node2);
333  bool get_edge_exists(int node1, int node2);
334  void drop_all_edges_for_node( int node );
335 
336  void print_vertices() const;
337  virtual void print() const {};
338  void output_connectivity(std::ostream & os) const;
339  void output_dimacs(std::ostream & os) const;
340 
341  /// @brief iterate across edges and nodes and allow them to prepare
342  /// for simulated annealing
343  virtual void prepare_for_simulated_annealing();
344 
345  virtual void blanket_assign_state_0() = 0;
346  virtual core::PackerEnergy set_state_for_node(int node_ind, int new_state) = 0;
347  virtual core::PackerEnergy set_network_state( ObjexxFCL::FArray1_int & node_states) = 0;
348  virtual void consider_substitution(
349  int node_ind,
350  int new_state,
351  core::PackerEnergy & delta_energy,
352  core::PackerEnergy & prev_energy_for_node) = 0;
355 
356  void set_edge_weight( int node1, int node2, Real edge_weight );
357  Real get_edge_weight( int node1, int node2 ) const;
358 
359  virtual int get_edge_memory_usage() const = 0;
360  virtual void print_current_state_assignment() const = 0;
361  virtual void set_errorfull_deltaE_threshold( core::PackerEnergy deltaE ) = 0;
362 
363  bool any_vertex_state_unassigned() const;
364 
365  void add_to_one_body_energies( ObjexxFCL::FArray1< core::PackerEnergy > & one_body_energies );
366  void update_one_body_energies( ObjexxFCL::FArray1< core::PackerEnergy > & old_energy1b, ObjexxFCL::FArray1< core::PackerEnergy > & new_energy1b);
367  void zero_one_body_energies_for_node( int node );
368 
370  (
371  int node_ind,
372  utility::vector1< core::PackerEnergy > const & one_body_energies
373  );
374 
376  (
377  int node_ind,
378  ObjexxFCL::FArray1< core::PackerEnergy > const & one_body_energies
379  );
380 
381  /// @brief interface to PrecomputedPairEnergiesNode::add_to_nodes_one_body_energy
383  (
384  int node_ind,
385  int state_id,
386  core::PackerEnergy const one_body_energy
387  );
388 
389 
390  void set_number_of_energy_sum_vertex_groups( int num_groups );
391  void set_vertex_member_of_group( int vertex, int group );
392  void print_vertex_groups();
393  virtual core::PackerEnergy get_energy_sum_for_vertex_group( int group_id ) = 0;
395  void note_vertex_reached( int node_index );
396  bool vertex_already_reached( int node_index );
397 
398  inline
399  bool get_vertex_member_of_energy_sum_group( int node_index, int group_id )
400  {
401  assert( num_energy_sum_groups_ != -1 &&
402  node_index > 0 && node_index <= num_ig_nodes_ &&
403  group_id > 0 && group_id <= num_energy_sum_groups_);
404  return energy_sum_group_membership_( node_index, group_id );
405  }
406 
407  virtual unsigned int getTotalMemoryUsage() const;
408 
409  /// Methods for iterating across edges of the interaction graph.
410  /// Protected access since the raw edge lists contain non-const pointers.
411 
412  /// @brief set the Graph's (single) edge list iterator to the beginning of the edge list
413  /// for a particular node
414  void reset_edge_list_iterator_for_node( int node_index ) const;
415  /// @brief increment the (single) edge list iterator to the next element
416  void increment_edge_list_iterator() const;
417  /// @brief test: have we arrived at the edge list end?
418  bool edge_list_iterator_at_end() const;
419  /// @brief return a const reference to an edge pointed at by the list iterator
420  EdgeBase const & get_edge() const;
421 
422  friend class NodeBase;
423  friend class EdgeBase;
424 
425 protected:
426 
427  virtual unsigned int count_static_memory() const = 0;
428  virtual unsigned int count_dynamic_memory() const;
429 
430  void drop_edge(std::list< EdgeBase* >::iterator edge);
431 
432 public:
433 
434  /// The following functions provide access to the nodes and edges in the graph
435  /// though, their use is strongly discouraged except for in writing unit tests
436  /// to ensure that the graphs are properly implemented.
437 
438  EdgeBase const * find_edge(int node1, int node2) const;
439  EdgeBase * find_edge(int node1, int node2);
440 
441  virtual NodeBase* create_new_node( int node_index, int num_states) = 0;
442  virtual EdgeBase* create_new_edge( int index1, int index2) = 0;
443 
444  inline
445  NodeBase* get_node( int index ) const
446  {
447  assert( index > 0 && index <= num_ig_nodes_ );
448  return ig_nodes_[ index ];
449  }
450 
451  inline
452  int get_num_edges() const
453  {
454  return ig_edge_list_.size();
455  }
456 
457  inline
458  std::list< EdgeBase* >::iterator get_edge_list_begin()
459  {
460  return ig_edge_list_.begin();
461  }
462 
463  inline
464  std::list< EdgeBase* >::const_iterator get_edge_list_begin() const
465  {
466  return ig_edge_list_.begin();
467  }
468 
469  inline
470  std::list< EdgeBase* >::const_iterator get_edge_list_end() const
471  {
472  return ig_edge_list_.end();
473  }
474 
475 protected:
476 
477  inline
478  int get_node_state_offset( int index ) const
479  {
480  assert( index > 0 && index <= num_ig_nodes_ );
481  return node_state_offsets_[ index ];
482  }
483 
484  bool
485  mine( EdgeBase const * edge ) const {
486  return edge->get_owner() == this;
487  }
488 
489 private:
491  std::vector< NodeBase* > ig_nodes_;
492  std::list< EdgeBase* > ig_edge_list_;
493 
494  std::vector< int > node_state_offsets_;
497  mutable std::list< EdgeBase * >::const_iterator focused_edge_iterator_;
498  mutable std::list< EdgeBase * >::const_iterator focused_edge_iterator_end_;
499 
501  ObjexxFCL::FArray2D_bool energy_sum_group_membership_;
502  ObjexxFCL::FArray1D_int component_membership_;
503 
504  //no default constructor, uncopyable
508 };
509 
510 } //end namespace interaction_graph
511 } //end namespace pack
512 } //end namespace core
513 
514 #endif //INTERACTION_GRAPH_BASE_CLASS_H