From 33512b2af4f93c9df689b802dcdeb5fcbd679d7d Mon Sep 17 00:00:00 2001 From: Florian Maurer Date: Sat, 30 Sep 2023 08:38:37 +0200 Subject: [PATCH] fix market clearing if more supply is available (#214) tests for verification are added --- assume/markets/clearing_algorithms/simple.py | 6 +- tests/test_simple_market_mechanisms.py | 66 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/assume/markets/clearing_algorithms/simple.py b/assume/markets/clearing_algorithms/simple.py index d86a920d..bf93b42c 100644 --- a/assume/markets/clearing_algorithms/simple.py +++ b/assume/markets/clearing_algorithms/simple.py @@ -123,7 +123,7 @@ def clear( gen_vol -= diff # add left over to supply_orders again - # supply_orders.insert(0, supply_order) + supply_orders.insert(0, supply_order) demand_order["accepted_volume"] = demand_order["volume"] else: demand_order["accepted_volume"] = demand_order["volume"] @@ -131,7 +131,9 @@ def clear( accepted_demand_orders.append(demand_order) for order in supply_orders: - rejected_orders.append(order) + # if the order was not accepted partially, it is rejected + if not order.get("accepted_volume"): + rejected_orders.append(order) # set clearing price - merit order - uniform pricing if accepted_supply_orders: diff --git a/tests/test_simple_market_mechanisms.py b/tests/test_simple_market_mechanisms.py index bbc198d7..70c0a511 100644 --- a/tests/test_simple_market_mechanisms.py +++ b/tests/test_simple_market_mechanisms.py @@ -106,9 +106,75 @@ def test_market_pay_as_clear(): orderbook = extend_orderbook(products, 300, 100, orderbook) orderbook = extend_orderbook(products, 200, 50, orderbook) + mr = PayAsClearRole(simple_dayahead_auction_config) + accepted, rejected, meta = mr.clear(orderbook, products) + assert meta[0]["demand_volume"] > 0 + assert meta[0]["price"] > 0 + assert len(accepted) == 4 + assert len(rejected) == 0 + assert meta[0]["supply_volume"] == 500 + assert meta[0]["demand_volume"] == 500 + assert meta[0]["price"] == 100 + for bid in accepted: + assert bid["volume"] == bid["accepted_volume"] + + +def test_market_pay_as_clears_single_demand(): + next_opening = simple_dayahead_auction_config.opening_hours.after(datetime.now()) + products = get_available_products( + simple_dayahead_auction_config.market_products, next_opening + ) + assert len(products) == 1 + + """ + Create Orderbook with constant order volumes and prices: + - dem1: volume = -1000, price = 3000 + - gen1: volume = 1000, price = 100 + - gen2: volume = 900, price = 50 + """ + orderbook = extend_orderbook(products, -700, 3000) + orderbook = extend_orderbook(products, 300, 100, orderbook) + orderbook = extend_orderbook(products, 200, 50, orderbook) + + mr = PayAsClearRole(simple_dayahead_auction_config) + accepted, rejected, meta = mr.clear(orderbook, products) + assert meta[0]["demand_volume"] > 0 + assert meta[0]["price"] > 0 + assert len(accepted) == 3 + assert len(rejected) == 0 + assert meta[0]["supply_volume"] == 500 + assert meta[0]["demand_volume"] == 500 + assert meta[0]["price"] == 100 + assert accepted[0]["volume"] == -700 + assert accepted[0]["accepted_volume"] == -500 + + +def test_market_pay_as_clears_single_demand_more_generation(): + next_opening = simple_dayahead_auction_config.opening_hours.after(datetime.now()) + products = get_available_products( + simple_dayahead_auction_config.market_products, next_opening + ) + assert len(products) == 1 + + """ + Create Orderbook with constant order volumes and prices: + - dem1: volume = -1000, price = 3000 + - gen1: volume = 1000, price = 100 + - gen2: volume = 900, price = 50 + """ + orderbook = extend_orderbook(products, -400, 3000) + orderbook = extend_orderbook(products, 300, 100, orderbook) + orderbook = extend_orderbook(products, 200, 50, orderbook) + orderbook = extend_orderbook(products, 230, 60, orderbook) + mr = PayAsClearRole(simple_dayahead_auction_config) accepted, rejected, meta = mr.clear(orderbook, products) assert meta[0]["demand_volume"] > 0 assert meta[0]["price"] > 0 assert len(accepted) == 3 assert len(rejected) == 1 + assert meta[0]["supply_volume"] == 400 + assert meta[0]["demand_volume"] == 400 + assert meta[0]["price"] == 60 + assert accepted[0]["volume"] == -400 + assert accepted[0]["accepted_volume"] == -400