From 6969a816168dec9b1b0dc751e4e5828f3eac0dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 24 May 2020 18:48:53 +0200 Subject: [PATCH] Add method generating dt examples --- src/AI/DecisionTrees/ExamplesManager.py | 63 +++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/AI/DecisionTrees/ExamplesManager.py b/src/AI/DecisionTrees/ExamplesManager.py index cb5c463..4bc44c1 100644 --- a/src/AI/DecisionTrees/ExamplesManager.py +++ b/src/AI/DecisionTrees/ExamplesManager.py @@ -1,3 +1,4 @@ +import random from typing import Any, Union, List from src.AI.DecisionTrees.projectSpecificClasses.DistFromObject import DistFromObject @@ -144,3 +145,65 @@ class ExamplesManager: example.distFromRestPlace.name) file.write(strToWrite) file.close() + + def generateExamples(self): + # retrieve list of examples that are currently saved in file - they will be needed to check for duplicates + examplesThatAreInFile = self.readExamples() + + # make list of all possible values + statsValues = [value for value in PlayerStatsValue] + distances = [value for value in DistFromObject] + + while True: + + howManyExamplesGenerate = int(input("How many example do you want to generate? : ")) + + for i in range(howManyExamplesGenerate): + + print("Generating example {} ...".format(str(i + 1))) + + # pick random values from each list and make survival example out of them, with None classification + newExample = SurvivalDTExample(None, + random.choice(statsValues), + random.choice(statsValues), + random.choice(statsValues), + random.choice(distances), + random.choice(distances), + random.choice(distances)) + + # check if made example is not a duplicate + isDuplicate = False + + possibleDuplicate: SurvivalDTExample + for possibleDuplicate in examplesThatAreInFile: + if possibleDuplicate.compareAttributes(newExample): + isDuplicate = True + break + + if isDuplicate: + # return to create new example + print("Generated duplicate.") + i -= 1 + continue + + # if not a duplicate then ask for classification + print("Generated example: \n{}".format(newExample.getDescription())) + + givenClsIsGood = False + while not givenClsIsGood: + strClassification = input("What is classification of this example? : ") + for classification in SurvivalClassification: + if classification.name == strClassification: + givenClsIsGood = True + break + if not givenClsIsGood: + print("There is no such classification. Possible classifications: {}". + format(str([cls.name for cls in SurvivalClassification]))) + + # set example's classification and append it to examples file. + newExample.classification = classification + self.addExamplesToFile([newExample]) + + keepGenerating = input("Type 'E' to stop generating or anything else to continue") + if keepGenerating == "E": + break