Skip to content

Instantly share code, notes, and snippets.

@Scoppio
Created May 4, 2016 02:45
Show Gist options
  • Save Scoppio/2d5cf12311239ca9807f643ef21b88d1 to your computer and use it in GitHub Desktop.
Save Scoppio/2d5cf12311239ca9807f643ef21b88d1 to your computer and use it in GitHub Desktop.
A simple gradient descent devised to capture the match ID of a game in a specific day, month and year.
def save_Log( e):
with open("logFile.log", "a") as logFile:
timestamp = time.strftime("%d/%m/%y %H:%M:%S", time.localtime())
logFile.write( timestamp + " " +str(e) + "\n")
def findFirstMatch(region: str='br',
api_key: str='api_key.json',
verbosity: bool=False,
year: int=2016,
month: int=1,
day: int=1,
hour: int=0,
minute: int=0,
seconds: int=0,
seed_id: int=774365872,
stop_at: int=2000,
alpha: float=0.000004,
threshold: int=43200):
credentials = str
try:
with open(api_key) as json_data:
credentials = json.load(json_data)
except Exception as e:
#error_log (message = "Could not load the api", error=e)
print (e)
c = rc.Client(credentials["apiKey"])
t = datetime.datetime(year, month, day, hour, minute, seconds)
timestamp = time.mktime(t.timetuple())
if verbosity:
print("starting at id",seed_id, "learning rate", alpha)
print("========================")
print("Looking for the first ID match at", t.strftime('%Y-%m-%d %H:%M:%S'))
print("========================")
#starting Gradient Descent!
id_ = seed_id
generations = 0
match = None
while stop_at:
try:
match = c.get_match(id_, True, region)
matchstamp = match["matchCreation"] / 1e3
matchCreation = datetime.datetime.fromtimestamp(matchstamp)
cost =1/2*(timestamp - matchstamp)**2
print ("match:", match["matchId"], matchCreation.strftime('%Y-%m-%d %H:%M:%S'))
if abs(timestamp - matchstamp) < threshold:
print ("Close enough!")
print (generations)
return match["matchId"]
else:
cost =1/2*(timestamp - matchstamp)**2
print ("id = id - alpha*( 1/2*( cost( id))**2 )")
if timestamp < matchstamp:
print ("id = {0} - {1} * ({2})".format(id_, alpha, cost))
id_ = int(id_ - alpha*(1/2*(timestamp - matchstamp)**2))
else:
print ("id = {0} + {1} * ({2})".format(id_, alpha, cost))
id_ = int(id_ + alpha*(1/2*(timestamp - matchstamp)**2))
print ("next id =", id_, "Generation: ", generations)
generations += 1
except Exception as e:
#error_log(message="Wopsy Daisy",e)
id_ -= 7
#print (e)
time.sleep(0.5)
stop_at -= 1
print (generations)
return match["matchId"]
if __name__ == '__main__':
try:
season2015 = findFirstMatch(year=2015, seed_id=453280920, alpha=0.000004, verbosity=True)
season2016 = findFirstMatch(year=2016, seed_id=694384123, alpha=0.000004, verbosity=True)
lastMonth = season2016
log = "End of season2015 " + str(season2016)
save_Log(log)
for month in reversed(range(1,13)):
lastMonth = findFirstMatch(year=2015, month=month, seed_id=lastMonth, alpha=0.000004, stop_at=200, threshold=64800, verbosity=True)
log = "Month:" + str(month) + "Starting ID:" + str(lastMonth)
save_Log(log)
except (KeyboardInterrupt, SystemExit):
print ("Closing process, Good evening")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment