mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(ai): add genome evaluation function
This commit is contained in:
parent
858d2a98fc
commit
a49f03248f
33
src/ai/evaluation.py
Normal file
33
src/ai/evaluation.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import neat
|
||||||
|
from game import Main
|
||||||
|
|
||||||
|
from .fitness import calculate_fitness
|
||||||
|
from .log import log
|
||||||
|
|
||||||
|
|
||||||
|
def eval_genomes(genomes, config: neat.Config) -> None:
|
||||||
|
app = Main()
|
||||||
|
app.run()
|
||||||
|
for genome_id, genome in genomes:
|
||||||
|
genome.fitness = calculate_fitness(app)
|
||||||
|
net = neat.nn.FeedForwardNetwork.create(genome, config)
|
||||||
|
while not app.game.game_over():
|
||||||
|
output = net.activate(app.game.field)
|
||||||
|
|
||||||
|
decision = output.index(max(output))
|
||||||
|
|
||||||
|
decisions = {
|
||||||
|
0: app.game.move_left,
|
||||||
|
1: app.game.move_right,
|
||||||
|
2: app.game.rotate,
|
||||||
|
3: app.game.rotate_reverse,
|
||||||
|
4: app.game.drop,
|
||||||
|
}
|
||||||
|
|
||||||
|
decisions[decision]()
|
||||||
|
|
||||||
|
genome.fitness = calculate_fitness(app)
|
||||||
|
log.info(
|
||||||
|
f"{genome_id=}\t{genome.fitness=}\t{app.game.score=}\t{app.game.lines=}\t{app.game.level=}"
|
||||||
|
)
|
||||||
|
app.game.restart()
|
||||||
Loading…
Reference in New Issue
Block a user