Skip to content

Instantly share code, notes, and snippets.

@foenix
Created June 25, 2010 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foenix/452259 to your computer and use it in GitHub Desktop.
Save foenix/452259 to your computer and use it in GitHub Desktop.
'''
superagent.py
This agent will always win
'''
from constants import *
import random
import time
## For agent evaluation:
import numpy as np
import matplotlib.pyplot as plt
class superagent(object):
def __init__(self, hscore, agscore, totaltime, timeleft, condition=None):
if condition is not None:
if condition == "easy":
self.hard = False
else:
self.hard = True
self.hscore = hscore
self.agscore = agscore
self.diffscore = agscore - hscore
self.totaltime = totaltime
self.timeleft = timeleft
self.diffscorerate = self.agscorerate - self.hscorerate
self.last_part = int(round(self.totaltime / 20))
self.leafs = self._initial_slot_it()
return None
def update(self, hscore, agscore, timeleft):
self.timespent = self.totaltime - timeleft
self.timeslot += int(round(self.timespent + 1))
print "time slot: %s" % self.timeslot
self.hscorerate = hscore / self.timespent
self.agscorerate = agscore / self.timespent
if self.agscore > self.hscore:
self.winning = True
else:
self.winning = False
self.agscore += self.descision()
return self.agscore
def descision(self):
emmit = 1
score = 0
emmit *= self._predict_slot(self.timeslot)
if emmit == 1:
if self.winning:
score += 10
else:
score += 10 * random.randint(0, 5)
return score
def _initial_slot_it(self):
# Each timeframe is on each integer second
timeframes = range(0, int(round(self.totaltime)))
leafs = []
for timeframe in timeframes:
leafs.append(
{
'hscore': self.hscore,\
'hscorerate': self.hscorerate,\
'agscore': self.agscore,\
'agscorerate': self.agscorerate,\
'precisetime': self.timespent\
}
)
return leafs
def _predict_slot(self, slot):
emmit = 0
if slot > len(self.leafs):
t = len(self.leafs) - 1
else:
t = slot - 1
##print "Requesting slot %s" % t
self.leafs[t] = \
{
'hscore': self.hscore,\
'hscorerate': self.hscorerate,\
'agscore': self.agscore,\
'agscorerate': self.agscorerate,\
'precisetime': self.timespent\
}
emmit += self._foresight(t+1)
return emmit
def _foresight(self, slot):
emmit = 0
count = 1
for leaf in self.leafs[slot:]:
leaf['hscore'] += self.hscorerate / self.timespent * count
leaf['hscorerate'] = self.hscore / self.timespent * count
leaf['agscore'] = self.agscorerate / self.timespent * count
leaf['agscorerate'] = self.agscore / self.timespent * count
count += 1
emmit += 0.2 * self.timespent
return emmit
if __name__ == "__main__":
ROUND_SEC = 5
start_time = time.time()
hscore = 0
agscore = 0
totaltime = ROUND_SEC
timeleft = 0
easy_agent = superagent(hscore, agscore, totaltime, timeleft, "easy")
hard_agent = superagent(hscore, agscore, totaltime, timeleft)
hscores =[]
agscores = []
total = 0
while True:
timeleft = ROUND_SEC - (time.time() - start_time)
hscores.append(hscore)
agscores.append(agscore)
total += 1
print "time left: %s" % round(timeleft)
if round(timeleft) % 5 == 0.0:
hscore += random.randint(0,1) * random.randint(0,1)
# Out of time
if time.time() - start_time >= ROUND_SEC:
print "done. Human score: %s. Agent score: %s. " % (hscore, agscore)
## Plotting functions
fig = plt.figure()
plt.xlim(0.0, total)
plt.ylim(0.0, max(hscores))
ax = fig.add_subplot(111)
timeax = range(0, total)
agent_scores = zip(timeax, agscores)
human_scores = zip(timeax, hscores)
ax.plot(agent_scores)
ax.plot(human_scores)
plt.show()
break
# Main LOOP
else:
next_score = easy_agent.update(hscore, agscore, timeleft)
agscore += next_score
print "human score: %s . Agent score: %s . " % (hscore, agscore)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment