Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MetropolisHastingsMover.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/MetropolisHastingsMover.cc
11 /// @brief MetropolisHastingsMover methods implemented
12 /// @author
13 
14 
15 // Unit Headers
18 
19 
20 // protocols headers
22 // AUTO-REMOVED #include <protocols/moves/DataMap.hh>
24 #include <protocols/jd2/util.hh>
26 #include <protocols/moves/Mover.hh>
34 
35 // #include <core/scoring/constraints/ConstraintIO.hh>
36 // #include <core/scoring/constraints/ConstraintSet.hh>
37 // #include <core/scoring/constraints/ConstraintSet.fwd.hh>
38 
39 // core headers
41 // AUTO-REMOVED #include <basic/options/option_macros.hh>
42 #include <basic/options/keys/in.OptionKeys.gen.hh>
43 #include <basic/options/keys/packing.OptionKeys.gen.hh>
47 #include <core/types.hh>
48 #include <basic/Tracer.hh>
49 // AUTO-REMOVED #include <core/conformation/Residue.hh> // REQUIRED FOR WINDOWS
50 
51 // numeric headers
52 #include <numeric/random/random.hh>
53 
54 // utility headers
55 #include <utility/file/file_sys_util.hh>
56 #include <utility/io/ozstream.hh>
57 #include <utility/pointer/owning_ptr.hh>
58 #include <utility/tag/Tag.hh>
59 
61 #include <utility/vector0.hh>
62 #include <utility/vector1.hh>
63 
64 //Auto Headers
65 #include <utility/excn/Exceptions.hh>
66 #include <basic/options/option.hh>
67 
68 // C++ Headers
69 
70 using basic::T;
71 using basic::Error;
72 using basic::Warning;
73 
74 static basic::Tracer TR( "protocols.canonical_sampling.MetropolisHastingsMover" );
75 static numeric::random::RandomGenerator RG(638767547);
76 
77 namespace protocols {
78 namespace canonical_sampling {
79 
80 
84 }
85 
88  return new MetropolisHastingsMover;
89 }
90 
93  return "MetropolisHastings";
94 }
95 
97  monte_carlo_(0),
98  ntrials_(1000),
99  trial_(0)
100 {}
101 
103  MetropolisHastingsMover const & metropolis_hastings_mover
104 ) :
105  //utility::pointer::ReferenceCount(),
106  Mover(metropolis_hastings_mover),
107  monte_carlo_( new protocols::moves::MonteCarlo(*metropolis_hastings_mover.monte_carlo_) ),
108  ntrials_(metropolis_hastings_mover.ntrials_),
109  output_name_(metropolis_hastings_mover.output_name_),
110  weighted_sampler_(metropolis_hastings_mover.weighted_sampler_),
111  trial_(metropolis_hastings_mover.trial_)
112 {
113  for (core::Size i = 1; i <= metropolis_hastings_mover.movers_.size(); ++i) {
114  movers_.push_back(reinterpret_cast<ThermodynamicMover *>(metropolis_hastings_mover.movers_[i]()));
115  }
116 
117  for (core::Size i = 1; i <= metropolis_hastings_mover.observers_.size(); ++i) {
118  observers_.push_back(reinterpret_cast<ThermodynamicObserver *>(metropolis_hastings_mover.observers_[i]()));
119  }
120 
121  if (metropolis_hastings_mover.tempering_) {
122  tempering_ = reinterpret_cast<TemperatureController *>(metropolis_hastings_mover.tempering_());
123  if (monte_carlo_) tempering_->set_monte_carlo(monte_carlo_);
124  }
125 }
126 
128 
131  if (output_name() == "") {
133  TR.Info << " obtained output name from JobDistributor " << std::endl;
135  } else {
136  TR.Info << " running with preset output name: " << output_name() << std::endl;
137  }
138 
139  if ( !tempering_ ) {
140  //get this done before "initialize_simulation" is called no movers and observers
141  TR.Info << "no temperature controller in MetropolisHastings defined... generating FixedTemperatureController" << std::endl;
143  }
144  using namespace core;
145  bool restart = false;
146  core::Size cycle_number = 0;
147  Size temp_level = 0;
148  Real temperature = -1.0;
149 // for (core::Size i = 1; i <= observers_.size() && !restart; ++i) {
150 // TR << "Attempting restart using " << observers_[i]->get_name() << std::endl;
151 // restart = observers_[i]->restart_simulation(pose, *this, cycle_number, temp_level, temperature );
152 // if ( restart ) TR << "Restarted using " << observers_[i]->get_name() << std::endl;
153 // }
154 
155  if ( !restart ) {
156  cycle_number = 0; //make sure this is zero if we don't have a restart.
157  tempering_->initialize_simulation(pose, *this, cycle_number );
158  } else {
159  tempering_->initialize_simulation(pose, *this, temp_level, temperature, cycle_number );
160  }
161 
162  for (core::Size i = 1; i <= movers_.size(); ++i) {
163  TR << "Initializing " << movers_[i]->get_name() << std::endl;
164  movers_[i]->set_metropolis_hastings_mover(this);
165  movers_[i]->initialize_simulation(pose, *this, cycle_number);
166  }
167 
168  runtime_assert( monte_carlo_ );
169  monte_carlo_->reset(pose);
170  monte_carlo_->reset_counters();
171 
172  for (core::Size i = 1; i <= observers_.size(); ++i) {
173  TR << "Initializing " << observers_[i]->get_name() << std::endl;
174  observers_[i]->initialize_simulation(pose, *this, cycle_number);
175  }
176 
177  TR << "Initial Score:\n";
178  monte_carlo_->score_function().show(TR, pose);
179 
180  TR << "Running " << ntrials_-cycle_number << " trials..." << std::endl;
181  return cycle_number;
182 }
183 
184 void
187 
188  Size start_cycle = prepare_simulation( pose );
189 
190  for (trial_ = start_cycle+1; trial_ <= ntrials_; ++trial_) {
191 
193  mover->apply(pose);
194  bool accepted = monte_carlo_->boltzmann(
195  pose,
196  mover->type(),
197  mover->last_proposal_density_ratio(),
198  mover->last_inner_score_temperature_delta()
199  );
200  set_last_move( mover );
201  set_last_accepted( accepted );
202  tempering_->temperature_move( pose, monte_carlo_->last_accepted_score() );
203  mover->observe_after_metropolis(*this);
204  TR.Debug << "current move accepted " << accepted <<std::endl;
205  for (core::Size i = 1; i <= observers_.size(); ++i) {
206  observers_[i]->observe_after_metropolis(*this);
207  }
208 // core::scoring::constraints::ConstraintSetCOP cst_set = pose.constraint_set();
209 // if ( !cst_set->has_constraints() )
210 // utility_exit_with_message("cst_set is empty!");
211 // if ( trial_ % 500 == 0 ) {
212 // cst_set->show_violations(TR.Info, pose, 76, 1);
213 // }
214  }
215 
216  wind_down_simulation( pose );
217 }
218 
219 void
221  for (core::Size i = 1; i <= movers_.size(); ++i) {
222  TR << "Finalizing " << movers_[i]->get_name() << std::endl;
223  movers_[i]->finalize_simulation(pose, *this);
224  }
225 
226  for (core::Size i = 1; i <= observers_.size(); ++i) {
227  TR << "Finalizing " << observers_[i]->get_name() << std::endl;
228  observers_[i]->finalize_simulation(pose, *this);
229  }
230 
231  tempering_->finalize_simulation(pose, *this);
232 
233  TR << "Final Score:" << std::endl;
234  monte_carlo_->score_function().show(TR, pose);
235  TR.flush();
236 
237  monte_carlo_->show_counters();
238 
240 }
241 
244 {
245  return "MetropolisHastingsMover";
246 }
247 
250 {
252 }
253 
256 {
257  return new MetropolisHastingsMover;
258 }
259 
260 bool
262 
263 bool
265 
266 void
268  utility::tag::TagPtr const tag,
270  protocols::filters::Filters_map const & filters,
271  protocols::moves::Movers_map const & movers,
272  core::pose::Pose const & pose
273 ) {
274  ///ntrials
275  ntrials_ = tag->getOption< core::Size >( "trials", ntrials_ );
276 
277  //monte-carlo
278  //read tag scorefxn
280  core::Real const temperature(tag->getOption< core::Real >( "temperature", 0.6 ) );
281  monte_carlo_ = new protocols::moves::MonteCarlo(*score_fxn, temperature);
282 
283  //add movers and observers
284  utility::vector0< utility::tag::TagPtr > const subtags( tag->getTags() );
285  for( utility::vector0< utility::tag::TagPtr >::const_iterator subtag_it = subtags.begin(); subtag_it != subtags.end(); ++subtag_it ) {
286  TagPtr const subtag = *subtag_it;
288  if (subtag->getName() == "Add") { //add existing mover
289  std::string mover_name = subtag->getOption<std::string>( "mover_name", "null" );
290  protocols::moves::Movers_map::const_iterator mover_iter( movers.find( mover_name ) );
291  if ( mover_iter == movers.end() ) {
292  TR << "Mover not found for XML tag:\n" << subtag << std::endl;
293  throw utility::excn::EXCN_RosettaScriptsOption("");
294  }
295  mover = mover_iter->second;
296  } else { //generate new mover
298  mover = mover_factory->newMover(subtag, data, filters, movers, pose);
299  }
300 
301  ThermodynamicMoverOP th_mover( dynamic_cast<ThermodynamicMover *>( mover() ) );
302  ThermodynamicObserverOP th_observer( dynamic_cast<ThermodynamicObserver *>( mover() ) );
303  TemperatureControllerOP temp_controller( dynamic_cast< TemperatureController* >( mover() ) );
304  //figure out if ThermodynamicMover or ThermodynamicObserver
305  if ( th_mover ) { //its a mover
306  core::Real const weight( subtag->getOption< core::Real >( "sampling_weight", 1 ) );
307  add_mover( th_mover, weight, subtag );
308  // add_mover( th_mover, weight );
309  } else if ( th_observer ) { //its an observer
310  //it might also be a tempering module...
311  if ( temp_controller ) { // it is a temperature controller
312  if ( tempering_ ) {
313  throw utility::excn::EXCN_RosettaScriptsOption( "cannot define two TemperatureControllers" );
314  }
315  set_tempering( temp_controller );
316  } else { //no just an plain old observer
317  add_observer( th_observer );
318  }
319  } else { //its something different
320  TR << "Mover is not a ThermodynamicMover or ThermodynamicObserver for XML tag:\n" << subtag << std::endl;
321  throw utility::excn::EXCN_RosettaScriptsOption("");
322  }
323  }
324 }
325 
328  return monte_carlo_;
329 }
330 
332  return *monte_carlo_;
333 }
334 
335 void
338 )
339 {
341  if ( tempering_ ) tempering_->set_monte_carlo( monte_carlo_ );
342 }
343 
344 void
347 ) {
349  if ( monte_carlo_ ) tempering_->set_monte_carlo( monte_carlo_ );
350 }
351 
354  return tempering_;
355 }
356 
357 void
360 ) {
361  ntrials_ = ntrials;
362 }
363 
364 bool
366  return trial_ > ntrials_;
367 }
368 
369 std::string const &
371  return output_name_;
372 }
373 
374 void
376  std::string const & output_name
377 ) {
379 }
380 
383  std::string const & suffix,
384  bool cumulate_jobs, //= false
385  bool cumulate_replicas //= false
386 ) const {
387 
388  if (cumulate_jobs || cumulate_replicas) runtime_assert(utility::io::ozstream::MPI_reroute_rank() >= 0);
389 
390  std::ostringstream file_name_stream;
391 
392  if (!cumulate_jobs) {
393  file_name_stream << output_name_;
394  }
395 
396  core::Size const replica( protocols::jd2::current_replica() );
397  if (!cumulate_replicas && replica) {
398  if ( file_name_stream.str().length() ) file_name_stream << "_";
399  file_name_stream << std::setfill('0') << std::setw(3) << replica;
400  }
401 
402  if ( suffix.length() ) {
403  if ( file_name_stream.str().length() ) file_name_stream << "_";
404  file_name_stream << suffix;
405  }
406 
407  return file_name_stream.str();
408 }
409 
410 void
412  ThermodynamicMoverOP setting
413 ) {
414  last_move_ = setting;
415 }
416 
417 ThermodynamicMover const&
419  assert( last_move_ );
420  return *last_move_;
421 }
422 
425 {
426  return movers_[weighted_sampler_.random_sample(RG)];
427 }
428 
429 void
431  ThermodynamicMoverOP mover,
432  core::Real weight
433 )
434 {
435  mover->set_preserve_detailed_balance(true);
436  movers_.push_back(mover);
437  weighted_sampler_.add_weight(weight);
438 }
439 
440 void
442  ThermodynamicMoverOP mover,
443  core::Real weight,
444  utility::tag::TagPtr const&
445  )
446 {
447  add_mover( mover, weight );
448 }
449 
450 void
452  core::Real weight
453 )
454 {
455  if (!weight) return;
456 
458  backrub_mover->branchopt().read_database();
459 
460  add_mover(backrub_mover, weight);
461 }
462 
463 void
465  core::Real weight
466 )
467 {
468  if (!weight) return;
469 
470  using namespace basic::options;
471  using namespace basic::options::OptionKeys;
472 
474  small_mover->nmoves(1);
476  if (utility::file::file_exists(option[ in::file::movemap ])) {
477  movemap->init_from_file(option[ in::file::movemap ]);
478  } else {
479  movemap->set_bb(true);
480  }
481  small_mover->movemap(movemap);
482 
483  add_mover(small_mover, weight);
484 }
485 
486 void
488  core::Real weight
489 )
490 {
491  if (!weight) return;
492 
493  using namespace basic::options;
494  using namespace basic::options::OptionKeys;
495 
497  shear_mover->nmoves(1);
499  if (utility::file::file_exists(option[ in::file::movemap ])) {
500  movemap->init_from_file(option[ in::file::movemap ]);
501  } else {
502  movemap->set_bb(true);
503  }
504  shear_mover->movemap(movemap);
505 
506  add_mover(shear_mover, weight);
507 }
508 
509 void
511  core::Real weight,
512  core::Real prob_uniform,
513  core::Real prob_withinrot,
514  bool preserve_cbeta
515 )
516 {
517  if (!weight) return;
518 
519  using namespace basic::options;
520  using namespace basic::options::OptionKeys;
521 
524  if ( option[ packing::resfile ].user() ) {
525  main_task_factory->push_back( new core::pack::task::operation::ReadResfile );
526  } else {
527  main_task_factory->push_back( new core::pack::task::operation::RestrictToRepacking );
528  }
529  if (preserve_cbeta) main_task_factory->push_back( new core::pack::task::operation::PreserveCBeta );
530 
532  sidechain_mover->set_task_factory(main_task_factory);
533  sidechain_mover->set_prob_uniform(prob_uniform);
534  sidechain_mover->set_prob_withinrot(prob_withinrot);
535 
536  add_mover(sidechain_mover, weight);
537 }
538 
539 void
541  core::Real weight,
542  core::Real prob_uniform,
543  core::Real prob_withinrot,
544  bool preserve_cbeta,
546 )
547 {
548  if (!weight) return;
549 
550  using namespace basic::options;
551  using namespace basic::options::OptionKeys;
552 
555  if ( option[ packing::resfile ].user() ) {
556  main_task_factory->push_back( new core::pack::task::operation::ReadResfile );
557  } else {
558  main_task_factory->push_back( new core::pack::task::operation::RestrictToRepacking );
559  }
560  if (preserve_cbeta) main_task_factory->push_back( new core::pack::task::operation::PreserveCBeta );
561 
563  sidechain_mc_mover->set_task_factory(main_task_factory);
564  sidechain_mc_mover->set_prob_uniform(prob_uniform);
565  sidechain_mc_mover->set_prob_withinrot(prob_withinrot);
566  sidechain_mc_mover->setup(monte_carlo_->score_function().clone());
567  sidechain_mc_mover->set_ntrials(ntrials);
568 
569  add_mover(sidechain_mc_mover, weight);
570 }
571 
572 void
574  ThermodynamicObserverOP observer
575 )
576 {
577  observers_.push_back(observer);
578 }
579 
580 } //moves
581 } //protocols
582