Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/generationreplay #124

Open
wants to merge 5 commits into
base: dev/rabbitmq
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions experiments/isaac/manager_direct_cpghyper_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
from typing import List, Dict
import yaml

import math
from isaacgym import gymapi
Expand Down Expand Up @@ -129,7 +130,7 @@ async def run():
mutation_p_generate_subtree=morph_single_mutation_prob,
mutation_p_swap_subtree=morph_single_mutation_prob,
mutation_p_mutate_oscillators=brain_single_mutation_prob,
mutation_p_mutate_oscillator=0.5,
mutation_p_mutate_oscillator=0,
mutate_oscillator_amplitude_sigma=0.3,
mutate_oscillator_period_sigma=0.3,
mutate_oscillator_phase_sigma=0.3,
Expand Down Expand Up @@ -202,7 +203,7 @@ def worker_crash(process, exit_code) -> None:
# CELERY CONNECTION (includes database connection)
# simulator_queue = CeleryQueue(args, args.port_start, dbname='revolve', db_addr='127.0.0.1', use_isaacgym=True)
simulator_queue = CeleryPopulationQueue(args, use_isaacgym=True, local_computing=True)
await simulator_queue.start(cleanup_database=True)
await simulator_queue.start(cleanup_database=(not do_recovery))

# CELERY GAZEBO WORKER
celery_workers: List[GazeboCeleryWorkerSupervisor] = []
Expand Down Expand Up @@ -232,17 +233,19 @@ def worker_crash(process, exit_code) -> None:
if do_recovery:
# loading a previous state of the experiment
population.load_snapshot(gen_num, multi_development=True)
load_database_ids(gen_num, experiment_management,population.individuals)
if gen_num >= 0:
logger.info(f'Recovered snapshot {gen_num}, pop with {len(population.individuals)} individuals')
if has_offspring:
assert False
individuals = population.load_offspring(gen_num, population_size, offspring_size, next_robot_id)
gen_num += 1
logger.info(f'Recovered unfinished offspring {gen_num}')

if gen_num == 0:
await population.initialize_from_single_individual(individuals)
else:
population = await population.next_generation(gen_num, individuals)
population = await population.next_generation(gen_num)

experiment_management.export_snapshots(population.individuals, gen_num)
else:
Expand All @@ -259,6 +262,8 @@ def worker_crash(process, exit_code) -> None:
generate_candidate_partners(population, simulator_queue._db, args.grace_time)
new_population = await population.next_generation(gen_num)
update_robot_pose(population.individuals, simulator_queue._db)
with open(f'{experiment_management.generation_folder(gen_num-1)}/database_ids.yml', 'w') as database_id_file:
save_db_ids(database_id_file, population.individuals)
experiment_management.export_snapshots(population.individuals, gen_num)
with open(f'{experiment_management.generation_folder(gen_num-1)}/extra.tsv', 'w') as extra_data_file:
export_special_data(extra_data_file, population.individuals, new_population.individuals, gen_num, simulator_queue._db)
Expand All @@ -269,6 +274,18 @@ def worker_crash(process, exit_code) -> None:
await celery_worker.stop()
await simulator_queue.stop()

def save_db_ids(outfile, individuals: List[Individual]):
database_ids = {}
for ind in individuals:
database_ids[ind.phenotype.id] = int(ind.phenotype.database_id)
yaml.dump(database_ids, outfile)

def load_database_ids(gen_num: int, experiment_management: ExperimentManagement, individuals: List[Individual]):
database_ids = {}
with open(f'{experiment_management.generation_folder(gen_num-1)}/database_ids.yml', 'r') as database_id_file:
database_ids = yaml.safe_load(database_id_file)
for ind in individuals:
ind.phenotype.database_id = database_ids[ind.phenotype.id]

def update_robot_pose(individuals: List[Individual], db: PostgreSQLDatabase) -> None:

Expand Down
216 changes: 216 additions & 0 deletions experiments/isaac/manager_replay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
#!/usr/bin/env python3
from __future__ import annotations

import os
from typing import List, Dict
import yaml

import math
from isaacgym import gymapi

from experiments.isaac.positioned_population import PositionedPopulation
from pyrevolve import parser
from pyrevolve.custom_logging.logger import logger
from pyrevolve.evolution import fitness
from pyrevolve.evolution.individual import Individual
from pyrevolve.evolution.population.population_config import PopulationConfig
from pyrevolve.evolution.population.population_management import generational_population_management
from pyrevolve.evolution.selection import best_selection
from pyrevolve.experiment_management import ExperimentManagement
from pyrevolve.genotype.direct_tree.direct_tree_genotype import DirectTreeGenotypeConfig
from pyrevolve.genotype.neat_brain_genome.neat_brain_genome import NeatBrainGenomeConfig, BrainType
from pyrevolve.genotype.tree_body_hyperneat_brain import DirectTreeCPGHyperNEATGenotypeConfig, \
DirectTreeCPGHyperNEATGenotype
from pyrevolve.genotype.tree_body_hyperneat_brain.crossover import standard_crossover
from pyrevolve.genotype.tree_body_hyperneat_brain.mutation import standard_mutation
from pyrevolve.util.supervisor.rabbits import GazeboCeleryWorkerSupervisor, PostgreSQLDatabase, RobotEvaluation, \
RobotState
from pyrevolve.util.supervisor.rabbits.celery_queue import CeleryPopulationQueue

INTERNAL_WORKERS = False


def environment_constructor(gym: gymapi.Gym,
sim: gymapi.Sim,
_env_lower: gymapi.Vec3,
_env_upper: gymapi.Vec3,
_num_per_row: int,
env: gymapi.Env) -> float:
radius: float = 0.2
asset_options: gymapi.AssetOptions = gymapi.AssetOptions()
asset_options.density = 1.0
asset_options.linear_damping = 0.5
asset_options.angular_damping = 0.5
sphere_asset = gym.create_sphere(sim, radius, asset_options)
return 0



# MATING_RANGE = 0.45
MATING_RANGE = 45

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

# experiment params #
num_generations = 200
population_size = 32
offspring_size = population_size

morph_single_mutation_prob = 0.2
morph_no_single_mutation_prob = 1 - morph_single_mutation_prob # 0.8
morph_no_all_mutation_prob = morph_no_single_mutation_prob ** 4 # 0.4096
morph_at_least_one_mutation_prob = 1 - morph_no_all_mutation_prob # 0.5904

brain_single_mutation_prob = 0.5

tree_genotype_conf: DirectTreeGenotypeConfig = DirectTreeGenotypeConfig(
max_parts=25,
min_parts=5,
max_oscillation=5,
init_n_parts_mu=10,
init_n_parts_sigma=4,
init_prob_no_child=0.1,
init_prob_child_block=0.4,
init_prob_child_active_joint=0.5,
mutation_p_duplicate_subtree=morph_single_mutation_prob,
mutation_p_delete_subtree=morph_single_mutation_prob,
mutation_p_generate_subtree=morph_single_mutation_prob,
mutation_p_swap_subtree=morph_single_mutation_prob,
mutation_p_mutate_oscillators=brain_single_mutation_prob,
mutation_p_mutate_oscillator=0,
mutate_oscillator_amplitude_sigma=0.3,
mutate_oscillator_period_sigma=0.3,
mutate_oscillator_phase_sigma=0.3,
)

neat_conf: NeatBrainGenomeConfig = NeatBrainGenomeConfig(
brain_type=BrainType.CPG,
random_seed=None
)

genotype_conf: DirectTreeCPGHyperNEATGenotypeConfig = DirectTreeCPGHyperNEATGenotypeConfig(
direct_tree_conf=tree_genotype_conf,
neat_conf=neat_conf,
number_of_brains=1,
)

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

logger.info(f'Activated run {args.run} of experiment {args.experiment_name}')

#gen_num, has_offspring, next_robot_id, next_species_id = \
# experiment_management.read_recovery_state(population_size, offspring_size, species=False)
gen_num = args.generation
if gen_num < 0:
logger.info('Experiment continuing from first generation')
gen_num = 1

population_conf = PopulationConfig(
population_size=population_size,
genotype_constructor=lambda conf, _id: DirectTreeCPGHyperNEATGenotype(conf, _id, random_init_body=True),
genotype_conf=genotype_conf,
fitness_function=fitness.displacement_velocity,
objective_functions=None,
mutation_operator=lambda genotype, gen_conf: standard_mutation(genotype, gen_conf),
mutation_conf=genotype_conf,
crossover_operator=lambda parents, gen_conf, _: standard_crossover(parents, gen_conf),
crossover_conf=None,
selection=best_selection,
parent_selection=None,
population_management=generational_population_management,
population_management_selector=None,
evaluation_time=args.evaluation_time,
grace_time=args.grace_time,
offspring_size=offspring_size,
experiment_name=args.experiment_name,
experiment_management=experiment_management,
environment_constructor=environment_constructor, #TODO IMPLEMENT THIS!!!! pass it to isaacqueue that passes it to the manage_isaac_multiple
)

n_cores = args.n_cores

def worker_crash(process, exit_code) -> None:
logger.fatal(f'GazeboCeleryWorker died with code: {exit_code} ({process})')

# CELERY CONNECTION (includes database connection)
# simulator_queue = CeleryQueue(args, args.port_start, dbname='revolve', db_addr='127.0.0.1', use_isaacgym=True)
simulator_queue = CeleryPopulationQueue(args, use_isaacgym=True, local_computing=True)
await simulator_queue.start(cleanup_database=False)

# CELERY GAZEBO WORKER
celery_workers: List[GazeboCeleryWorkerSupervisor] = []
if INTERNAL_WORKERS:
for n in range(n_cores):
celery_worker = GazeboCeleryWorkerSupervisor(
world_file='worlds/plane.celery.world',
gui=args.gui,
simulator_args=['--verbose'],
plugins_dir_path=os.path.join('../heritability', 'build', 'lib'),
models_dir_path=os.path.join('../heritability', 'models'),
simulator_name=f'GazeboCeleryWorker_{n}',
process_terminated_callback=worker_crash,
)
await celery_worker.launch_simulator(port=args.port_start + n)
celery_workers.append(celery_worker)

# ANALYZER CONNECTION
# analyzer_port = args.port_start + (n_cores if INTERNAL_WORKERS else 0)
# analyzer_queue = AnalyzerQueue(1, args, port_start=analyzer_port)
# await analyzer_queue.start()
analyzer_queue = None

# INITIAL POPULATION OBJECT
population = PositionedPopulation(population_conf, simulator_queue, analyzer_queue)

# loading a previous state of the experiment
population.load_snapshot(gen_num, multi_development=True)
load_database_ids(gen_num, experiment_management,population.individuals)
if gen_num >= 0:
logger.info(f'Recovered snapshot {gen_num}, pop with {len(population.individuals)} individuals')

#new_population = await population.next_generation(gen_num)
update_robot_pose(population.individuals, simulator_queue._db)
await population.evaluate(population.individuals, gen_num)

# CLEANUP
for celery_worker in celery_workers:
await celery_worker.stop()
await simulator_queue.stop()

def update_robot_pose(individuals: List[Individual], db: PostgreSQLDatabase) -> None:

with db.session() as session:

last_eval: RobotEvaluation = session \
.query(RobotEvaluation) \
.filter(RobotEvaluation.robot_id == individuals[0].id) \
.order_by(RobotEvaluation.n.desc()) \
.one()
last_eval_n = last_eval.n
assert last_eval_n == 0

for individual in individuals:
dbid = int(individual.phenotype.database_id)
final_position = session \
.query(RobotState.pos_x, RobotState.pos_y) \
.filter(RobotState.evaluation_n == last_eval_n) \
.filter(RobotState.evaluation_robot_id == dbid) \
.order_by(RobotState.time_sec.desc(), RobotState.time_nsec.desc()) \
.first()
print(f"DB:{individual} ({final_position})")
individual.pose.x = final_position[0]
individual.pose.y = final_position[1]

def load_database_ids(gen_num: int, experiment_management: ExperimentManagement, individuals: List[Individual]):
database_ids = {}
with open(f'{experiment_management.generation_folder(gen_num-1)}/database_ids.yml', 'r') as database_id_file:
database_ids = yaml.safe_load(database_id_file)
for ind in individuals:
ind.phenotype.database_id = database_ids[ind.phenotype.id]
6 changes: 6 additions & 0 deletions pyrevolve/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ def str_to_address(v):
"\nDefault \"False\"."
)

parser.add_argument(
'--generation',
default=0, type=int,
help="Generation to replay"
)


