Add game mode for generating dt examples and delete unnecessary class

This class was storing dt examples. Now they are generated to file and read from it to be used.
This commit is contained in:
Michał Czekański 2020-05-24 19:19:56 +02:00
parent 3f2e5550c7
commit 34eb74d980
5 changed files with 35 additions and 15 deletions

View File

@ -0,0 +1,5 @@
WATER|ZERO_TO_QUARTER|HALF_TO_THREE_QUARTERS|ZERO_TO_QUARTER|GE_3_LT_8|LT_3|GE_3_LT_8
FOOD|ZERO_TO_QUARTER|QUARTER_TO_HALF|THREE_QUARTERS_TO_FULL|GE_15|GE_8_LT_15|GE_15
WATER|ZERO_TO_QUARTER|QUARTER_TO_HALF|QUARTER_TO_HALF|LT_3|GE_3_LT_8|GE_15
FOOD|HALF_TO_THREE_QUARTERS|ZERO_TO_QUARTER|QUARTER_TO_HALF|LT_3|LT_3|GE_8_LT_15
WATER|QUARTER_TO_HALF|THREE_QUARTERS_TO_FULL|QUARTER_TO_HALF|GE_8_LT_15|LT_3|GE_15

View File

@ -1,7 +0,0 @@
from src.AI.DecisionTrees.projectSpecificClasses.SurvivalClassification import SurvivalClassification
from src.AI.DecisionTrees.projectSpecificClasses.SurvivalDTExample import SurvivalDTExample
from src.AI.DecisionTrees.projectSpecificClasses.PlayerStatsValue import PlayerStatsValue as Stats
from src.AI.DecisionTrees.projectSpecificClasses.DistFromObject import DistFromObject as Dist
examples = [
]

View File

@ -4,7 +4,6 @@ from datetime import datetime
import numpy import numpy
import src.AI.DecisionTrees.InductiveDecisionTreeLearning as DT import src.AI.DecisionTrees.InductiveDecisionTreeLearning as DT
import src.AI.DecisionTrees.projectSpecificClasses.Examples as Examples
from src.AI.Affinities import Affinities from src.AI.Affinities import Affinities
from src.AI.DecisionTrees.DecisionTree import DecisionTree from src.AI.DecisionTrees.DecisionTree import DecisionTree
from src.AI.DecisionTrees.projectSpecificClasses.SurvivalAttributesDefinitions import \ from src.AI.DecisionTrees.projectSpecificClasses.SurvivalAttributesDefinitions import \
@ -14,17 +13,18 @@ from src.AI.SurvivalDT import SurvivalDT
from src.entities.Player import Player from src.entities.Player import Player
def geneticAlgorithmWithDecisionTree(map, iter, solutions, mutationAmount=0.05): def geneticAlgorithmWithDecisionTree(map, iter, solutions, decisionTreeExamples, mutationAmount=0.05):
""" """
This is fusion of genetic algorithm and decision tree. Decision tree is giving travel goals for player. This is fusion of genetic algorithm and decision tree. Decision tree is giving travel goals for player.
:param decisionTreeExamples:
:param map: Map with all entities :param map: Map with all entities
:param iter: Generations count :param iter: Generations count
:param solutions: Solutions per generation :param solutions: Solutions per generation
:param mutationAmount: Mutation strength :param mutationAmount: Mutation strength
""" """
survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(Examples.examples, survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(decisionTreeExamples,
AttrDefs.allAttributesDefinitions, AttrDefs.allAttributesDefinitions,
SurvivalClassification.FOOD, SurvivalClassification.FOOD,
SurvivalClassification)) SurvivalClassification))

View File

