-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agent.py
78 lines (58 loc) · 2.17 KB
/
Agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from task_01 import Task
from environment_01 import Env
from pybrain.rl.learners.valuebased import ActionValueTable
from pybrain.rl.agents import LearningAgent
from pybrain.rl.learners import Q
from pybrain.rl.experiments import Experiment
from pybrain.rl.explorers import EpsilonGreedyExplorer
import pylab
import numpy as np
#Rasberry Pi imports
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
# define action-value table:
# number of environment states:
# 1024 states agent can be in the environment - comfortable / uncomfortable
#
# number of actions:
# 3 the number of action values the environment accepts - Foreward, Backward and Snooze
states = 1024 #Has to match class Env(Environment) - outdim in environment_01.py
actions = 3 #Has to match class Env(Environment) - indim in environment_01.py
try:
arr = np.loadtxt('/home/pi/Desktop/uexkull_animal/uexkull.csv', delimiter=';')
# open action value table from .csv file
except Exception as e:
# print e
arr = np.zeros((states, actions))
# except if the file does not exist - ie. first time - then creat and initialize it with numpy of zeros
av_table = ActionValueTable(states, actions)
av_table.initialize(arr.flatten())
# define Q-learning agent
learner = Q(0.1, 0.5)
learner._setExplorer(EpsilonGreedyExplorer(0.5))
agent = LearningAgent(av_table, learner)
# define the environment
env = Env()
# define the task
task = Task(env)
# define experiment
experiment = Experiment(task, agent)
# ready to go, start the process
while True:
experiment.doInteractions(12)
'''After n-number (here 12) steps, we call the agent’s learn() method and then reset it.
This will make the agent forget the previously executed steps but of course it won’t undo the changes it learned.'''
agent.learn()
agent.reset()
export_arr = av_table.getActionValues(np.arange(states))
export_arr = export_arr.reshape((states, actions))
np.savetxt("/home/pi/Desktop/uexkull_animal/uexkull.csv", export_arr, fmt='%.3f', delimiter=';')
# save action value table to .csv file
# Clean-up actions
try:
pass
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()