Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DensePDInteractionGraph.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/DensePDInteractionGraph.hh
11 /// @brief Dense Edge-Matrix, Pairwise Decomposable interaction graph class header
12 /// This graph does not take advantage of the memory-saving AANeighborSparseMatrix
13 /// for the storage of RPEs (rotamer pair energies); for this reason it's a little
14 /// bit faster and weights a little less than the PDInteractionGraph for fixed-sequence
15 /// repackings -- memory becomes a significant issue for design simulations, and
16 /// this graph should not be used for that purpose.
17 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
18 
19 
20 #ifndef INCLUDED_core_pack_interaction_graph_DensePDInteractionGraph_hh
21 #define INCLUDED_core_pack_interaction_graph_DensePDInteractionGraph_hh
22 
23 // Unit headers
25 
26 // Package Headers
27 
30 
31 //STL Headers
32 // AUTO-REMOVED #include <vector>
33 #include <list>
34 
35 //ObjexxFCL Headers
36 #include <ObjexxFCL/FArray1D.hh>
37 #include <ObjexxFCL/FArray2D.hh>
38 
39 #include <ObjexxFCL/FArray2A.hh>
40 
41 
42 namespace core {
43 namespace pack {
44 namespace interaction_graph {
45 
46 // belongs in DensePDInteractionGraph.fwd.hh
47 class DensePDNode;
48 class DensePDEdge;
49 class DensePDInteractionGraph;
50 
52 {
53 public:
54  DensePDNode(InteractionGraphBase * owner, int node_id, int num_states);
55  virtual ~DensePDNode();
56  virtual void print() const;
57 
58  void update_one_body_energy( int state, core::PackerEnergy energy );
59  virtual void update_one_body_energies( ObjexxFCL::FArray1< core::PackerEnergy > & energies );
60  void add_to_one_body_energy( int state, core::PackerEnergy energy );
61  virtual void add_to_one_body_energies( ObjexxFCL::FArray1< core::PackerEnergy > & energies );
62  virtual void zero_one_body_energies();
64 
65  virtual void prepare_for_simulated_annealing();
66  //virtual unsigned int getMemoryUsageInBytes() const;
67 
68  void assign_zero_state();
69  virtual bool state_unassigned() const { return current_state_ == 0;}
70  void assign_state(int new_state);
71  int get_current_state() const;
74  (
75  int alternate_state,
76  core::PackerEnergy & prev_node_energy
77  );
79 
80  // <directed_design>
81  /* void project_deltaE_for_substitution
82  (
83  int alternate_state,
84  core::PackerEnergy & deltaE_unweighted,
85  core::PackerEnergy & prevE_unweighted,
86  core::PackerEnergy & deltaE_weighted,
87  core::PackerEnergy & prevE_weighted,
88  ObjexxFCL::FArray2D< core::PackerEnergy > const& weights
89  ); */
90 
91  //core::PackerEnergy get_weighted_energy_with_higher_indexed_nodes(ObjexxFCL::FArray2D< core::PackerEnergy > const& weights) const;
92  // </directed_design>
93 
94  inline
96  int edge_to_altered_neighbor,
97  core::PackerEnergy new_edge_energy,
98  int other_node_new_state);
99 
100  void print_internal_energies() const;
101 
103 
104  /*
105  void prepare_to_write_to_file();
106  void initialize_aa_for_state_array();
107  void clean_up_after_writing_to_file();
108  void prepare_to_read_energies_from_file( int num_states_for_node_in_file );
109  void clean_up_after_reading_energies_from_file();
110 
111  void set_aa_for_file_state(int file_state, int aa );
112  void set_instance_state_correspondence( int instance_state, int state_from_file );
113  int get_correspondence_for_state( int instance_state );
114 
115  int get_num_rots_absent_from_file();
116  void get_absent_rots( ObjexxFCL::FArray1_int & rots_absent );
117 
118  int get_num_states_in_file();
119  int & get_aatypes_for_file_states();
120 
121  int & get_aatypes_for_states();
122  int & get_num_file_states_for_aa();
123  int & get_file_states_2_instance_states_array();
124 
125  bool get_node_corresponded_to_file_node();
126  */
127 
128  virtual unsigned int count_static_memory() const;
129  virtual unsigned int count_dynamic_memory() const;
130 
131 protected:
133 
134 private:
135 
136  inline DensePDEdge * get_incident_dpd_edge( int index );
137  inline DensePDEdge const * get_incident_dpd_edge( int index ) const;
138 
139  inline DensePDNode * get_adjacent_dpd_node( int index );
140  inline DensePDNode const * get_adjacent_dpd_node( int index ) const;
141 
143  inline DensePDInteractionGraph const * get_dpdig_owner() const;
144 
145  std::vector< core::PackerEnergy > one_body_energies_;
146 
147  std::vector< int > neighbors_curr_state_;
148  std::vector< ObjexxFCL::FArray2A< core::PackerEnergy > > edge_matrix_ptrs_;
149 
153  std::vector< core::PackerEnergy > curr_state_two_body_energies_;
154 
158  std::vector< core::PackerEnergy > alternate_state_two_body_energies_;
159 
160  /*
161  //variables for I/O
162  int num_states_in_file_;
163  ObjexxFCL::FArray1D_int instance_states_2_file_states_;
164  ObjexxFCL::FArray1D_int file_states_2_instance_states_;
165  ObjexxFCL::FArray1D_int aa_types_for_file_states_;
166  ObjexxFCL::FArray1D_int aa_types_for_instance_states_; //useful only for I/O
167  ObjexxFCL::FArray1D_int num_file_states_for_aa_;
168  */
169 
171 
172  //no default constructor, uncopyable
173  DensePDNode();
174  DensePDNode( DensePDNode const & );
175  DensePDNode & operator = ( DensePDNode const & );
176 };
177 
179 {
180 public:
181  DensePDEdge(InteractionGraphBase* owner, int first_node_ind, int second_node_ind);
182  virtual ~DensePDEdge();
183  virtual void set_sparse_aa_info(ObjexxFCL::FArray2_bool const & ) {}
184  virtual bool get_sparse_aa_info( int, int) const;// {return true;} //"all amino acids are neighbors"
185  virtual void add_to_two_body_energy(int const, int const, core::PackerEnergy const);
186  virtual void
187  add_to_two_body_energies( ObjexxFCL::FArray2< core::PackerEnergy > const & res_res_energy_array );
188  virtual
189  void set_two_body_energy(int const, int const, core::PackerEnergy const);
190  virtual
191  void clear_two_body_energy(int const, int const);
192  virtual core::PackerEnergy get_two_body_energy( int const, int const ) const;
193 
194  virtual void force_aa_neighbors(int, int) {} //all aa's are already neighbors -- dense representation
195  virtual void force_all_aa_neighbors() {} //same thing
196 
197 
198  virtual void declare_energies_final();
199  virtual void prepare_for_simulated_annealing();
200  //virtual unsigned int getMemoryUsageInBytes() const;
201 
203 
205  int node_ind,
206  int new_state,
207  core::PackerEnergy & new_energy
208  );
209  void acknowledge_state_zeroed( int node_ind );
210 
211  static
212  inline
214  int first_node_state,
215  int second_node_state,
216  ObjexxFCL::FArray2< core::PackerEnergy > & edge_energy_table
217  );
218 
219  inline void acknowledge_substitution(
220  int substituted_node_index,
221  core::PackerEnergy const curr_state_energy,
222  int nodes_new_state
223  );
224 
225  int get_two_body_table_size() const;
226  ObjexxFCL::FArray2A< core::PackerEnergy > get_edge_table_ptr();
227 
228  virtual unsigned int count_static_memory() const;
229  virtual unsigned int count_dynamic_memory() const;
230 
231  virtual void set_edge_weight( Real weight );
232 
233  /// @brief return all energies on this edge...
234  ObjexxFCL::FArray2D< core::PackerEnergy >
236 
237 
238  void
240  ObjexxFCL::FArray2D< core::PackerEnergy > & new_edge_table
241  );
242 
243 private:
244 
245  inline DensePDNode * get_dpd_node( int index );
246  inline DensePDNode const * get_dpd_node( int index ) const;
247 
249  inline DensePDInteractionGraph const * get_dpdig_owner() const;
250 
251  ObjexxFCL::FArray2D< core::PackerEnergy > two_body_energies_; //Dense matrix
254 
255  //no default constructor, uncopyable
256  DensePDEdge();
257  DensePDEdge( DensePDEdge const & );
258  DensePDEdge & operator = ( DensePDEdge const & );
259 };
260 
262 {
263 public:
264  DensePDInteractionGraph(int num_nodes);
265  virtual void initialize( rotamer_set::RotamerSetsBase const & rot_sets );
266 
267  virtual core::PackerEnergy get_one_body_energy_for_node_state( int node, int state);
268 
269  //virtual void set_num_aatypes(int) {}
270  virtual int get_num_aatypes() const {return 1;}
271 
272  virtual void blanket_assign_state_0();
273  virtual core::PackerEnergy set_state_for_node(int node_ind, int new_state);
274  virtual core::PackerEnergy set_network_state( ObjexxFCL::FArray1_int & node_states);
275  virtual void consider_substitution
276  (
277  int node_ind,
278  int new_state,
279  core::PackerEnergy & delta_energy,
280  core::PackerEnergy & prev_energy_for_node
281  );
282 
283  /// @brief Accepts (commits) the state change previously considered in a call to
284  /// consider_substitution and returns the energy of the entire graph
286 
287  /// @brief removes all accumulated numerical drift and returns the
288  /// energy for the current state assignment.
290  /// @brief returns the number of floats used in all edge two-body energy tables
291  virtual int get_edge_memory_usage() const;
292 
293  /// @brief outputs the current state for each node, useful for debugging
294  virtual void print_current_state_assignment() const;
296 
297  /// @brief a user may define subsets of the vertex set for which they would like to
298  /// know the internal energy sum.
299  virtual core::PackerEnergy get_energy_sum_for_vertex_group( int group_id );
300 
301  virtual unsigned int count_static_memory() const;
302  virtual unsigned int count_dynamic_memory() const;
303 
304  /// @brief Override the InteractionGraphBase class's implementation of this function
305  /// to return 'true'.
306  virtual
307  bool
309 
310  virtual
312  int node_ind,
313  int node_state
314  ) const;
315 
316  virtual
317  ObjexxFCL::FArray2D< core::PackerEnergy >
319  int node1,
320  int node2,
321  int node1aa,
322  int node2aa
323  ) const;
324 
325 
326  /// @brief Swap the contents of the edge pair-energy table with the contents in the new_edge_table
327  void
328  swap_edge_energies( int node1, int node2, ObjexxFCL::FArray2D< core::PackerEnergy > & new_edge_table );
329 
330 protected:
331  //virtual unsigned int getMemoryUsageInBytes() const;
332 
333  virtual NodeBase* create_new_node( int node_index, int num_states);
334  virtual EdgeBase* create_new_edge( int index1, int index2);
335 
336  /// @brief removes numerical drift that can accumulate over the course of
337  /// many state assignment changes within simulated annealing
339 
340  inline
341  DensePDNode* get_dpd_node(int index) const
342  {
343  return (DensePDNode*) get_node( index );
344  }
345 
346  inline
347  DensePDEdge const * get_dpd_edge( int node1, int node2 ) const
348  {
349  return (DensePDEdge const *) find_edge( node1, node2 );
350  }
351 
352  inline
353  DensePDEdge * get_dpd_edge( int node1, int node2 )
354  {
355  return (DensePDEdge *) find_edge( node1, node2 );
356  }
357 
358 private:
363 
364  static const int COMMIT_LIMIT_BETWEEN_UPDATES = 1024; // 2^10
365 
366  //no default constructor, uncopyable
370 };
371 
372 inline
374 {
375  return static_cast< DensePDEdge * > (get_incident_edge( index ));
376 }
377 
379 {
380  return static_cast< DensePDEdge const * > (get_incident_edge( index ));
381 }
382 
383 inline
385 {
386  return static_cast< DensePDNode * > (get_adjacent_node( index ));
387 }
388 
389 inline
391 {
392  return static_cast< DensePDNode const * > (get_adjacent_node( index ));
393 }
394 
395 inline
397 {
398  return static_cast< DensePDInteractionGraph * > (get_owner());
399 }
400 
401 inline
403 {
404  return static_cast< DensePDInteractionGraph const * > (get_owner());
405 }
406 
407 inline
409 {
410  return static_cast< DensePDNode* > (get_node( index ));
411 }
412 
413 inline
415 {
416  return static_cast< DensePDInteractionGraph const * > (get_owner());
417 }
418 
419 inline
421 {
422  return static_cast< DensePDInteractionGraph* > (get_owner());
423 }
424 
425 /// @brief updates bookkeeping arrays for when a neighbor has changed its state
426 ///
427 /// @param edge_to_altered_neighbor - [in] - the index for the edge that connects
428 /// this node to the node that just changed its state
429 /// @param new_edge_energ - [in] - the pair energy between this node in its current
430 /// state and the new state of the node that just changed its state
431 /// @param other_node_new_state - [in] - the state the neighbor just adopted
432 inline
434  int edge_to_altered_neighbor,
435  core::PackerEnergy new_edge_energy,
436  int other_node_new_state
437 )
438 {
439 
441  new_edge_energy - curr_state_two_body_energies_[edge_to_altered_neighbor];
442  curr_state_two_body_energies_[edge_to_altered_neighbor] = new_edge_energy;
443  neighbors_curr_state_[ edge_to_altered_neighbor ] = other_node_new_state;
444  return;
445 }
446 
447 /// @brief static method that looks up the two body energy when the
448 /// node with the smaller index on an edge is considering an alternate state
449 ///
450 /// @param first_node_alt_state - [in] - the alternate state for the lower-indexed node
451 /// @param second_node_orig_state - [in] - the current state for the higher-indexed node
452 /// @param edge_energy_table - [in] - the proxy FArray pointing at the edge table
453 /// connecting the two nodes.
454 inline
457  int first_node_state,
458  int second_node_state,
459  ObjexxFCL::FArray2< core::PackerEnergy > & edge_energy_table
460 )
461 {
462  if (first_node_state == 0 || second_node_state == 0) {
463  return 0.0f;
464  } else {
465  return edge_energy_table( second_node_state, first_node_state );
466  }
467 }
468 
469 
470 /// @brief update bookkeeping information when one of the nodes an edge is incident
471 /// upon changes state
472 ///
473 /// @param substituted_node_index - [in] - index of the node that chagned its state
474 /// @param curr_state_energy - [in] - the two body energy given the new state
475 /// @param nodes_new_state - [in] - the state the node just transitioned into
476 /// @param nodes_new_state_sparse_info - [in] - sparse matrix info for the new state
477 inline
478 void
480  int substituted_node_index,
481  core::PackerEnergy const curr_state_energy,
482  int nodes_new_state
483 )
484 {
485  int node_substituted = substituted_node_index == get_node_index(0) ? 0 : 1;
486  int node_not_substituted = ! node_substituted;
487 
488  curr_state_energy_ = curr_state_energy;
489 
490  get_dpd_node( node_not_substituted )->
491  acknowledge_neighbors_state_substitution
492  (
493  get_edges_position_in_nodes_edge_vector( node_not_substituted ),
495  nodes_new_state
496  );
497 }
498 
499 /// @brief returns the change in energy that would be induced by switching this node
500 /// from its current state into another state
501 ///
502 /// iterates across the incident edges for a node in two phases:
503 /// in the first phase, it examines edges leading to higher-indexed nodes
504 /// in the second phase, it examines edges leading to smaller-indexed nodes.
505 /// for cache efficiency, all of the amino-acid-neighbor-offset information
506 /// that each edge calculates is stored on the nodes themselves. The edges
507 /// are never touched; rather, their private information is stored on the nodes
508 /// and handed to static member functions of the DensePDEdge class. This "store
509 /// edge information on the nodes" strategy gives me performance equivalent
510 /// to the previous energy2b lookup tables.
511 ///
512 /// @param alternate_state - [in] - the alternate state to consider
513 /// @param previous_energy_for_node - [out] - the old energy1b/energy2b sum for this
514 /// node; used by simulate annealing.
515 ///
516 inline
519  int alternate_state,
520  core::PackerEnergy & prev_energy_for_node
521 )
522 {
523 
525  //std::cerr << "proj_deltaE: node - " << get_node_index()
526  // << " alt state " << alternate_state << "...";
527 
528  alternate_state_ = alternate_state;
531  prev_energy_for_node = curr_state_total_energy_;
532 
533 
534  for (int ii = 1; ii <= get_num_edges_to_smaller_indexed_nodes(); ++ii)
535  {
536 
542  );
544  }
545 
546  for (int ii = get_num_edges_to_smaller_indexed_nodes() + 1;
547  ii <= get_num_incident_edges(); ++ii)
548  {
554  );
556  }
557 
558  //std::cerr<< "..done" << std::endl;
559 
561 
562 }
563 
564 
565 } // namespace interaction_graph
566 } // namespace pack
567 } // namespace core
568 
569 #endif