def CreateOcclusions(self, value, agentCoord, goalCoord):
        occlusionCoords = []
        if value == 0:
            return occlusionCoords

        areaSizes = []
        while True:
            limit = value
            if value > 10:
                limit = 10
            size = Random(1, limit)
            if size + np.sum(np.asarray(areaSizes)) <= value:
                areaSizes.append(size)
            if np.sum(np.asanyarray(areaSizes)) == value:
                break

        allOcclusionCoords = []
        for area in areaSizes:
            triedCoordinates = []
            while True:
                areaOcclusionCoords = []
                while True:
                    startCoord = COORD(Random(0, self.XSize),
                                       Random(0, self.YSize))
                    if self.ValidOcclusion(
                            startCoord, agentCoord, goalCoord,
                            allOcclusionCoords
                    ) and startCoord not in triedCoordinates:
                        break

                areaOcclusionCoords.append(startCoord)
                n = 0
                for i in range(area - 1):
                    if n > 100:
                        break
                    n = 0
                    while True:
                        if n > 100:
                            break
                        adjacentCell = areaOcclusionCoords[-1] + Compass[
                            Random(0, 4)]
                        if self.Inside(adjacentCell) and self.ValidOcclusion(
                                adjacentCell, agentCoord, goalCoord,
                                allOcclusionCoords):
                            areaOcclusionCoords.append(adjacentCell)
                            break
                        n += 1
                if n > 100:
                    triedCoordinates.append(startCoord)
                    continue
                allOcclusionCoords.extend(areaOcclusionCoords)
                break

        return allOcclusionCoords
    def CreateRandomOcclusions(self, value, agentCoord, goalCoord):
        allOcclusionCoords = []
        for i in range(value):
            while True:
                coord = COORD(Random(0, self.XSize), Random(0, self.YSize))
                if self.ValidOcclusion(coord, agentCoord, goalCoord,
                                       allOcclusionCoords):
                    break
            allOcclusionCoords.append(coord)

        return allOcclusionCoords
Beispiel #3
0
    def SelectRandom(self, state, history, status):
        if self.Knowledge.RolloutLevel >= MOVES.SMART:
            actions = []
            actions = self.GeneratePreferred(state, history, actions, status)
            if actions:
                return actions[Random(0, len(actions))]
        if self.Knowledge.RolloutLevel >= MOVES.LEGAL:
            actions = []
            actions = self.GenerateLegal(state, history, actions, status)
            if actions:
                return actions[Random(0, len(actions))]

        return Random(0, self.NumActions)
    def PredatorAgentPosPropogation(self, state):
        copyState = self.Copy(state)

        N2 = 15
        N1 = 15

        allPossibleAgentNewPositions = [copyState.PredatorBeliefState[0]]
        testedAgentPositions = []
        for n1 in range(N1):
            try:
                agentPosition = copyState.PredatorBeliefState[Random(0, len(copyState.PredatorBeliefState))]
            except IndexError:
                break
            propogateState = self.Copy(copyState)
            propogateState.AgentPos = agentPosition
            if self.PredatorObservation(propogateState):
                continue
            newAgentPositions = []
            for n2 in range(N2):
                for action in range(self.NumActions):
                    newAgentPosition = self.NextPos(propogateState.AgentPos, action)
                    if self.Valid(newAgentPosition) and newAgentPosition != copyState.PredatorPos:
                        newAgentPositions.append(newAgentPosition)
            if newAgentPositions:
                allPossibleAgentNewPositions.extend(newAgentPositions)

        for agentCoord in copyState.PredatorBeliefState:
            if agentCoord not in testedAgentPositions:
                allPossibleAgentNewPositions.append(agentCoord)

        return allPossibleAgentNewPositions