@ -1,11 +1,13 @@
import json import json
from os import path from os import path
from pathlib import Path from pathlib import Path
import os
import pygame import pygame
from src.AI.Affinities import Affinities from src.AI.Affinities import Affinities
from src.AI.DecisionTrees.DecisionTree import DecisionTree from src.AI.DecisionTrees.DecisionTree import DecisionTree
from src.AI.DecisionTrees.ExamplesManager import ExamplesManager
from src.AI.DecisionTrees.projectSpecificClasses.SurvivalClassification import SurvivalClassification from src.AI.DecisionTrees.projectSpecificClasses.SurvivalClassification import SurvivalClassification
from src.AI.GA import geneticAlgorithm from src.AI.GA import geneticAlgorithm
from src.entities.Player import Player from src.entities.Player import Player
@ -14,7 +16,6 @@ from src.game.Map import Map
from src.game.Screen import Screen from src.game.Screen import Screen
from src.game.Timer import Timer from src.game.Timer import Timer
import src.AI.DecisionTrees.InductiveDecisionTreeLearning as DT import src.AI.DecisionTrees.InductiveDecisionTreeLearning as DT
import src.AI.DecisionTrees.projectSpecificClasses.Examples as Examples
from src.AI.DecisionTrees.projectSpecificClasses.SurvivalAttributesDefinitions import \ from src.AI.DecisionTrees.projectSpecificClasses.SurvivalAttributesDefinitions import \
SurvivalAttributesDefinitions as AttrDefs SurvivalAttributesDefinitions as AttrDefs
from src.AI.SurvivalDT import SurvivalDT from src.AI.SurvivalDT import SurvivalDT
@ -60,6 +61,7 @@ class Game:
exit(1) exit(1)
elif argv[1] == "test": elif argv[1] == "test":
self.testRun(filesPath) self.testRun(filesPath)
# Decision tree
elif argv[1] == "dt": elif argv[1] == "dt":
if len(argv) >= 3: if len(argv) >= 3:
if argv[2] == "-p": if argv[2] == "-p":
@ -68,6 +70,7 @@ class Game:
else: else:
print("Running Decision Tree.") print("Running Decision Tree.")
self.dtRun(filesPath) self.dtRun(filesPath)
# Genetic algorithm
elif argv[1] == "ga" and len(argv) >= 3: elif argv[1] == "ga" and len(argv) >= 3:
if len(argv) >= 4 and argv[3] == "-t": if len(argv) >= 4 and argv[3] == "-t":
print("Running Genetic Algorithm in multithreaded mode, iter = ", argv[2]) print("Running Genetic Algorithm in multithreaded mode, iter = ", argv[2])
@ -75,10 +78,17 @@ class Game:
else: else:
print("Running Genetic Algorithm in singlethreaded mode, iter = ", argv[2]) print("Running Genetic Algorithm in singlethreaded mode, iter = ", argv[2])
self.gaRun(filesPath, int(argv[2])) self.gaRun(filesPath, int(argv[2]))
# Genetic algorithm with decision tree
elif argv[1] == "ga_dt" and len(argv) >= 3: elif argv[1] == "ga_dt" and len(argv) >= 3:
print("Running Genetic Algorithm with Decision Tree, iter = ", argv[2]) print("Running Genetic Algorithm with Decision Tree, iter = ", argv[2])
self.gaDTRun(filesPath, int(argv[2])) self.gaDTRun(filesPath, int(argv[2]))
# Generating examples for decision tree
elif argv[1] == "g_e_dt":
print("Running in mode generating examples for decision tree.")
examplesFilePath = str(filesPath) + os.sep + "data" + os.sep + "AI_data" + os.sep + "dt_exmpls" + os.sep + "dt_examples"
dtExampleManager = ExamplesManager(examplesFilePath)
dtExampleManager.generateExamples()
# Invalid game mode
else: else:
print("Invalid game mode. \n Possible options: test, ga") print("Invalid game mode. \n Possible options: test, ga")
exit(1) exit(1)
@ -230,8 +240,14 @@ class Game:
print("The screen cannot be in a vertical orientation. Exiting...") print("The screen cannot be in a vertical orientation. Exiting...")
exit(1) exit(1)
# Read examples to decision tree learning
examplesFilePath = str(
filesPath) + os.sep + "data" + os.sep + "AI_data" + os.sep + "dt_exmpls" + os.sep + "dt_examples"
examplesManager = ExamplesManager(examplesFilePath)
examples = examplesManager.readExamples()
# Create decision tree # Create decision tree
survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(Examples.examples, survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(examples,
AttrDefs.allAttributesDefinitions, AttrDefs.allAttributesDefinitions,
SurvivalClassification.FOOD, SurvivalClassification.FOOD,
SurvivalClassification)) SurvivalClassification))
@ -327,6 +343,12 @@ class Game:
# Run GA: # Run GA:
self.pgTimer.tick() self.pgTimer.tick()
geneticAlgorithmWithDecisionTree(self.map, iter, 10, 0.1)
examplesFilePath = str(
filesPath) + os.sep + "data" + os.sep + "AI_data" + os.sep + "dt_exmpls" + os.sep + "dt_examples"
dtExamplesManager = ExamplesManager(examplesFilePath)
dtExamples = dtExamplesManager.readExamples()
geneticAlgorithmWithDecisionTree(self.map, iter, 10, dtExamples, 0.1)
print("Time elapsed: ", self.pgTimer.tick() // 1000) print("Time elapsed: ", self.pgTimer.tick() // 1000)