def __str__(self): """ Used for displaying the grid on the command line """ grid_rep = [] grid_rep.append('%ix%i World' % (self.h, self.w)) grid_rep.append('\n\n\n') for i, row in enumerate(self.grid): for j, elem in enumerate(row): if elem == Simulator.__wall_sym: grid_rep.append(Color.red(' %s ' % elem)) else: if (i,j) in self.absorb: if self.absorb[(i, j)] > 0: grid_rep.append(Color.green(' %s ' % Simulator.__goal_sym)) elif self.absorb[(i, j)] < 0: grid_rep.append(Color.red(' %s ' % Simulator.__goal_sym)) else: grid_rep.append(' %s ' % Simulator.__goal_sym) elif (i, j) == self.state: grid_rep.append(Color.yellow(' %s ' % Simulator.__agent_sym)) else: grid_rep.append(' %s ' % elem) grid_rep.append('\n') grid_rep.append('\n\n\n') return ''.join(grid_rep)
def watch_execution(sim, policy, state=None, maxsteps=500): """ Visually displays the grid and the agents movements in the terminal as the agent executes the given policy """ absorb = False sim.reset(state) totreward = 0.0 steps = 0 while not absorb and steps < maxsteps: steps += 1 os.system('cls' if os.name=='nt' else 'clear') print sim stateindex = sim.state_to_index(sim.state) action = policy.select_action(stateindex)[0] print "Step: %i State: %s" % (steps, sim.state) print "------------" print "Executing action %s (%i)" % (print_action(action), action) print "Total Reward: %f" % totreward print "Reward per Step: %f" % (totreward/steps) sample = sim.execute(action) totreward += sample.reward absorb = sample.absorb sleep(.4) print if absorb: print Color.green("Agent Reached Goal State %s with reward of %f" % (sim.state, sample.reward)) success = True else: print Color.red("Agent did not reach a goal state within %i steps!" % maxsteps) success = False print "Total Reward: %f" % totreward print "Reward per Step: %f" % (totreward/steps) return success, totreward, steps, (totreward/steps)
def __str__(self): grid_rep = '' for i,row in enumerate(self.grid): for j,elem in enumerate(row): if(elem == RoomRunner.__wall_sym): grid_rep += Color.red(' %s ' % elem) elif(elem == RoomRunner.__agent_sym): grid_rep += Color.yellow(' %s ' % elem) else: if((i,j) in self.goals): grid_rep += Color.green(' %s ' % elem) else: grid_rep += ' %s ' % elem grid_rep += '\n' return grid_rep