-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment4x1.py
More file actions
182 lines (148 loc) · 7.37 KB
/
Environment4x1.py
File metadata and controls
182 lines (148 loc) · 7.37 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 21 14:21:46 2017
@author: ECOWIZARD
"""
from collections import OrderedDict
import random as Random
import copy
class uniqueState(object):
def __init__(self,statetype :str):
self.statetype = statetype
class Environment4x1(object):
def __init__(self):
self.valid_actions = ['left', 'right']
self.valid_observables = ['blue', 'green']
self.states = dict()
self.valid_inputs = {'state': list(self.states.keys())}
self.agent_states = dict()
self.states[0] = uniqueState(self.valid_observables[0]) #blue state
self.states[1] = uniqueState(self.valid_observables[0]) #blue state
self.states[2] = uniqueState(self.valid_observables[1]) #green state
self.states[3] = uniqueState(self.valid_observables[0]) #blue state
self.done = False
self.primary_agent = None
self.step_data = {}
self.success = 0
self.TIMELIMIT = 10
self.timelapsed = 0
self.trial_data = {
'testing': False, # if the trial is for testing a learned policy
'net_reward': 0.0, # total reward earned in current trial
'success': 0, # whether the agent reached the destination in time
'age' : 0
}
@classmethod
def copy(cls,env):
newenv = cls()
newenv.valid_actions = copy.copy(env.valid_actions)
newenv.valid_observables = copy.deepcopy(env.valid_observables)
newenv.states = copy.deepcopy(env.states)
newenv.valid_inputs = copy.copy(env.valid_inputs)
newenv.agent_states = copy.copy(env.agent_states)
newenv.done = copy.deepcopy(env.done)
newenv.primary_agent = copy.deepcopy(env.primary_agent)
newenv.step_data = copy.deepcopy(env.step_data)
newenv.success = copy.deepcopy(env.success)
newenv.TIMELIMIT = copy.deepcopy(env.TIMELIMIT)
newenv.timelapsed = copy.deepcopy(env.timelapsed)
newenv.trial_data = copy.deepcopy(env.trial_data)
return newenv
def getStates(self):
return self.states
def sense(self,state=None):
if state == None:
return self.states[self.agent_states[self.primary_agent]["location"]].statetype #returns the color of the tile (in this case either blue or green depending on what state the agent is in..)
else:
#print "state is {}".format(state)
if state == -1:
pass
# print "the observation was {}".format(self.states[state].statetype)
return self.states[state].statetype
def step(self):
self.primary_agent.update() #this is the correct place to put this. It feels weird that the existence of the agents update function causes time to progress.
#I think this weirdness comes from the fact that it is not a "physical" agent. (It's not a computer emedded inside the environment that runs the code.)
#what I mean is that the rules of the environment are not the same rules that govern the execution of its code.
print("agent is in state {}".format(self.agent_states[self.primary_agent]['location']))
self.trial_data['age'] += 1
self.timelapsed += 1
if self.agent_states[self.primary_agent]['location'] == 2:
self.success = 1
self.trial_data['success'] = self.success
self.done = True
else:
if self.timelapsed >= self.TIMELIMIT:
self.done = True
def Transition(self,preState, action):
answer = OrderedDict()
location = preState
#print "in environment transition states are {}".format(self.states.keys())
for state in list(self.states.keys()):
if state == -1:
# print "what have we here {}".format(self.states.keys())
pass
answer[state] = 0
if action in self.valid_actions:
if action == self.valid_actions[0]:
action = -1
if action == self.valid_actions[1]:
action = 1
location += action
if location < 0:
location = 0
if location > 3:
location = 3
answer[location] = float(1) #this environment's transition function in particular is deterministic. The starting point is the only stochastic variable.
# print "after transition pcloud is {}".format(answer.keys())
return answer
def administerReward(self):
reward = 0.0
if self.agent_states[self.primary_agent]['location'] == 2:
reward = 1.0
else:
reward = -0.05
self.trial_data['net_reward'] += reward
return reward
def act(self,action):
pcloud = self.Transition(self.agent_states[self.primary_agent]['location'],action) #gets all the states and their probability of becoming the next state where the probability is greater than 0.
self.collapse(pcloud) #chooses one of the states at random with a bias dependent on the probability of it occuring. (states with higher prob will be more likely)
return self.administerReward() #administers reward based on new state.
def collapse(self,pcloud): #needs to be implemented.
randomnum = Random.uniform(0,1)
threshold = 0
for indx , probability in pcloud.items():
threshold += probability
if randomnum <= threshold:
self.agent_states[self.primary_agent]['location'] = indx
return
raise ValueError("For some reason the probabilities can't be compared with the <= operator.")
#it should never get here.
return
def create_agent(self,agent_class,*args, **kwargs):
agent = agent_class(self, Environment4x1, *args, **kwargs)
keys = list(self.states.keys())
print("when creating an agent the states are {}".format(keys))
self.agent_states[agent] = {'location': Random.choice(keys)}
print("print assigned agent to location: " + str(self.agent_states[agent]['location']))
return agent
def set_primary_agent(self,agent):
self.primary_agent = agent
self.agent_states[self.primary_agent]["location"] = self.randomlocation()
def randomlocation(self):
location = Random.choice([0,1,3])
return location
def reset(self,testing):
self.primary_agent.reset(testing)
location = self.randomlocation()
print("New Starting location is {}".format(location))
self.agent_states[self.primary_agent]['location'] = location
self.reward = 0
self.success = 0
self.done = False
self.timelapsed = 0
# Reset metrics for this trial (step data will be set during the step)
self.trial_data['testing'] = testing
self.trial_data['net_reward'] = 0.0
self.trial_data['parameters'] = {'e': self.primary_agent.epsilon, 'a': self.primary_agent.alpha}
self.trial_data['success'] = 0
self.trial_data['age'] = 0