Skip to content

Tutorial 5: Set up experiments

Jie Luo edited this page Jan 24, 2021 · 1 revision

Here we will learn the process of setting up experiments.

  1. Within the project root create an empty file tutorial5.py.
  2. Make sure it is executable:
    sudo chmod +x tutorial5.py
  3. Open the file with a text editor, add this piece of a code to the script and save it
"""
Online evolution experiment. This experiment is just a test script:
- Using default settings
- Evolving 10 generations
- With a population size of 20 robots
- Constructing 10 new robots after every gen.
"""
#!/usr/bin/env python3
import asyncio

from pyrevolve import parser
from pyrevolve.custom_logging.logger import logger
from pyrevolve.evolution import fitness
from pyrevolve.evolution.selection import multiple_selection, tournament_selection
from pyrevolve.evolution.population import Population, PopulationConfig
from pyrevolve.evolution.pop_management.steady_state import steady_state_population_management
from pyrevolve.experiment_management import ExperimentManagement
from pyrevolve.genotype.plasticoding.crossover.crossover import CrossoverConfig
from pyrevolve.genotype.plasticoding.crossover.standard_crossover import standard_crossover
from pyrevolve.genotype.plasticoding.initialization import random_initialization
from pyrevolve.genotype.plasticoding.mutation.mutation import MutationConfig
from pyrevolve.genotype.plasticoding.mutation.standard_mutation import standard_mutation
from pyrevolve.genotype.plasticoding.plasticoding import PlasticodingConfig
from pyrevolve.util.supervisor.analyzer_queue import AnalyzerQueue
from pyrevolve.util.supervisor.simulator_queue import SimulatorQueue



async def run():
   
#################################### SETTING UP THE EXPERIMENT #####################################
    # Define experiment paramameters
    num_generations = 10
    population_size = 20
    offspring_size = 10

    # Define the maximum number of structural modules per robot
    genotype_conf = PlasticodingConfig(
        max_structural_modules=100,
    )
    # Define the mutation probability
    mutation_conf = MutationConfig(
        mutation_prob=0.8,
        genotype_conf=genotype_conf,
    )
    # Define the Crossover probability
    crossover_conf = CrossoverConfig(
        crossover_prob=0.8,
    )

    # Parse command line / file input arguments
    settings = parser.parse_args()
    experiment_management = ExperimentManagement(settings)
    do_recovery = settings.recovery_enabled and not experiment_management.experiment_is_new()

    # Print experiment information
    logger.info('Activated run '+settings.run+' of experiment '+settings.experiment_name)

    # Loading the generation count of a previous experiment (after a crash),
    #   or initialize the generation count for a new experiment
    if do_recovery:
        gen_num, has_offspring, next_robot_id = experiment_management.read_recovery_state(population_size, offspring_size)

        if gen_num == num_generations-1:
            logger.info('Experiment is already complete.')
            return
    else:
        gen_num = 0
        next_robot_id = 1

    # Insert the population parameters PopulationConfig:
    population_conf = PopulationConfig(
        population_size=population_size,
        genotype_constructor=random_initialization,
        genotype_conf=genotype_conf,
        fitness_function=fitness.displacement_velocity,
        mutation_operator=standard_mutation,
        mutation_conf=mutation_conf,
        crossover_operator=standard_crossover,
        crossover_conf=crossover_conf,
        selection=lambda individuals: tournament_selection(individuals, 2),
        parent_selection=lambda individuals: multiple_selection(individuals, 2, tournament_selection),
        population_management=steady_state_population_management,
        population_management_selector=tournament_selection,
        evaluation_time=settings.evaluation_time,
        offspring_size=offspring_size,
        experiment_name=settings.experiment_name,
        experiment_management=experiment_management,
    )

    # Define the number of simultaneous simulations [default =1]
    n_cores = settings.n_cores

