Example #1
0
    public Dictionary <State, float> GetPossibleStatesFromIntent(State currentState, GridWorldIntent gridWorldIntent)
    {
        Dictionary <State, float> possibleStates = new Dictionary <State, float>();

        possibleStates.Add(GetNextState(currentState, gridWorldIntent), 1.0f);
        return(possibleStates);
    }
Example #2
0
    public bool PolicyImprovement()
    {
        bool policyStable = true;

        foreach (var currentState in _allStates)
        {
            if (GameSelector.type == GameSelector.GameType.GridWorld)
            {
                GridWorldIntent tempPolicy = currentState.gridWorldPolicy;
                currentState.gridWorldPolicy = GetBestIntent(currentState);
                if (tempPolicy != currentState.gridWorldPolicy)
                {
                    policyStable = false;
                }
            }
            else if (GameSelector.type == GameSelector.GameType.Sokoban)
            {
                SokobanIntent tempPolicy = currentState.sokobanPolicy;
                currentState.sokobanPolicy = GetBestIntentSokoban(currentState);
                if (tempPolicy != currentState.sokobanPolicy)
                {
                    policyStable = false;
                }
            }
        }

        if (!policyStable)
        {
            PolicyEvaluation();
        }

        return(policyStable);
    }
Example #3
0
    public void InitializePolicyIteration()
    {
        Debug.Log("Initialization");
        _allStates = new List <State>();
        if (GameSelector.type == GameSelector.GameType.GridWorld)
        {
            for (int i = 0; i < gridWorldController.grid.gridHeight; ++i)
            {
                for (int j = 0; j < gridWorldController.grid.gridWidth; ++j)
                {
                    State currentState = new State();
                    currentState.currentPlayerPos = new Vector3(i, 0, j);
                    if (currentState.currentPlayerPos == gridWorldController.grid.endPos)
                    {
                        currentState.stateValue = 1000.0f;
                    }
                    else
                    {
                        currentState.stateValue = 0;
                    }

                    _allStates.Add(currentState);
                }
            }

            Debug.Log("Number of states : " + _allStates.Count);

            foreach (var currentState in _allStates)
            {
                GridWorldIntent wantedGridWorldIntent = (GridWorldIntent)Random.Range(0, 3);
                currentState.gridWorldPolicy = wantedGridWorldIntent;
            }
        }
    }
Example #4
0
 public bool CheckIntent(State currentState, GridWorldIntent wantedGridWorldIntent)
 {
     if (GetNextState(currentState, wantedGridWorldIntent) == null)
     {
         return(false);
     }
     if (GetCellType(GetNextState(currentState, wantedGridWorldIntent).currentPlayerPos) == CellType.Obstacle)
     {
         return(false);
     }
     return(true);
 }
Example #5
0
    public GridWorldIntent GetBestIntent(State currentState)
    {
        float           max = float.MinValue;
        GridWorldIntent bestGridWorldIntent = currentState.gridWorldPolicy;

        for (int i = 0; i < 4; ++i)
        {
            if (CheckIntent(currentState, (GridWorldIntent)i))
            {
                State tempState = GetNextState(currentState, (GridWorldIntent)i);
                if (tempState.stateValue > max)
                {
                    max = tempState.stateValue;
                    bestGridWorldIntent = (GridWorldIntent)i;
                }
            }
        }

        return(bestGridWorldIntent);
    }
Example #6
0
    public State GetNextState(State currentState, GridWorldIntent gridWorldIntent)
    {
        Vector3 nextPlayerPos = Vector3.zero;

        switch (gridWorldIntent)
        {
        case GridWorldIntent.Down:
            nextPlayerPos = currentState.currentPlayerPos - Vector3.forward;
            break;

        case GridWorldIntent.Up:
            nextPlayerPos = currentState.currentPlayerPos + Vector3.forward;
            break;

        case GridWorldIntent.Left:
            nextPlayerPos = currentState.currentPlayerPos + Vector3.left;
            break;

        case GridWorldIntent.Right:
            nextPlayerPos = currentState.currentPlayerPos - Vector3.left;
            break;
        }
        return(GetStateFromPos(nextPlayerPos));
    }