Beispiel #5
0
    def MovePredator(self, state, move, previousPredatorLocation):
        numberOfMoves = 1
        randomMove = True
        believedState = self.Copy(state)

        if move:
            numberOfMoves = state.PredatorSpeedMult

        if state.PredatorBeliefState:
            try:
                believedAgentPosition = state.PredatorBeliefState[0]
                randomMove = False
            except IndexError:
                numberOfMoves = 1
                pass

            if len(state.PredatorBeliefState) > 1:
                believedAgentPosition = state.PredatorBeliefState[Random(
                    0, len(state.PredatorBeliefState))]
                numberOfMoves = 1
                randomMove = False
            believedState.AgentPos = believedAgentPosition

        for i in range(numberOfMoves):
            if Bernoulli(self.ChaseProbability) or i > 0 and not randomMove:
                believedState = self.MovePredatorAggressive(
                    believedState, previousPredatorLocation)
            else:
                believedState = self.MovePredatorRandom(believedState)

            state.PredatorPos = believedState.PredatorPos
            if state.AgentPos == state.PredatorPos:
                return state, (state.AgentPos == state.PredatorPos)

        return state, (state.AgentPos == state.PredatorPos)
Beispiel #6
0
    def CreateRandomStartState(self):
        state = GameState()
        state = self.NewLevel(state)
        self.GetValidPredatorLocations()

        predLocation = (state.PredatorPos).Copy()
        if self.PredatorObservation(state):
            state.PredatorBeliefState = [state.AgentPos]
        else:
            allAgentLocations = [
                COORD(x, y) for x in range(self.XSize)
                for y in range(self.YSize)
            ]
            validAgentLocations = list(
                set(allAgentLocations) - set(self.Occlusions))
            invisibleAgentLocations = [
                coord for coord in validAgentLocations
                if self.Grid.VisualRay(coord, predLocation, self.Occlusions)
            ]
            state.PredatorBeliefState = invisibleAgentLocations

        agentObservation = np.zeros(self.NumActions)
        for scan in range(self.NumActions):
            agentObservation[scan] = self.MakeObservation(state, scan)
        if agentObservation.any():
            return state

        state.PredatorPos = self.StartPredatorLocations[Random(
            0, len(self.StartPredatorLocations))]
        return state
Beispiel #7
0
 def LocalMove(self, state, history, stepObs, status):
     allPredatorLocations = [
         COORD(x, y) for x in range(self.XSize) for y in range(self.YSize)
     ]
     possiblePredatorLocations = list(
         set(allPredatorLocations) - set(self.Occlusions)
     )  #Remove occlusions from possible predator location list
     state.PredatorPos = possiblePredatorLocations[Random(
         0, len(possiblePredatorLocations))]
     #state.PredatorPos = COORD(Random(0, self.Grid.GetXSize()),
     #                          Random(0, self.Grid.GetYSize()))
     if history.Size() == 0:
         return True, state
     observation = self.MakeObservation(state, state.AgentDir)
     return history.Back().Observation == observation
