From 939c3f907211e44d24df411abbf7e44d9f1a6a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 24 May 2020 23:40:13 +0200 Subject: [PATCH] Add entity picking being adjustable for pure dt run and ga with dt run In pure dt mode player can choose the same object like water any times in a row. In dt with ga mode player can't choose same object two times in a row. --- src/AI/GA_With_DT.py | 2 +- src/AI/SurvivalDT.py | 48 ++++++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/AI/GA_With_DT.py b/src/AI/GA_With_DT.py index 26cd1b6..3c76149 100644 --- a/src/AI/GA_With_DT.py +++ b/src/AI/GA_With_DT.py @@ -144,7 +144,7 @@ def doSimulation(weights, map, decisionTree: SurvivalDT): player.disableMovementTime() while player.alive: if player.movementTarget is None: - target = decisionTree.pickEntity(player, map) + target = decisionTree.pickEntity(player, map, pickForGa=True) player.gotoToTarget(target, map) player.update() fitness = player.movePoints diff --git a/src/AI/SurvivalDT.py b/src/AI/SurvivalDT.py index 36de565..9d7ad70 100644 --- a/src/AI/SurvivalDT.py +++ b/src/AI/SurvivalDT.py @@ -16,10 +16,11 @@ class SurvivalDT: def __init__(self, entityPickingDecisionTree: DecisionTree): self.entityPickingDecisionTree = entityPickingDecisionTree - def pickEntity(self, player, map): + def pickEntity(self, player, map, pickForGa=False): """ Select an entity to become the next goal for the player. + :param pickForGa: If picking is done for genetic algorithm then pick can't be the same as last. :param player: Player object :param map: Map object """ @@ -65,29 +66,32 @@ class SurvivalDT: dtRestPlaces, dtWaters) - # If the choice happens to be the same as the last one pick something else. - if choice == map.getEntityOnCoord(player.getFacingCoord()): - if treeDecision == SurvivalClassification.FOOD: - dtFoods.remove(dtFoods[0]) - nearestDtFood = dtFoods[0] - elif treeDecision == SurvivalClassification.WATER: - dtWaters.remove(dtWaters[0]) - nearestDtWater = dtWaters[0] - elif treeDecision == SurvivalClassification.REST: - dtRestPlaces.remove(dtRestPlaces[0]) - nearestDtRest = dtRestPlaces[0] + """ + If choice is being made for genetic algorithm then do not allow to pick same entity as before, + because fitness is being calculated by travelled fields, not time being alive. + So player shouldn't be standing and drinking water, but moving from one water field to another. + """ + if pickForGa: + # If the choice happens to be the same as the last one pick something else. + if choice.interactable == map.getEntityOnCoord(player.getFacingCoord()): + if treeDecision == SurvivalClassification.FOOD: + dtFoods.remove(dtFoods[0]) + nearestDtFood = dtFoods[0] + elif treeDecision == SurvivalClassification.WATER: + dtWaters.remove(dtWaters[0]) + nearestDtWater = dtWaters[0] + elif treeDecision == SurvivalClassification.REST: + dtRestPlaces.remove(dtRestPlaces[0]) + nearestDtRest = dtRestPlaces[0] - currentSituation = SurvivalDTExample(None, playerStats.hungerAmount, playerStats.thirstAmount, - playerStats.staminaAmount, - dtFoods[0].dtDistanceFromPlayer, dtWaters[0].dtDistanceFromPlayer, - dtRestPlaces[0].dtDistanceFromPlayer, - nearestDtFood.getDtDistanceFromOtherInteractable(nearestDtWater.interactable)) + currentSituation = SurvivalDTExample(None, playerStats.hungerAmount, playerStats.thirstAmount, + playerStats.staminaAmount, + nearestDtFood.dtDistanceFromPlayer, nearestDtWater.dtDistanceFromPlayer, + nearestDtRest.dtDistanceFromPlayer, + nearestDtFood.getDtDistanceFromOtherInteractable(nearestDtWater.interactable)) - treeDecision, choice = self.__pickEntityAfterTreeDecision__(currentSituation, dtFoods, - dtRestPlaces, dtWaters) - - print("Tree choice: ") - print(choice.getDescription()) + treeDecision, choice = self.__pickEntityAfterTreeDecision__(currentSituation, dtFoods, + dtRestPlaces, dtWaters) return choice.interactable