def fitnessFunction(genotype): nn = ffann.ANN(nI,nH1,nH2,nO) nn.setParameters(genotype,WeightRange,BiasRange) # Task 1 body = invpend.InvPendulum() fit = 0.0 for theta in theta_range_IP: for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot for t in time_IP: nn.step(np.concatenate((body.state(),np.zeros(4),np.zeros(3)))) #arrays for inputs for each task f = body.step(stepsize_IP, np.array([nn.output()[0]])) if f < 0: f = 0 fit += f fitness1 = fit/(duration_IP*total_trials_IP) fitness1 = ((fitness1+7.65)/7) # Normalize to run between 0 and 1 #print('fitness1',fitness1) # Task 2 body = cartpole.Cartpole() fit = 0.0 for theta in theta_range_CP: for theta_dot in thetadot_range_CP: for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot for t in time_CP: nn.step(np.concatenate((np.zeros(3),body.state(),np.zeros(3)))) f = body.step(stepsize_CP, np.array([nn.output()[1]])) fit += f fitness2 = fit/(duration_CP*total_trials_CP) #print('fitness2',fitness2) #Task 3 body = leggedwalker.LeggedAgent(0.0,0.0) fit = 0.0 for theta in theta_range_LW: for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega for t in time_LW: nn.step(np.concatenate((np.zeros(3),np.zeros(4),body.state()))) body.step(stepsize_LW, np.array(nn.output()[2:5])) fit += body.cx/duration_LW if fit < 0: fit = 0 fitness3 = ((fit/total_trials_LW)/MaxFit) #print('fitness3',fitness3) #print('overallfitness',fitness1*fitness2*fitness3) return fitness1*fitness2*fitness3
def fitnessFunction(genotype): nn = ffann.ANN(nI,nH1,nH2,nO) nn.setParameters(genotype,WeightRange,BiasRange) # Task 1 body = invpend.InvPendulum() fit = 0.0 for theta in theta_range_IP: for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot for t in time_IP: nn.step(np.concatenate((body.state(),np.zeros(4),np.zeros(3)))) #arrays for inputs for each task f = body.step(stepsize_IP, np.concatenate(((nn.output() + np.random.normal(0.0,noisestd)),np.zeros(2)))) fit += f fitness1 = fit/(duration_IP*total_trials_IP) fitness1 = (fitness1+7.65)/7 # Normalize to run between 0 and 1 #save data from fitnesses of tasks #np.save('Fitness_invpend',fitness1) # Task 2 body = cartpole.Cartpole() fit = 0.0 for theta in theta_range_CP: for theta_dot in thetadot_range_CP: for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot for t in time_CP: nn.step(np.concatenate((np.zeros(3),body.state(),np.zeros(3)))) f = body.step(stepsize_IP, nn.output() + np.random.normal(0.0,noisestd)) #f = body.step(stepsize_IP, nn.output()) f = body.step(stepsize_CP, np.concatenate(((nn.output() + np.random.normal(0.0,noisestd)),np.zeros(2)))) fit += f fitness2 = fit/(duration_CP*total_trials_CP) #np.save('Fitness_cartpole',fitness2) #Task 3 body = leggedwalker.LeggedAgent(0.0,0.0) fit = 0.0 for theta in theta_range_LW: for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega for t in time_LW: nn.step(np.concatenate((np.zeros(3),np.zeros(4),body.state()))) body.step(stepsize_LW, nn.output() + np.random.normal(0.0,noisestd)) fit += body.cx/duration_LW fitness3 = (fit/total_trials_LW)/MaxFit #np.save('Fitness_legged',fitness3) return fitness1*fitness2*fitness3
def fitnessFunction(genotype): nn = ffann.ANN(nI, nH1, nH2, nO) nn.setParameters(genotype, WeightRange, BiasRange) body = leggedwalker.LeggedAgent(0.0, 0.0) fit = 0.0 for theta in theta_range: for omega in omega_range: body.reset() body.angle = theta body.omega = omega for t in time: nn.step(body.state()) body.step(stepsize, nn.output() + np.random.normal(0.0, noisestd)) fit += body.cx / duration return (fit / total_trials) / MaxFit
def fitnessFunction(genotype): nn = ffann.ANN(nI,nH1,nH2,nO) nn.setParameters(genotype,WeightRange,BiasRange) body = mountaincar_cont.MountainCarAgent(0.0) fit = 0.0 for theta in theta_range: for velocity in velocity_range: for position in position_range: body.theta = theta body.position = position body.velocity = velocity for t in time: nn.step(body.state()) f = body.step(stepsize,nn.output()+np.random.normal(0.0,noisestd)) fit += f fitness = fit/(duration*total_trials) return fitness
def plot(g): nn = ffann.ANN(nI,nH1,nH2,nO) nn.setParameters(genotype,WeightRange,BiasRange) body = mountaincar_cont.MountainCarAgent(0.0) out_hist = np.zeros((DurationSteps,nO)) velocity = np.zeros(DurationSteps) time = np.arange(0,duration,stepsize) #t = 0.0 for i in range(DurationSteps): nn.step(body.state()) body.step(stepsize, nn.output() + np.random.normal(0.0,noisestd)) out_hist[i] = nn.output() velocity[i] = body.vx plt.plot(time, velocity) plt.xlabel('Time') plt.ylabel('Outputs') plt.axis([0, duration, -0.1, 1.1]) plt.show() plt.plot(time, out_hist) plt.xlabel('Time') plt.ylabel('Outputs') plt.show()
def performance_analysis(genotype): # Common setup nn = ffann.ANN(nI,nH1,nH2,nO) nn.setParameters(genotype,WeightRange,BiasRange) Hidden1_Avg = np.zeros((3,nH1)) Hidden2_Avg = np.zeros((3,nH2)) # Task 1 body = invpend.InvPendulum() fit_cum = 0.0 fip = np.zeros(x,y) total_steps = len(time_IP)*total_trials_IP i = 0 for theta in theta_range_IP: j = 0 for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot fit=0.0 for t in time_IP: nn.step(np.concatenate((body.state(),np.zeros(4),np.zeros(3)))) #arrays for inputs for each task Hidden1_Avg[0] += nn.Hidden1Activation/total_steps Hidden2_Avg[0] += nn.Hidden2Activation/total_steps f = body.step(stepsize_IP, np.array([nn.output()[0]])) fit += f fip[i][j] = fit/duration_IP fit_cum += fit/duration_IP j += 1 i += 1 fitness1 = fit/total_trials_IP fitness1 = (fitness1+7.65)/7 # Normalize to run between 0 and 1 np.save('ip_hidden_1', Hidden1_Avg[0]) np.save('ip_hidden_2', Hidden2_Avg[0]) # Task 2 #for behaviors: make i and j: theta and theta_dot #x and xdot == make those values the average of all visuzliations body = cartpole.Cartpole() fit = 0.0 total_steps = len(time_CP)*total_trials_CP for theta in theta_range_CP: for theta_dot in thetadot_range_CP: for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot for t in time_CP: nn.step(np.concatenate((np.zeros(3),body.state(),np.zeros(3)))) Hidden1_Avg[1] += nn.Hidden1Activation/total_steps Hidden2_Avg[1] += nn.Hidden2Activation/total_steps f = body.step(stepsize_CP, np.array([nn.output()[1]])) fit += f fitness2 = fit/(duration_CP*total_trials_CP) np.save('cp_hidden_1', Hidden1_Avg[1]) np.save('cp_hidden_2', Hidden2_Avg[1]) #Task 3 body = leggedwalker.LeggedAgent(0.0,0.0) fit = 0.0 total_steps = len(time_LW)*total_trials_LW for theta in theta_range_LW: for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega for t in time_LW: nn.step(np.concatenate((np.zeros(3),np.zeros(4),body.state()))) Hidden1_Avg[2] += nn.Hidden1Activation/total_steps Hidden2_Avg[2] += nn.Hidden2Activation/total_steps body.step(stepsize_LW, np.array(nn.output()[2:5])) fit += body.cx/duration_LW fitness3 = (fit/total_trials_LW)/MaxFit np.save('lw_hidden_1', Hidden1_Avg[2]) np.save('lw_hidden_2', Hidden2_Avg[2]) return fitness1,fitness2,fitness3,Hidden1_Avg,Hidden2_Avg,fip
def analysis(genotype): # Common setup nn = ffann.ANN(nI,nH1,nH2,nO) nn.setParameters(genotype,WeightRange,BiasRange) fitness = np.zeros(3) # Task 1 body = invpend.InvPendulum() nn_state_ip = np.zeros((total_trials_IP*len(time_IP),nI+nH1+nH2+nO)) total_steps = len(theta_range_IP) * len(thetadot_range_IP) * len(time_IP) fit_IP = np.zeros((len(theta_range_IP),len(thetadot_range_IP))) i=0 k=0 for theta in theta_range_IP: j=0 for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot f = 0.0 for t in time_IP: nn.step(np.concatenate((body.state(),np.zeros(4),np.zeros(3)))) #arrays for inputs for each task nn_state_ip[k] = nn.states() k += 1 f += body.step(stepsize_IP, np.array([nn.output()[0]])) fit_IP[i][j] = ((f/duration_IP)+7.65)/7 j += 1 i += 1 fitness[0] = np.mean(fit_IP) # Task 2 body = cartpole.Cartpole() nn_state_cp = np.zeros((total_trials_CP*len(time_CP),nI+nH1+nH2+nO)) total_steps = len(theta_range_CP) * len(thetadot_range_CP) * len(x_range_CP) * len(xdot_range_CP) * len(time_CP) fit_CP = np.zeros((len(theta_range_CP),len(thetadot_range_CP))) i = 0 k = 0 for theta in theta_range_CP: j = 0 for theta_dot in thetadot_range_CP: f_cumulative = 0 for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot f = 0.0 for t in time_CP: nn.step(np.concatenate((np.zeros(3),body.state(),np.zeros(3)))) nn_state_cp[k] = nn.states() k += 1 f_temp,d = body.step(stepsize_CP, np.array([nn.output()[1]])) f += f_temp f_cumulative += f/duration_CP fit_CP[i][j] = f_cumulative/(len(x_range_CP)*len(xdot_range_CP)) j += 1 i += 1 fitness[1] = np.mean(fit_CP) #Task 3 body = leggedwalker.LeggedAgent(0.0,0.0) nn_state_lw = np.zeros((total_trials_LW*len(time_LW),nI+nH1+nH2+nO)) total_steps = len(theta_range_LW) * len(omega_range_LW) * len(time_LW) fit_LW = np.zeros((len(theta_range_LW),len(omega_range_LW))) i = 0 k = 0 for theta in theta_range_LW: j = 0 for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega for t in time_LW: nn.step(np.concatenate((np.zeros(3),np.zeros(4),body.state()))) nn_state_lw[k] = nn.states() k += 1 body.step(stepsize_LW, np.array(nn.output()[2:5])) fit_LW[i][j] = (body.cx/duration_LW)/MaxFit j += 1 i += 1 fitness[2] = np.mean(fit_LW) return fitness,fit_IP,fit_CP,fit_LW,nn_state_ip,nn_state_cp,nn_state_lw
def lesions(genotype,actvalues): nn = ffann.ANN(nI,nH1,nH2,nO) # Task 1 ip_fit = np.zeros(nH1+nH2) body = invpend.InvPendulum() nn.setParameters(genotype,WeightRange,BiasRange) index = 0 for layer in [1,2]: for neuron in range(nH1): if layer == 1: n = neuron else: n = nH1 + neuron print("IP:",n) maxfit = 0.0 for act in actvalues[:,0,n]: fit = 0.0 for theta in theta_range_IP: for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot for t in time_IP: nn.step_lesioned(np.concatenate((body.state(),np.zeros(4),np.zeros(3))),neuron,layer,act) f = body.step(stepsize_IP, np.array([nn.output()[0]])) fit += f fit = fit/(duration_IP*total_trials_IP) fit = (fit+7.65)/7 if fit < 0.0: fit = 0.0 if fit < maxfit: maxfit = fit ip_fit[index]=fit index += 1 # Task 2 cp_fit = np.zeros(nH1+nH2) body = cartpole.Cartpole() nn.setParameters(genotype,WeightRange,BiasRange) index = 0 for layer in [1,2]: for neuron in range(nH1): if layer == 1: n = neuron else: n = nH1 + neuron print("CP:",n) maxfit = 0.0 for act in actvalues[:,1,n]: fit = 0.0 for theta in theta_range_CP: for theta_dot in thetadot_range_CP: for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot for t in time_CP: nn.step_lesioned(np.concatenate((np.zeros(3),body.state(),np.zeros(3))),neuron,layer,act) f,d = body.step(stepsize_CP, np.array([nn.output()[1]])) fit += f fit = fit/(duration_CP*total_trials_CP) if fit < 0.0: fit = 0.0 if fit < maxfit: maxfit = fit cp_fit[index]=fit index += 1 #Task 3 lw_fit = np.zeros(nH1+nH2) body = leggedwalker.LeggedAgent(0.0,0.0) nn.setParameters(genotype,WeightRange,BiasRange) index = 0 for layer in [1,2]: for neuron in range(nH1): if layer == 1: n = neuron else: n = nH1 + neuron print("LW:",n) maxfit = 0.0 for act in actvalues[:,2,n]: fit = 0.0 for theta in theta_range_LW: for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega for t in time_LW: nn.step_lesioned(np.concatenate((np.zeros(3),np.zeros(4),body.state())),neuron,layer,act) body.step(stepsize_LW, np.array(nn.output()[2:5])) fit += body.cx/duration_LW fit = (fit/total_trials_LW)/MaxFit if fit < 0.0: fit = 0.0 if fit < maxfit: maxfit = fit lw_fit[index]=fit index += 1 return ip_fit,cp_fit,lw_fit
def performance_analysis(genotype): # Common setup nn = ffann.ANN(nI, nH1, nH2, nO) nn.setParameters(genotype, WeightRange, BiasRange) Hidden1_Avg = np.zeros((3, nH1)) Hidden2_Avg = np.zeros((3, nH2)) # Task 1 body = invpend.InvPendulum() fit_cum = 0.0 s = (6, 6) fip = np.zeros(s) #behavior matrix total_steps = len(time_IP) * total_trials_IP i = 0 for theta in theta_range_IP: j = 0 for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot fit = 0.0 for t in time_IP: nn.step( np.concatenate( (body.state(), np.zeros(4), np.zeros(3)))) #arrays for inputs for each task Hidden1_Avg[0] += nn.Hidden1Activation / total_steps Hidden2_Avg[0] += nn.Hidden2Activation / total_steps f = body.step(stepsize_IP, np.array([nn.output()[0]])) fit += f fit_cum += f fit_cum = fit_cum / duration_IP fip[i][j] = fit / duration_IP j += 1 i += 1 fitness1 = fit_cum / (duration_IP * total_trials_IP) fitness1 = (fitness1 + 7.65) / 7 # Normalize to run between 0 and 1 #print('ip hidden1',Hidden1_Avg[0]) #print('ip hidden2',Hidden2_Avg[0]) #print(fip) fig, ax = plt.subplots() ax.matshow(fip, cmap=plt.cm.Blues) fip = np.around(fip, decimals=2) for o in range(i): for n in range(j): c = fip[n, o] ax.text(o, n, str(c), va='center', ha='center') plt.title('Theta VS theta_dot Behavior: IP', fontsize=13) plt.show() # Task 2 body = cartpole.Cartpole() fit_cum = 0.0 s = (2, 2) fip = np.zeros(s) total_steps = len(time_CP) * total_trials_CP i = 0 for theta in theta_range_CP: j = 0 for theta_dot in thetadot_range_CP: for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot fit = 0.0 for t in time_CP: nn.step( np.concatenate( (np.zeros(3), body.state(), np.zeros(3)))) Hidden1_Avg[1] += nn.Hidden1Activation / total_steps Hidden2_Avg[1] += nn.Hidden2Activation / total_steps f = body.step(stepsize_CP, np.array([nn.output()[1]])) fit_cum += f fit += f #fit_cum = fit_cum/duration_CP fip[i][j] = fit / duration_CP j += 1 i += 1 fitness2 = fit_cum / (duration_CP * total_trials_CP) fig, ax = plt.subplots() ax.matshow(fip, cmap=plt.cm.Blues) fip = np.around(fip, decimals=2) for o in range(i): for n in range(j): c = fip[n, o] ax.text(o, n, str(c), va='center', ha='center') plt.title('Theta VS theta_dot Behavior: CP', fontsize=13) plt.show() #Task 3 body = leggedwalker.LeggedAgent(0.0, 0.0) fit_cum = 0.0 s = (3, 3) fip = np.zeros(s) total_steps = len(time_LW) * total_trials_LW i = 0 for theta in theta_range_LW: j = 0 for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega fit = 0.0 for t in time_LW: nn.step( np.concatenate((np.zeros(3), np.zeros(4), body.state()))) Hidden1_Avg[2] += nn.Hidden1Activation / total_steps Hidden2_Avg[2] += nn.Hidden2Activation / total_steps body.step(stepsize_LW, np.array(nn.output()[2:5])) fit_cum += body.cx / duration_LW fip[i][j] += body.cx / duration_LW j += 1 i += 1 fitness3 = (fit_cum / total_trials_LW) / MaxFit fig, ax = plt.subplots() ax.matshow(fip, cmap=plt.cm.Blues) fip = np.around(fip, decimals=2) for o in range(i): for n in range(j): c = fip[n, o] ax.text(o, n, str(c), va='center', ha='center') plt.title('Theta VS Omega Behavior: LW', fontsize=13) plt.show() return fitness1, fitness2, fitness3, Hidden1_Avg, Hidden2_Avg
def single_neuron_ablations(genotype): nn = ffann.ANN(nI, nH1, nH2, nO) # Task 1 ip_fit = np.zeros(nH1 + nH2) body = invpend.InvPendulum() index = 0 for neuron in range(nI, nI + nH1 + nH2): #iterates through each neuron (20 neurons) fit = 0.0 nn.setParameters(genotype, WeightRange, BiasRange) nn.ablate(neuron) for theta in theta_range_IP: for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot for t in time_IP: nn.step( np.concatenate( (body.state(), np.zeros(4), np.zeros(3)))) f = body.step(stepsize_IP, np.array([nn.output()[0]])) fit += f fit = fit / (duration_IP * total_trials_IP) fit = (fit + 7.65) / 7 # Normalize to run between 0 and 1 if fit < 0.0: fit = 0.0 ip_fit[index] = fit index += 1 # Task 2 cp_fit = np.zeros(nH1 + nH2) body = cartpole.Cartpole() index = 0 for neuron in range(nI, nI + nH1 + nH2): fit = 0.0 nn.setParameters(genotype, WeightRange, BiasRange) nn.ablate(neuron) #isolates each neuron, sets outdegree to 0 for theta in theta_range_CP: for theta_dot in thetadot_range_CP: for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot for t in time_CP: nn.step( np.concatenate( (np.zeros(3), body.state(), np.zeros(3)))) f = body.step(stepsize_CP, np.array([nn.output()[1]])) fit += f fit = fit / (duration_CP * total_trials_CP) if fit < 0.0: fit = 0.0 cp_fit[index] = fit index += 1 #Task 3 lw_fit = np.zeros(nH1 + nH2) body = leggedwalker.LeggedAgent(0.0, 0.0) index = 0 for neuron in range(nI, nI + nH1 + nH2): #print(neuron) fit = 0.0 nn.setParameters(genotype, WeightRange, BiasRange) nn.ablate(neuron) for theta in theta_range_LW: for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega for t in time_LW: nn.step( np.concatenate( (np.zeros(3), np.zeros(4), body.state()))) body.step(stepsize_LW, np.array(nn.output()[2:5])) fit += body.cx / duration_LW # XXX fit = (fit / total_trials_LW) / MaxFit if fit < 0.0: fit = 0.0 lw_fit[index] = fit index += 1 return ip_fit, cp_fit, lw_fit
def fitnessFunction(genotype): nn = ffann.ANN(nI, nH1, nH2, nO) nn.setParameters(genotype, WeightRange, BiasRange) # Task 1 body = invpend.InvPendulum() fit = 0.0 for theta in theta_range_IP: for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot for t in time_IP: # nn.step(np.concatenate((body.state(),np.zeros(4),np.zeros(3),np.zeros(2)))) #arrays for inputs for each task nn.step( np.concatenate( (body.state(), np.zeros(4), np.zeros(3)))) #arrays for inputs for each task f = body.step(stepsize_IP, np.array([nn.output()[0]])) fit += f # Minimize the cost of moving the pole up fitness1 = fit / (duration_IP * total_trials_IP) fitness1 = (fitness1 + 7.65) / 7 # Normalize to run between 0 and 1 if fitness1 < 0.0: fitness1 = 0.0 # Task 2 body = cartpole.Cartpole() fit = 0.0 for theta in theta_range_CP: for theta_dot in thetadot_range_CP: for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot for t in time_CP: # nn.step(np.concatenate((np.zeros(3),body.state(),np.zeros(3),np.zeros(2)))) nn.step( np.concatenate( (np.zeros(3), body.state(), np.zeros(3)))) f, done = body.step(stepsize_CP, np.array([nn.output()[1]])) fit += f # (Maximize) Amount of time pole is balanced ### if done: break fitness2 = fit / (duration_CP * total_trials_CP) if fitness2 < 0.0: fitness2 = 0.0 #Task 3 body = leggedwalker.LeggedAgent(0.0, 0.0) fit = 0.0 for theta in theta_range_LW: for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega for t in time_LW: # nn.step(np.concatenate((np.zeros(3),np.zeros(4),body.state(),np.zeros(2)))) nn.step( np.concatenate((np.zeros(3), np.zeros(4), body.state()))) body.step(stepsize_LW, np.array(nn.output()[2:5])) fit += body.cx / duration_LW # Maximize the final forward distance covered fitness3 = (fit / total_trials_LW) / MaxFit if fitness3 < 0.0: fitness3 = 0.0 # # Task 4 # body = mountaincar.MountainCar() # fit = 0.0 # for position in position_range_MC: # for velocity in velocity_range_MC: # body.position = position # body.velocity = velocity # for t in time_MC: # nn.step(np.concatenate((np.zeros(3),np.zeros(4),np.zeros(3),body.state()))) # f,done = body.step(stepsize_MC, np.array([nn.output()[5]])) # fit += f # if done: # break # fitness4 = ((fit/(duration_MC*total_trials_MC)) + 1.0)/0.65 # return fitness1*fitness2*fitness3*fitness4 return fitness1 * fitness2 * fitness3
def single_neuron_ablations(genotype): nn = ffann.ANN(nI, nH1, nH2, nO) # Task 1 ip_fit = np.zeros(nI + nH1 + nH2) #empty array of all neurons body = invpend.InvPendulum() for i in range( nI + nH1 + nH2 ): #iterates through each neuron (20 neurons), creating a new network, each ablating through a different neuron fit = 0.0 nn.setParameters(genotype, WeightRange, BiasRange) nn.ablate(i) for theta in theta_range_IP: for theta_dot in thetadot_range_IP: body.theta = theta body.theta_dot = theta_dot for t in time_IP: nn.step( np.concatenate( (body.state(), np.zeros(4), np.zeros(3)))) f = body.step(stepsize_IP, np.array([nn.output()[0]])) fit += f fit = abs(fit / (duration_IP * total_trials_IP)) fit = (fit + 7.65) / 7 # Normalize to run between 0 and 1 ip_fit[i] = fit #adds each neuron to matrix #print('ip fit:',ip_fit) # Task 2 cp_fit = np.zeros(nI + nH1 + nH2) body = cartpole.Cartpole() for i in range(nI + nH1 + nH2): fit = 0.0 nn.setParameters(genotype, WeightRange, BiasRange) nn.ablate(i) #isolates each neuron, sets outdegree to 0 for theta in theta_range_CP: for theta_dot in thetadot_range_CP: for x in x_range_CP: for x_dot in xdot_range_CP: body.theta = theta body.theta_dot = theta_dot body.x = x body.x_dot = x_dot for t in time_CP: nn.step( np.concatenate( (np.zeros(3), body.state(), np.zeros(3)))) f = body.step(stepsize_CP, np.array([nn.output()[1]])) fit += f fit = fit / (duration_CP * total_trials_CP) cp_fit[i] = fit print('cp fit:', cp_fit) #Task 3 lw_fit = np.zeros(nI + nH1 + nH2) body = leggedwalker.LeggedAgent(0.0, 0.0) for i in range(nI + nH1 + nH2): #print("task3,each neuron",i) fit = 0.0 nn.setParameters(genotype, WeightRange, BiasRange) nn.ablate(i) for theta in theta_range_LW: for omega in omega_range_LW: body.reset() body.angle = theta body.omega = omega for t in time_LW: nn.step( np.concatenate( (np.zeros(3), np.zeros(4), body.state()))) body.step(stepsize_LW, np.array(nn.output()[2:5])) fit += body.cx / duration_LW fit = abs((fit / total_trials_LW) / MaxFit) #print(fit) lw_fit[i] = fit #print('lw fit:',lw_fit) #print('ip fit',ip_fit) #print('cp fit',cp_fit) #print('lw fit',lw_fit) # ============================================================================= # X = np.arange(3) # fig = plt.figure() # ax = fig.add_axes([0,0,1,1]) # # # ax.bar(X + 0.00, ip_fit, color = 'b', width = 0.25) # ax.bar(X + 0.25, cp_fit, color = 'g', width = 0.25) # ax.bar(X + 0.50, lw_fit, color = 'r', width = 0.25) # ============================================================================= # ============================================================================= plt.plot(ip_fit, label='invpend') plt.plot(cp_fit, label='cartpole') plt.plot(lw_fit, label='leggedwalker') plt.legend() plt.show() # ============================================================================= return ip_fit, cp_fit, lw_fit