Beispiel #8
0
    def GreedyUCB(self, vnode, ucb, softmax=False):
        besta = []
        bestq = -Infinity
        beta = 1.0 / 3.0

        N = vnode.Value.GetCount()
        logN = np.log(N + 1)

        qValues = []
        for action in range(self.Simulator.NumActions):
            qnode = vnode.Child(action)
            if qnode:
                q = qnode.Value.GetValue()
                n = qnode.Value.GetCount()

                if SearchParams.UseRave and qnode.AMAF.GetCount() > 0:
                    n2 = qnode.AMAF.GetCount()
                    beta = n2 / (n + n2 + SearchParams.RaveConstant * n * n2)
                    q = (1.0 - beta) * q + beta * qnode.AMAF.GetValue()

                if ucb:
                    q += self.FastUCB(N, n, logN)

                if q >= bestq:
                    if q > bestq:
                        besta = []
                    bestq = q
                    besta.append(action)

                qValues.append(q)
        assert (besta)

        if softmax:
            tempQ = []
            indices = []
            for i, qValue in enumerate(qValues):
                if qValue > -1 * LargeInteger:
                    tempQ.append(qValue)
                    indices.append(i)

            qValues = np.array(tempQ, dtype=np.float64)
            logsoftmax = qValues - np.log(
                np.sum(np.exp(qValues * beta), axis=0))
            besta = [indices[np.argmax(logsoftmax, axis=0)]]

        return besta[Random(0, len(besta))]
    def MovePredatorRandom(self, state, previousPredatorLocation):
        copyState = self.Copy(state)
        numActions = 4
        predatorPos = copyState.PredatorPos
        testedActions = []
        while True:
            action = Random(0, numActions)
            testedActions.append(action)
            newpos = self.NextPos(predatorPos, action)
            if newpos.Valid() and newpos != previousPredatorLocation:
                break
            if set(testedActions) == set(range(self.NumActions)):
                newpos = predatorPos
                break

        copyState.PredatorPos = newpos
        copyState.PredatorDir = action
        return copyState
    def MovePredatorRandom(self, state):
        copyState = self.Copy(state)
        predatorPos = copyState.PredatorPos
        testedActions = []
        while True:
            action = Random(0, 4)
            testedActions.append(action)
            newpos = self.NextPos(predatorPos, action)
            if self.Valid(newpos):
                break

            if set(testedActions) == {0, 1, 2, 3}:
                newpos = copyState.PredatorPos
                break

        copyState.PredatorPos = newpos
        copyState.PredatorDir = action
        return copyState
    def MovePredator(self, state, move, previousPredatorLocation):
        numberOfMoves = 1

        observation = self.PredatorObservation(self.Copy(state))
        try:
            believedAgentPosition = state.PredatorBeliefState[0]
        except IndexError:
            if observation:
                believedAgentPosition = (state.AgentPos).Copy()
                state.PredatorBeliefState = [(state.AgentPos).Copy()]
            else:
                allAgentLocations = [COORD(x, y) for x in range(self.XSize) for y in range(self.YSize)]
                invisibleAgentLocations = [coord for coord in allAgentLocations if
                                           self.Grid.VisualRay(coord, (state.PredatorPos).Copy(), self.Occlusions)]
                validAgentLocations = list(set(invisibleAgentLocations) - set(self.Occlusions))
                state.PredatorBeliefState = validAgentLocations

        if len(state.PredatorBeliefState) > 1:
            believedAgentPosition = state.PredatorBeliefState[Random(0, len(state.PredatorBeliefState))]
            numberOfMoves = 1

        if move:
            numberOfMoves = state.PredatorSpeedMult

        believedState = self.Copy(state)
        believedState.AgentPos = believedAgentPosition
        believedState.AgentPos = (state.AgentPos).Copy()

        for i in range(numberOfMoves):
            if Bernoulli(self.ChaseProbability) or (i > 0 and len(self.Occlusions) > 15):
                believedState = self.MovePredatorAggressive(believedState, previousPredatorLocation)
            else:
                believedState = self.MovePredatorRandom(believedState)

            state.PredatorPos = believedState.PredatorPos
            if state.AgentPos == state.PredatorPos:
                return state, (state.AgentPos == state.PredatorPos)

        return state, (state.AgentPos == state.PredatorPos)
Beispiel #12
0
from utils import Random, Drawer, crand

generator = Random(0, 20, generator=crand)
drawer = Drawer(
    generator,
    sliders=[
        {
            "label": "N",
            "valmin": 10,
            "valmax": 50_000,
            "valinit": 500,
            "valstep": 100,
        },
    ],
    title="Uniform distribution",
)
drawer.draw_distribution(n=500)
Beispiel #13
0
 def CreateSample(self, simulator):
     index = Random(0, len(self.Samples))
     return simulator.Copy(self.Samples[index])
Beispiel #14
0
                                numOcclusions, AgentHome, GoalPos)
                            numOcclusions += 1

                        path = GetPath(occlusions)
                        if path:
                            occlusionList[simulationInd][i] = occlusions
                            break

                    allPredatorLocations = Grid(XSize,
                                                YSize).CreatePredatorLocations(
                                                    ExperimentParams.SpawnArea,
                                                    AgentHome, GoalPos,
                                                    occlusions)

                    temp_predator = [
                        allPredatorLocations[Random(0,
                                                    len(allPredatorLocations))]
                        for p in range(ExperimentParams.NumPredators)
                    ]
                    predatorList[simulationInd][i] = temp_predator

            with open('init_vars.pkl', 'wb') as f:
                pickle.dump([occlusionList, predatorList], f)

        with open('init_vars.pkl', 'rb') as f:
            occlusionList, predatorList = pickle.load(f)

        tasks = []
        for simulationInd in range(ExperimentParams.NumRuns):
            for occlusionInd in range(len(ExperimentParams.EntropyLevels)):
                for predatorInd in range(ExperimentParams.NumPredators):
                    if visualRange: