Skip to content

Commit

Permalink
reproducible code
Browse files Browse the repository at this point in the history
  • Loading branch information
karinemiras committed Feb 12, 2020
1 parent 764c485 commit ffad29e
Show file tree
Hide file tree
Showing 10 changed files with 1,315 additions and 46 deletions.
Empty file modified experiments/plasticoding_frontiers2020/baseline_big.py
100755 → 100644
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

library(sqldf)
require('magick')

##### change paths/labels/params here #####


paths = c( 'baseline_big', 'plastic_big' )

environments = list(
c( 'plane','tilted5'),
c( 'plane','tilted5')
)

base_directory <- paste('journal2/', sep='')

runs = list( c(1:20), c(1:20) )
gens = 200
pop = 100
num_top = 1

analysis = 'images'

##### change paths/labels/params here #####

output_directory = paste(base_directory,analysis, sep='')


file <-file(paste(output_directory,'/best.txt',sep=''), open="w")

# for each method
for(m in 1:length(paths))
{
# for each repetition
for (exp in runs[[m]])
{

input_directory1 <- paste(base_directory, paths[m],'_',exp, '/data_fullevolution/',environments[[m]][1],sep='')
input_directory2 <- paste(base_directory, paths[m],'_',exp, '/selectedpop_', sep='')

ids_gens = data.frame()
list = strsplit(list.files(paste(input_directory2, environments[[m]][1],'/selectedpop_',gens-1, sep='')), ' ')
for(geno in 1:pop)
{
genome = data.frame(cbind(c(gens), c(strsplit(strsplit(list [[geno]],'_')[[1]][3],'.png')[[1]][1] )))
names(genome)<-c('generation','robot_id')
ids_gens = rbind(ids_gens,genome)
}

measures = read.table(paste(input_directory1,"/all_measures.tsv", sep=''), header = TRUE, fill=TRUE)
bests = sqldf(paste("select robot_id, cons_fitness from measures inner join ids_gens using (robot_id) order by cons_fitness desc limit",num_top))

for(b in 1:nrow(bests))
{

writeLines( paste(paths[m],'exp',exp,bests[b,'robot_id'] ,bests[b,'cons_fitness'] ), file )
print( paste(paths[m],'exp',exp,bests[b,'robot_id'] ,bests[b,'cons_fitness'] ))

phenotype= bests[b,'robot_id']

for (env in 1:length(environments[[m]]))
{
patha = paste(input_directory2, environments[[m]][env], "/selectedpop_",gens-1,sep="")

body <- list.files(patha, paste("body_robot_",phenotype,".png$",sep=""), full.names = TRUE)
body = image_read(body)
body = image_scale(body, "100x100")
body = image_border(image_background(body, "white"), "white", "5x5")

if(b == 1 && env == 1)
{
bodies = body
}else{
bodies = c(bodies, body)
}
}
}

side_by_side = image_append(bodies, stack=F)
image_write(side_by_side, path = paste(output_directory,"/",paths[m],'_', environments[[m]][env], "_bodies_best_",exp,".png",sep=''), format = "png")

}
}


close(file)
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
# set these variables according to your experiments #
dirpath = 'data/'
experiments_type = [

'flat_big',
'tilted_big',
'plastic_big'
,'baseline_big'
]
environments = {

'flat_big': ['plane'],
'tilted_big': ['tilted5'],
'plastic_big': ['plane','tilted5']
,'baseline_big': ['plane','tilted5']
}
Expand Down
150 changes: 150 additions & 0 deletions experiments/plasticoding_frontiers2020/flat_big.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/usr/bin/env python3
import asyncio

from pyrevolve import parser
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.tol.manage import measures
from pyrevolve.util.supervisor.simulator_queue import SimulatorQueue
from pyrevolve.util.supervisor.analyzer_queue import AnalyzerQueue
from pyrevolve.custom_logging.logger import logger
import sys

async def run():
"""
The main coroutine, which is started below.
"""

# experiment params #
num_generations = 200
population_size = 100
offspring_size = 100
front = None

# environment world and z-start
environments = {'plane': 0.03}

genotype_conf = PlasticodingConfig(
max_structural_modules=15,
plastic=False,
)

mutation_conf = MutationConfig(
mutation_prob=0.8,
genotype_conf=genotype_conf,
)

crossover_conf = CrossoverConfig(
crossover_prob=0.8,
)
# experiment params #

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

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

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

def fitness_function_plane(robot_manager, robot):
return fitness.displacement_velocity_hill(robot_manager, robot, False)

fitness_function = {'plane': fitness_function_plane}

population_conf = PopulationConfig(
population_size=population_size,
genotype_constructor=random_initialization,
genotype_conf=genotype_conf,
fitness_function=fitness_function,
mutation_operator=standard_mutation,
mutation_conf=mutation_conf,
crossover_operator=standard_crossover,
crossover_conf=crossover_conf,
selection=lambda individuals: tournament_selection(individuals, environments, 2),
parent_selection=lambda individuals: multiple_selection(individuals, 2, tournament_selection, environments),
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,
environments=environments,
front=front
)

settings = parser.parse_args()

simulator_queue = {}
analyzer_queue = None

previous_port = None
for environment in environments:

settings.world = environment
settings.z_start = environments[environment]

if previous_port is None:
port = settings.port_start
previous_port = port
else:
port = previous_port+settings.n_cores
previous_port = port

simulator_queue[environment] = SimulatorQueue(settings.n_cores, settings, port)
await simulator_queue[environment].start()

analyzer_queue = AnalyzerQueue(1, settings, port+settings.n_cores)
await analyzer_queue.start()

population = Population(population_conf, simulator_queue, analyzer_queue, next_robot_id)

if do_recovery:

if gen_num >= 0:
# loading a previous state of the experiment
await population.load_snapshot(gen_num)
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)

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...
Empty file modified experiments/plasticoding_frontiers2020/plastic_big.py
100755 → 100644
Empty file.
25 changes: 17 additions & 8 deletions experiments/plasticoding_frontiers2020/run-experiments
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@ while true
done
done




while true
do
for i in {1..20}; do
./revolve.py --experiment-name karines_experiments/data/flat_big_$i --run $i --manager experiments/karines_experiments/flat_big.py --n-cores 4 --port-start 11141 --evaluation-time 50;

while true
sleep 5s
done
done


while true
do
sleep 900s;
kill $( ps aux | grep 'gzserver' | awk '{print $2}');
kill $( ps aux | grep 'revolve.py' | awk '{print $2}');
sleep 60s;
for i in {1..20}; do
./revolve.py --experiment-name karines_experiments/data/tilted_big_$i --run $i --manager experiments/karines_experiments/tilted_big.py --n-cores 4 --port-start 11141 --evaluation-time 50;

sleep 5s
done
done




Loading

0 comments on commit ffad29e

Please sign in to comment.