Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OptEMultifunc.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/optimization/OptEMultifunc.hh
11 /// @brief OptE mode multifunction class
12 /// @author Jim Havranek
13 
14 
15 #ifndef INCLUDED_protocols_optimize_weights_OptEMultifunc_hh
16 #define INCLUDED_protocols_optimize_weights_OptEMultifunc_hh
17 
18 // Unit headers
20 
21 // Package headers
22 #include <numeric/expression_parser/Arithmetic.hh>
23 // AUTO-REMOVED #include <protocols/optimize_weights/OptEData.hh>
24 
25 // Project headers
28 // AUTO-REMOVED #include <core/chemical/AA.hh>
29 // AUTO-REMOVED #include <core/scoring/ScoreFunction.hh>
30 #include <core/scoring/ScoreType.hh> // there is no .fwd.hh for this
31 // AUTO-REMOVED #include <basic/prof.hh>
32 
33 /// C++ headers
34 #include <map>
35 #include <set>
36 // AUTO-REMOVED #include <string>
37 #include <iosfwd>
38 
41 #include <utility/vector1.hh>
42 
43 
44 namespace protocols {
45 namespace optimize_weights {
46 
51 };
52 
53 /// @brief OptE mode multifunction class
55 {
56 public:
60  typedef core::Real Real;
61  typedef core::Size Size;
62 
63 public: // Creation
64 
65  // c-tor
67  OptEData & opte_data_in,
68  EnergyMap const & fixed_terms_in,
69  int num_free_in,
70  ScoreTypes & score_list_in,
71  ScoreTypes & fixed_score_list_in,
72  Multivec const & component_weights
73  );
74 
75  // c-tor
77  OptEData & opte_data_in,
78  EnergyMap const & fixed_terms_in,
79  int num_free_in,
80  ScoreTypes const & score_list_in,
81  ScoreTypes const & fixed_score_list_in,
82  utility::vector1< Real > const & reference_energies_in,
83  Multivec const & component_weights
84  );
85 
86  /// @brief Destructor
87  virtual
89  {}
90 
91 
92 public: // Methods
93 
94 
95  // @brief OptE func
96  virtual
97  Real
98  operator ()( Multivec const & vars ) const;
99 
100  /// @brief OptE dfunc
101  virtual
102  void
103  dfunc(
104  Multivec const & vars,
105  Multivec & dE_dvars
106  ) const;
107 
108  /// @brief Does actual work for OptE minimization
109  /* Real
110  get_score_at_single_position(
111  OptEPositionDataOP const this_pos,
112  Multivec const & vars,
113  Multivec & dvars
114  ) const; */
115 
116  /// @brief Extract variable weights from an Energy Map
117  Multivec
118  get_dofs_from_energy_map( EnergyMap const & start_vals ) const;
119 
120  /// @brief Expand free variables and combine with fixed to make an Energy Map
121  EnergyMap
123  Multivec const & dofs
124  ) const;
125 
128  Multivec const & dofs
129  ) const;
130 
132  {
134  }
135 
136  /// @brief Non-driver node wait for MPI vars to evaluate either the func or the dfunc.
137  void wait_for_remote_vars() const;
138 
139  /// @brief For driver node: inform the non-driver nodes that minimization is over. Must
140  /// be called before object is destructed (Should not be called in the destructor, as
141  /// dstors should not throw exceptions, and MPI communication can absolutely result in exceptions).
142  void declare_minimization_over() const;
143 
144  void fix_reference_energies( bool setting ) {
145  fix_reference_energies_ = setting;
146  }
147 
148  /// @brief Are the reference energies being optimized at all, or are they being held fixed?
149  bool fix_reference_energies() const {
151  }
152 
153 private:
154 
155  /// @brief send out messages over MPI for remote nodes to evaluate their func given the input vars.
156  void mpi_broadcast_eval_func( Multivec const & vars ) const;
157  /// @brief send out messages over MPI for remote nodes to evaluate their dfunc given the input vars.
158  void mpi_broadcast_eval_dfunc( Multivec const & vars ) const;
159 
160  void mpi_broadcast_send_vars( Multivec const & vars ) const;
161 
162  void mpi_broadcast_receive_vars( Multivec & vars ) const;
163 
164 
165  /// @brief collect func values from remote nodes and return their sum.
166  Real mpi_receive_func() const;
167  /// @brief collect dfunc valresultsues from remote nodes and increment the values in the dE_dvars input array.
168  void mpi_receive_dfunc( Multivec & dE_vars ) const;
169 
170 private: // data
171 
173  int const num_ref_dofs_;
174  int const num_total_dofs_;
175 
176  /// Rotamer energy components for all positions
181 
182  ///
185 
187 
191 
192 }; // OptEMultifunc
193 
194 
195 /// @brief OptE mode multifunction class
196 /*
197 class NoRefernceEnergyOptEMultifunc : public core::optimization::Multifunc
198 {
199 public:
200  typedef core::scoring::ScoreTypes ScoreTypes;
201  typedef core::scoring::EnergyMap EnergyMap;
202  typedef core::optimization::Multivec Multivec;
203  typedef core::Real Real;
204  typedef core::Size Size;
205 
206 public: // Creation
207 
208  // c-tor
209  OptEMultifunc(
210  OptEData & opte_data_in,
211  EnergyMap const & fixed_terms_in,
212  int num_free_in,
213  ScoreTypes & score_list_in,
214  ScoreTypes & fixed_score_list_in,
215  Multivec const & component_weights
216  );
217 
218  // c-tor
219  OptEMultifunc(
220  OptEData & opte_data_in,
221  EnergyMap const & fixed_terms_in,
222  int num_free_in,
223  ScoreTypes const & score_list_in,
224  ScoreTypes const & fixed_score_list_in,
225  utility::vector1< Real > const & reference_energies_in,
226  Multivec const & component_weights
227  );
228 
229  /// @brief Destructor
230  virtual
231  ~OptEMultifunc()
232  {}
233 
234 
235 public: // Methods
236 
237 
238  // @brief OptE func
239  virtual
240  Real
241  operator ()( Multivec const & vars ) const;
242 
243  /// @brief OptE dfunc
244  virtual
245  void
246  dfunc(
247  Multivec const & vars,
248  Multivec & dE_dvars
249  ) const;
250 
251  /// @brief Does actual work for OptE minimization
252  Real
253  get_score_at_single_position(
254  OptEPositionDataOP const this_pos,
255  Multivec const & vars,
256  Multivec & dvars
257  ) const;
258 
259  /// @brief Extract variable weights from an Energy Map
260  Multivec
261  get_dofs_from_energy_map( EnergyMap const & start_vals ) const;
262 
263  /// @brief Expand free variables and combine with fixed to make an Energy Map
264  EnergyMap
265  get_energy_map_from_dofs(
266  Multivec const & dofs
267  ) const;
268 
269  void set_starting_reference_energies( utility::vector1< Real > const & values )
270  {
271  starting_reference_energies_ = values;
272  }
273 
274 private: // data
275 
276  core::optimization::MultifuncOP multifunc_;
277  utility::vector1< Real > reference_energies_;
278  utility::vector1< Real > expanded_vars_;
279 
280 };*/
281 
282 
283 /// DANGER DANGER DANGER
284 /// This class must never be allocated on the stack. Instead, it should be allocated
285 /// (with "new") on the heap. This class hands an owning-pointer to itself to another
286 /// class to create a call-back mechanism; this owning pointer will be invalid and
287 /// result in stack corruption if this class is allocated on the stack.
289 {
290 public:
294  typedef core::Real Real;
295  typedef core::Size Size;
296 
297 public:
299  ScoreTypes const & free_score_list,
300  Size free_count, // the number of named dofs (score types) + the number of reference energies
301  ScoreTypes const & fixed_score_list,
302  EnergyMap const & fixed_scores,
303  OptEMultifuncOP optEfunc
304  );
305 
307 
308  // @brief OptE func
309  virtual
310  Real
311  operator ()( Multivec const & vars ) const;
312 
313  /// @brief OptE dfunc
314  virtual
315  void
316  dfunc(
317  Multivec const & vars,
318  Multivec & dE_dvars
319  ) const;
320 
321  Size
322  n_real_dofs() const;
323 
325  derived_dofs( Multivec const & vars ) const;
326 
327  void
328  print_dofs( Multivec const & vars, std::ostream & ostr ) const;
329 
330  numeric::expression_parser::VariableExpressionOP
332 
333  void
334  set_multifunc( OptEMultifuncOP multifunc );
335 
336 private:
340 
341  std::set< std::string > free_score_names_;
342  std::set< std::string > fixed_score_names_;
343 
344  /// Dofs added by "NEW_DOF" lines in the wrapper input file
345  std::set< std::string > new_dof_names_;
346 
347  /// Dofs marked as dependent by "DEPENDENT_DOF" lines in the wrapper input file.
348  std::set< std::string > dependent_dof_names_;
349 
350  /// union of ( free_score_names_ - dependent_dof_names_ ) + fixed_score_names_ + new_dof_names
351  std::set< std::string > valid_variable_names_;
352 
353  /// During expression creation; keep track of which variables are active. This set
354  /// Eventually ends up in the active_varibles_ array, and will be used to create
355  /// derivative expressions.
356  std::set< std::string > active_variables_this_dependent_dof_;
357 
358  /// Not every VariableExpression object registered by the ExpressionVisitor
359  /// will end up in this map -- variables for fixed_score terms are not kept here, they
360  /// are simply given a fixed value and never asked to update that value.
361  /// Every vaiable in this list has its value updated at the beginning of scoring.
362  std::map< std::string, OptEVariableExpressionOP > dof_variables_;
363 
364  // what order should dofs appear in for the OptEMultifunc?
365  std::map< std::string, Size > optEmultifunc_dof_order_;
366 
367  /// non-depdendent dofs are handled by VariableExpressions that map
368  /// from the real_dof id to the optE-percieved-dof id.
370 
371  /// Which variables influence each optE-percieved-dofs?
373  /// For each real dof, a list of each derivative expression and associated optE-percieved-dof id.
375 
379 
380 };
381 
382 class OptEVariableExpression : public numeric::expression_parser::VariableExpression
383 {
384 public:
385  OptEVariableExpression( std::string const & name );
386  OptEVariableExpression( std::string const & name, core::Real value );
387 
388  void set_id( core::Size id );
389  void update_value_from_list( utility::vector1< core::Real > const & value_vector );
390  core::Size id() const { return id_; }
391 
392 private:
394 };
395 
396 
397 class WrappedOptEExpressionCreator : public numeric::expression_parser::ExpressionCreator
398 {
399 public:
403 
404  virtual
405  numeric::expression_parser::ExpressionCOP
406  handle_variable_expression( numeric::expression_parser::ArithmeticASTValue const & );
407 
408  virtual
409  numeric::expression_parser::ExpressionCOP
411  numeric::expression_parser::FunctionTokenCOP function,
413  );
414 
415  void
417 
418 private:
419 
421 
422 };
423 
424 
425 
426 
427 } // namespace optimization
428 } // namespace core
429 
430 
431 #endif // INCLUDED_protocols_optimize_weights_OptEMultifunc_HH