def simulate_learning(neurons, receptors, nrns_sd, rctrs_sd, arena): """ Run simulation for 40 seconds based on network topology encoded in individual. @param individual: Binary string encoding network topology. @param arena: The arena object in which the simulation is carried out. @param n_step: Number of steps the simulation is divided in. @param t_step: Number of milliseconds in each step. @param reset: bool triggering the reset of the nest kernel. """ global simtime simdata = create_empty_data_lists() if data['init'] == 'random': simdata['x_init'], simdata['y_init'], theta_init = \ select_random_pose(arena) x_cur = simdata['x_init'] y_cur = simdata['y_init'] theta_cur = theta_init err_l, err_r = 0, 0 simdata['rctr_nrn_trace'].append(np.zeros((18, 10))) simdata['nrn_nrn_trace'].append(np.zeros((10, 10))) motor_spks = nrns_sd[6:] for t in range(n_step): ev.update_refratory_perioud() simdata['traj'].append((x_cur, y_cur)) px = network.set_receptors_firing_rate(x_cur, y_cur, theta_cur, err_l, err_r) simdata['pixel_values'].append(px) # Run simulation for time-step nest.Simulate(t_step) simtime += t_step motor_firing_rates = network.get_motor_neurons_firing_rates(motor_spks) v_l_act, v_r_act, v_t, w_t, err_l, err_r, col, v_l_des, v_r_des = \ motion.update_wheel_speeds(x_cur, y_cur, theta_cur, motor_firing_rates) # Save dsired and actual speeds simdata['speed_log'].append((v_l_act, v_r_act)) simdata['desired_speed_log'].append((v_l_des, v_r_des)) # Save motor neurons firing rates simdata['motor_fr'].append(motor_firing_rates) # Save linear and angualr velocities simdata['linear_velocity_log'].append(v_t) simdata['angular_velocity_log'].append(w_t) # Move robot according to the read-out speeds from motor neurons x_cur, y_cur, theta_cur = motion.move(x_cur, y_cur, theta_cur, v_t, w_t) nrns_st, rctrs_st = learning.get_spike_times(nrns_sd, rctrs_sd) rec_nrn_tags, nrn_nrn_tags, rctr_t, nrn_t, pt = \ learning.get_eligibility_trace(nrns_st, rctrs_st, simtime, simdata['rctr_nrn_trace'][t], simdata['nrn_nrn_trace'][t]) print simtime simdata['rctr_nrn_trace'].append(rec_nrn_tags) simdata['nrn_nrn_trace'].append(nrn_nrn_tags) # Stop simulation if collision occurs if col: simdata['speed_log'].extend((0, 0) for k in range(t, 400)) break simdata['fitness'] = ev.get_fitness_value(simdata['speed_log']) return simdata, rctr_t, nrn_t, pt, col
# Save linear and angualr velocities simdata['linear_velocity_log'].append(v_t) simdata['angular_velocity_log'].append(w_t) # Move robot according to the read-out speeds from motor neurons x_cur, y_cur, theta_cur = env.move(x_cur, y_cur, theta_cur, v_t, w_t) nrns_st, rctrs_st = learning.get_spike_times(nrns_sd, rctrs_sd) rec_nrn_tags, nrn_nrn_tags, rctr_t, nrn_t, pt = learning.get_eligibility_trace( nrns_st, rctrs_st, simtime, simdata['rctr_nrn_trace'][t], simdata['nrn_nrn_trace'][t]) simdata['rctr_nrn_trace'].append(rec_nrn_tags) simdata['nrn_nrn_trace'].append(nrn_nrn_tags) if (t+1) % 10 == 0: fitness = ev.get_fitness_value(simdata['speed_log'][t-10:]) print "Fitness" print fitness print "Reward" reward = learning.get_reward(reward, fitness, x_cur, y_cur) print reward simdata['reward'].append(reward) delta_w_rec, delta_w_nrn = learning.get_weight_changes(reward, rec_nrn_tags, nrn_nrn_tags) learning.update_weights(delta_w_rec, delta_w_nrn, nrns, rctrs) weights.append(learning.get_weights(rctrs, nrns)) # Stop simulation if collision occurs if col: # simdata['speed_log'].extend((0, 0) for k in range(t, 400)) # break print "COLLISION"
def simulate_evolution(individual, arena, n_step, t_step, reset=True): """ Run simulation for 40 seconds based on network topology encoded in individual. @param individual: Binary string encoding network topology. @param arena: The arena object in which the simulation is carried out. @param n_step: Number of steps the simulation is divided in. @param t_step: Number of milliseconds in each step. @param reset: bool triggering the reset of the nest kernel. """ # Reset nest kernel just in case it was not reset & adjust # resolution. nest.ResetKernel() nest.SetKernelStatus({'resolution': 1.0, 'local_num_threads': 4}) nest.set_verbosity('M_ERROR') if data['init'] == 'random': x, y, theta = select_random_pose(arena) simdata = create_empty_data_lists() simdata['x_init'] = x_init simdata['y_init'] = y_init simdata['theta_init'] = theta_init motor_spks = network.create_evolutionary_network(individual, data['model']) x_cur = x_init y_cur = y_init theta_cur = theta_init err_l, err_r = 0, 0 for t in range(n_step): # Save current position in trajectory list simdata['traj'].append((x_cur, y_cur)) # Add nioise to network.update_refratory_perioud(data['model']) # Set receptors' firing probability px = network.set_receptors_firing_rate(x_cur, y_cur, theta_cur, err_l, err_r, arena) simdata['pixel_values'].append(px) # Run simulation for 100 ms nest.Simulate(t_step) motor_firing_rates = network.get_motor_neurons_firing_rates(motor_spks) # Get desired and actual speeds col, speed_dict = motion.update_wheel_speeds(x_cur, y_cur, theta_cur, motor_firing_rates, t_step, arena) # Stop simulation if collision occurs if col: simdata['speed_log'].extend((0, 0) for k in range(t, 400)) break # Save simulation data simdata['speed_log'].append((speed_dict['actual_left'], speed_dict['actual_right'])) simdata['desired_speed_log'].append((speed_dict['desired_left'], speed_dict['desired_right'])) simdata['motor_fr'].append(motor_firing_rates) simdata['linear_velocity_log'].append(speed_dict['linear']) simdata['angular_velocity_log'].append(speed_dict['angular']) # Move robot according to the read-out speeds from motor neurons x_cur, y_cur, theta_cur = motion.move(x_cur, y_cur, theta_cur, speed_dict['linear'], speed_dict['angular'], t_step/1000.) # Calculate individual's fitness value simdata['fitness'] = ev.get_fitness_value(simdata['speed_log']) # Get average number of spikes per second for each neuron simdata['average_spikes'] = network.get_average_spikes_per_second() # Get Voltmeter data simdata['voltmeter_data'] = network.get_voltmeter_data() if reset: nest.ResetKernel() return simdata