Skip to content

Commit

Permalink
Merge pull request #1 from gus0k/muda_float_quantity
Browse files Browse the repository at this point in the history
Muda float quantity
  • Loading branch information
kiedanski committed Jul 2, 2019
2 parents f821da3 + 8a1d6d2 commit d7a2de1
Show file tree
Hide file tree
Showing 14 changed files with 2,047 additions and 54 deletions.
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"python.linting.pylintEnabled": false,
"python.linting.pep8Enabled": true,
"python.linting.enabled": true,
"python.pythonPath": "/usr/bin/python",
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.formatting.provider": "yapf"
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ Please consider citing the following papers if you use the corresponding algorit

* Segal-Halevi, Erel, Avinatan Hassidim, and Yonatan Aumann. "MUDA: a truthful multi-unit double-auction mechanism." Thirty-Second AAAI Conference on Artificial Intelligence. 2018.
* Huang, Pu, Alan Scheller–Wolf, and Katia Sycara. "Design of a multi–unit double auction e–market." Computational Intelligence 18.4 (2002): 596-617.
* Blouin, Max R., and Roberto Serrano. "A decentralized market with common values uncertainty: Non-steady states." The Review of Economic Studies 68.2 (2001): 323-346.

# Articles using this algorithms

* Horta, José, et al. "Novel market approach for locally balancing renewable energy production and flexible demand." 2017 IEEE International Conference on Smart Grid Communications (SmartGridComm). IEEE, 2017.
* Kiedanski, Diego, et al. "Strategy-proof local energy market with sequential stochastic decision process for battery control." IEEE Innovative Smart Grid Technologies 2019 NA. 2019.
* Mengelkamp, Esther, et al. "Trading on local energy markets: A comparison of market designs and bidding strategies." 2017 14th International Conference on the European Energy Market (EEM). IEEE, 2017.
1,497 changes: 1,497 additions & 0 deletions Untitled.ipynb

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pymarket/bids/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
__email__ = 'gusok@protonmail.com'
__version__ = '0.5.3'


from pymarket.bids.bids import *
import pymarket.bids.demand_curves
from pymarket.bids.demand_curves import *
from pymarket.bids.processing import *

import pymarket.bids.demand_curves
120 changes: 120 additions & 0 deletions pymarket/bids/processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""
Implements processing techniques applied to bids before
mechanisms can use them
"""
import numpy as np
import pandas as pd


def new_player_id(index):
"""
Helper function for
merge_same price
Maps list of one user to
the exact user and list of several
users to a new value
Paramters
-----------
index: int
First identifier to use for the
new fake players
Returns
--------
new_id: function
"""

def new_id(users):
"""
Maps a list of users
to the only value if the length
is 1, or to the current value of the
closured index
Paramters
----------
users: list
list of user indentifiers
Returns:
new_index: int
The new index to use
"""
nonlocal index
if len(users) > 1:
new_index = index
index += 1
else:
new_index = users[0]

return new_index
return new_id


def merge_same_price(df, prec=5):
"""
Takes a bid where there are two players
in the same side of the market with the same
price and merges them into a new player with
aggregated quantity
Parameters
-----------
df: pandas dataframe
Dataframe with bids where there are at least
two players in the same side of the market
with the same price
Returns
-------
dataframe_new: pandas dataframe
A new bid dataframe where all players with
the same price have been merged into a single
player
final_maping: dict
A map between the new bid index and the old bids index
"""

id_gen = new_player_id(df.user.max() + 1)
columns = df.columns.copy()

df = df.copy().reset_index().rename(columns={'index': 'bid'})
print(df)

buy = df.loc[df['buying'], :]
sell = df.loc[~df['buying'], :]

dataframes = [buy, sell]

agg_fun = {
'bid': list,
'user': list,
'quantity': sum,
'buying': lambda x: x.sample(1),
'time': lambda x: x.sample(1),
'divisible': lambda x: x.sample(1),
}

dataframe_new = []
user_to_bid = {}
for df_ in dataframes:
rounded_prices = df_.price.apply(lambda x: np.round(x, prec))
df_new = df_.groupby(rounded_prices).agg(agg_fun).reset_index()
df_new.user = df_new.user.apply(id_gen)
maping = df_new.set_index('user').bid.to_dict()
for k, v in maping.items():
user_to_bid[k] = v

dataframe_new.append(df_new)

dataframe_new = pd.concat(dataframe_new)[columns].reset_index(drop=True)
index_to_user = dataframe_new.user.to_dict()

final_maping = {}
for k, v in index_to_user.items():
final_maping[k] = user_to_bid[v]

return dataframe_new, final_maping
2 changes: 1 addition & 1 deletion pymarket/mechanisms/huang_auction.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ def __init__(self, bids, *args, **kwargs):
"""
Mechanism.__init__(self, huang_auction, bids, *args, **kwargs)
Mechanism.__init__(self, huang_auction, bids, *args, merge=True, **kwargs)

28 changes: 23 additions & 5 deletions pymarket/mechanisms/mechanism.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import pandas as pd

from pymarket.bids.processing import merge_same_price
from pymarket.transactions.processing import split_transactions_merged_players


class Mechanism():

"""Implements a standard interface for mechanisms"""

def __init__(self, algo, bids, *args, **kwargs):
def __init__(self, algo, bids, *args, merge=False, **kwargs):
"""Creates a mechanisms with bids
If 'merge', then bids with the same price in the
same side will be merged
Parameters
----------
bids : TODO
Expand All @@ -16,12 +22,14 @@ def __init__(self, algo, bids, *args, **kwargs):
"""
self.algo = algo
self.bids = self._sanitize_bids(bids)
self.args = args
self.kwargs = kwargs
self.merge = merge
self.bids = self._sanitize_bids(bids)

def _sanitize_bids(self, bids):
"""Adapts the bids to a friendly format"
"""Adapts the bids to a friendly format
Parameters
----------
Expand All @@ -32,7 +40,14 @@ def _sanitize_bids(self, bids):
TODO
"""
return bids
if self.merge:
self.old_bids = bids
new_bids, maping = merge_same_price(bids)
self.maping = maping
else:
new_bids = bids

return new_bids

def _run(self):
"""Runs the mechanisms
Expand All @@ -57,8 +72,11 @@ def _cleanup(self, trans):
TODO
"""
return trans

if self.merge:
trans = split_transactions_merged_players(trans, self.old_bids, self.maping)

return trans

def run(self):
"""Runs the mechanisms
Expand Down

0 comments on commit d7a2de1

Please sign in to comment.