############################### SETTING UP THE FIRST NEW GENERATION ###############################
    # Start the connection to the gazebo simulator with the proper DynamicSupervisor() settings in SimulatorQueue
    settings = parser.parse_args()
    simulator_queue = SimulatorQueue(n_cores, settings, settings.port_start)
    await simulator_queue.start()

    # Start the connection of the evaluation script RobotAnalyzer() to the simulator in AnalyzerQueue
    analyzer_queue = AnalyzerQueue(1, settings, settings.port_start+n_cores)
    await analyzer_queue.start()

    # Create a Population object that initialises:
    #   individuals in the population with an empty list,
    #   and stores the configuration of the system to the conf variable.
    population = Population(population_conf, simulator_queue, analyzer_queue, next_robot_id)

    # Create the next generation based on a current population by:
    #   Loading the last generation/offspring of a previous experiment (after a crash),
    #   or initializing a first generation for a new experiment
    if do_recovery:
        # loading a previous state of the experiment
        await population.load_snapshot(gen_num)
        if gen_num >= 0:
            logger.info('Recovered snapshot '+str(gen_num)+', pop with ' + str(len(population.individuals))+' individuals')
        if has_offspring:
            individuals = await population.load_offspring(gen_num, population_size, offspring_size, next_robot_id)
            gen_num += 1
            logger.info('Recovered unfinished offspring '+str(gen_num))

            if gen_num == 0:
                await population.init_pop(individuals)
            else:
                population = await population.next_gen(gen_num, individuals)

            experiment_management.export_snapshots(population.individuals, gen_num)
    else:
        # starting a new experiment
        experiment_management.create_exp_folders()
        await population.init_pop()
        experiment_management.export_snapshots(population.individuals, gen_num)

################################## RUN THE REST OF THE EXPERIMENT ##################################
    # Start the while loop that produces new generations based on previous one
    while gen_num < num_generations-1:
        gen_num += 1
        population = await population.next_gen(gen_num)
        experiment_management.export_snapshots(population.individuals, gen_num)

    # output result after completing all generations...
    # experiment_folder = .../revolve/data/<experiment_name>/<run>
  1. Run the script:
    (.venv) ./revolve.py --simulator-cmd=gazebo --manager ./tutorial5.py

You should get an output like this (the robot in the picture might be different):

Gazebo screen capture

STARTING
[2021-01-23 18:40:38,295    revolve] INFO     Activated run 1 of experiment default_experiment
[2021-01-23 18:40:38,296   gazebo_0] INFO     Created Supervisor with:
	- simulator command: gazebo ['--verbose']
	- world file: worlds/plane.world
	- GAZEBO_PLUGIN_PATH: /Users/jieluo/revolve/build/lib
	- GAZEBO_MODEL_PATH: /Users/jieluo/revolve/models
[2021-01-23 18:40:43,300   gazebo_0] INFO     Launching the simulator...
[2021-01-23 18:40:44,257   gazebo_0] INFO     [starting] [Msg] Waiting for master.
[2021-01-23 18:40:44,258   gazebo_0] INFO     [Msg] Connected to gazebo master @ http://127.0.0.1:11345
[2021-01-23 18:40:44,258   gazebo_0] INFO     [Msg] Publicized address: 130.37.156.43
[2021-01-23 18:40:44,258   gazebo_0] INFO     [Msg] Loading world file [/Users/jieluo/revolve/worlds/plane.world]
[2021-01-23 18:40:44,258   gazebo_0] INFO     World plugin loaded.
[2021-01-23 18:40:44,260   gazebo_0] INFO     Gazebo multi-robot simulator, version 11.3.0
[2021-01-23 18:40:44,260   gazebo_0] INFO     Copyright (C) 2012 Open Source Robotics Foundation.
[2021-01-23 18:40:44,260   gazebo_0] INFO     Released under the Apache 2 License.
[2021-01-23 18:40:44,260   gazebo_0] INFO     http://gazebosim.org
[2021-01-23 18:40:44,260   gazebo_0] INFO    
[2021-01-23 18:40:44,260   gazebo_0] INFO     Gazebo multi-robot simulator, version 11.3.0
[2021-01-23 18:40:44,260   gazebo_0] INFO     Copyright (C) 2012 Open Source Robotics Foundation.
[2021-01-23 18:40:44,260   gazebo_0] INFO     Released under the Apache 2 License.
[2021-01-23 18:40:44,260   gazebo_0] INFO     http://gazebosim.org
[2021-01-23 18:40:44,260   gazebo_0] INFO    
[2021-01-23 18:40:44,287   gazebo_0] INFO     Setting robot state update frequency to 5.
[2021-01-23 18:40:44,287    revolve] INFO     simulator 0 waiting for robot
[2021-01-23 18:40:45,288 analyzer_0] INFO     Created Supervisor with:
	- simulator command: gzserver ['--verbose']
	- world file: tools/analyzer/analyzer-world.world
	- GAZEBO_PLUGIN_PATH: /Users/jieluo/revolve/build/lib
	- GAZEBO_MODEL_PATH: /Users/jieluo/revolve/models
