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.
This commit is contained in:
Michał Czekański 2020-05-24 23:40:13 +02:00
parent 1e4b44558c
commit 939c3f9072
2 changed files with 27 additions and 23 deletions

View File

@ -144,7 +144,7 @@ def doSimulation(weights, map, decisionTree: SurvivalDT):
player.disableMovementTime() player.disableMovementTime()
while player.alive: while player.alive:
if player.movementTarget is None: if player.movementTarget is None:
target = decisionTree.pickEntity(player, map) target = decisionTree.pickEntity(player, map, pickForGa=True)
player.gotoToTarget(target, map) player.gotoToTarget(target, map)
player.update() player.update()
fitness = player.movePoints fitness = player.movePoints

View File

@ -16,10 +16,11 @@ class SurvivalDT:
def __init__(self, entityPickingDecisionTree: DecisionTree): def __init__(self, entityPickingDecisionTree: DecisionTree):
self.entityPickingDecisionTree = entityPickingDecisionTree 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. 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 player: Player object
:param map: Map object :param map: Map object
""" """
@ -65,29 +66,32 @@ class SurvivalDT:
dtRestPlaces, dtRestPlaces,
dtWaters) dtWaters)
# If the choice happens to be the same as the last one pick something else. """
if choice == map.getEntityOnCoord(player.getFacingCoord()): If choice is being made for genetic algorithm then do not allow to pick same entity as before,
if treeDecision == SurvivalClassification.FOOD: because fitness is being calculated by travelled fields, not time being alive.
dtFoods.remove(dtFoods[0]) So player shouldn't be standing and drinking water, but moving from one water field to another.
nearestDtFood = dtFoods[0] """
elif treeDecision == SurvivalClassification.WATER: if pickForGa:
dtWaters.remove(dtWaters[0]) # If the choice happens to be the same as the last one pick something else.
nearestDtWater = dtWaters[0] if choice.interactable == map.getEntityOnCoord(player.getFacingCoord()):
elif treeDecision == SurvivalClassification.REST: if treeDecision == SurvivalClassification.FOOD:
dtRestPlaces.remove(dtRestPlaces[0]) dtFoods.remove(dtFoods[0])
nearestDtRest = dtRestPlaces[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, currentSituation = SurvivalDTExample(None, playerStats.hungerAmount, playerStats.thirstAmount,
playerStats.staminaAmount, playerStats.staminaAmount,
dtFoods[0].dtDistanceFromPlayer, dtWaters[0].dtDistanceFromPlayer, nearestDtFood.dtDistanceFromPlayer, nearestDtWater.dtDistanceFromPlayer,
dtRestPlaces[0].dtDistanceFromPlayer, nearestDtRest.dtDistanceFromPlayer,
nearestDtFood.getDtDistanceFromOtherInteractable(nearestDtWater.interactable)) nearestDtFood.getDtDistanceFromOtherInteractable(nearestDtWater.interactable))
treeDecision, choice = self.__pickEntityAfterTreeDecision__(currentSituation, dtFoods, treeDecision, choice = self.__pickEntityAfterTreeDecision__(currentSituation, dtFoods,
dtRestPlaces, dtWaters) dtRestPlaces, dtWaters)
print("Tree choice: ")
print(choice.getDescription())
return choice.interactable return choice.interactable