Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check open market auctions #192

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions assume/markets/base_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from datetime import datetime
from itertools import groupby
from operator import itemgetter
from queue import Queue

from mango import Role

Expand Down Expand Up @@ -35,7 +34,7 @@ class MarketMechanism:

def __init__(self, marketconfig: MarketConfig):
self.marketconfig = marketconfig
self.open_auctions = Queue()
self.open_auctions = set()
self.all_orders = []

def validate_registration(self, meta: dict) -> bool:
Expand Down Expand Up @@ -71,6 +70,10 @@ def validate_orderbook(self, orderbook: Orderbook, agent_tuple) -> None:
assert order["price"] <= max_price, f"maximum_bid_price {order['price']}"
assert order["price"] >= min_price, f"minimum_bid_price {order['price']}"

# check that the product is part of an open auction
product = (order["start_time"], order["end_time"], order["only_hours"])
assert product in self.open_auctions, "no open auction"

if max_volume:
assert (
abs(order["volume"]) <= max_volume
Expand Down Expand Up @@ -212,7 +215,7 @@ async def opening(self):
"products": products,
}

self.open_auctions.put(opening_message)
self.open_auctions |= set(opening_message["products"])

for agent in self.registered_agents:
agent_addr, agent_id = agent
Expand Down Expand Up @@ -340,7 +343,11 @@ async def clear_market(self, market_products: list[MarketProduct]):
# pending_orderbook,
market_meta,
) = self.clear(self.all_orders, market_products)
self.open_auctions.get()
self.all_orders = []
for order in rejected_orderbook:
order["accepted_volume"] = 0
order["accepted_price"] = 0
self.open_auctions - set(market_products)
# self.all_orders = pending_orderbook

accepted_orderbook.sort(key=itemgetter("agent_id"))
Expand Down
12 changes: 0 additions & 12 deletions assume/markets/clearing_algorithms/all_or_nothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ def clear(
# resulting i is the cut point
accepted_product_orders.extend(demand_orders[:i])
accepted_product_orders.extend(supply_orders[:i])

for order in supply_orders[i:]:
order["accepted_volume"] = 0
for order in demand_orders[i:]:
order["accepted_volume"] = 0

rejected_orders.extend(demand_orders[i:])
rejected_orders.extend(supply_orders[i:])

Expand Down Expand Up @@ -168,12 +162,6 @@ def clear(

accepted_product_orders.extend(demand_orders[:i])
accepted_product_orders.extend(supply_orders[:i])

for order in supply_orders[i:]:
order["accepted_volume"] = 0
for order in demand_orders[i:]:
order["accepted_volume"] = 0

rejected_orders.extend(demand_orders[i:])
rejected_orders.extend(supply_orders[i:])

Expand Down
2 changes: 0 additions & 2 deletions assume/markets/clearing_algorithms/complex_clearing.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,6 @@ def clear(
if all(order_profit >= 0 for order_profit in orders_profit):
break

self.all_orders = []

return extract_results(
model=instance,
orders=orderbook,
Expand Down
16 changes: 9 additions & 7 deletions assume/markets/clearing_algorithms/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def clear(
for demand_order in demand_orders:
if not supply_orders:
# if no more generation - reject left over demand
demand_order["accepted_volume"] = 0
rejected_orders.append(demand_order)
continue

Expand All @@ -100,7 +99,6 @@ def clear(
to_commit.append(supply_order)
gen_vol += supply_order["volume"]
else:
supply_order["accepted_volume"] = 0
rejected_orders.append(supply_order)
# now we know which orders we need
# we only need to see how to arrange it.
Expand All @@ -118,7 +116,7 @@ def clear(
# generation left over - split last generation bid
supply_order = to_commit[-1]
split_supply_order = supply_order.copy()
split_supply_order["accepted_volume"] = diff
split_supply_order["volume"] = diff
supply_order["accepted_volume"] = supply_order["volume"] - diff
# changed supply_order is still part of to_commit and will be added
# only volume-diff can be sold for current price
Expand All @@ -133,6 +131,9 @@ def clear(
accepted_product_orders.append(demand_order)
accepted_product_orders.extend(to_commit)

for order in supply_orders:
rejected_orders.append(order)

# set clearing price - merit order - uniform pricing
accepted_supply_orders = [
x for x in accepted_product_orders if x["accepted_volume"] > 0
Expand Down Expand Up @@ -204,7 +205,6 @@ def clear(
for demand_order in demand_orders:
if not supply_orders:
# if no more generation - reject left over demand
demand_order["accepted_volume"] = 0
rejected_orders.append(demand_order)
continue

Expand All @@ -218,7 +218,6 @@ def clear(
to_commit.append(supply_order)
gen_vol += supply_order["volume"]
else:
supply_order["accepted_volume"] = 0
rejected_orders.append(supply_order)
# now we know which orders we need
# we only need to see how to arrange it.
Expand All @@ -236,8 +235,8 @@ def clear(
# generation left over - split generation
supply_order = to_commit[-1]
split_supply_order = supply_order.copy()
split_supply_order["accepted_volume"] = diff
supply_order["accepted_volume"] -= supply_order["volume"] - diff
split_supply_order["volume"] = diff
supply_order["accepted_volume"] = supply_order["volume"] - diff
# only volume-diff can be sold for current price
# add left over to supply_orders again
gen_vol -= diff
Expand All @@ -255,6 +254,9 @@ def clear(
demand_order["accepted_price"] = supply_order["price"]
accepted_product_orders.extend(to_commit)

for order in supply_orders:
rejected_orders.append(order)

accepted_supply_orders = [
x for x in accepted_product_orders if x["accepted_volume"] > 0
]
Expand Down
3 changes: 1 addition & 2 deletions examples/inputs/example_01c/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ dam_with_complex_opt_clearing:
maximum_bid_price: 3000
minimum_bid_price: -500
price_unit: EUR/MWh
market_mechanism: pay_as_clear_complex_opt
market_mechanism: pay_as_clear_complex
additional_fields:
- bid_type
- accepted_price
- profile
9 changes: 6 additions & 3 deletions tests/test_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ async def test_market_init(market_role: MarketRole):
"sender_addr": market_role.context.addr,
"sender_id": market_role.context.aid,
}

end = start + rd(hours=1)
orderbook = [
{
"start_time": start,
"end_time": start + rd(hours=1),
"end_time": end,
"volume": 120,
"price": 120,
"agent_id": "gen1",
"only_hours": None,
}
]

market_role.open_auctions |= {(start, end, None)}
market_role.handle_orderbook(content={"orderbook": orderbook}, meta=meta)
assert len(market_role.all_orders) == 1

Expand All @@ -77,6 +77,7 @@ async def test_market_tick(market_role: MarketRole):
"only_hours": None,
}
]
market_role.open_auctions |= {(start, end, None)}
market_role.handle_orderbook(content={"orderbook": orderbook}, meta=meta)
assert len(market_role.all_orders) == 1
assert market_role.all_orders[0]["price"] == 1201
Expand Down Expand Up @@ -105,6 +106,7 @@ async def test_market_max(market_role: MarketRole):
market_role.marketconfig.maximum_bid_price = 1000
market_role.marketconfig.minimum_bid_price = -500
market_role.marketconfig.maximum_bid_volume = 9090
market_role.open_auctions |= {(start, end, None)}

orderbook = [
{
Expand Down Expand Up @@ -192,6 +194,7 @@ async def test_market_unmatched(market_role: MarketRole):
"only_hours": None,
}
]
market_role.open_auctions |= {(start, end, None)}
market_role.handle_orderbook(content={"orderbook": orderbook}, meta=meta)

content = {
Expand Down