[2021-01-23 18:40:45,721   gazebo_0] INFO     [Msg] Waiting for master.
[2021-01-23 18:40:45,721   gazebo_0] INFO     [Msg] Connected to gazebo master @ http://127.0.0.1:11345
[2021-01-23 18:40:45,721   gazebo_0] INFO     [Msg] Publicized address: 130.37.156.43
[2021-01-23 18:40:45,721   gazebo_0] ERROR    [Wrn] [GuiIface.cc:120] Populating font family aliases took 881 ms. Replace uses of missing font family "Sans" with one that exists to avoid this cost. 
[2021-01-23 18:40:50,288 analyzer_0] INFO     Launching the simulator...
[2021-01-23 18:40:50,912 analyzer_0] INFO     [starting] [Msg] Waiting for master.
[2021-01-23 18:40:50,913 analyzer_0] INFO     [Msg] Connected to gazebo master @ http://127.0.0.1:11346
[2021-01-23 18:40:50,913 analyzer_0] INFO     [Msg] Publicized address: 130.37.156.43
[2021-01-23 18:40:50,913 analyzer_0] INFO     [Msg] Loading world file [/Users/jieluo/revolve/tools/analyzer/analyzer-world.world]
[2021-01-23 18:40:50,913 analyzer_0] INFO     Gazebo multi-robot simulator, version 11.3.0
[2021-01-23 18:40:50,913 analyzer_0] INFO     Copyright (C) 2012 Open Source Robotics Foundation.
[2021-01-23 18:40:50,913 analyzer_0] INFO     Released under the Apache 2 License.
[2021-01-23 18:40:50,914 analyzer_0] INFO     http://gazebosim.org
[2021-01-23 18:40:50,914 analyzer_0] INFO    
[2021-01-23 18:40:50,914 analyzer_0] INFO     [Wrn] [ODEPhysics.cc:215] Gravity vector is (0, 0, 0). Objects will float.
unhandled message type: topic_namespace_add
unhandled message type: topic_namespace_add
[2021-01-23 18:40:50,941    revolve] INFO     simulator 0 waiting for robot
[2021-01-23 18:40:51,243 analyzer_0] INFO     Body analyzer ready
[2021-01-23 18:40:51,955    revolve] INFO     Robot 1 was late-developed.
[2021-01-23 18:40:52,555    revolve] INFO     Robot robot_1 was measured.
[2021-01-23 18:40:52,556    revolve] INFO     Robot 2 was late-developed.
[2021-01-23 18:40:53,094    revolve] INFO     Robot robot_2 was measured.
[2021-01-23 18:40:53,097    revolve] INFO     Robot 3 was late-developed.
[2021-01-23 18:40:53,754    revolve] INFO     Robot robot_3 was measured.
[2021-01-23 18:40:53,756    revolve] INFO     Robot 4 was late-developed.
[2021-01-23 18:40:54,396    revolve] INFO     Robot robot_4 was measured.
[2021-01-23 18:40:54,400    revolve] INFO     Robot 5 was late-developed.
[2021-01-23 18:40:54,982    revolve] INFO     Robot robot_5 was measured.
[2021-01-23 18:40:54,986    revolve] INFO     Robot 6 was late-developed.
[2021-01-23 18:40:55,533    revolve] INFO     Robot robot_6 was measured.
[2021-01-23 18:40:55,537    revolve] INFO     Robot 7 was late-developed.
[2021-01-23 18:40:56,255    revolve] INFO     Robot robot_7 was measured.
[2021-01-23 18:40:56,261    revolve] INFO     Robot 8 was late-developed.
[2021-01-23 18:40:56,868    revolve] INFO     Robot robot_8 was measured.
[2021-01-23 18:40:56,876    revolve] INFO     Robot 9 was late-developed.
[2021-01-23 18:40:57,600    revolve] INFO     Robot robot_9 was measured.
[2021-01-23 18:40:57,602    revolve] INFO     Robot 10 was late-developed.
[2021-01-23 18:40:58,217    revolve] INFO     Robot robot_10 was measured.
[2021-01-23 18:40:58,222    revolve] INFO     Robot 11 was late-developed.
[2021-01-23 18:40:58,878    revolve] INFO     Robot robot_11 was measured.
[2021-01-23 18:40:58,880    revolve] INFO     Robot 12 was late-developed.


See next: Tutorial 6: Debug the Code


_________________
/ Premature      \
| optimization   |
| is the root of |
| all evil.      |
|                |
\ -- D.E. Knuth  /
-----------------
    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||
Clone this wiki locally