-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
271 lines (217 loc) · 9.32 KB
/
util.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#------------------------------------------------------------------------------
# Copyright (C) 2007-2010 Richard Lincoln
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#------------------------------------------------------------------------------
""" Defines plotting tools.
"""
#------------------------------------------------------------------------------
# Imports:
#------------------------------------------------------------------------------
import random
import scipy
from itertools import count, izip
from pylab import figure, xlabel, ylabel, plot, show, legend
from pybrain.rl.agents.logging import LoggingAgent
from pybrain.rl.explorers.continuous import NormalExplorer
from pylon.generator import PW_LINEAR, POLYNOMIAL
#------------------------------------------------------------------------------
# "ManualNormalExplorer" class:
#------------------------------------------------------------------------------
class ManualNormalExplorer(NormalExplorer):
def __init__(self, dim, sigma=0.0, decay=0.995, sigmaOffset=0.0):
super(ManualNormalExplorer, self).__init__(dim, sigma)
self.decay = decay
self.sigmaOffset = sigmaOffset
self.manualSigma = [sigma] * dim
def newEpisode(self):
off = self.sigmaOffset
sigma = [((s - off) * self.decay) + off for s in self.manualSigma]
self.manualSigma = sigma
def _forwardImplementation(self, inbuf, outbuf):
self.sigma = self.manualSigma
super(ManualNormalExplorer, self)._forwardImplementation(inbuf, outbuf)
def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
self.sigma = self.manualSigma
super(ManualNormalExplorer, self)._backwardImplementation(
outerr, inerr, outbuf, inbuf)
#------------------------------------------------------------------------------
# "ZeroAgent" class:
#------------------------------------------------------------------------------
class ZeroAgent(LoggingAgent):
def __init__(self, indim, outdim):
super(ZeroAgent, self).__init__(indim, outdim)
self.learner = None
def getAction(self):
self.lastaction = 0.0 * scipy.ones(self.outdim)
return self.lastaction
def learn(self):
pass
#------------------------------------------------------------------------------
# "NegOneAgent" class:
#------------------------------------------------------------------------------
class NegOneAgent(LoggingAgent):
def __init__(self, indim, outdim):
super(NegOneAgent, self).__init__(indim, outdim)
self.learner = None
def getAction(self):
self.lastaction = -1.0 * scipy.ones(self.outdim)
return self.lastaction
def learn(self):
pass
#------------------------------------------------------------------------------
# "weighted_choice" function:
#------------------------------------------------------------------------------
def weighted_choice(lst):
""" Makes weighted choices. Accepts a list of tuples with the item and
probability as a pair like:
>>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
>>> y=windex(x) """
n = random.uniform(0, 1)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item
#------------------------------------------------------------------------------
# "xselections" function:
#------------------------------------------------------------------------------
def xselections(items, n):
""" Takes n elements (not necessarily distinct) from the sequence, order
matters.
@see: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465
"""
if n==0:
yield []
else:
for i in xrange(len(items)):
for ss in xselections(items, n-1):
yield [items[i]]+ss
#------------------------------------------------------------------------------
# "plotGenCost" function:
#------------------------------------------------------------------------------
def plotGenCost(generators):
""" Plots the costs of the given generators.
"""
figure()
plots = []
for generator in generators:
if generator.pcost_model == PW_LINEAR:
x = [x for x, _ in generator.p_cost]
y = [y for _, y in generator.p_cost]
elif generator.pcost_model == POLYNOMIAL:
x = scipy.arange(generator.p_min, generator.p_max, 5)
y = scipy.polyval(scipy.array(generator.p_cost), x)
else:
raise
plots.append(plot(x, y))
xlabel("P (MW)")
ylabel("Cost ($)")
legend(plots, [g.name for g in generators])
show()
#------------------------------------------------------------------------------
# "sparklineData" function:
#------------------------------------------------------------------------------
def sparklineData(data, filename):
""" Writes reward and action data for plotting sparklines with PGF/TikZ.
@see: http://www.texample.net/tikz/examples/weather-stations-data/
"""
fd = file(filename, "w+b")
for name in data.keys():
action, reward = data[name]
altName = name.lower().replace("_", "")
fd.write("\def")
fd.write("\REWARDDATA%s{" % altName)
for i, r in enumerate(reward):
fd.write("(%.2f,%.3f)" % (i / 10.0, r / 10.0)) # dimension too large
fd.write("}\n")
maxreward, maxindex = max(izip(reward, count()))
minreward, minindex = min(izip(reward, count()))
meanreward = scipy.mean(reward)
fd.write("\def\REWARDMAX%s{%.1f}\n" % (altName, maxreward))
fd.write("\def\REWARDMAXIDX%s{%d}\n" % (altName, maxindex))
fd.write("\def\REWARDMIN%s{%.1f}\n" % (altName, minreward))
fd.write("\def\REWARDMINIDX%s{%d}\n" % (altName, minindex))
fd.write("\def\REWARDMEAN%s{%.1f}\n" % (altName, meanreward))
fd.write("\def")
fd.write("\ACTIONDATA%s{" % altName)
for i, a in enumerate(action):
fd.write("(%.2f,%.3f)" % (i / 10.0, a / 10.0))
fd.write("}\n")
maxaction, maxindex = max(izip(reward, count()))
minaction, minindex = min(izip(reward, count()))
meanaction = scipy.mean(reward)
fd.write("\def\ACTIONMAX%s{%.1f}\n" % (altName, maxaction))
fd.write("\def\ACTIONMAXIDX%s{%d}\n" % (altName, maxindex))
fd.write("\def\ACTIONMIN%s{%.1f}\n" % (altName, minaction))
fd.write("\def\ACTIONMINIDX%s{%d}\n" % (altName, minindex))
fd.write("\def\ACTIONMEAN%s{%.1f}\n" % (altName, meanaction))
fd.close()
#------------------------------------------------------------------------------
# "ReSTExperimentWriter" class:
#------------------------------------------------------------------------------
class ReSTExperimentWriter(object):
""" Writes market experiment data to file in ReStructuredText format.
"""
def __init__(self, experiment):
""" Initialises a new ReSTExperimentWriter instance.
"""
# Market experiment whose data is to be written.
self.experiment = None
def write(self, file):
""" Writes market experiment data to file in ReStructuredText format.
"""
# Write environment state data.
file.write("State\n")
file.write( ("-" * 5) + "\n")
self.writeDataTable(file, type="state")
# Write action data.
file.write("Action\n")
file.write( ("-" * 6) + "\n")
self.writeDataTable(file, type="action")
# Write reward data.
file.write("Reward\n")
file.write( ("-" * 6) + "\n")
self.writeDataTable(file, type="reward")
def writeDataTable(self, file, type):
""" Writes agent data to an ReST table. The 'type' argument may
be 'state', 'action' or 'reward'.
"""
agents = self.experiment.agents
numAgents = len(self.experiment.agents)
colWidth = 8
idxColWidth = 3
sep = ("=" * idxColWidth) + " " + \
("=" * colWidth + " ") * numAgents + "\n"
file.write(sep)
# Table column headers.
file.write("..".rjust(idxColWidth) + " ")
for agent in agents:
# The end of the name is typically the unique part.
file.write(agent.name[-colWidth:].center(colWidth) + " ")
file.write("\n")
file.write(sep)
# Table values.
if agents:
rows, _ = agents[0].history.getField( type ).shape
else:
rows, _ = (0, 0)
for sequence in range( min(rows, 999) ):
file.write( str(sequence + 1).rjust(idxColWidth) + " " )
for agent in agents:
field = agent.history.getField( type )
# FIXME: Handle multiple state values.
file.write("%8.3f " % field[sequence, 0])
file.write("\n")
file.write(sep)
# EOF -------------------------------------------------------------------------