GeneticSearch.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #ifndef OMPL_GEOMETRIC_GENETIC_SEARCH_
38 #define OMPL_GEOMETRIC_GENETIC_SEARCH_
39 
40 #include "ompl/base/SpaceInformation.h"
41 #include "ompl/base/goals/GoalRegion.h"
42 #include "ompl/geometric/HillClimbing.h"
43 #include "ompl/util/Console.h"
44 
45 namespace ompl
46 {
47  namespace geometric
48  {
60  class GeneticSearch
61  {
62  public:
65  GeneticSearch(const base::SpaceInformationPtr &si);
66  ~GeneticSearch();
67 
69  bool solve(double solveTime, const base::GoalRegion &goal, base::State *result,
70  const std::vector<base::State *> &hint = std::vector<base::State *>());
71 
74  void setMaxImproveSteps(unsigned int maxSteps)
75  {
76  hc_.setMaxImproveSteps(maxSteps);
77  }
78 
81  unsigned int getMaxImproveSteps() const
82  {
83  return hc_.getMaxImproveSteps();
84  }
85 
87  void setValidityCheck(bool valid)
88  {
89  checkValidity_ = valid;
90  hc_.setValidityCheck(valid);
91  }
92 
94  bool getValidityCheck() const
95  {
96  return checkValidity_;
97  }
98 
101  void setTryImprove(bool flag)
102  {
103  tryImprove_ = flag;
104  }
105 
108  bool getTryImprove() const
109  {
110  return tryImprove_;
111  }
112 
114  void setPoolSize(unsigned int size)
115  {
116  poolSize_ = size;
117  }
118 
120  unsigned int getPoolSize() const
121  {
122  return poolSize_;
123  }
124 
126  void setPoolMutationSize(unsigned int size)
127  {
128  poolMutation_ = size;
129  }
130 
132  unsigned int getPoolMutationSize() const
133  {
134  return poolMutation_;
135  }
136 
138  void setPoolRandomSize(unsigned int size)
139  {
140  poolRandom_ = size;
141  }
142 
144  unsigned int getPoolRandomSize() const
145  {
146  return poolRandom_;
147  }
148 
150  void setRange(double distance)
151  {
152  maxDistance_ = distance;
153  }
154 
156  double getRange() const
157  {
158  return maxDistance_;
159  }
160 
162  void clear();
163 
164  private:
166  void tryToImprove(const base::GoalRegion &goal, base::State *state, double distance);
167 
170  bool valid(const base::State *state) const
171  {
172  return checkValidity_ ? si_->isValid(state) : true;
173  }
174 
175  struct Individual
176  {
177  base::State *state;
178  double distance;
179  bool valid;
180  };
181 
182  struct IndividualSort
183  {
184  bool operator()(const Individual &a, const Individual &b)
185  {
186  if (a.valid == b.valid)
187  return a.distance < b.distance;
188  return a.valid;
189  }
190  };
191 
192  HillClimbing hc_;
193  base::SpaceInformationPtr si_;
194  base::StateSamplerPtr sampler_;
195  std::vector<Individual> pool_;
196  unsigned int poolSize_;
197  unsigned int poolMutation_;
198  unsigned int poolRandom_;
199  unsigned int generations_;
200  bool checkValidity_;
201  bool tryImprove_;
202 
203  double maxDistance_;
204  };
205  }
206 }
207 
208 #endif
unsigned int getPoolSize() const
Get the number number of individuals in the population.
bool solve(double solveTime, const base::GoalRegion &goal, base::State *result, const std::vector< base::State * > &hint=std::vector< base::State * >())
Find a state that fits the request.
double getRange() const
Get the range GeneticSearch is using.
void setPoolSize(unsigned int size)
Set the number of individuals in the population.
Hill Climbing search.
Definition: HillClimbing.h:124
Definition of an abstract state.
Definition: State.h:113
unsigned int getPoolMutationSize() const
Get the number of individuals that are mutated at each generation.
void setValidityCheck(bool valid)
Set the state validity flag; if this is false, states are not checked for validity.
Definition: HillClimbing.h:186
void setMaxImproveSteps(unsigned int maxSteps)
Set the number of steps to perform when using hill climbing to improve an individual in the populatio...
unsigned int getPoolRandomSize() const
Get the number of individuals to randomly sample at each generation.
bool getTryImprove() const
Returns true if improvements using hill climbing should be attempted for solutions generated by the g...
void setPoolRandomSize(unsigned int size)
Set the number of individuals to randomly sample at each generation.
unsigned int getMaxImproveSteps() const
Get the number of steps to perform.
Definition: HillClimbing.h:180
void setTryImprove(bool flag)
Set the flag that determines whether improvements using hill climbing should be attempted for solutio...
void setValidityCheck(bool valid)
Set the state validity flag; if this is false, states are not checked for validity.
void setRange(double distance)
Set the range (distance) to be used when sampling around a state.
bool getValidityCheck() const
Get the state validity flag; if this is false, states are not checked for validity.
unsigned int getMaxImproveSteps() const
Get the number of steps to perform when using hill climbing to improve an individual in the populatio...
void clear()
Clear the pool of samples.
void setMaxImproveSteps(unsigned int steps)
Set the number of steps to perform.
Definition: HillClimbing.h:174
GeneticSearch(const base::SpaceInformationPtr &si)
Construct an instance of a genetic algorithm for inverse kinematics given the space information to se...
void setPoolMutationSize(unsigned int size)
Set the number of individuals to mutate at each generation.
Main namespace. Contains everything in this library.