Add decision tree run game mode

This commit is contained in:
Michał Czekański 2020-05-24 13:48:05 +02:00
parent 7d12c9f59e
commit 5f7cd0dce7

View File

@ -5,12 +5,19 @@ from pathlib import Path
import pygame
from src.AI.Affinities import Affinities
from src.AI.DecisionTrees.DecisionTree import DecisionTree
from src.AI.DecisionTrees.projectSpecificClasses.SurvivalClassification import SurvivalClassification
from src.AI.GA import geneticAlgorithm
from src.entities.Player import Player
from src.game.EventManager import EventManager
from src.game.Map import Map
from src.game.Screen import Screen, Locations
from src.game.Screen import Screen
from src.game.Timer import Timer
import src.AI.DecisionTrees.InductiveDecisionTreeLearning as DT
import src.AI.DecisionTrees.projectSpecificClasses.Examples as Examples
from src.AI.DecisionTrees.projectSpecificClasses.SurvivalAttributesDefinitions import \
SurvivalAttributesDefinitions as AttrDefs
from src.AI.SurvivalDT import SurvivalDT
# Main Game class
@ -52,6 +59,8 @@ class Game:
exit(1)
elif argv[1] == "test":
self.testRun(filesPath)
elif argv[1] == "dt":
self.dtRun(filesPath)
elif argv[1] == "ga" and len(argv) >= 3:
if len(argv) >= 4 and argv[3] == "-t":
print("Running Genetic Algorithm in multithreaded mode, iter = ", argv[2])
@ -193,3 +202,78 @@ class Game:
# Flip the display
pygame.display.flip()
def dtRun(self, filesPath):
"""
Runs game in decision tree mode.
In this mode user can only watch how player performs decisions with usage of decision tree.
:param filesPath:
"""
self.running = True
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ")
# Vertical rotation is unsupported due to UI layout
if self.config["window"]["height"] > self.config["window"]["width"]:
print("The screen cannot be in a vertical orientation. Exiting...")
exit(1)
# Create decision tree
survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(Examples.examples,
AttrDefs.allAttributesDefinitions,
SurvivalClassification.FOOD,
SurvivalClassification))
print("\nDecision tree: \n")
DecisionTree.printTree(survivalDecisionTree.entityPickingDecisionTree, 0)
print()
# Initialize timers
# Virtual timer to track in-game time
self.ingameTimer = Timer()
self.ingameTimer.startClock()
# Initialize screen
self.screen = Screen(self, self.config["window"])
print("OK")
self.initializeMap(filesPath)
# Initialize the player
self.player = Player((6, 2), self.map.tileSize, Affinities(0.3, 0.6, 0.1, 0.5))
self.map.addEntity(self.player, DONTADD=True)
# main loop without user input
while self.running:
# Tick the timers
self.ingameTimer.updateTime(self.pgTimer.tick())
self.screen.ui.updateTime()
# If player is dead write information to console and break main loop
if not self.player.alive:
self.screen.ui.updateOnDeath(self.player)
self.spritesList.update()
self.spritesList.draw(self.screen.pygameScreen)
pygame.display.flip()
break
# Choose target for player using decision tree
if self.player.movementTarget is None:
self.player.gotoToTarget(survivalDecisionTree.pickEntity(self.player, self.map), self.map)
self.screen.ui.updateBarsBasedOnPlayerStats(self.player.statistics)
# Call update() method for each entity
self.spritesList.update()
# Draw all sprites
self.spritesList.draw(self.screen.pygameScreen)
# Flip the display
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)