def run():
"""Run the agent for a finite number of trials."""
for simulation in range(0, N_SIMULATIONS):
# Set up environment and agent
e = Environment() # create environment (also adds some dummy traffic)
a = e.create_agent(LearningAgent) # create agent
e.set_primary_agent(a, enforce_deadline=True) # specify agent to track
# NOTE: You can set enforce_deadline=False while debugging to allow longer trials
# TODO: Change later enforce_deadline=True
# Now simulate it
sim = Simulator(e, update_delay=0.001, display=False) # create simulator (uses pygame when display=True, if available)
# NOTE: To speed up simulation, reduce update_delay and/or set display=False
sim.run(n_trials=N_TRIALS) # run for a specified number of trials
# NOTE: To quit midway, press Esc or close pygame window, or hit Ctrl+C on the command-line
if simulation == N_SIMULATIONS - 1:
with open('results.csv', 'a') as csvfile:
fieldnames = ['alpha', 'gamma', 'epsilon', 'success_rate', 'last_failure']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for index in range(0,len(simulation_rates)):
writer.writerow({
'alpha': get_simulation_params(0)[0],
'gamma': get_simulation_params(0)[1],
'epsilon': get_simulation_params(0)[2],
'success_rate': simulation_rates[index],
'last_failure': last_errors[index]})
if N_SIMULATIONS > 1: #multiple simulation AND last simulation
plt.figure(1)
plt.subplot(211)
plt.plot(simulation_rates)
plt.title('Success Rate/Simulation')
plt.xlabel('# Simulation')
plt.ylabel('Success Rate')
plt.subplot(212)
plt.plot(last_errors)
plt.title('Last failed trial per simulation')
plt.xlabel('# Simulation')
plt.ylabel('Last failed trial')
plt.show()
评论列表
文章目录