diff --git a/data/AI_data/DecisionTreeExamples/dt_examples b/data/AI_data/DecisionTreeExamples/dt_examples deleted file mode 100644 index e69de29..0000000 diff --git a/data/AI_data/dt_exmpls/dt_examples b/data/AI_data/dt_exmpls/dt_examples new file mode 100644 index 0000000..56d9241 --- /dev/null +++ b/data/AI_data/dt_exmpls/dt_examples @@ -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 diff --git a/src/AI/DecisionTrees/projectSpecificClasses/Examples.py b/src/AI/DecisionTrees/projectSpecificClasses/Examples.py deleted file mode 100644 index cca013b..0000000 --- a/src/AI/DecisionTrees/projectSpecificClasses/Examples.py +++ /dev/null @@ -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 = [ -] \ No newline at end of file diff --git a/src/AI/GA_With_DT.py b/src/AI/GA_With_DT.py index 52a1e63..26cd1b6 100644 --- a/src/AI/GA_With_DT.py +++ b/src/AI/GA_With_DT.py @@ -4,7 +4,6 @@ from datetime import datetime import numpy import src.AI.DecisionTrees.InductiveDecisionTreeLearning as DT -import src.AI.DecisionTrees.projectSpecificClasses.Examples as Examples from src.AI.Affinities import Affinities from src.AI.DecisionTrees.DecisionTree import DecisionTree from src.AI.DecisionTrees.projectSpecificClasses.SurvivalAttributesDefinitions import \ @@ -14,17 +13,18 @@ from src.AI.SurvivalDT import SurvivalDT 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. + :param decisionTreeExamples: :param map: Map with all entities :param iter: Generations count :param solutions: Solutions per generation :param mutationAmount: Mutation strength """ - survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(Examples.examples, + survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(decisionTreeExamples, AttrDefs.allAttributesDefinitions, SurvivalClassification.FOOD, SurvivalClassification)) diff --git a/src/game/Game.py b/src/game/Game.py index a165085..f6766fc 100644 --- a/src/game/Game.py +++ b/src/game/Game.py @@ -1,11 +1,13 @@ import json from os import path from pathlib import Path +import os import pygame from src.AI.Affinities import Affinities 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.GA import geneticAlgorithm 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.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 @@ -60,6 +61,7 @@ class Game: exit(1) elif argv[1] == "test": self.testRun(filesPath) + # Decision tree elif argv[1] == "dt": if len(argv) >= 3: if argv[2] == "-p": @@ -68,6 +70,7 @@ class Game: else: print("Running Decision Tree.") self.dtRun(filesPath) + # Genetic algorithm 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]) @@ -75,12 +78,19 @@ class Game: else: print("Running Genetic Algorithm in singlethreaded mode, iter = ", argv[2]) self.gaRun(filesPath, int(argv[2])) + # Genetic algorithm with decision tree elif argv[1] == "ga_dt" and len(argv) >= 3: print("Running Genetic Algorithm with Decision Tree, iter = ", 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: - print("Invalid gamemode. \n Possible options: test, ga") + print("Invalid game mode. \n Possible options: test, ga") exit(1) def initializePygame(self): @@ -230,8 +240,14 @@ class Game: print("The screen cannot be in a vertical orientation. Exiting...") 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 - survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(Examples.examples, + survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(examples, AttrDefs.allAttributesDefinitions, SurvivalClassification.FOOD, SurvivalClassification)) @@ -327,6 +343,12 @@ class Game: # Run GA: 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)