Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DynamicAggregateFunction.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 protocols/pack_daemon/DynamicAggregateFunction.hh
11 /// @brief Declaration for a file-driven definition for the active states in a multistate design
12 /// and the set of mathematical expressions that together define the fitness function
13 /// for the sequence being designed.
14 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
15 
16 #ifndef INCLUDED_protocols_pack_daemon_DynamicAggregateFunction_hh
17 #define INCLUDED_protocols_pack_daemon_DynamicAggregateFunction_hh
18 
19 // Unit headers
21 
22 // Package headers
24 // AUTO-REMOVED #include <protocols/pack_daemon/PackDaemon.hh>
25 
26 // Project headers
27 #include <core/types.hh>
29 #include <numeric/expression_parser/Arithmetic.hh>
30 
31 // Utility headers
32 #include <utility/vector1.hh>
33 #include <utility/pointer/ReferenceCount.hh>
34 
35 // C++ headers
36 #include <map>
37 #include <set>
38 // AUTO-REMOVED #include <string>
39 
41 #include <utility/vector0.hh>
42 
43 
44 namespace protocols {
45 namespace pack_daemon {
46 
47 class VectorExpression : public numeric::expression_parser::Expression
48 {
49 public:
50  typedef numeric::expression_parser::Expression parent;
52  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
53 
54 public:
56  virtual ~VectorExpression();
57 
58  /// @brief DO NOT CALL THIS FUNCTION. Vector expressions return
59  /// vectors of values instead of a singular value.
60  virtual
62  operator() () const;
63 
64  /// @brief DO NOT CALL THIS FUNCTION. Vector expressions cannot
65  /// be differentiated.
66  virtual
67  numeric::expression_parser::ExpressionCOP
68  differentiate( std::string const & varname ) const;
69 
70  virtual
71  values
72  vector_values() const = 0;
73 
74  virtual
76  active_variables_vector() const = 0;
77 
78 
79  /// @brief Returns the number of vector values that this Expression returns without
80  /// computing those values.
81  virtual
83  size() const = 0;
84 
85  std::string const & name() const;
86 
87 private:
89 };
90 
92 {
93 public:
96 
97 public:
100 
101  virtual
102  values
103  vector_values() const;
104 
105  /// @brief Returns the number of variable expressions this VectorExpression points at
106  virtual
107  core::Size
108  size() const;
109 
110  virtual
111  std::list< std::string >
112  active_variables() const;
113 
114  virtual
116  active_variables_vector() const;
117 
118 
119 private:
121 
122 };
123 
125 {
126 public:
128  typedef numeric::expression_parser::ArithmeticASTExpression ArithmeticASTExpression;
129  typedef numeric::expression_parser::VariableExpressionOP VariableExpressionOP;
130  typedef numeric::expression_parser::VariableExpressionCOP VariableExpressionCOP;
131 public:
134 
135  void initialize(
136  std::map< std::string, VectorExpressionCOP > const & vector_varnames,
137  ArithmeticASTExpression const & expresion_ast,
138  VectorExpressionCreator & expression_creator // holds a reference to my owning DynamicAggregateFunction
139  );
140 
141  virtual
142  values
143  vector_values() const;
144 
145  virtual
146  core::Size
147  size() const;
148 
149  numeric::expression_parser::VariableExpressionCOP
150  local_variable( std::string const & varname ) const;
151 
152  virtual
153  std::list< std::string >
154  active_variables() const;
155 
156  virtual
158  active_variables_vector() const;
159 
160 
161 private:
163  utility::vector1< VariableExpressionOP > local_variables_; // op data members can have non-const operations performed on them
164  std::map< std::string, VariableExpressionCOP > local_variable_map_;
166 };
167 
168 class VectorFunction : public numeric::expression_parser::UnaryExpression
169 {
170 public:
171  typedef numeric::expression_parser::UnaryExpression parent;
172  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
174 
175 public:
176 
178  virtual ~VectorFunction();
179 
180 protected:
181  VectorExpressionCOP vec_ex() const;
182 
183 private:
185 };
186 
187 class VectorFunction2 : public numeric::expression_parser::BinaryExpression
188 {
189 public:
190  typedef numeric::expression_parser::BinaryExpression parent;
191  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
193 
194 public:
195 
197  virtual ~VectorFunction2();
198 
199 protected:
202 
203 private:
206 };
207 
208 
209 class VMax : public VectorFunction
210 {
211 public:
213 
214 public:
215 
217  virtual ~VMax();
218 
219  virtual
220  core::Real
221  operator() () const;
222 
223  virtual
224  numeric::expression_parser::ExpressionCOP
225  differentiate( std::string const & varname ) const;
226 
227  virtual
228  std::list< std::string >
229  active_variables() const;
230 
231 };
232 
233 class VMin : public VectorFunction
234 {
235 public:
237 
238 public:
240 
241  virtual ~VMin();
242 
243  virtual
244  core::Real
245  operator() () const;
246 
247  virtual
248  numeric::expression_parser::ExpressionCOP
249  differentiate( std::string const & varname ) const;
250 
251  virtual
252  std::list< std::string >
253  active_variables() const;
254 
255 };
256 
257 /// @brief Take two vector expressions of equal length; returns the value from position i in
258 /// expression 2 where position i is the position with the largest value in expression 1
259 class VMaxBy : public VectorFunction2
260 {
261 public:
263 
264 public:
265 
267  virtual ~VMaxBy();
268 
269  virtual
270  core::Real
271  operator() () const;
272 
273  virtual
274  numeric::expression_parser::ExpressionCOP
275  differentiate( std::string const & varname ) const;
276 
277  virtual
278  std::list< std::string >
279  active_variables() const;
280 
281 };
282 
283 /// @brief Take two vector expressions of equal length; returns the value from position i in
284 /// expression 2 where position i is the position with the smallest value in expression 1
285 class VMinBy : public VectorFunction2
286 {
287 public:
289 
290 public:
292 
293  virtual ~VMinBy();
294 
295  virtual
296  core::Real
297  operator() () const;
298 
299  virtual
300  numeric::expression_parser::ExpressionCOP
301  differentiate( std::string const & varname ) const;
302 
303  virtual
304  std::list< std::string >
305  active_variables() const;
306 
307 };
308 
309 class PowExpression : public numeric::expression_parser::BinaryExpression
310 {
311 public:
312  typedef numeric::expression_parser::BinaryExpression parent;
313  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
314 
315 public:
316  PowExpression( ExpressionCOP base, ExpressionCOP exponent );
317  virtual ~PowExpression();
318 
319  virtual
320  core::Real
321  operator() () const;
322 
323  virtual
325  differentiate( std::string const & varname ) const;
326 
327 };
328 
329 class ExpExpression : public numeric::expression_parser::UnaryExpression
330 {
331 public:
332  typedef numeric::expression_parser::UnaryExpression parent;
333  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
334 
335 public:
337  virtual ~ExpExpression();
338 
339  virtual
340  core::Real
341  operator() () const;
342 
343  virtual
345  differentiate( std::string const & varname ) const;
346 
347 };
348 
349 class LnExpression : public numeric::expression_parser::UnaryExpression
350 {
351 public:
352  typedef numeric::expression_parser::UnaryExpression parent;
353  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
354 
355 public:
357  virtual ~LnExpression();
358 
359  virtual
360  core::Real
361  operator() () const;
362 
363  virtual
365  differentiate( std::string const & varname ) const;
366 
367 };
368 
369 
370 /// @brief Returns "true" if the expression ex evaluates to one
371 /// of a set of indicated values.
372 class InSetExpression : public numeric::expression_parser::UnaryExpression
373 {
374 public:
375  typedef numeric::expression_parser::UnaryExpression parent;
376  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
377 
378 public:
380  virtual ~InSetExpression();
381 
382  void value_set( utility::vector1< core::Real > const & values );
383 
384  virtual
385  core::Real
386  operator() () const;
387 
388  virtual
390  differentiate( std::string const & varname ) const;
391 
392 private:
394 };
395 
396 /// @brief Stores the result of the surragate expression as if this
397 /// expression were a variable, but defers to the root expression for
398 /// questions of deriviatives and which variables are active
399 /// (this is not a real variable).
400 class SurrogateVariableExpression : public numeric::expression_parser::VariableExpression
401 {
402 public:
403  typedef numeric::expression_parser::VariableExpression parent;
404  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
405 public:
408 
409  virtual
410  std::list< std::string >
411  active_variables() const;
412 
413  void
414  root_expression( ExpressionCOP setting );
415 
416  virtual
418  differentiate( std::string const & varname ) const;
419 
420 private:
422 
423 };
424 
425 
426 class VectorExpressionCreator : public numeric::expression_parser::ExpressionCreator
427 {
428 public:
429  typedef numeric::expression_parser::ExpressionCreator parent;
430  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
431 public:
433  virtual ~VectorExpressionCreator();
434 
435  /// @brief Override the parent-class definition of this function to trap
436  /// one specific kind of function call: max or min with a vector variable
437  /// as one of their function argument
438  //virtual
439  //void
440  //visit( numeric::expression_parser::ArithmeticASTFunction const & );
441 
442  virtual
444  handle_variable_expression( numeric::expression_parser::ArithmeticASTValue const & );
445 
446  virtual
449  numeric::expression_parser::FunctionTokenCOP function,
451  );
452 
453 private:
455 
456 };
457 
459 {
460 public:
464 };
465 
467 {
468 public:
470 
472 
473  typedef numeric::expression_parser::Expression Expression;
474  typedef numeric::expression_parser::ExpressionOP ExpressionOP;
475  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
476 
477  typedef numeric::expression_parser::VariableExpression VariableExpression;
478  typedef numeric::expression_parser::VariableExpressionOP VariableExpressionOP;
479  typedef numeric::expression_parser::VariableExpressionCOP VariableExpressionCOP;
480 
481  typedef numeric::expression_parser::ArithmeticASTExpressionOP ArithmeticASTExpressionOP;
482 
484  typedef core::Size Size;
485 
486 public:
488  virtual ~DynamicAggregateFunction();
489 
490  void set_num_entity_elements( Size setting );
492 
493  core::Size num_states() const;
495 
496  virtual core::Real evaluate( StateEnergies const &, StateEnergies const & npd_properties, Entity const & );
497  virtual StateIndices select_relevant_states( StateEnergies const & en, StateEnergies const & npd, Entity const & );
498 
499  void initialize_from_input_file( DaemonSetOP daemon_set, std::istream & input );
500 
502  variable_expression( numeric::expression_parser::ArithmeticASTValue const & ) const;
503 
506  numeric::expression_parser::FunctionTokenCOP function,
508  ) const;
509 
510  /// @brief Pair a file name with a string -- instead of opening a file
511  /// when asked to read this file, the DAF will use the contents of the file
512  /// as provided.
513  void add_file_contents( std::string const & fname, std::string const & contents );
514 
515  std::string state_name( Size state_index ) const;
516 
517 private:
518 
519  void read_all_variables_from_input_file( std::istream & input );
520 
521  void
523 
524  void
526  std::string const & line,
527  Size line_number,
528  std::istream & input_line
529  );
530 
531  void
533  std::string const & line,
534  Size line_number,
535  std::istream & input_line,
536  utility::vector1< std::pair< std::string, std::string > > & strucvec_filenames
537  );
538 
539  void
541  std::string const & line,
542  Size line_number,
543  std::istream & input_line
544  );
545 
546  void
548  std::string const & line,
549  Size line_number,
550  std::istream & input_line
551  );
552 
553 
554  void
556  std::string const & line,
557  Size line_number,
558  std::istream & input_line
559  );
560 
561  void
563  std::string const & line,
564  Size line_number,
565  std::istream & input_line,
566  std::map< std::string, std::list< std::string > > & vector_variables
567  );
568 
569 
570  void
572  std::string const & line,
573  Size line_number,
574  std::istream & input_line,
575  std::map< std::string, ArithmeticASTExpressionOP > & scalar_expression_asts
576  );
577 
578  void
580  std::string const & line,
581  Size line_number,
582  std::istream & input_line,
583  std::map< std::string, std::pair< std::map< std::string, std::string >, ArithmeticASTExpressionOP > > & vector_expression_list
584  );
585 
586  void
588  std::string const & line,
589  Size line_number,
590  std::istream & input_line
591  );
592 
593  void
595  std::string const & line,
596  Size line_number,
597  std::istream & input_line,
598  ArithmeticASTExpressionOP & fitness_expression_ast
599  );
600 
601  void
603  std::string const & vec_varname,
604  std::string const & fname,
605  Size & n_vector_states
606  );
607 
608  void
610  Size & count_state,
611  Size & count_npd_index,
612  Size & count_variable_index
613  );
614 
615  void
617  Size & count_state,
618  Size & count_npd_index,
619  Size & count_variable_index
620  );
621 
622  void
624  std::map< std::string, ArithmeticASTExpressionOP > const & scalar_expression_asts,
625  std::map< std::string, std::list< std::string > > const & vector_variables,
626  Size & count_state
627  );
628 
629  void
631  std::map< std::string, ArithmeticASTExpressionOP > const & scalar_expression_asts,
632  std::map< std::string, std::pair< std::map< std::string, std::string >, ArithmeticASTExpressionOP > > const & vector_expression_asts,
633  ArithmeticASTExpressionOP fitness_expression_ast
634  );
635 
638  std::string const & fname,
640  Size expected_nargs
641  ) const;
642 
643  void
645  std::string const & vname,
646  std::string const & command_name,
647  std::string const & line,
648  Size line_number
649  );
650 
651  void
652  initialize_pack_daemons( DaemonSetOP daemon_set );
653 
654  void
656  std::map< std::string, Size > & total_reads,
657  std::map< std::string, Size > & reads_completed
658  ) const;
659 
660  void
662  std::string const & fname,
663  std::map< std::string, Size > & total_reads,
664  std::map< std::string, Size > & reads_completed
665  ) const;
666 
667 
670  std::string const & filename,
671  std::map< std::string, Size > const & total_reads,
672  std::map< std::string, Size > & reads_completed,
673  std::map< std::string, std::string > & file_contents_map
674  ) const;
675 
676 
678  DaemonSetOP daemon_set,
679  std::map< std::string, Size > const & total_reads,
680  std::map< std::string, Size > & reads_completed
681  );
682 
683  void
685  DaemonSetOP daemon_set,
686  std::map< std::string, Size > const & total_reads,
687  std::map< std::string, Size > & reads_completed
688  );
689 
690  StructureFileNames const &
691  file_inputs_for_job( int job_index ) const;
692 
693  void
695  std::list< int > const & job_indices,
696  DaemonSetOP daemon_set,
697  std::map< std::string, Size > const & total_reads,
698  std::map< std::string, Size > & reads_completed,
699  std::map< std::string, std::string > & file_contents_map
700  ) const;
701 
702  void
704  int proc_id,
705  std::list< int > const & job_indices,
706  std::map< std::string, Size > const & total_reads,
707  std::map< std::string, Size > & reads_completed,
708  std::map< std::string, std::string > & file_contents_map
709  ) const;
710 
711  bool verify_remote_daemon_set_initialization_successful( int proc_id ) const;
712  void send_success_message_to_remote_daemon_set( int proc_id ) const;
714 
715  void
717  StateEnergies const & state_energies,
718  StateEnergies const & npd_properties,
719  Entity const & entity,
720  bool verbose = false
721  );
722 
723  /// @brief used to determine the number of requested NPD properties that will be
724  /// calculated; used to size the variable_expressions_for_npd_properties_ array.
726 
727 private:
728 
731 
736  //utility::vector1< utility::vector1< VariableExpressionOP > > npd_properties_for_states_;
738 
739  numeric::expression_parser::ArithmeticScannerOP scanner_;
740 
741  std::list< VectorFunctionOP > vfuncs_;
742  std::list< std::pair< Size, std::string > > expression_evaluation_order_by_name_; // first == 1 for scalar, 2 for vector, second = name
744  std::map< Size, SurrogateVariableExpressionOP > surrogate_expression_map_;
745  std::map< std::string, VariableExpressionCOP > scalar_expression_map_;
746  std::map< std::string, VectorExpressionCOP > vector_expression_map_;
747 
748  // Variables that correspond to protein states
749  std::set< std::string > state_variable_names_;
750  std::set< std::string > state_vector_variable_names_;
751  std::map< std::string, ExpressionCOP > named_state_expression_map_;
752  std::map< std::string, VariableVectorExpressionOP > state_vector_variables_;
753 
754  // first = property, second = name of variable for that property
755  std::map< std::string, std::list< std::pair< std::string, std::string > > > npd_properties_for_state_variables_;
756 
757  std::map< std::string, ExpressionCOP > npd_property_expression_map_;
758  std::map< std::string, VariableVectorExpressionOP > npd_property_vector_expression_map_;
759 
760  /// Function names may not be used as variable names.
761  std::set< std::string > function_names_;
762 
763  /// This set represents strings that may not be used as variable names
764  /// but which are not funciton names
765  std::set< std::string > illegal_variable_names_;
766 
767  //// The following variables hold variable names and the lines on which they were declared.
768  std::map< std::string, Size > variable_names_dec_line_;
769  std::map< std::string, Size > scalar_variable_names_dec_line_;
770  std::map< std::string, Size > vector_variable_names_dec_line_;
771 
772  std::map< std::string, std::pair< EntityFuncOP, VariableExpressionOP > > entity_funcs_;
773  std::map< std::string, Size > entity_funcs_dec_line_;
774 
775  /// Keep track for each varible name (or scalar expression name) its index in the ?? vector
776  std::map< std::string, Size > variable_name_2_variable_exp_index_;
777  /// Keep track for each state-variable name its state's index
778  std::map< std::string, Size > state_variable_name_2_state_index_;
779 
780  std::map< std::string, StructureFileNames > named_state_data_file_names_;
781  std::map< std::string, utility::vector1< StructureFileNames > > state_vector_data_file_names_;
782 
784  std::map< std::string, utility::vector1< Size > > state_indices_for_state_vector_;
785 
787 
788  std::map< std::string, std::string > file_contents_;
789 
790 };
791 
792 
793 class EntityFuncExpressionCreator : public numeric::expression_parser::ExpressionCreator
794 {
795 public:
796  typedef numeric::expression_parser::ExpressionCreator parent;
797  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
798 public:
799  EntityFuncExpressionCreator( EntityFunc const & owner );
801 
802  virtual
804  handle_variable_expression( numeric::expression_parser::ArithmeticASTValue const & );
805 
806  virtual
809  numeric::expression_parser::FunctionTokenCOP function,
811  );
812 
813 private:
815 
816 };
817 
818 
820 {
821 public:
823 
825 
826  typedef numeric::expression_parser::Expression Expression;
827  typedef numeric::expression_parser::ExpressionOP ExpressionOP;
828  typedef numeric::expression_parser::ExpressionCOP ExpressionCOP;
829 
830  typedef numeric::expression_parser::VariableExpression VariableExpression;
831  typedef numeric::expression_parser::VariableExpressionOP VariableExpressionOP;
832  typedef numeric::expression_parser::VariableExpressionCOP VariableExpressionCOP;
833 
834  typedef numeric::expression_parser::ArithmeticASTExpressionOP ArithmeticASTExpressionOP;
835 
836  typedef core::Size Size;
837 
838 public:
839  EntityFunc();
840  virtual ~EntityFunc();
841 
842  void set_num_entity_elements( Size num_ee );
843 
844  void initialize_from_input_file( std::istream & input );
845 
846  core::Real
847  evaluate( Entity const & entity, bool verbose = false );
848 
850  variable_expression( numeric::expression_parser::ArithmeticASTValue const & ) const;
851 
854  numeric::expression_parser::FunctionTokenCOP function,
856  ) const;
857 
858 private:
859  void
861 
862  void
864  std::string const & line,
865  Size line_number,
866  std::istream & input_line
867  );
868 
869  void
871  std::string const & line,
872  Size line_number,
873  std::istream & input_line
874  );
875 
876  void
878  std::string const & line,
879  Size line_number,
880  std::istream & input_line,
881  std::map< std::string, ArithmeticASTExpressionOP > & expression_asts
882  );
883 
884  void
886  std::string const & line,
887  Size line_number,
888  std::istream & input_line,
889  ArithmeticASTExpressionOP & score_expression_ast
890  );
891 
892  void
894  std::map< std::string, ArithmeticASTExpressionOP > const & expression_asts,
895  ArithmeticASTExpressionOP score_expression_ast
896  );
897 
898  void assign_entity_sequence_to_variables( Entity const & entity );
899 
900 private:
902 
904 
905  std::map< std::string, utility::vector1< core::Real > > aa_sets_name_map_;
906  std::map< std::string, Size > aa_sets_dec_line_;
907 
908  std::map< std::string, ExpressionCOP > subexpression_name_map_;
909  std::map< std::string, Size > subexpression_name_dec_line_;
910 
911  std::map< std::string, VariableExpressionOP > variable_expression_map_;
912 
913  /// Evaluate the subexpressions and then store them in the surrogate variable expressions
914  /// in order.
916 
918 
919  numeric::expression_parser::ArithmeticScannerOP scanner_;
920  /// Function names may not be used as variable names.
921  std::set< std::string > function_names_;
922  /// This set represents strings that may not be used as variable names
923  /// but which are not funciton names
924  std::set< std::string > illegal_variable_names_;
925 
926 
927 };
928 
929 }
930 }
931 
932 #endif