[Documentation] [TitleIndex] [WordIndex

Installation

DEAP is in the ROS dependency system (rosdep). On Ubuntu, simply add

   1     <rosdep name="python-deap-pip"/>

this will install the latest version of DEAP in the system python. For any other configuration use pip directly.

pip install deap

Or refer to DEAP's homepage for more information.

DEAP

This is the ROS documentation for DEAP a distributed evolutionary algorithm written in Python. As DEAP is completly independent of ROS we suggest to install the latest version in your python directory either by downloading the latest version or using pip. The complete API documentation is available here.

Using DEAP as a Node

DEAP is used with ROS as independent as any other python package would be. The only difference is that the optimizer will probably call some ROS service. Here is a brief example of how to optimize the position of some robot according to a some fitness function.

   1 import roslib; roslib.load_manifest('my_optimization_package')
   2 import random, math
   3 from deap import creator, base, tools, algorithms
   4 
   5 # This line enable maximisation of a fitness function, for minimization use (-1.0,)
   6 creator.create("FitnessMax", base.Fitness, weights=(1.0,))
   7 creator.create("Individual", list, fitness=creator.FitnessMax)
   8 
   9 toolbox = base.Toolbox()
  10 
  11 # Helper function to generate pose 3-tuple
  12 def poseGenerator(icls, xmin, xmax, ymin, ymax):
  13     """Simple function to generate a random robot position and fill the
  14     *icls* class with it, *icls* must be initializable from an iterable.
  15     The robot position is generated in 2D (x, y, theta) with
  16     *xmin* <= x <= *xmax*, *ymin* <= y <= ymax and 0 <= theta <= pi."""
  17     return icls([random.uniform(xmin, xmax),
  18                  random.uniform(ymin, ymax),
  19                  random.uniform(0, 2*math.pi)])
  20 
  21 
  22 # Structure initializers
  23 toolbox.register("individual", poseGenerator, creator.Individual,
  24                  xmin=-2, xmax=2, ymin=-2, ymax=2)
  25 toolbox.register("population", tools.initRepeat, list, toolbox.individual)
  26 
  27 def evalPose(individual):
  28     """Function that evaluates the fitness value of a pose."""
  29     # Usualy the calls to ROS will be here
  30 
  31 toolbox.register("evaluate", evalPose)
  32 toolbox.register("mate", tools.cxTwoPoints)
  33 toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1.0, indpb=0.05)
  34 toolbox.register("select", tools.selTournament, tournsize=3)
  35 
  36 def main():
  37     # Create a population of 300 individuals
  38     population = toolbox.population(n=300)
  39     CXPB, MUTPB, NGEN = 0.6, 0.4, 50
  40 
  41     population, _ = algorithms.eaSimple(population, toolbox, cxpb=CXPB,
  42         mutpb=MUTPB, ngen=NGEN)
  43 
  44     # Find the best individual(s) in the resulting population
  45     # This returns a list
  46     bests = tools.selBest(population, k=1)
  47 
  48     # An individual is a complex object containing a fitness,
  49     # This will return only a three-tuple position (x, y, theta)
  50     return tuple(bests[0])
  51 
  52 if __name__ == "__main__":
  53     pose = main()
  54 
  55     # Send that pose to some other ROS node.
  56     # ...

For more example see DEAP's documentation.


2024-11-23 14:35