Created
July 12, 2019 15:59
-
-
Save EvansWinner/064932a724caa2c816a768792a6903da to your computer and use it in GitHub Desktop.
Little eBay auction simulation using simpy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Simulate listing items on ebay | |
""" | |
import simpy, random, math | |
NUMBER_OF_ITEMS = 3000 # Number of items available to be acutioned | |
AUCTION_DURATION = 7 * 24 * 60 # One week of minutes | |
SIM_TIME = AUCTION_DURATION * 3 # Number of auction durations to run the sim | |
REVENUE = 0 # Running count of revenue | |
env = simpy.Environment() | |
items = simpy.Resource(env, NUMBER_OF_ITEMS) | |
MEDIAN_TIME_TO_LIST = 20 | |
def list(env): | |
for i in range(1, NUMBER_OF_ITEMS + 1): | |
with items.request() as item: | |
timeToCreateListing = int(math.floor(random.lognormvariate(0.2, 0.5) * 10)) | |
print("Current time: %s: Listing item %d taking %d minutes to list." % (format_time(env.now), i, timeToCreateListing)) | |
yield env.timeout(timeToCreateListing) | |
env.process(sell(env, i)) | |
def sell(env, item): | |
global REVENUE | |
price = math.floor(random.lognormvariate(2, 0.5)) * 10 | |
yield env.timeout(env.now + AUCTION_DURATION) | |
REVENUE+=price | |
print("Current time: %s: Item %d sold for $%d" % (format_time(env.now), item, price)) | |
print("Current time: %s: Total revenue = $%d" % (format_time(env.now), REVENUE)) | |
## Helper functions | |
def minToDays(m): | |
return math.floor(m / (60 * 24)) | |
## These functions have confusing names. They return the current hour | |
## or minute of the day, independent of the day.. | |
def minToHours(m): | |
return math.floor(m / 60) % 24 | |
def minToMin(m): | |
return m % 60 | |
def format_time(m): | |
return "Day %d %02d:%02d" % (minToDays(env.now), minToHours(env.now), minToMin(env.now)) | |
## Begin simulation | |
def main(n, d, t): | |
"""Run simulation with n items for d durtation per auction, for t | |
total time. | |
""" | |
print("\nSimulation starting:\n") | |
env.process(list(env)) | |
env.run(until=t) | |
main(NUMBER_OF_ITEMS, AUCTION_DURATION, SIM_TIME) | |
## Tests | |
assert minToDays(1440) == 1 | |
assert minToHours(1500) == 1 | |
assert minToMin(1501) == 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment