Add method generating dt examples

This commit is contained in:
Michał Czekański 2020-05-24 18:48:53 +02:00
parent f025df0381
commit 6969a81616

View File

@ -1,3 +1,4 @@
import random
from typing import Any, Union, List from typing import Any, Union, List
from src.AI.DecisionTrees.projectSpecificClasses.DistFromObject import DistFromObject from src.AI.DecisionTrees.projectSpecificClasses.DistFromObject import DistFromObject
@ -144,3 +145,65 @@ class ExamplesManager:
example.distFromRestPlace.name) example.distFromRestPlace.name)
file.write(strToWrite) file.write(strToWrite)
file.close() 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