-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauction.py
326 lines (270 loc) · 12.4 KB
/
auction.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#------------------------------------------------------------------------------
# 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 a power auction for clearing offers/bids, where pricing is
adjusted for network losses and binding constraints
"""
#------------------------------------------------------------------------------
# Imports:
#------------------------------------------------------------------------------
import logging
#------------------------------------------------------------------------------
# Logging:
#------------------------------------------------------------------------------
logger = logging.getLogger(__name__)
#------------------------------------------------------------------------------
# Constants:
#------------------------------------------------------------------------------
DISCRIMINATIVE = "discriminative"
#LAO = "lao"
#FRO = "fro"
#LAB = "lab"
#FRB = "frb"
FIRST_PRICE = "first price"
#SECOND_PRICE = "second price"
#SPLIT = "split"
#DUAL_LAOB = "dual laob"
#------------------------------------------------------------------------------
# "Auction" class:
#------------------------------------------------------------------------------
class Auction(object):
""" Defines a power auction for clearing offers/bids, where pricing is
adjusted for network losses and binding constraints.
Based on auction.m from MATPOWER by Ray Zimmerman, developed at PSERC
Cornell. See U{http://www.pserc.cornell.edu/matpower/} for more info.
"""
def __init__(self, case, offers, bids, auctionType,
gteeOfferPrice=True, gteeBidPrice=True, limits=None):
""" Initialises an new Auction instance.
"""
#: Power system case.
self.case = case
#: Offers to produce a quantity of energy at a specified price.
self.offers = offers
#: Bids to buy a quantity of energy at a specified price.
self.bids = bids
#: Pricing option.
self.auctionType = auctionType
#: Guarantee that cleared offers are >= offers.
self.guaranteeOfferPrice = gteeOfferPrice
#: Guarantee that cleared bids are <= bids.
self.guaranteeBidPrice = gteeBidPrice
#: Offer/bid price limits.
self.limits = limits if limits is not None else {}
def run(self):
""" Clears a set of bids and offers.
"""
# Compute cleared offer/bid quantities from total dispatched quantity.
self._clearQuantities()
# Compute shift values to add to lam to get desired pricing.
# lao, fro, lab, frb = self._first_rejected_last_accepted()
# Clear offer/bid prices according to auction type.
self._clearPrices()
# self._clear_prices(lao, fro, lab, frb)
# Clip cleared prices according to guarantees and limits.
self._clipPrices()
self._logClearances()
return self.offers, self.bids
def _clearQuantities(self):
""" Computes the cleared quantities for each offer/bid according
to the dispatched output from the OPF solution.
"""
generators = [g for g in self.case.generators if not g.is_load]
vLoads = [g for g in self.case.generators if g.is_load]
for g in generators:
self._clearQuantity(self.offers, g)
for vl in vLoads:
self._clearQuantity(self.bids, vl)
def _clearQuantity(self, offbids, gen):
""" Computes the cleared bid quantity from total dispatched quantity.
"""
# Filter out offers/bids not applicable to the generator in question.
gOffbids = [offer for offer in offbids if offer.generator == gen]
# Offers/bids within valid price limits (not withheld).
valid = [ob for ob in gOffbids if not ob.withheld]
# Sort offers by price in ascending order and bids in decending order.
valid.sort(key=lambda ob: ob.price, reverse=[False, True][gen.is_load])
acceptedQty = 0.0
for ob in valid:
# Compute the fraction of the block accepted.
accepted = (ob.totalQuantity - acceptedQty) / ob.quantity
# Clip to the range 0-1.
if accepted > 1.0:
accepted = 1.0
elif accepted < 1.0e-05:
accepted = 0.0
ob.clearedQuantity = accepted * ob.quantity
ob.accepted = (accepted > 0.0)
# Log the event.
# if ob.accepted:
# logger.info("%s [%s, %.3f, %.3f] accepted at %.2f MW." %
# (ob.__class__.__name__, ob.generator.name, ob.quantity,
# ob.price, ob.clearedQuantity))
# else:
# logger.info("%s [%s, %.3f, %.3f] rejected." %
# (ob.__class__.__name__, ob.generator.name, ob.quantity,
# ob.price))
# Increment the accepted quantity.
acceptedQty += ob.quantity
# def _first_rejected_last_accepted(self):
# """ Compute shift values to add to lam to get desired pricing.
# """
# accepted = [of for of in self.offers if of.accepted]
# rejected = [of for of in self.offers if not of.accepted]
#
# # Sort according to the difference between the offer price and the
# # reference nodal marginal price in ascending order.
# accepted.sort(key=lambda x: x.difference)
# rejected.sort(key=lambda x: x.difference)
#
# # lao + lambda is equal to the last accepted offer.
# lao = accepted[-1] if accepted else None
# # fro + lambda is equal to the first rejected offer.
# fro = rejected[0] if rejected else None
#
# if lao is not None:
# logger.info("LAO: %s, %.2fMW (%.2fMW), %.2f$/MWh" %
# (lao.generator.name, lao.quantity,
# lao.clearedQuantity, lao.price))
# elif self.offers:
# logger.info("No accepted offers.")
#
# if fro is not None:
# logger.info("FRO: %s, %.2fMW (%.2fMW), %.2f$/MWh" %
# (fro.generator.name, fro.quantity,
# fro.clearedQuantity, fro.price))
# elif self.offers:
# logger.info("No rejected offers.")
#
#
# # Determine last accepted bid and first rejected bid.
# acceptedBids = [bid for bid in self.bids if bid.accepted]
# acceptedBids.sort(key=lambda bid: bid.difference, reverse=True)
#
# rejectedBids = [bid for bid in self.bids if not bid.accepted]
# rejectedBids.sort(key=lambda bid: bid.difference, reverse=True)
#
# lab = self.lab = acceptedBids[-1] if accepted_bids else None
# frb = self.frb = rejectedBids[0] if rejected_bids else None
#
# if lab is not None:
# logger.info("LAB: %s, %.2fMW (%.2fMW), %.2f$/MWh" %
# (lab.generator.name, lab.quantity,
# lab.clearedQuantity, lab.price))
# elif self.bids:
# logger.info("No accepted bids.")
#
# if frb is not None:
# logger.info("FRB: %s, %.2fMW (%.2fMW), %.2f$/MWh" %
# (frb.generator.name, frb.quantity,
# frb.clearedQuantity, frb.price))
# elif self.bids:
# logger.info("No rejected bids.")
#
# return lao, fro, lab, frb
def _clearPrices(self):
""" Clears prices according to auction type.
"""
for offbid in self.offers + self.bids:
if self.auctionType == DISCRIMINATIVE:
offbid.clearedPrice = offbid.price
elif self.auctionType == FIRST_PRICE:
offbid.clearedPrice = offbid.lmbda
else:
raise ValueError
# def _clearPrices(self, lao, fro, lab, frb):
# """ Cleared offer/bid prices for different auction types.
# """
# for offbid in self.offers + self.bids:
#
# if self.auctionType == DISCRIMINATIVE:
# offbid.clearedPrice = offbid.price
# elif self.auctionType == LAO:
# offbid.clearedPrice = offbid.lmbda + lao.price
# elif self.auctionType == FRO:
# offbid.clearedPrice = offbid.lmbda + fro.price
# elif self.auctionType == LAB:
# offbid.clearedPrice = offbid.lmbda + lab.price
# elif self.auctionType == FRB:
# offbid.clearedPrice = offbid.lmbda + frb.price
# elif self.auctionType == FIRST_PRICE:
# offbid.clearedPrice = offbid.lmbda
# elif self.auctionType == SECOND_PRICE:
# if abs(lao.price) < 1e-5:
# clearedPrice = offbid.lmbda + min(fro.price, lab.price)
# offbid.clearedPrice = clearedPrice
# else:
# clearedPrice = offbid.p_lmbda + max(lao.price, frb.price)
# offbid.clearedPrice = clearedPrice
# elif self.auctionType == SPLIT:
# splitPrice = (lao.price - lab.price) / 2.0
# offbid.clearedPrice = offbid.lmbda + splitPrice
# elif self.auctionType == DUAL_LAOB:
# if isinstance(offbid, Offer):
# offbid.clearedPrice = offbid.lmbda + lao.price
# else:
# offbid.clearedPrice = offbid.lmbda + lab.price
def _clipPrices(self):
""" Clip cleared prices according to guarantees and limits.
"""
# Guarantee that cleared offer prices are >= offers.
if self.guaranteeOfferPrice:
for offer in self.offers:
if offer.accepted and offer.clearedPrice < offer.price:
offer.clearedPrice = offer.price
# Guarantee that cleared bid prices are <= bids.
if self.guaranteeBidPrice:
for bid in self.bids:
if bid.accepted and bid.clearedPrice > bid.price:
bid.clearedPrice = bid.price
# Clip cleared offer prices.
if self.limits.has_key("maxClearedOffer"):
maxClearedOffer = self.limits["maxClearedOffer"]
for offer in self.offers:
if offer.clearedPrice > maxClearedOffer:
offer.clearedPrice = maxClearedOffer
# Clip cleared bid prices.
if self.limits.has_key("minClearedBid"):
minClearedBid = self.limits["minClearedBid"]
for bid in self.bids:
if bid.clearedPrice < minClearedBid:
bid.clearedPrice = minClearedBid
# Make prices uniform across all offers/bids for each generator after
# clipping (except for discrim auction) since clipping may only affect
# a single block of a multi-block generator.
if self.auctionType != DISCRIMINATIVE:
for g in self.case.generators:
gOffers = [of for of in self.offers if of.generator == g]
if gOffers:
uniformPrice = max([of.clearedPrice for of in gOffers])
for of in gOffers:
of.clearedPrice = uniformPrice
gBids = [bid for bid in self.bids if bid.vLoad == g]
if gBids:
uniformPrice = min([bid.cleared_price for bid in gBids])
for bid in gBids:
bid.clearedPrice = uniformPrice
def _logClearances(self):
""" Logs offer/bid cleared values.
"""
for offer in self.offers:
logger.info("%.2fMW offer cleared at %.2f$/MWh for %s (%.2f)." %
(offer.clearedQuantity, offer.clearedPrice,
offer.generator.name, offer.revenue))
for bid in self.bids:
logger.info("%.2fMW bid cleared at %.2f$/MWh for %s (%.2f)." %
(bid.clearedQuantity, bid.clearedPrice,
bid.vLoad.name, bid.revenue))
# EOF -------------------------------------------------------------------------