Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PNatLigPoseOptEData.cc
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/optimize_weights/PNatLigPoseOptEData.cc
11 ///
12 /// @brief
13 /// @author Ian W. Davis
14 
15 
16 #ifdef USEMPI
17 #include <mpi.h>
18 #endif
19 
21 
23 // AUTO-REMOVED #include <basic/options/util.hh>
24 
25 #include <ObjexxFCL/format.hh>
26 
27 // AUTO-REMOVED #include <utility/LexicographicalIterator.hh>
28 // AUTO-REMOVED #include <utility/string_util.hh>
29 #include <utility/vector1.functions.hh>
30 
31 // AUTO-REMOVED #include <fstream>
32 #include <ostream>
33 #include <sstream>
34 #include <string>
35 // AUTO-REMOVED #include <cmath>
36 
37 #include <basic/Tracer.hh>
38 
39 #include <utility/vector1.hh>
40 
41 using basic::T;
42 using basic::Error;
43 using basic::Warning;
44 static basic::Tracer TR("protocols.optimize_weights.OptEData");
45 
46 using namespace core;
47 using namespace scoring;
48 using namespace ObjexxFCL::fmt;
49 
50 namespace protocols {
51 namespace optimize_weights {
52 
53 
54 PNatLigPoseOptEData::PNatLigPoseOptEData():
56  // Arbitrary values for now:
57  kT_(1.0),
58  multiplier_(1.0) // don't use this value; use the value passed in to the component weights file (ronj)
59 {
60 }
61 
62 
64 
65 
66 Real
68  std::ostream & ostr,
69  Multivec const & component_weights,
70  Multivec const & vars,
71  Multivec & dE_dvars,
72  /// Basically, turn over all the private data from OptEMultiFunc
73  Size const num_energy_dofs,
74  int const ,//num_ref_dofs,
75  int const ,//num_total_dofs,
76  EnergyMap const & fixed_terms,
77  ScoreTypes const & ,//score_list,
78  ScoreTypes const & fixed_score_list,
79  bool const print
80 ) const
81 {
82  using namespace core::optimization;
83  using namespace utility;
84  //std::cout << "In get_score() ... " << natives_.size() << " natives, " << decoys_.size() << " decoys" << std::endl;
85  //std::cout << "Weights:";
86  //for ( Size ii = 1; ii <= num_energy_dofs; ++ii ) std::cout << " " << vars[ ii ];
87  //std::cout << std::endl;
88 
89  if ( decoys_.size() == 0 || natives_.size() == 0 ) return 0.0; // wtf?
90 
91  utility::vector1< Real > decoy_energies( decoys_.size(), 0.0 );
92  utility::vector1< Real > native_energies( natives_.size(), 0.0 );
93  for ( Size ii = 1; ii <= num_energy_dofs; ++ii ) {
94  for ( Size jj = 1; jj <= natives_.size(); ++jj ) {
95  native_energies[ jj ] += vars[ ii ] * natives_[ jj ]->free_data()[ ii ];
96  }
97  for ( Size jj = 1; jj <= decoys_.size(); ++jj ) {
98  decoy_energies[ jj ] += vars[ ii ] * decoys_[ jj ]->free_data()[ ii ];
99  }
100  }
101  for ( Size ii = 1; ii <= fixed_score_list.size(); ++ii ) {
102  for ( Size jj = 1; jj <= natives_.size(); ++jj ) {
103  native_energies[ jj ] += fixed_terms[ fixed_score_list[ ii ] ] * natives_[ jj ]->fixed_data()[ ii ];
104  }
105  for ( Size jj = 1; jj <= decoys_.size(); ++jj ) {
106  decoy_energies[ jj ] += fixed_terms[ fixed_score_list[ ii ] ] * decoys_[ jj ]->fixed_data()[ ii ];
107  }
108  }
109 
110  Real const best_native_energy = min( native_energies );
111  Real const best_decoy_energy = min( decoy_energies );
112  //std::cout << "Best native E = " << best_native_energy << " , best decoy E = " << best_decoy_energy << std::endl;
113  Real const best_energy = best_native_energy < best_decoy_energy ? best_native_energy : best_decoy_energy;
114  for ( Size ii = 1; ii <= natives_.size(); ++ii ) {
115  native_energies[ ii ] -= best_energy;
116  }
117  for ( Size ii = 1; ii <= decoys_.size(); ++ii ) {
118  decoy_energies[ ii ] -= best_energy;
119  }
120 
121  Real numerator(0.0), partition(0.0);
122  Multivec dpartition( vars.size(), 0.0 ), dnumerator( vars.size(), 0.0 );
123 
124  Real const neginv_kT = (-1.0 / kT_);
125  for( Size ii(1); ii <= natives_.size(); ++ii ) {
126 
127  // Limit the improbability of each native to 1 in a million.
128  // This prevents numerator ~ 0, which causes NANs and INFs in the derivatives.
129  // It also limits the "force" that any one structure can exert on the minimization.
130  Real const exp_term = std::max( 1e-6, std::exp( neginv_kT * native_energies[ ii ] ) );
131  //if( exp_term > 1 || exp_term < 0 || std::isinf(exp_term) || std::isnan(exp_term) ) std::cout << "[" << tag() << "] native exp_term = " << exp_term << std::endl;
132  numerator += exp_term;
133  partition += exp_term;
134 
135  for( Size e_dof(1); e_dof <= num_energy_dofs; ++e_dof ) {
136  // note for derivatives: d/dw( e^-(E*w+...) ) = -E * e^-(E*w+...)
137  Real e_dof_deriv( neginv_kT * natives_[ ii ]->free_data()[ e_dof ] * exp_term );
138  //if( std::isinf(e_dof_deriv) || std::isnan(e_dof_deriv) ) std::cout << "[" << tag() << "," << e_dof << "] native e_dof_deriv = " << e_dof_deriv << "; Eterm = " << natives_[ ii ]->free_data()[ e_dof ]<< std::endl;
139  dnumerator[ e_dof ] += e_dof_deriv;
140  dpartition[ e_dof ] += e_dof_deriv;
141  }
142  }
143  for( Size ii(1); ii <= decoys_.size(); ++ii ) {
144 
145  // Because partition >= numerator in all cases, there is no minimum value for this term:
146  Real const exp_term( std::exp( neginv_kT * decoy_energies[ ii ] ) );
147  //if( exp_term > 1 || exp_term < 0 || std::isinf(exp_term) || std::isnan(exp_term) ) std::cout << "[" << tag() << "] decoy exp_term = " << exp_term << std::endl;
148  partition += exp_term;
149 
150  // partitions for energy derivatives
151  for( Size e_dof(1); e_dof <= num_energy_dofs; ++e_dof ) {
152  // note for derivatives: d/dw( e^-(E*w+...) ) = -E * e^-(E*w+...)
153  Real e_dof_deriv( neginv_kT * decoys_[ ii ]->free_data()[ e_dof ] * exp_term );
154  //if( std::isinf(e_dof_deriv) || std::isnan(e_dof_deriv) ) std::cout << "[" << tag() << "," << e_dof << "] decoy e_dof_deriv = " << e_dof_deriv << "; Eterm = " << decoys_[ ii ]->free_data()[ e_dof ]<< std::endl;
155  dpartition[ e_dof ] += e_dof_deriv;
156  }
157  }
158 
159  // -1 is here just to make lower scores better -- don't need kT (I think)
160  // abs() is here in case log(N/P) == 0, so that we get +0 instead of -0 (which seems to break the minimizer?)
161  Real const total_score = std::abs( -1.0 * multiplier_ * std::log( numerator / partition ) );
162  //std::cout << " total score: " << total_score << std::endl;
163 
164  // If score is small enough, don't compute derivatives -- just say they're zero.
165  // This may help protect us from weird minimizer run-away when "perfection" is attainable.
166  if( total_score >= 1e-2 ) {
167  // accumulate to passed-in derivative sums -- excludes reference energies
168  //std::cout << "vars (dvars): ";
169  for ( Size dof(1); dof <= num_energy_dofs; ++dof ) {
170  Real const dP_P = dpartition[ dof ] / partition;
171  Real const dN_N = dnumerator[ dof ] / numerator;
172  Real const dE_dvar = multiplier_ * (dP_P - dN_N);
173  // This error *should* never occur, thanks to the minimum value for numerator (above).
174 #ifndef WIN32
175 // std::isinf and std::isnan seem to give Visual Studio problems for some reason.
176  if( std::isinf(dE_dvar) || std::isnan(dE_dvar) ) std::cout << "[" << tag() << "," << dof << "] final deriv = " << dE_dvar << "; " << dpartition[ dof ] << "/" << partition << " - " << dnumerator[ dof ] << "/" << numerator << std::endl;
177  else dE_dvars[ dof ] += component_weights[ type() ] * dE_dvar;
178  //std::cout << " " << vars[ dof ] << "(" << dE_dvar << ")";
179 #endif
180  }
181  }
182 
183  if ( print ) {
184  ostr << "PNatLigPose " << tag() << X(1)
185  << " num: " << F(7,3,numerator) << " part: " << F(7,3,partition)
186  << " p: " << F(7,5,numerator / partition)
187  << " -lnp: " << F(6,4,-1.0 * std::log( numerator / partition ))
188  << " -compwt_lnp: " << F(6, 4, component_weights[ type() ] * (-1.0 * std::log( numerator / partition )) ) << std::endl;
189  }
190 
191  return component_weights[ type() ] * total_score;
192 }
193 
194 
197 {
199 }
200 
201 
202 } // namespace optimize_weights
203 } // namespace protocols