Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SimulatedTempering.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/canonical_sampling/SimulatedTemperingMover.cc
11 /// @brief SimulatedTempering methods implemented
12 /// @author Oliver Lange ( oliver.lange@tum.de )
13 
14 
15 // Unit Headers
18 
19 
20 // protocols headers
23 #include <protocols/moves/Mover.hh>
27 
29 
30 //#include <protocols/jd2/JobDistributor.hh>
31 #include <protocols/jd2/util.hh>
32 #include <protocols/jd2/Job.hh>
33 
34 // core headers
35 #include <basic/options/option_macros.hh>
36 #include <basic/options/keys/in.OptionKeys.gen.hh>
37 #include <basic/options/keys/packing.OptionKeys.gen.hh>
38 
39 #include <basic/Tracer.hh>
40 
43 
45 #include <core/types.hh>
46 
47 // numeric headers
48 #include <numeric/random/random.hh>
49 
50 // utility headers
51 #include <utility/file/file_sys_util.hh>
52 #include <utility/pointer/owning_ptr.hh>
53 #include <utility/tag/Tag.hh>
54 #include <utility/io/ozstream.hh>
55 #include <utility/io/izstream.hh>
56 
57 // C++ Headers
58 #include <cmath>
59 
60 // cmd-line options
61 OPT_2GRP_KEY( Integer, tempering, reweight, stride )
62 OPT_2GRP_KEY( Boolean, tempering, temp, jump )
63 OPT_2GRP_KEY( Real, tempering, temp, offset )
64 
65 using basic::T;
66 using basic::Error;
67 using basic::Warning;
68 
69 static basic::Tracer tr( "protocols.canonical_sampling.SimulatedTempering" );
70 static numeric::random::RandomGenerator RG(6127547);
71 
72 
73 bool protocols::canonical_sampling::SimulatedTempering::options_registered_( false );
74 
75 //Mike: when you want to remove these Macros... leave them at least here as comment - since they provide documentation
76 void protocols::canonical_sampling::SimulatedTempering::register_options() {
77  if ( !options_registered_ ) {
78  options_registered_ = true;
80  NEW_OPT( tempering::temp::offset, "offset for score (effectively scales all weights)", 40 );
81  NEW_OPT( tempering::reweight::stride, "every X trials update the weights distribution - 0 for no reweighting", 0 );
82  NEW_OPT( tempering::temp::jump, "if true we can jump to any temperature instead of +/- 1 level", false );
83  }
84 }
85 
86 namespace protocols {
87 namespace canonical_sampling {
88 using namespace core;
89 
93 }
94 
97  return new SimulatedTempering;
98 }
99 
102  return "SimulatedTempering";
103 }
104 
106  set_defaults();
107 }
108 
110  protocols::canonical_sampling::TemperingBase(other)
111 {
114  weights_ = other.weights_ ;
115  counts_ = other.counts_ ;
117 
118 }
119 
120 /// @brief callback executed before any Monte Carlo trials
121 /// use to fill count_ with 0
122 void
124  counts_.resize( 0 );
125  counts_.assign( n_temp_levels(), 1 ); //initialize with 1 to avoid div by zero...
126 }
127 
128 void
130  pose::Pose & pose,
131  protocols::canonical_sampling::MetropolisHastingsMover const & metropolis_hastings_mover,
132  core::Size cycle //default=0; non-zero if trajectory is restarted
133 ) {
134  Parent::initialize_simulation(pose, metropolis_hastings_mover,cycle);
135  total_count_ = 0;
137  tr.Debug << std::setprecision(2);
138  if ( weights_.size() != n_temp_levels() ) {
139  weights_.clear();
140  weighted_counts_.clear();
141  for ( Size ct = 0; ct < n_temp_levels(); ++ct ) {
142  weights_.push_back( 1.0 );
143  weighted_counts_.push_back( 0 );
144  }
145  }
146 }
147 
148 void
150  for ( Size i = 1; i <= counts_.size(); ++i ) {
151  weighted_counts_[ i ] += 1.0 * counts_[ i ] / weights_[ i ];
152  }
153  //update weights...
154  for ( Size i = 1; i <= weights_.size(); ++i ) {
155  weights_[i] = 1.0 / weighted_counts_[i];
156  }
157  Real const norm_w ( weights_.back() ); //and normalize by last element...
158  for ( Size i = 1; i <= weights_.size(); ++i ) {
159  weights_[i] /= norm_w;
160  }
161 }
162 
163 void
165  pose::Pose& pose,
167 ) {
169  Parent::finalize_simulation( pose, mhm );
170 }
171 
172 void
174  reweight();
175  write_to_file( stats_file(), output_name, weighted_counts_ );
177 
178 }
179 
183  if ( !time_for_temp_move() ) return temperature();
184  //temperature increase, decrease or wait?
185  Size new_temp( current_temp() );
186  Size const nlevels( n_temp_levels() );
187  if ( temperature_jumps_ ) {
188  new_temp=RG.random_range( 1, nlevels );
189  } else {
190  Real const r1( RG.uniform() );
191  if ( r1 > 0.5+self_transition_*0.5 ) {
192  ++new_temp;
193  } else if ( r1 < 0.5-self_transition_*0.5 ) {
194  --new_temp;
195  }
196  }
197  if ( new_temp > nlevels ) new_temp = nlevels;
198  if ( new_temp < 1 ) new_temp = 1;
199 
200  //make the decision based on score and temperature ratio
201  Real real_temp( temperature() );
202  if ( new_temp!=current_temp() ) {
203  Real const prefac( weights_[ new_temp ]/weights_[ current_temp() ] );
204  Real const temp_ratio =
205  (temperature() - temperature( new_temp ))/(temperature() * temperature( new_temp ));
206  if ( RG.uniform() < std::min( 1.0, prefac*std::exp(-(score+score_offset_)*temp_ratio) )) {
207  set_current_temp( new_temp );
208  real_temp = temperature();
209  tr.Debug << "set new temperature to level " << new_temp << " T=" << real_temp << std::endl;
210  }
211  }
212  ++counts_[ current_temp() ];
213  ++total_count_;
214  if ( reweight_stride_ > 0 && total_count_ % reweight_stride_ == 0 ) {
215  reweight();
218  }
219  return real_temp;
220 }
221 
222 
225 {
226  return "SimulatedTempering";
227 }
228 
231 {
233 }
234 
237 {
238  return new SimulatedTempering;
239 }
240 
241 void
243  utility::tag::TagPtr const tag,
245  protocols::filters::Filters_map const & filters,
246  protocols::moves::Movers_map const & movers,
247  pose::Pose const & pose
248 ) {
249  Parent::parse_my_tag( tag, data, filters, movers, pose );
250  //simple options
251  score_offset_ = tag->getOption< Real >( "score_offset", 40.0 );
252  temperature_jumps_ = tag->getOption< bool >( "temperature_jumps", false );
253  reweight_stride_ = tag->getOption< Size >( "reweight_stride", false );
254 }
255 
256 /// handling of options including command-line
258  self_transition_ = 0.0;
259  score_offset_ = 40;
260 }
261 
262 /// @brief Assigns user specified values to primitive members using command line options
264  using namespace basic::options;
265  using namespace basic::options::OptionKeys;
266  using namespace core;
267  tr.Debug << "initialize from options..." << std::endl;
269  temperature_jumps_ = option[ tempering::temp::jump ]();
270  reweight_stride_ = option[ tempering::reweight::stride ]();
271  score_offset_ = option[ tempering::temp::offset ]();
272 }
273 
275  utility::vector1< core::Real > temperatures;
276  weights_.clear();
277 
278  utility::io::izstream in( filename );
279  if ( !in.good() ) {
280  tr.Error << "cannot open file " << filename << std::endl;
281  return false;
282  }
283  std::string line;
284  getline( in, line );
285  std::istringstream line_stream( line );
286  std::string tag;
287  Size n_levels( 0 );
288 
289  line_stream >> tag >> n_levels >> score_offset_;
290 
291  bool line_format( false );
292  if ( !line_stream.good() ) {
293  tr.Error << "format not recognized in temperature file: " << filename << " at line " << line << std::endl;
294  tr.Error << "excpected TEMPERING or TEMPERING_TABLE" << std::endl;
295  return false;
296  }
297 
298  if ( tag == "TEMPERING" ) {
299  line_format=true;
300  } else if ( tag == "TEMPERING_TABLE" ) {
301  line_format=false;
302  } else {
303  tr.Error << "format not recognized in temperature file: " << filename << " at line " << line << std::endl;
304  tr.Error << "excpected TEMPERING or TEMPERING_TABLE" << std::endl;
305  return false;
306  }
307 
308  Real temp, weight, count, wcount;
309  if ( line_format ) {
310  for ( Size ct=1; ct <= n_levels; ++ct ) {
311  line_stream >> temp >> weight >> wcount >> count;
312  temperatures.push_back( temp );
313  weights_.push_back( weight );
314  weighted_counts_.push_back( wcount );
315  //ignore the counts
316  }
317  if ( !line_stream.good() ) {
318  tr.Error << "format not recognized in temperature file: " << filename << " at line " << line << std::endl;
319  tr.Error << "excpected TEMPERING N t1 w1 c1 t2 w2 c2 ... tN wN cN" << std::endl;
320  return false;
321  }
322  return true;
323  }
324 
325  // table format
326  while ( getline( in, line ) ) {
327  std::istringstream line_stream( line );
328  Real temp, weight, wcount;
329  line_stream >> temp;
330  if ( !line_stream.good() ) tr.Error << "format error in temperature file: " << filename << " at line " << line << std::endl;
331  line_stream >> weight;
332  if ( !line_stream.good() ) {
333  tr.Warning << "no weights in temperature file: " << filename << " initialize with 1.0" << std::endl;
334  weight = 1.0;
335  }
336  line_stream >> wcount;
337  if ( !line_stream.good() ) {
338  wcount=1;
339  }
340  temperatures.push_back( temp );
341  weights_.push_back( weight );
342  weighted_counts_.push_back( wcount );
343  }
344  set_temperatures( temperatures );
345  return true; //succesfully initialized
346 }
347 
348 void SimulatedTempering::write_to_file( std::string const& file_in, std::string const& output_name, utility::vector1< Real > const& wcounts ) {
349 
350  //either write all these things to a single file, or write to <jobname>.file_in
351  std::string file;
352  if ( stats_silent_output() ) {
353  file=file_in;
354  } else {
355  file=output_name+"."+file_in;
356  }
357 
358  //open file
359  utility::io::ozstream out( file, stats_line_output() ? std::ios::app : std::ios::out );
360 
361  //write
362  if ( stats_line_output() ) { //line format
363  out << "TEMPERING " << n_temp_levels() << " " << score_offset_ << " " << total_count_;
364  for ( Size i=1; i <= n_temp_levels(); ++i ) {
365  out << " " << temperature( i ) << " " << weights_[ i ] << " " << wcounts[ i ] << " " << counts_[ i ];
366  }
367  out << " " << output_name << std::endl;
368  } else { //table format
369  out << std::setw( 10 );
370  out << "TEMPERING_TABLE " << n_temp_levels() << " " << score_offset_ << " " << " " << total_count_ << " " << output_name << std::endl;
371  for ( Size i=1; i <= n_temp_levels(); ++i ) {
372  out << temperature( i ) << " " << weights_[ i ] << " " << wcounts[ i ] << " " << counts_[ i ] << std::endl;
373  }
374  }
375 }
376 
377 
378 } //moves
379 } //protocols
380