def selection(self, tournament_count=0): if tournament_count > self.MAX_POPULATION: raise ImproperlyConfiguredError, "Tournament size is bigger than population" def get_random(count): play_off = [] for i in xrange(0, count): selected = random.choice(self.population) while selected in play_off: selected = random.choice(self.population) play_off.append(selected) return play_off if tournament_count <= 2: play_off = get_random(2) return play_off[0], play_off[1] if tournament_count == self.MAX_POPULATION: play_off = self.population[:] else: play_off = get_random(tournament_count) mt = tournament.MancalaTournament(play_off) mt.run() return mt.score_board()[:2]
def fitness(self): for cro in self.population: cro.won = [] cro.lost = [] cro.drawn = [] print "Population is: %s " % len(self.population) mt = tournament.MancalaTournament(self.population) mt.run() sb = mt.score_board(method='difference', second='football') self.population_fitness = sb return sb
def crossover(parent1, parent2): # a b c d e f g # A B C D E F G # =a b c D E F G weight_keys = parent1.weights.keys() length = len(weight_keys) childa = engine.players.ConfiguredPlayer( parent1.player_class, settings=parent1.settings, skip_validation=True, ) childb = engine.players.ConfiguredPlayer( parent1.player_class, settings=parent2.settings, skip_validation=True, ) for i in xrange(0, length): weight_name = weight_keys[i] p1_weight = parent1.weights[weight_name] p2_weight = parent2.weights[weight_name] if i <= (length / 2): childa.weights[weight_name] = p1_weight childb.weights[weight_name] = p2_weight else: childa.weights[weight_name] = p2_weight childb.weights[weight_name] = p1_weight play_off = [ childa, childb, ] if parents_can_survive: play_off = [ childa, childb, parent1, parent2, ] mt = tournament.MancalaTournament(play_off) mt.run() return mt.winner()
def test_score_board(self): """ Run through an example tournament and check the score board creation is working properly. """ PLAYER_LIST = [] for i in range(0, 5): cp = ConfiguredPlayer( basic.RandomPlayer, name="R%s" % i, ) PLAYER_LIST.append(cp, ) mt = tournament.MancalaTournament(PLAYER_LIST) mt.run() # TODO: Think of a test to validate the score_board result. # Always different so its hard... The only test could really # check the order but thats more testing the python sort # built in than anything else... sb = mt.score_board()
def meet(parent1, parent2): # 8 6 2 4 8 0 9 # 2 6 6 4 0 4 7 # =6 6 4 4 4 2 8 weight_keys = parent1.weights.keys() length = len(weight_keys) child = engine.players.ConfiguredPlayer( parent1.player_class, settings=parent1.settings, skip_validation=True, ) for i in xrange(0, length): weight_name = weight_keys[i] p1_weight = parent1.weights[weight_name] p2_weight = parent2.weights[weight_name] if not weight_name.endswith('_multi'): meet_val = (p1_weight + p2_weight) / 2 child.weights[weight_name] = meet_val else: new_multi = [] for j in xrange(0, len(p1_weight)): new_multi.append((p1_weight[j] + p2_weight[j]) / 2) child.weights[weight_name] = new_multi if parents_can_survive: play_off = [ child, parent1, parent2, ] mt = tournament.MancalaTournament(play_off) mt.run() return mt.winner() else: return child
def test_minimax_depth(self): """ A tournament of minimax players with the same weights but different depths - should give a fairly good test as to how the depth of the search tree effects the result. """ number_of_each = 1 max_depth = 4 PLAYER_LIST = [] random_bases = [] for x in xrange(0,number_of_each): random_bases.append(basic.MiniMaxPlayer.objects.random(name="M2%s" %x, settings={'max_depth':1,})) random_bases.append(basic.MiniMaxPlayer.objects.random(name="M2%s" %x, settings={'max_depth':1,})) for y in xrange(1,max_depth+1): for i in xrange(0,number_of_each): PLAYER_LIST.append(basic.MiniMaxPlayer.objects.random(name="M%s%s" %(y,i), weights=random_bases[i].weights, settings={'max_depth':y,})) mt = tournament.MancalaTournament(PLAYER_LIST) mt.run() sb = mt.score_board()
'max_depth': 3, } weights = weights = { 'scrape': 100.0, 'store': 10.0, 'score': 0.5, 'stance': 0.0, 'pits_multi': [10.0, 9.0, 8.0, 7.0, 6.0, 5.0], } cro = players.ConfiguredPlayer(basic.MiniMaxPlayer, weights, settings=settings, name="MMCC") player_list.append(cro) t = tournament.MancalaTournament(player_list) r = t.run(yield_length=True) l = list(r) print sum([len(i) for i in l]) / len(l) print max([len(i) for i in l]) print min([len(i) for i in l]) games = l[0] for game in games: print game.board import time time.sleep(10)
random_bases.append( basic.MiniMaxPlayer.objects.random(name="M2%s" % x, settings={ 'max_depth': 1, })) for i in range(100): r = players.ConfiguredPlayer( basic.RandomPlayer, name="R %s" % i, ) PLAYER_LIST.append(r) for y in range(1, max_depth + 1): for i in range(0, number_of_each): mm = basic.MiniMaxPlayer.objects.random( name="M%s%s" % (y, i), weights=random_bases[i].weights, settings={ 'max_depth': y, }) PLAYER_LIST.append(mm) mt = tournament.MancalaTournament(PLAYER_LIST) mt.VERBOSE = True mt.run() sb = mt.score_board() for c in sb: print c