def evaluate_individual(self, genome): """ Evaluate an individual solution. Args: genome: genome of the individual to evaluate Returns: fitness value of the individual """ global man, current_network, quadruped # Initialize the manager to be unique to the process. man = ODEManager(near_callback, stepsize=self.dt / self.n, log_data=self.log_frames) # Initialize the quadruped quadruped = Quadruped(man=man) # Load in the ANN from the population self.current_network = NEAT.NeuralNetwork() if not self.hyperNEAT: genome.BuildPhenotype(self.current_network) else: genome.BuildHyperNEATPhenotype(self.current_network, self.substrate) # Conduct the evaluation fit = self.physics_only_simulation() #print(genome.GetID(), fit) return fit, len(self.current_network.neurons), len( self.current_network.connections)
def evaluate_individual(individual): """ Wrapper to call Simulation which will evaluate an individual. Args: individual: arguments to pass to the simulation Returns: fitness of an individual """ log_frames = GlobalVarWorkaround.args.log_frames run_num = GlobalVarWorkaround.args.run_num eval_time = GlobalVarWorkaround.args.eval_time dt = .02 n = 4 GlobalVarWorkaround.man = ODEManager(my_worm_simulation.near_callback, stepsize=dt / n, log_data=log_frames, fluid_dynamics=1, gravity=0) GlobalVarWorkaround.worm = my_worm_simulation.Worm( man=GlobalVarWorkaround.man, morphology_genome=individual[1], num_joints=7, logging=log_frames) simulation = my_worm_simulation.Simulation(eval_time, dt=dt, n=n, man=GlobalVarWorkaround.man, worm=GlobalVarWorkaround.worm) return simulation.evaluate_individual(individual)
def validator(self, NEAT_file): """ Validate a single run. Args: NEAT_File: file for the NEAT genome """ global man, quadruped # Initialize the manager to be unique to the process. man = ODEManager(near_callback, stepsize=self.dt / self.n, log_data=self.log_frames, run_num=self.run_num) # Initialize the quadruped quadruped = Quadruped(man=man) # If logging the output, tell manager to write the body type, dimensions, and position to the logging file. if self.log_frames: man.log_world_setup(self.eval_time, ind_num=self.run_num) # Load in the best performing NEAT genome genome = NEAT.Genome(NEAT_file) self.current_network = NEAT.NeuralNetwork() if not self.hyperNEAT: genome.BuildPhenotype(self.current_network) else: genome.BuildHyperNEATPhenotype(self.current_network, self.substrate) fit = self.physics_only_simulation_validator() print(fit)
def debug_validator(self): """ Validate a single run. """ global man, worm if self.aquatic: grav = 0 fluid_dyn = 1 else: grav = -9.81 fluid_dyn = 0 # Initialize the manager to be unique to the process. man = ODEManager(near_callback, stepsize=self.dt / self.n, log_data=self.log_frames, run_num=self.run_num, gravity=grav, fluid_dynamics=fluid_dyn) # Initialize the worm worm = Worm(man=man, num_joints=self._num_joints) # If logging the output, tell manager to write the body type, dimensions, and position to the logging file. if self.log_frames: man.log_world_setup(self.eval_time, ind_num=self.run_num) fit = self.physics_only_simulation_validator() print(fit)
def __init__(self, log_frames=0, run_num=0, eval_time=10., dt=.02, n=4, con_type="single",hyperNEAT=False,substrate=False,periodic=True,sym_mus_mods=False): """ Initialize the simulation class. """ global simulate man = ODEManager(near_callback, stepsize=dt/n, log_data=log_frames, run_num=run_num) # Settings for the simulation. self.log_frames = log_frames self.run_num = run_num self.eval_time = eval_time self.elapsed_time = 0. self.dt = dt # Timestep for simulation. self.n = n # How many timesteps to simulate per callback. # Quadruped Specific Setup quadruped = Quadruped(man=man) self.current_network = 0 self.cur_mus_net = 0 self.hyperNEAT = True if hyperNEAT else False self.substrate = substrate self.ann_mus_mod_con_type = con_type # Whether we include a periodic oscillating input signal. self.periodic = periodic # Whether the Muscle Models are symmetric (Same mus groups just mirrored left/right or not.) self.sym_mus_mods = sym_mus_mods
def validator(self): """ Validate a single run. Args: NEAT_File: file for the NEAT genome """ global man, test_setup # Initialize the manager to be unique to the process. man = ODEManager(near_callback, gravity=0, stepsize=self.dt / self.n, log_data=self.log_frames, run_num=self.run_num) # Initialize the quadruped test_setup = Test_Setup(man=man) # If logging the output, tell manager to write the body type, dimensions, and position to the logging file. if self.log_frames: man.log_world_setup(self.eval_time, ind_num=self.run_num) fit = self.physics_only_simulation_validator() print(fit)
def __init__(self, log_frames=0, run_num=0, eval_time=10., dt=.02, n=4, con_type="single", hyperNEAT=False, substrate=False, periodic=True, num_joints=1, aquatic=False): """ Initialize the simulation class. """ global simulate # Whether or not we simulate in the aquatic environment or not. self.aquatic = aquatic if self.aquatic: grav = 0 fluid_dyn = 1 else: grav = -9.81 fluid_dyn = 0 man = ODEManager(near_callback, stepsize=dt / n, log_data=log_frames, run_num=run_num, gravity=grav, fluid_dynamics=fluid_dyn) # Settings for the simulation. self.log_frames = log_frames self.run_num = run_num self.eval_time = eval_time self.elapsed_time = 0. self.dt = dt # Timestep for simulation. self.n = n # How many timesteps to simulate per callback. self._num_joints = num_joints # Worm Specific Setup worm = "" self.current_network = 0 self.hyperNEAT = True if hyperNEAT else False self.substrate = substrate # Whether we include a periodic oscillating input signal. self.periodic = periodic # Check for explosions self.exploded = False
def eval_ribbot(individual): """Evaluate Ribbot for distance traveled. """ log_frames = False dt = .02 n = 4 run_num = GlobalVarWorkaround.args.run_num eval_time = GlobalVarWorkaround.args.eval_time num_joints = GlobalVarWorkaround.args.num_joints AMP, FRQ, PHS = 0, 1, 2 # Extract genes amps = individual[AMP] #individual[AMP:AMP + NUM_JOINTS] frqs = individual[FRQ] #individual[FRQ:FRQ + NUM_JOINTS] phss = individual[PHS] #individual[PHS:] bias = 0 #[0] * NUM_JOINTS GlobalVarWorkaround.man = ODEManager(simulation.near_callback, stepsize=dt / n, log_data=log_frames, fluid_dynamics=1, gravity=0, run_num=run_num) GlobalVarWorkaround.worm = simulation.Worm(man=GlobalVarWorkaround.man, num_joints=num_joints, logging=log_frames) print '--amps:' + str(amps) + ' frqs:' + str(frqs) + ' phss:' + str(phss) jointctrlers = [ simulation.JointActuationController(amplitude=amps, frequency=frqs, phaseshift=phss, offset=bias) for i in range(num_joints - 8) ] simulationobj = simulation.Simulation(eval_time, dt=dt, n=n, man=GlobalVarWorkaround.man, worm=GlobalVarWorkaround.worm, jointctrlers=jointctrlers) fit = simulationobj.evaluate_individual() print '--fit=' + str(fit) fitness = fit[0] # reward only distance traveled in positive x direction return fitness,
def validator(NEAT_file, Morph_file, log_frames, run_num, eval_time, dt, n, num_joints, output_path, gennumber, eucquad): genome = [0, {}] # Load in the best performing NEAT genome genome[0] = NEAT.Genome(NEAT_file) # Load the morphology with open(Morph_file, "r") as f: line = f.readline() line = line.strip().split(',') genome[1][0] = float(line[0]) # read frequency GlobalVarWorkaround.man = ODEManager(near_callback, stepsize=dt / n, log_data=log_frames, fluid_dynamics=1, gravity=0, run_num=run_num) GlobalVarWorkaround.worm = Worm(man=GlobalVarWorkaround.man, morphology_genome=genome[1], num_joints=num_joints, logging=log_frames, log_path=output_path, logfileprefix='rep' + str(run_num) + '_gen' + str(gennumber)) GlobalVarWorkaround.foodenv = FoodEnvironment(man=GlobalVarWorkaround.man, square_size=40, eucquadrant=eucquad) # If logging the output, tell manager to write the body type, dimensions, and position to the logging file. # Must be placed after creating the quadruped. if log_frames: GlobalVarWorkaround.man.log_world_setup(eval_time, ind_num=run_num) simulation = Simulation(eval_time, dt=dt, n=n, man=GlobalVarWorkaround.man, worm=GlobalVarWorkaround.worm) #simulation.current_network = NEAT.NeuralNetwork() #genome[0].BuildPhenotype(simulation.current_network) fit = simulation.evaluate_individual(genome) print(fit)
def debug_validator(self): """ Validate a single run. """ global man, quadruped # Initialize the manager to be unique to the process. man = ODEManager(near_callback, stepsize=self.dt/self.n, log_data=self.log_frames, run_num=self.run_num) # Initialize the quadruped quadruped = Quadruped(man=man) # If logging the output, tell manager to write the body type, dimensions, and position to the logging file. if self.log_frames: man.log_world_setup(self.eval_time,ind_num=self.run_num) fit = self.physics_only_simulation_validator() print(fit)
def __init__(self, log_frames=0, run_num=0, eval_time=10., dt=.002, n=4): """ Initialize the simulation class. """ global simulate man = ODEManager(near_callback, stepsize=dt / n, log_data=log_frames, run_num=run_num) # Settings for the simulation. self.log_frames = log_frames self.run_num = run_num self.eval_time = eval_time self.elapsed_time = 0. self.dt = dt # Timestep for simulation. self.n = n # How many timesteps to simulate per callback. # Quadruped Specific Setup test_setup = Test_Setup(man=man)
def validator(self,NEAT_file,Morph_file): """ Validate a single run. Args: NEAT_File: file for the NEAT genome Morph_file: file for the morphology components """ global man, quadruped self.validating = True # Initialize the manager to be unique to the process. man = ODEManager(near_callback, stepsize=self.dt/self.n, log_data=self.log_frames, run_num=self.run_num,erp=0.5,cfm=1E-2) genome = [0,{}] if NEAT_file: # Load in the best performing NEAT genome genome[0] = NEAT.Genome(NEAT_file) self.current_network = NEAT.NeuralNetwork() if not self.hyperNEAT: genome[0].BuildPhenotype(self.current_network) else: genome[0].BuildHyperNEATPhenotype(self.current_network,self.substrate) if Morph_file: with open(Morph_file,"r") as f: line = f.readline() line = line.strip().split(',') genome[1]['erp'] = float(line[0]) genome[1]['cfm'] = float(line[1]) # Initialize the quadruped quadruped = Quadruped(man=man,morphology_genome={'erp':genome[1]['erp'],'cfm':genome[1]['cfm']}) # If logging the output, tell manager to write the body type, dimensions, and position to the logging file. # Must be placed after creating the quadruped. if self.log_frames: man.log_world_setup(self.eval_time,ind_num=self.run_num) fit = self.physics_only_simulation_validator() return self.ann_activations,self.joint_feedback
def evaluate_individual(self, genome): """ Evaluate an individual solution. Args: genome: genome of the individual to evaluate Returns: fitness value of the individual """ global man, current_network, worm if self.aquatic: grav = 0 fluid_dyn = 1 else: grav = -9.81 fluid_dyn = 0 # Initialize the manager to be unique to the process. man = ODEManager(near_callback, stepsize=self.dt / self.n, log_data=self.log_frames, gravity=grav, fluid_dynamics=fluid_dyn) # Initialize the worm worm = Worm(man=man, num_joints=self._num_joints) # Load in the ANN from the population self.current_network = NEAT.NeuralNetwork() if not self.hyperNEAT: genome.BuildPhenotype(self.current_network) else: genome.BuildHyperNEATPhenotype(self.current_network, self.substrate) # Conduct the evaluation fit = self.physics_only_simulation() return fit
def __init__(self, log_frames=0, run_num=0, eval_time=10., dt=.02, n=4, con_type="single", hyperNEAT=False, substrate=False, periodic=True): """ Initialize the simulation class. """ global simulate man = ODEManager(near_callback, stepsize=dt / n, log_data=log_frames, run_num=run_num) # Settings for the simulation. self.log_frames = log_frames self.run_num = run_num self.eval_time = eval_time self.elapsed_time = 0. self.dt = dt # Timestep for simulation. self.n = n # How many timesteps to simulate per callback. # Quadruped Specific Setup quadruped = Quadruped(man=man) self.current_network = 0 self.hyperNEAT = True if hyperNEAT else False self.substrate = substrate # Whether we include a periodic oscillating input signal. self.periodic = periodic
default=10., help="Apply force till a certain time") args = parser.parse_args() print args log_frames = args.log_frames eval_time = args.eval_time run_num = args.run_num dt = .02 n = 4 forcetill = args.applyforcetill_time GlobalVarWorkaround.man = ODEManager(box_simu.near_callback, stepsize=dt / n, log_data=log_frames, fluid_dynamics=1, gravity=0, run_num=run_num) box = box_simu.Box(man=GlobalVarWorkaround.man, desnsity=50., base_pos=[.0, 1., .0]) # If logging the output, tell manager to write the body type, dimensions, and position to the logging file. # Must be placed after creating the quadruped. if log_frames: GlobalVarWorkaround.man.log_world_setup(eval_time, ind_num=run_num) simulation = box_simu.Simulation(eval_time, dt=dt, n=n,
print args log_frames = args.log_frames eval_time = args.eval_time run_num = args.run_num output_path = args.output_path dt = .02 n = 4 numofjoints = args.numofjoints length = args.length GlobalVarWorkaround.man = ODEManager( ribosomalrobot_ctrl_simu_stop.near_callback, stepsize=dt / n, log_data=log_frames, fluid_dynamics=1, gravity=0, run_num=run_num) density = args.rdensity GlobalVarWorkaround.worm = ribosomalrobot_ctrl_simu_stop.Worm( man=GlobalVarWorkaround.man, num_joints=numofjoints, morphology={ 'density': density, 'xlength': length }, logging=log_frames, log_path=output_path, logfileprefix='rep' + str(run_num))