ompl/geometric/GeneticSearch.h
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 /* Author: Ioan Sucan */ 00036 00037 #ifndef OMPL_GEOMETRIC_GENETIC_SEARCH_ 00038 #define OMPL_GEOMETRIC_GENETIC_SEARCH_ 00039 00040 #include "ompl/base/SpaceInformation.h" 00041 #include "ompl/base/goals/GoalRegion.h" 00042 #include "ompl/geometric/HillClimbing.h" 00043 #include "ompl/util/Console.h" 00044 00045 namespace ompl 00046 { 00047 00048 namespace geometric 00049 { 00050 00062 class GeneticSearch 00063 { 00064 public: 00065 00067 GeneticSearch(const base::SpaceInformationPtr &si); 00068 ~GeneticSearch(); 00069 00071 bool solve(double solveTime, const base::GoalRegion &goal, base::State *result, 00072 const std::vector<base::State*> &hint = std::vector<base::State*>()); 00073 00075 void setMaxImproveSteps(unsigned int maxSteps) 00076 { 00077 hc_.setMaxImproveSteps(maxSteps); 00078 } 00079 00081 unsigned int getMaxImproveSteps() const 00082 { 00083 return hc_.getMaxImproveSteps(); 00084 } 00085 00087 void setValidityCheck(bool valid) 00088 { 00089 checkValidity_ = valid; 00090 hc_.setValidityCheck(valid); 00091 } 00092 00094 bool getValidityCheck() const 00095 { 00096 return checkValidity_; 00097 } 00098 00100 void setTryImprove(bool flag) 00101 { 00102 tryImprove_ = flag; 00103 } 00104 00106 bool getTryImprove() const 00107 { 00108 return tryImprove_; 00109 } 00110 00112 void setPoolSize(unsigned int size) 00113 { 00114 poolSize_ = size; 00115 } 00116 00118 unsigned int getPoolSize() const 00119 { 00120 return poolSize_; 00121 } 00122 00124 void setPoolMutationSize(unsigned int size) 00125 { 00126 poolMutation_ = size; 00127 } 00128 00130 unsigned int getPoolMutationSize() const 00131 { 00132 return poolMutation_; 00133 } 00134 00136 void setPoolRandomSize(unsigned int size) 00137 { 00138 poolRandom_ = size; 00139 } 00140 00142 unsigned int getPoolRandomSize() const 00143 { 00144 return poolRandom_; 00145 } 00146 00148 void setRange(double distance) 00149 { 00150 maxDistance_ = distance; 00151 } 00152 00154 double getRange() const 00155 { 00156 return maxDistance_; 00157 } 00158 00160 void clear(); 00161 00162 private: 00163 00165 void tryToImprove(const base::GoalRegion &goal, base::State *state, double distance); 00166 00168 bool valid(const base::State *state) const 00169 { 00170 return checkValidity_ ? si_->isValid(state) : true; 00171 } 00172 00173 struct Individual 00174 { 00175 base::State *state; 00176 double distance; 00177 bool valid; 00178 }; 00179 00180 struct IndividualSort 00181 { 00182 bool operator()(const Individual& a, const Individual& b) 00183 { 00184 if (a.valid == b.valid) 00185 return a.distance < b.distance; 00186 return a.valid; 00187 } 00188 }; 00189 00190 HillClimbing hc_; 00191 base::SpaceInformationPtr si_; 00192 base::StateSamplerPtr sampler_; 00193 std::vector<Individual> pool_; 00194 unsigned int poolSize_; 00195 unsigned int poolMutation_; 00196 unsigned int poolRandom_; 00197 unsigned int generations_; 00198 bool checkValidity_; 00199 bool tryImprove_; 00200 00201 double maxDistance_; 00202 }; 00203 00204 } 00205 } 00206 00207 #endif