Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParticleSwarmMinimizer.hh
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 core/optimization/ParticleSwarmMinimizer.hh
11 ///
12 /// @brief
13 /// @author Ian W. Davis
14 
15 
16 #ifndef INCLUDED_core_optimization_ParticleSwarmMinimizer_hh
17 #define INCLUDED_core_optimization_ParticleSwarmMinimizer_hh
18 
20 #include <utility/pointer/ReferenceCount.hh>
21 
24 
25 #include <utility/vector1.hh>
26 
27 
28 namespace core {
29 namespace optimization {
30 
31 
32 /// @brief Simple data container for PSO algorithm.
34 {
35 public:
36 
38  p_(size, 0.0),
39  fitness_(0.0),
40  v_(size, 0.0),
41  best_valid_( false ),
42  pbest_(size, 0.0),
43  fitness_pbest_(0.0)
44  {}
45 
46  Particle(Multivec const & p_in):
47  p_(p_in),
48  fitness_(0.0),
49  v_(p_in.size(), 0.0),
50  best_valid_( false ),
51  pbest_(p_in),
52  fitness_pbest_(0.0)
53  {}
54 
55  virtual ~Particle() {}
56 
58  {
59  // Reverse the sign: for normal multifunc, lower is better
60  // For historical reasons, the code maximizes the "fitness"
61  fitness_ = -f(p_);
62  if( ! best_valid_ || fitness_pbest_ < fitness_ ) {
63  best_valid_ = true;
64  pbest_ = p_; // make a copy
66  }
67  return fitness_;
68  }
69 
70  Real set_score(Real & new_score)
71  {
72  // Reverse the sign: for normal multifunc, lower is better
73  // For historical reasons, the code maximizes the "fitness"
74  fitness_ = -new_score;
75  if( ! best_valid_ || fitness_pbest_ < fitness_ ) {
76  best_valid_ = true;
77  pbest_ = p_; // make a copy
79  }
80  return fitness_;
81  }
82 
83  /// @brief Make sure that all arrays are large enough -- prevents index-out-of-bound errors.
84  void ensure_size(Size minsize)
85  {
86  if( p_.size() < minsize ) p_.resize(minsize);
87  Size const s = p_.size();
88  if( v_.size() < s ) v_.resize(s);
89  if( pbest_.size() < s ) pbest_.resize(s);
90  }
91 
92  /// @brief This is why data should be private: you get to ensure it's valid when you read it.
93  Multivec const &
94  pbest() const {
95  assert( best_valid_ );
96  return pbest_;
97  }
98 
99  Real
100  fitness_pbest() const {
101  assert( best_valid_ );
102  return fitness_pbest_;
103  }
104 
105 public:
106  Multivec p_; //< current position (list of reals)
107  Real fitness_; //< current fitness, higher is better (real)
108  Multivec v_; //< current velocity (list of reals)
109 
110 private:
111  bool best_valid_; //< Does the fitness_pbest_ value represent a real value of the multifunc?
112  Multivec pbest_; //< copy of the "p" with the highest fitness seen by this particle
113  Real fitness_pbest_; //< fitness value for pbest
114 
115 }; // Particle
116 
117 std::ostream & operator << ( std::ostream & os, Particle const & p );
118 
119 
120 ///@brief Particle Swarm Optimization engine.
121 ///
122 ///@details Algorithm details based heavily on
123 ///
124 /// Chen, Liu, Huang, Hwang, Ho (2006).
125 /// "SODOCK: Swarm Optimization for Highly Flexible Protein-Ligand Docking"
126 /// J Comput Chem 28: 612-623, 2007
127 ///
128 /// Also on
129 /// http://en.wikipedia.org/wiki/Particle_swarm_optimization
130 /// http://www.swarmintelligence.org/
131 ///
132 /// One can imagine writing another version that distributed the work via MPI...
134 {
135 public:
136 
138  virtual ~ParticleSwarmMinimizer();
139 
140  ParticleOPs run(Size num_cycles, Multifunc & f_fitness, Size num_part = 50);
141  ParticleOPs run(Size num_cycles, Multifunc & f_fitness, Size num_part, Multivec init_values );
142  void run(Size num_cycles, Multifunc & f_fitness, ParticleOPs & particles);
143  void print_particles( ParticleOPs & particles, std::string header );
144 
145 protected:
146 
147  virtual void score_all_particles(Multifunc & f_fitness, ParticleOPs & particles);
148 
149 private:
150 
151  Size size_; //< number of degrees of freedom
154  Real C_pbest_; //< the "cognitive" parameter
155  Real C_lbest_; //< the neighborhood "social" parameter
156  Real C_gbest_; //< the global "social" parameter
163 
164 }; // ParticleSwarmMinimizer
165 
166 
167 } // namespace optimization
168 } // namespace core
169 
170 #endif // INCLUDED_core_optimization_ParticleSwarmMinimizer_HH