Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MinMover.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 MinMover.cc
11 /// @brief
12 /// @author ashworth
13 
14 // Unit headers
17 
19 
20 // Package headers
21 
29 #include <core/scoring/ScoreFunctionFactory.hh> // getScoreFunction
30 #include <core/pose/Pose.fwd.hh>
31 // AUTO-REMOVED #include <core/pose/Pose.hh>
32 #include <basic/prof.hh>
33 #include <utility/string_util.hh>
34 #include <utility/tag/Tag.hh>
35 #include <iostream>
38 
39 // Boost Headers
40 #include <boost/foreach.hpp>
41 #include <basic/Tracer.hh>
42 
43 #include <utility/vector0.hh>
44 #include <utility/vector1.hh>
45 
46 #define foreach BOOST_FOREACH
47 
48 namespace protocols {
49 namespace simple_moves {
50 
51 using namespace core;
52 using namespace kinematics;
53 using namespace optimization;
54 using namespace scoring;
56 
57 
58 
59 
60 static basic::Tracer TR("protocols.simple_moves.MinMover");
61 
64 {
66 }
67 
70  return new MinMover;
71 }
72 
75 {
76  return "MinMover";
77 }
78 
79 // default constructor
80 // proper lightweight default constructor
82  : protocols::moves::Mover("MinMover"),
83  movemap_(0),
84  scorefxn_(0),
85  min_options_(0),
86  cartesian_(false),
87  dof_tasks_()
88  // threshold_(1000000.0) // TODO: line can be deleted?
89 {
90  min_options_ = new MinimizerOptions( "linmin", 0.01, true, false, false );
91 }
92 
94  : protocols::moves::Mover(name),
95  movemap_(0),
96  scorefxn_(0),
97  min_options_(0),
98  cartesian_(false),
99  dof_tasks_()
100  // threshold_(1000000.0) // TODO: line can be deleted?
101 {
102  min_options_ = new MinimizerOptions( "linmin", 0.01, true, false, false );
103 }
104 
106 
107 // constructor with arguments
109  MoveMapOP movemap_in,
110  ScoreFunctionCOP scorefxn_in,
111  std::string const & min_type_in,
112  Real tolerance_in,
113  bool use_nb_list_in,
114  bool deriv_check_in /* = false */,
115  bool deriv_check_verbose_in /* = false */
116 ) : protocols::moves::Mover("MinMover"),
117  movemap_( movemap_in ),
118  scorefxn_( scorefxn_in ),
119  min_options_(0),
120  threshold_(1000000.0), // TODO: line can be deleted?
121  cartesian_(false),
122  dof_tasks_()
123 {
124  min_options_ = new MinimizerOptions(
125  min_type_in, tolerance_in, use_nb_list_in, deriv_check_in, deriv_check_verbose_in );
126 }
127 
128 /// @brief allow non-const access to the internal minimizer options object
131  return min_options_;
132 }
133 
134 /// @brief allow const access to the internal minimizer options object
137  return min_options_;
138 }
139 
140 void
141 MinMover::movemap( MoveMapCOP movemap_in )
142 {
143  runtime_assert( movemap_in );
144  movemap_ = new MoveMap( *movemap_in );
145 }
146 
149 {
150  return movemap_;
151 }
152 
153 void
155 {
156  runtime_assert( scorefxn_in );
157  scorefxn_ = scorefxn_in;
158 }
159 
160 void
162 {
163  scorefxn_ = scorefxn_in.clone();
164 }
165 
168 {
169  return scorefxn_;
170 }
171 
172 void MinMover::min_type( std::string min_type_in ) { min_options_->min_type( min_type_in ); }
173 std::string MinMover::min_type() const { return min_options_->min_type(); }
174 
175 
176 void MinMover::tolerance( Real tolerance_in ) { min_options_->minimize_tolerance( tolerance_in ); }
177 Real MinMover::tolerance() const { return min_options_->minimize_tolerance(); }
178 
179 
180 void MinMover::nb_list( bool nb_list_in ) { min_options_->use_nblist( nb_list_in ); }
181 bool MinMover::nb_list() const { return min_options_->use_nblist(); }
182 
183 
184 void MinMover::deriv_check( bool deriv_check_in ) { min_options_->deriv_check( deriv_check_in ); }
185 bool MinMover::deriv_check() const { return min_options_->deriv_check(); }
186 
187 
188 ///@detail restrict the move map by the packer task:
189 ///If a residue is not designable, the backbone is fixes
190 ///If a residue is not packable, the sidechain is fixed
191 ///
192 ///WARNING: This is extending the abuse of using task operations for
193 ///general ResidueSubsets
194 ///
195 ///When TaskOperations replaced with ResidueSetOperations please
196 ///change this too!
197 void
199  core::pose::Pose const & pose,
200  MoveMap & movemap
201 ) const {
202 
203  for(
204  DOF_TaskMap::const_iterator t=dof_tasks_.begin(), te=dof_tasks_.end();
205  t != te; ++t){
206  //generate task
207  PackerTaskOP task( t->second->create_task_and_apply_taskoperations( pose ) );
208 
209  //modify movemap by task
210  Size const nres( task->total_residue() );
211 
212  for ( Size i(1); i <= nres; ++i ) {
213  if ( !task->pack_residue( i ) ){
214  if( t->first.first == core::id::PHI ){
215  movemap.set( t->first.second, false );
216  } else {
217  movemap.set( t->first.first, false );
218  }
219  }
220  }
221  }
222 }
223 
224 void
226  pose::Pose & pose
227 ) {
228  // lazy default initialization
229  MoveMapOP active_movemap;
230  if ( ! movemap() ) movemap() = new MoveMap;
231  else active_movemap = movemap()->clone();
232 
233  apply_dof_tasks_to_movemap(pose, *active_movemap);
234 
235 
236  if ( ! scorefxn_ ) scorefxn_ = getScoreFunction(); // get a default (INITIALIZED!) ScoreFunction
237 
238  PROF_START( basic::MINMOVER_APPLY );
239  if (!cartesian( )) {
240  AtomTreeMinimizer minimizer;
241  (*scorefxn_)(pose);
242  minimizer.run( pose, *active_movemap, *scorefxn_, *min_options_ );
243  } else {
244  CartesianMinimizer minimizer;
245  (*scorefxn_)(pose);
246  minimizer.run( pose, *active_movemap, *scorefxn_, *min_options_ );
247  }
248  PROF_STOP( basic::MINMOVER_APPLY );
249 
250  // emit statistics
251  scorefxn_->show(TR.Debug, pose);
252  TR.Debug << std::endl;
253 }
254 
258 }
259 
262 
263 void MinMover::parse_def_opts( utility::lua::LuaObject const & def,
264  utility::lua::LuaObject const & score_fxns,
265  utility::lua::LuaObject const & /*tasks*/,
266  protocols::moves::MoverCacheSP /*cache*/ ) {
267  if( def["scorefxn"] ) {
268  score_function( protocols::elscripts::parse_scoredef( def["scorefxn"], score_fxns ) );
269  } else {
270  score_function( score_fxns["score12"].to<ScoreFunctionSP>()->clone() );
271  }
272 
273  if ( ! movemap_ ) movemap_ = new MoveMap;
274  if( def["jump"] ) {
275  for (utility::lua::LuaIterator i=def["jump"].begin(), end; i != end; ++i) {
276  if( (*i).to<int>() == -1 ) {
277  movemap_->set_jump( true );
278  break;
279  } else if( (*i).to<core::Size>() == 0 ) {
280  movemap_->set_jump( false );
281  break;
282  } else {
283  TR << "Setting min on jump " << (*i).to<core::Size>() << std::endl;
284  movemap_->set_jump( (*i).to<core::Size>(), true );
285  }
286  }
287  }
288 
289  min_type( def["type"] ? def[ "type" ].to<std::string>() : "dfpmin_armijo_nonmonotone" );
290  tolerance( def["tolerance"] ? def[ "tolerance" ].to<core::Real>() : 0.01 );
291  cartesian( def["cartesian"] ? def[ "cartesian" ].to<bool>() : false );
292  //fpd if cartesian default to lbfgs minimization
293  if ( cartesian() && ! def["type"] ) {
294  min_type( "lbfgs_armijo_nonmonotone" );
295  }
296 
297  // TODO: Add parsing of task operations?
298 }
299 
300 void MinMover::parse_def( utility::lua::LuaObject const & def,
301  utility::lua::LuaObject const & score_fxns,
302  utility::lua::LuaObject const & tasks,
304  if ( ! movemap_ ) movemap_ = new MoveMap;
305 
306  parse_def_opts( def, score_fxns, tasks, cache );
307 
308  if( def["chi"] )
309  movemap_->set_chi(def["chi"].to<bool>());
310  if( def["bb"] )
311  movemap_->set_chi(def["bb"].to<bool>());
312 
313 
314  if( def["movemap"] )
316 }
317 
319  TagPtr const tag,
321  Filters_map const & filters,
322  protocols::moves::Movers_map const & movers,
323  Pose const & pose )
324 {
325  if ( ! movemap_ ) movemap_ = new MoveMap;
326  parse_opts( tag, data, filters, movers, pose );
327  parse_chi_and_bb( tag );
328  parse_dof_tasks( tag, data );
329 
330 /// parse_movemap will reset the movemap to minimize all if nothing is stated on it. The following section ensures that the protocol specifies a MoveMap before going that way
331 // utility::vector1< TagPtr > const branch_tags( tag->getTags() );
332 // utility::vector1< TagPtr >::const_iterator tag_it;
333  protocols::rosetta_scripts::parse_movemap( tag, pose, movemap_, data, false );
334 
335 }
336 
338  TagPtr const tag,
340  Filters_map const &,
342  Pose const & )
343 {
344  std::string const scorefxn_name( tag->getOption< std::string >( "scorefxn", "score12" ) );
345  score_function( data.get< ScoreFunction * >( "scorefxns", scorefxn_name ) );
346  if ( tag->hasOption("jump") ) {
347  if ( ! movemap_ ) movemap_ = new MoveMap;
348  utility::vector1<std::string> jumps = utility::string_split( tag->getOption<std::string>( "jump" ), ',' );
349  // string 'ALL' makes all jumps movable
350  if (jumps.size() == 1 && (jumps[1] == "ALL" || jumps[1] == "All" || jumps[1] == "all" || jumps[1] == "*") ) {
351  movemap_->set_jump( true );
352  } else if( tag->getOption< core::Size > ( "jump" ) == 0 ) {
353  movemap_->set_jump( false );
354  } else {
355  foreach(std::string jump, jumps){
356  Size const value = std::atoi( jump.c_str() ); // convert to C string, then convert to integer, then set a Size (phew!)
357  TR << "Setting min on jump " << value << std::endl;
358  movemap_->set_jump( value, true );
359  }
360  }
361  }
362  min_type( tag->getOption< std::string >( "type", "dfpmin_armijo_nonmonotone" ) );
363  tolerance( tag->getOption< core::Real >( "tolerance", 0.01 ) );
364 
365  cartesian( tag->getOption< core::Real >( "cartesian", false ) );
366 
367  //fpd if cartesian default to lbfgs minimization
368  if ( cartesian() && !tag->hasOption("type") ) {
369  min_type( "lbfgs_armijo_nonmonotone" );
370  }
371 
372 }
373 
375 {
376  if ( ! movemap_ ) movemap_ = new MoveMap;
377  bool const chi( tag->getOption< bool >( "chi" ) ), bb( tag->getOption< bool >( "bb" ) );
378  movemap_->set_chi( chi );
379  movemap_->set_bb( bb );
380  TR<<"Options chi, bb: "<<chi<<", "<<bb<<std::endl;
381  if ( tag->hasOption("bondangle") ) {
382  bool const value( tag->getOption<bool>("bondangle") );
383  movemap_->set( core::id::THETA, value );
384  }
385  if ( tag->hasOption("bondlength") ) {
386  bool const value( tag->getOption<bool>("bondlength") );
387  movemap_->set( core::id::D, value );
388  }
389 }
390 
391 ///@detail helper function for parse_of_tasks
392 void
394  std::string const & tag_name,
396  core::id::TorsionType torsion_type,
397  TagPtr const tag,
399 ) {
400 
401  if(!tag->hasOption(tag_name)){
402  return;
403  }
404 
405  using namespace core::pack::task;
406  using namespace core::pack::task::operation;
407  using std::string;
408  typedef utility::vector1< std::string > StringVec;
409 
410  TaskFactoryOP task_factory(new TaskFactory());
411  string const t_o_val( tag->getOption<string>(tag_name) );
412  StringVec const t_o_keys( utility::string_split( t_o_val, ',' ) );
413 
414  foreach( string t_o_key, t_o_keys ){
415  if ( data.has( "task_operations", t_o_key ) ) {
416  task_factory->push_back( data.get< TaskOperation * >( "task_operations", t_o_key ) );
417  TR << "\t " << tag_name << ": " << t_o_key << std::endl;
418  } else {
419  utility_exit_with_message("TaskOperation " + t_o_key + " not found in DataMap.");
420  }
421  }
422  dof_tasks_[
423  std::make_pair<core::id::DOF_Type, core::id::TorsionType>(
424  dof_type, torsion_type)] =
425  task_factory;
426 }
427 
428 void
430  TagPtr const tag,
432 ) {
433 
434  if(
435  tag->hasOption("bb_task_operations") ||
436  tag->hasOption("chi_task_operations") ||
437  tag->hasOption("bondangle_task_operations") ||
438  tag->hasOption("bondlength_task_operations")){
439  TR
440  << "Adding the following task operations to mover " << tag->getName() << " "
441  << "called " << tag->getOption<std::string>( "name", "no_name" ) << ":" << std::endl;
442  }
443 
444  parse_dof_task_type( "bb_task_operations", core::id::PHI, core::id::BB, tag, data );
445  parse_dof_task_type( "chi_task_operations", core::id::PHI, core::id::CHI, tag, data );
447  "bondangle_task_operations",
449  core::id::BB, // (dummy parameter)
450  tag, data );
451 
453  "bondlength_task_operations",
454  core::id::D,
455  core::id::BB, // (dummy parameter)
456  tag, data );
457 }
458 
459 std::ostream &operator<< (std::ostream &os, MinMover const &mover)
460 {
461  moves::operator<<(os, mover);
462  os << "Minimization type:\t" << mover.min_type() << "\nScorefunction:\t\t";
463  if ( mover.score_function() != 0 ) {
464  os << mover.score_function()->get_name() << std::endl;
465  }
466  else { os << "none" << std::endl; }
467  os << "Score tolerance:\t" << mover.tolerance() << "\nNb list:\t\t" << (mover.nb_list() ? "True" : "False") <<
468  "\nDeriv check:\t\t" << (mover.deriv_check() ? "True" : "False") << std::endl << "Movemap:" << std::endl;
469  if (mover.movemap() != 0) {
470  mover.movemap()->show(os);
471  }
472  return os;
473 }
474 
475 
476 } // moves
477 } // protocols