/
evolver.py
74 lines (59 loc) · 1.97 KB
/
evolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from pyevolve import G1DList
from pyevolve import GSimpleGA
from pyevolve import Selectors
from pyevolve import DBAdapters
import pyevolve
from bots import Pushover, Freeloader, Alternator, MaxRepHunter, Random, FairHunter, BoundedHunter, AverageHunter
from Player import Player
from Game import Game
# Enemy players.
player_list = [
Pushover(),
Freeloader(),
Alternator(),
MaxRepHunter(),
Random(.2),
Random(.8),
FairHunter(),
BoundedHunter(0.4, 0.6),
BoundedHunter(0.6, 1),
AverageHunter()
]
# Fitness evaluation function.
def eval_func(chromosome):
player = Player(chromosome)
game = Game([player] + player_list, verbose=False, quiet=True) # Start the game in quiet mode.
ranking = game.play_game()
score = ranking.index('GA Player')
return score * score
# Enable the pyevolve logging system
pyevolve.logEnable()
# Genome instance, 1D List of 7 elements
genome = G1DList.G1DList(7)
# Sets the range max and min of the 1D List
bestScore = len(player_list) * len(player_list) + 1 # run forever
genome.setParams(rangemin=0, rangemax=100)
# The evaluator function (evaluation function)
genome.evaluator.set(eval_func)
# Genetic Algorithm Instance
ga = GSimpleGA.GSimpleGA(genome)
# Set the Roulette Wheel selector method, the number of generations and
# the termination criteria
ga.selector.set(Selectors.GRouletteWheel)
# GA vars.
ga.setCrossoverRate(1.0)
ga.setMutationRate(0.08)
ga.setPopulationSize(80)
ga.setGenerations(500)
ga.setMultiProcessing(False) # buggy when True
# ga.terminationCriteria.set(GSimpleGA.RawScoreCriteria)
# Sets the DB Adapter, the resetDB flag will make the Adapter recreate
# the database and erase all data every run. Doesn't seem to actually
# use the DB even when False.
sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True)
ga.setDBAdapter(sqlite_adapter)
# Do the evolution, with stats dump
# frequency of 20 generations
ga.evolve(freq_stats=2)
# Best individual
print ga.bestIndividual()