Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SOGFunc_Impl.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 src/core/scoring/constraints/SOGFunc_Impl.cc
11 /// @brief Definition for functions used in definition of constraints.
12 /// @author James Thompson
13 
14 #include <numeric/util.hh>
15 
18 #include <core/types.hh>
19 #include <basic/options/option.hh>
20 // AUTO-REMOVED #include <basic/options/keys/james.OptionKeys.gen.hh>
21 #include <basic/options/keys/constraints.OptionKeys.gen.hh>
22 
23 // C++ Headers
24 
25 #include <iostream>
26 
27 #include <utility/vector1.hh>
28 
29 
30 namespace core {
31 namespace scoring {
32 namespace constraints {
33 
35 
36 void
37 SOGFunc_Impl::read_data( std::istream & in ) {
38  Size n_funcs;
39 
40  clear_(); // don't forget to remove old data!
41 
42  Real lowest_sdev(100);
43  in >> n_funcs;
44  for ( Size i = 1; i <= n_funcs; ++i ) {
45  Real mean(0.0), sdev(0.0), weight(0.0);
46  in >> mean >> sdev >> weight;
47 
48  if ( sdev == 0 ) {
49  sdev = 4.0;
50  }
51 
52  means_. push_back( mean );
53  sdevs_. push_back( sdev );
54  weights_.push_back( weight );
55 
56  if ( sdev < lowest_sdev ) lowest_sdev = sdev;
57  }
58 
59  bool renormalize(
60  weights_.size() != sdevs_.size()
61  );
62 
63  if ( !renormalize ) {
64  for ( Size ii = 1; ii <= weights_.size(); ++ii ) {
65  //bool renormalize(false);
66  if ( numeric::isinf( weights_[ii] ) || numeric::isnan( weights_[ii] ) ) {
67  renormalize = true;
68  }
69  }
70  }
71 
72  if ( renormalize ) renormalize_weights();
73 
74  using namespace basic::options;
75  using namespace basic::options::OptionKeys;
76  if ( option[ OptionKeys::constraints::sog_upper_bound ].user() ) {
77  upper_bound(option[ OptionKeys::constraints::sog_upper_bound ]());
78  }
79  else {
80  upper_bound(10);
81  }
82 }
83 
85  using core::Size;
86  using core::Real;
87 
88  Real total(0.0);
89  for ( Size ii = 1; ii <= sdevs_.size(); ++ii ) {
90  total += std::pow( sdevs_[ii], -10 );
91  }
92 
93  weights_.resize( sdevs_.size(), 0.0 );
94  for ( Size ii = 1; ii <= sdevs_.size(); ++ii ) {
95  weights_[ii] = std::pow( sdevs_[ii], -10 ) / total;
96  }
97 
98  runtime_assert( sdevs_.size() == means_.size() );
99  runtime_assert( sdevs_.size() == weights_.size() );
100 }
101 
103  return smooth_;
104 }
105 
106 void SOGFunc_Impl::smooth_to_zero( bool const setting ) {
107  smooth_ = setting;
108 }
109 
111  assert( r > 0 );
112  upper_bound_ = r;
113  score_upper_ = -1 * std::log( prob_sum_of_gaussians(r) );
114 }
115 
117  return upper_bound_;
118 }
119 
121  return score_upper_;
122 }
123 
124 void
126  means_. clear();
127  sdevs_. clear();
128  weights_.clear();
129 }
130 
133  Real const alt_mean( 18.991 );
134  Real const alt_sdev( 7.353 );
135  return -1 * std::log( dgaussian( x, alt_mean, alt_sdev, 1.0 ) );
136 }
137 
139  Real score( 0.0 );
141  w = weights_.begin(), w_end = weights_.end(),
142  m = means_.begin(), m_end = means_.end(),
143  s = sdevs_.begin(), s_end = sdevs_.end(); // iterators
144  w != w_end && m != m_end && s != s_end; // condition
145  ++w, ++m, ++s // iteration
146  ) {
147  Real temp_sc = dgaussian( x, *m, *s, *w );
148  score += temp_sc;
149  }
150 
151  return score;
152 }
153 
154 Real
155 SOGFunc_Impl::func( Real const x ) const {
156  if ( x > upper_bound() ) {
157  return 0.0;
158  }
159  Real score = prob_sum_of_gaussians(x);
160  check_bounds( x, score );
161  if ( score <= 1e-50 ) { // avoid floating point comparison to 0
162  return 0.0;
163  } else {
164  Real sc = -std::log(score);
165  sc = sc - upper_bound_score();
166  return sc;
167  }
168 } // func
169 
170 Real
171 SOGFunc_Impl::dfunc( Real const x ) const {
172  Real const h( 1e-6 );
173  Real const df( (func(x+h) - func(x-h)) / (2*h) );
174  check_bounds( x, df );
175  return df;
176 } // dfunc
177 
178 void SOGFunc_Impl::check_bounds( Real const x, Real const val ) const {
179  if ( numeric::isinf( val ) || numeric::isnan( val ) ) {
180  std::cerr << "bounds error (radius = " << x << ", val = " << val << "), def = ";
181  show_definition( std::cerr );
182 #ifdef BOINC
183  utility_exit_with_message( "Fatal SOGFunc_Impl error." );
184 #endif
185  }
186 }
187 
188 void SOGFunc_Impl::show_definition( std::ostream & out ) const {
189  assert( weights_.size() == means_.size() );
190  assert( weights_.size() == sdevs_.size() );
191 
192  out << "SOGFUNC " << weights_.size();
193  for ( Size i = 1; i <= weights_.size(); ++i ) {
194  out << " " << means_[i] << " " << sdevs_[i] << " " << weights_[i];
195  }
196  out << '\n';
197 } // show_definition
198 
200  return sog_cst_param_;
201 }
202 
204  sog_cst_param_ = param;
205 }
206 
208  using namespace basic::options;
209  using namespace basic::options::OptionKeys;
210 
211  if ( option[ basic::options::OptionKeys::constraints::sog_cst_param ].user() ) {
212  sog_cst_param( option[ basic::options::OptionKeys::constraints::sog_cst_param ]() );
213  }
214 }
215 
216 } // namespace constraints
217 } // namespace scoring
218 } // namespace core