Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FASTERInteractionGraph.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/FASTERInteractionGraph.hh
11 /// @brief
12 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
13 
14 
15 #ifndef INCLUDED_core_pack_interaction_graph_FASTERInteractionGraph_hh
16 #define INCLUDED_core_pack_interaction_graph_FASTERInteractionGraph_hh
17 
18 // Unit headers
20 
21 // Package Headers
22 
25 
26 //STL Headers
27 // AUTO-REMOVED #include <vector>
28 #include <list>
29 
30 //ObjexxFCL Headers
31 #include <ObjexxFCL/FArray1D.hh>
32 #include <ObjexxFCL/FArray2D.hh>
33 #include <ObjexxFCL/FArray2A.hh>
34 
35 
36 namespace core {
37 namespace pack {
38 namespace interaction_graph {
39 
40 class FASTERNode;
41 class FASTEREdge;
42 
44 {
45 public:
47 public:
48  FASTERNode(InteractionGraphBase * owner, int node_id, int num_states);
49  virtual ~FASTERNode();
50  virtual void print() const;
51 
52  void update_one_body_energy( int state, core::PackerEnergy energy );
53  virtual void update_one_body_energies( ObjexxFCL::FArray1< core::PackerEnergy > & energies );
54  void add_to_one_body_energy( int state, core::PackerEnergy energy );
55  virtual void add_to_one_body_energies( ObjexxFCL::FArray1< core::PackerEnergy > & energies );
56  virtual void zero_one_body_energies();
58 
59  virtual void prepare_for_simulated_annealing();
60  void prepare_for_FASTER();
61  //virtual unsigned int getMemoryUsageInBytes() const;
62 
63  void assign_zero_state();
64  virtual bool state_unassigned() const { return current_state_ == 0; }
65  void assign_state(int new_state);
66  int get_current_state() const;
69  (
70  int alternate_state,
71  core::PackerEnergy & prev_node_energy
72  );
74 
75  inline
77  int edge_to_altered_neighbor,
78  core::PackerEnergy new_edge_energy,
79  int other_node_new_state);
80 
81  void print_internal_energies() const;
83 
84  /*
85  void prepare_to_write_to_file();
86  void initialize_aa_for_state_array();
87  void clean_up_after_writing_to_file();
88  void prepare_to_read_energies_from_file( int num_states_for_node_in_file );
89  void clean_up_after_reading_energies_from_file();
90 
91  void set_aa_for_file_state(int file_state, int aa );
92  void set_instance_state_correspondence( int instance_state, int state_from_file );
93  int get_correspondence_for_state( int instance_state );
94 
95  int get_num_rots_absent_from_file();
96  void get_absent_rots( ObjexxFCL::FArray1DB_int & rots_absent );
97 
98  int get_num_states_in_file();
99  int & get_aatypes_for_file_states();
100 
101  int & get_aatypes_for_states();
102  int & get_num_file_states_for_aa();
103  int & get_file_states_2_instance_states_array();
104 
105  bool get_node_corresponded_to_file_node();
106  */
107 
108  virtual unsigned int count_static_memory() const;
109  virtual unsigned int count_dynamic_memory() const;
110 
111 protected:
113 
114  inline FASTEREdge const * get_incident_faster_edge( int index ) const;
115  inline FASTEREdge * get_incident_faster_edge( int index );
116 
117  inline FASTERNode const * get_adjacent_faster_node( int index ) const;
118  inline FASTERNode * get_adjacent_faster_node( int index );
119 
120  inline FASTERInteractionGraph const * get_fasterig_owner() const;
122 
123 public:
124 
125  // Methods for FASTER
128  void partial_assign_relaxed_state( Real probability );
129  void partial_assign_state( int state );
131  int which_neighbor,
132  int neighbors_new_state
133  );
136  int which_neighbor,
137  int const neighbors_perturbed_state
138  );
139 
141 
143 
144  void set_perturbed_state( int perturbed_state );
145 
146 
147  void relax_neighbors();
148 
149  void relax();
150 
152  void reset_relaxed();
158  int get_relaxed_state() const;
159  int get_random_neighbor();
160 
161 private:
162  // Data
163 
164  std::vector< core::PackerEnergy > one_body_energies_;
165 
166  std::vector< int > neighbors_curr_state_;
167  std::vector< ObjexxFCL::FArray2A< core::PackerEnergy > > edge_matrix_ptrs_;
168 
172  std::vector< core::PackerEnergy > curr_state_two_body_energies_;
173 
177  std::vector< core::PackerEnergy > alternate_state_two_body_energies_;
178 
180 
181  //variables for FASTER
188  std::vector< core::PackerEnergy > state_energies_in_current_state_assignment_;
189  std::vector< core::PackerEnergy > state_energies_in_current_context_;
190  std::vector< bool > neighbor_relaxed_in_sBR_;
191  std::vector< core::PackerEnergy > perturbed_two_body_energies_;
192 
193  //no default constructor, uncopyable
194  FASTERNode();
195  FASTERNode( FASTERNode const & );
196  FASTERNode & operator = ( FASTERNode const & );
197 };
198 
200 {
201 public:
203 
204 public:
205  FASTEREdge(InteractionGraphBase* owner, int first_node_ind, int second_node_ind);
206  virtual ~FASTEREdge();
207  virtual void set_sparse_aa_info(ObjexxFCL::FArray2_bool const & ) {}
208  virtual bool get_sparse_aa_info( int, int) const {return true;} //"all amino acids are neighbors"
209  virtual void add_to_two_body_energy(int const, int const, core::PackerEnergy const);
210  virtual void add_to_two_body_energies( ObjexxFCL::FArray2< core::PackerEnergy > const & res_res_energy_array );
211  virtual
212  void set_two_body_energy(int const, int const, core::PackerEnergy const);
213  virtual
214  void clear_two_body_energy(int const, int const);
215  virtual core::PackerEnergy get_two_body_energy( int const, int const ) const;
216 
217  virtual void force_aa_neighbors(int, int) {} //all aa's are already neighbors -- dense representation
218  virtual void force_all_aa_neighbors() {} //same thing
219 
220 
221  virtual void declare_energies_final();
222  virtual void prepare_for_simulated_annealing();
223  virtual void prepare_for_FASTER();
224  //virtual unsigned int getMemoryUsageInBytes() const;
225 
227 
229  int node,
230  int new_state
231  );
232 
234  int node_ind,
235  int new_state,
236  core::PackerEnergy & new_energy
237  );
238  void acknowledge_state_zeroed( int node_ind );
239 
240  static
241  inline
243  int first_node_state,
244  int second_node_state,
245  ObjexxFCL::FArray2< core::PackerEnergy > & edge_energy_table
246  );
247 
248  inline void acknowledge_substitution(
249  int substituted_node_index,
250  core::PackerEnergy const curr_state_energy,
251  int nodes_new_state
252  );
253 
254  ObjexxFCL::FArray2A< core::PackerEnergy > get_edge_table_ptr();
255 
256  int get_two_body_table_size() const;
257 
258  //ObjexxFCL::FArray2Da< core::PackerEnergy > get_lonode_edge_table_ptr();
259  //ObjexxFCL::FArray2Da< core::PackerEnergy > get_hinode_edge_table_ptr();
260 
261  virtual unsigned int count_static_memory() const;
262  virtual unsigned int count_dynamic_memory() const;
263 
264  //methods for FASTER
266 
268 
271  int node,
272  int nodes_perturbed_state
273  );
274 
275  void
277  int node,
278  int neighbors_context_state
279  );
280 
284 
285  void set_edge_weight( Real weight );
286 
287  void
289  ObjexxFCL::FArray2D< core::PackerEnergy > & new_edge_table
290  );
291 
292 private:
293  inline
294  FASTERNode* get_faster_node( int index ) const
295  {
296  return (FASTERNode*) get_node( index ); //c-style cast since static_cast won't compile
297  }
298 
299  inline
301  {
302  return (FASTERInteractionGraph*) get_owner(); //c-style cast since static_cast won't compile
303  }
304 
305  //ObjexxFCL::FArray2D< core::PackerEnergy > lonode_two_body_energies_; //Dense matrix
306  //ObjexxFCL::FArray2D< core::PackerEnergy > hinode_two_body_energies_; //Dense matrix
307 
308 private:
309  // DATA
310  ObjexxFCL::FArray2D< core::PackerEnergy > two_body_energies_;
315 
316 private:
317  // Unimplemented functions
318  //no default constructor, uncopyable
319  FASTEREdge();
320  FASTEREdge( FASTEREdge const & );
321  FASTEREdge & operator = ( FASTEREdge const & );
322 };
323 
325 {
326 public:
328 
329 public:
330  FASTERInteractionGraph(int num_nodes);
331  virtual void initialize( rotamer_set::RotamerSetsBase const & rot_sets );
332 
333  virtual core::PackerEnergy get_one_body_energy_for_node_state( int node, int state);
334 
335  //virtual void set_num_aatypes(int) {}
336  virtual int get_num_aatypes() const {return 1;}
337 
338  virtual void blanket_assign_state_0();
339  virtual core::PackerEnergy set_state_for_node(int node_ind, int new_state);
340  int get_current_state_for_node( int node_ind ) const;
341  virtual core::PackerEnergy set_network_state( ObjexxFCL::FArray1_int & node_states);
342  virtual void consider_substitution(
343  int node_ind,
344  int new_state,
345  core::PackerEnergy & delta_energy,
346  core::PackerEnergy & prev_energy_for_node
347  );
348 
349  /// @brief Accepts (commits) the state change previously considered in a call to
350  /// consider_substitution and returns the energy of the entire graph
352 
353  /// @brief removes all accumulated numerical drift and returns the
354  /// energy for the current state assignment.
356  /// @brief returns the number of core::PackerEnergys used in all edge two-body energy tables
357  virtual int get_edge_memory_usage() const;
358 
359  virtual unsigned int count_static_memory() const;
360  virtual unsigned int count_dynamic_memory() const;
361 
362  //methods for FASTER optimization
363  virtual void prepare_for_FASTER();
364  void assign_BMEC();
367  void commit_relaxation();
368  void probabilistically_commit_relaxation( Real probability );
369  void get_current_network_state( ObjexxFCL::FArray1_int & netstate );
370  virtual core::PackerEnergy perturb_sBR_and_relax( int node, int perturbed_state );
371  virtual core::PackerEnergy perturb_dBR_and_relax( int node1, int perturbed_state1, int node2, int perturbed_state2 );
372  int get_random_neighbor_for_node( int node );
373  void reject_perturbation();
374 
375  /// @brief outputs the current state for each node, useful for debugging
376  virtual void print_current_state_assignment() const;
378 
379  /// @brief a user may define subsets of the vertex set for which they would like to
380  /// know the internal energy sum.
381  virtual core::PackerEnergy get_energy_sum_for_vertex_group( int group_id );
382 
383  /// @brief Swap the contents of the edge pair-energy table with the contents in the new_edge_table
384  void
385  swap_edge_energies( int node1, int node2, ObjexxFCL::FArray2D< core::PackerEnergy > & new_edge_table );
386 
387 protected:
388  //virtual unsigned int getMemoryUsageInBytes() const;
389 
390  virtual NodeBase* create_new_node( int node_index, int num_states);
391  virtual EdgeBase* create_new_edge( int index1, int index2);
392 
393  /// @brief removes numerical drift that can accumulate over the course of
394  /// many state assignment changes within simulated annealing
396 
397  inline
398  FASTERNode const * get_faster_node(int index) const
399  {
400  return static_cast< FASTERNode const * > (get_node( index ));
401  }
402 
403  inline
405  {
406  return static_cast< FASTERNode * > (get_node( index ));
407  }
408 
409  inline
410  FASTEREdge const * get_faster_edge(int node1, int node2) const
411  {
412  return static_cast< FASTEREdge const * > (find_edge(node1, node2));
413  }
414 
415  inline
416  FASTEREdge * get_faster_edge(int node1, int node2)
417  {
418  return static_cast< FASTEREdge * > (find_edge(node1, node2));
419  }
420 
421 
422 
423 private:
428 
429  //variables for FASTER
430  bool sBR_;
431  bool dBR_;
434 
435  static const int COMMIT_LIMIT_BETWEEN_UPDATES = 1024; // 2^10
436 
437  //no default constructor, uncopyable
441 };
442 
443 inline
445 {
446  return static_cast< FASTEREdge const * > (get_incident_edge( index ));
447 }
448 
449 inline
451 {
452  return static_cast< FASTEREdge * > (get_incident_edge( index ));
453 }
454 
455 inline
457 {
458  return static_cast< FASTERNode const * > (get_adjacent_node( index ));
459 }
460 
461 inline
463 {
464  return static_cast< FASTERNode * > (get_adjacent_node( index ));
465 }
466 
467 inline
469 {
470  return static_cast< FASTERInteractionGraph const * > (get_owner());
471 }
472 
473 inline
475 {
476  return static_cast< FASTERInteractionGraph * > (get_owner());
477 }
478 
479 /// @brief updates bookkeeping arrays for when a neighbor has changed its state
480 ///
481 /// @param edge_to_altered_neighbor - [in] - the index for the edge that connects
482 /// this node to the node that just changed its state
483 /// @param new_edge_energ - [in] - the pair energy between this node in its current
484 /// state and the new state of the node that just changed its state
485 /// @param other_node_new_state - [in] - the state the neighbor just adopted
486 inline
488  int edge_to_altered_neighbor,
489  core::PackerEnergy new_edge_energy,
490  int other_node_new_state
491 )
492 {
493 
494  curr_state_total_energy_ += new_edge_energy - curr_state_two_body_energies_[edge_to_altered_neighbor];
495  curr_state_two_body_energies_[edge_to_altered_neighbor] = new_edge_energy;
496  neighbors_curr_state_[ edge_to_altered_neighbor ] = other_node_new_state;
497  //neighbors_curr_state_plus_offset_[ edge_to_altered_neighbor ]
498  // = other_node_new_state + neighbors_rotindex_offset_[ edge_to_altered_neighbor ];
499  return;
500 }
501 
502 /// @brief static method that looks up the two body energy when the
503 /// node with the smaller index on an edge is considering an alternate state
504 ///
505 /// @param first_node_alt_state - [in] - the alternate state for the lower-indexed node
506 /// @param second_node_orig_state - [in] - the current state for the higher-indexed node
507 /// @param edge_energy_table - [in] - the proxy FArray pointing at the edge table
508 /// connecting the two nodes.
509 inline
512  int first_node_state,
513  int second_node_state,
514  ObjexxFCL::FArray2< core::PackerEnergy > & edge_energy_table
515 )
516 {
517  if (first_node_state == 0 || second_node_state == 0) {
518  return core::PackerEnergy( 0.0 );
519  } else {
520  return edge_energy_table( second_node_state, first_node_state );
521  }
522 }
523 
524 /// @brief update bookkeeping information when one of the nodes an edge is incident
525 /// upon changes state
526 ///
527 /// @param substituted_node_index - [in] - index of the node that chagned its state
528 /// @param curr_state_energy - [in] - the two body energy given the new state
529 /// @param nodes_new_state - [in] - the state the node just transitioned into
530 /// @param nodes_new_state_sparse_info - [in] - sparse matrix info for the new state
531 inline
532 void
534  int substituted_node_index,
535  core::PackerEnergy const curr_state_energy,
536  int nodes_new_state
537 )
538 {
539  int node_substituted = substituted_node_index == get_node_index(0) ? 0 : 1;
540  int node_not_substituted = ! node_substituted;
541 
542  curr_state_energy_ = curr_state_energy;
543 
545  get_edges_position_in_nodes_edge_vector( node_not_substituted ),
547  nodes_new_state
548  );
549 }
550 
551 /// @brief returns the change in energy that would be induced by switching this node
552 /// from its current state into another state
553 ///
554 /// iterates across the incident edges for a node in two phases:
555 /// in the first phase, it examines edges leading to higher-indexed nodes
556 /// in the second phase, it examines edges leading to smaller-indexed nodes.
557 /// for cache efficiency, all of the amino-acid-neighbor-offset information
558 /// that each edge calculates is stored on the nodes themselves. The edges
559 /// are never touched; rather, their private information is stored on the nodes
560 /// and handed to static member functions of the DensePDEdge class. This "store
561 /// edge information on the nodes" strategy gives me performance equivalent
562 /// to the previous energy2b lookup tables.
563 ///
564 /// @param alternate_state - [in] - the alternate state to consider
565 /// @param previous_energy_for_node - [out] - the old energy1b/energy2b sum for this
566 /// node; used by simulate annealing.
567 ///
568 inline
571  int alternate_state,
572  core::PackerEnergy & prev_energy_for_node
573 )
574 {
575 
577  //std::cerr << "proj_deltaE: node - " << get_node_index()
578  // << " alt state " << alternate_state << "...";
579 
580  alternate_state_ = alternate_state;
583  prev_energy_for_node = curr_state_total_energy_;
584 
585  for ( int ii = 1; ii <= get_num_edges_to_smaller_indexed_nodes(); ++ii ) {
591  );
593  }
594 
595  for ( int ii = get_num_edges_to_smaller_indexed_nodes() + 1; ii <= get_num_incident_edges(); ++ii ) {
601  );
603  }
604 
605  //std::cerr<< "..done" << std::endl;
606 
608 
609 }
610 
611 
612 
613 
614 } // namespace interaction_graph
615 } // namespace pack
616 } // namespace core
617 
618 #endif