def make_revolve_config(conf):
"""
Expand Down
4 changes: 2 additions & 2 deletions pyrevolve/experiment_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import List, AnyStr, Optional
from typing import List, AnyStr, Optional, Tuple
from pyrevolve.tol.manage.measures import BehaviouralMeasurements
from pyrevolve.evolution.speciation.genus import Genus
from pyrevolve.evolution.speciation.species import Species
Expand Down Expand Up @@ -301,7 +301,7 @@ def read_recovery_state(self,
population_size: int,
offspring_size: int,
species=False,
n_developments: int = 1) -> (int, bool, int):
n_developments: int = 1) -> Tuple[int, bool, int]:
"""
Read the saved data to determine how many generations have been completed and
if the last generation has partially started evaluating.
Expand Down
23 changes: 15 additions & 8 deletions pyrevolve/genotype/direct_tree/direct_tree_genotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyrevolve.genotype.direct_tree.direct_tree_config import DirectTreeGenotypeConfig
from pyrevolve.genotype.direct_tree.direct_tree_utils import duplicate_subtree
from pyrevolve.revolve_bot import RevolveBot
from pyrevolve.revolve_bot.brain import BrainNN, brain_nn
from pyrevolve.revolve_bot.brain import BrainNN, brain_nn, BrainCPPNCPG
from pyrevolve.revolve_bot.revolve_module import ActiveHingeModule
from pyrevolve.revolve_bot.revolve_module import CoreModule

Expand Down Expand Up @@ -53,18 +53,23 @@ def load_genotype(self, genotype_filename: AnyStr) -> None:
revolvebot.load_file(genotype_filename, conf_type='yaml')
self._load_genotype_from_revolvebot(revolvebot)

def _load_genotype_from_lines(self, genotype_lines: List[AnyStr]) -> None:
def _load_genotype_from_lines(self, genotype_lines: List[AnyStr], only_body=False) -> None:
revolvebot: RevolveBot = RevolveBot()
revolvebot.load('\n'.join(genotype_lines), conf_type='yaml')
self._load_genotype_from_revolvebot(revolvebot)
if only_body:
self._load_genotype_only_body(revolvebot)
else:
self._load_genotype_from_revolvebot(revolvebot)

def _load_genotype_from_revolvebot(self, revolvebot: RevolveBot) -> None:
def _load_genotype_only_body(self, revolvebot: RevolveBot) -> None:
self.id = revolvebot.id
self.representation = revolvebot._body
self.representation = revolvebot._body

def _load_genotype_only_brain(self, revolvebot: RevolveBot) -> None:
# load brain params into the modules
brain = revolvebot._brain
assert isinstance(brain, BrainNN)

assert isinstance(brain, BrainCPPNCPG)

module_map = {}
for module in revolvebot.iter_all_elements():
Expand All @@ -80,7 +85,9 @@ def _load_genotype_from_revolvebot(self, revolvebot: RevolveBot) -> None:
for module in revolvebot.iter_all_elements():
assert module_map[module.id] == module

return
def _load_genotype_from_revolvebot(self, revolvebot: RevolveBot) -> None:
self._load_genotype_only_body(revolvebot=revolvebot)
self._load_genotype_only_brain(revolvebot=revolvebot)

def export_genotype(self, filepath: str) -> None:
self.develop()
Expand All @@ -94,7 +101,7 @@ def _export_genotype_open_file(self, open_file: TextIO) -> None:
def develop(self) -> RevolveBot:
if self.phenotype is None:
self.phenotype: RevolveBot = RevolveBot(self.id)
self.phenotype._body: CoreModule = self.representation
self.phenotype._body = self.representation
self.phenotype._brain = self._develop_brain(self.representation)
return self.phenotype

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def load_genotype(self, file_path: str) -> None:
# remove first element - it's the number of brain genomes
number_of_brain_genomes = int(lines.pop(0))
# read the body genome
self._body_genome._load_genotype_from_lines(lines[:-number_of_brain_genomes])
self._body_genome._load_genotype_from_lines(lines[:-number_of_brain_genomes], only_body=True)
# read the brain genomes
for brain_i in range(number_of_brain_genomes):
i = -number_of_brain_genomes + brain_i
Expand Down
Loading