Esempio n. 1
0
    /// <summary>
    /// Deprecated! Reads the state map from file.
    /// </summary>
    /// <returns>
    /// The Dictionary representing the state map.
    /// </returns>
    /// <param name='filename'>
    /// The path to state map file.
    /// </param>
    /// <remarks>
    /// File format: <numVirtualStates> <numAgentProp> <numHelperProp> <numMonsterProp> <numSpecLocProp>.
    /// In the current implementation, numAgentProb = numHelperProp = 3, numMonsterProp = 4 or 5 depending
    /// on whether there is any hit point.
    ///
    /// Thereafter are: <agentRegion> <agentX> <agentY> <helperRegion> <helperX> <helperY>.
    /// Then: <monsterX> <monsterY> <seesAgent> <seesHelper> <additional props>[].
    /// </remarks>
    public Dictionary <VirtualState, int> readStateMapping(string filename)
    {
        //UnityEngine.Debug.Log("readStateMapping from "+filename);
        string uncompressedStr = ZlibDecompression.decompress(filename);

        string[] strArray = uncompressedStr.Split(' ');

        Dictionary <VirtualState, int> result = new Dictionary <VirtualState, int>();

        //int xSize = parentScript.levelData.xSize;
        //int ySize = parentScript.levelData.ySize;

        // 1. read number of virtual states
        int numVirtualStates = int.Parse(strArray[0]);

        UnityEngine.Debug.Log("readStateMapping from " + filename + ", num Virtual States =" + numVirtualStates);


        VirtualState state;                           // = new VirtualState();
        //state.monsterProperty.
        int sizeMonster = int.Parse(strArray[3]) - 2; // exclude seesAgent and seesHelper

        int sizePerState = int.Parse(strArray[1]) + int.Parse(strArray[2])
                           + int.Parse(strArray[3]) + int.Parse(strArray[4]);

        int offset;

        for (int i = 0; i < numVirtualStates; i++)
        {
            state  = new VirtualState(sizeMonster);
            offset = 4 + i * sizePerState;
            // a. agent and helper
            // exclude the first element, being the region id
            state.playerProperties[0, 0] = int.Parse(strArray[offset + 2]);
            state.playerProperties[0, 1] = int.Parse(strArray[offset + 3]);
            state.playerProperties[1, 0] = int.Parse(strArray[offset + 5]);
            state.playerProperties[1, 1] = int.Parse(strArray[offset + 6]);

            // b. NPC, exclude seesAgent and seesHelper because we are dealing with no abstraction here.
            offset = offset + 7;
            state.monsterProperty[0] = int.Parse(strArray[offset]);
            state.monsterProperty[1] = int.Parse(strArray[offset + 1]);
            for (int j = 4; j < sizeMonster + 2; j++)
            {
                state.monsterProperty[j - 2] = int.Parse(strArray[offset + j]);
            }

            // c. I'm not dealing with specialLocation here.
            // d. Add into result
            try{
                result.Add(state, i);
            }
            catch
            {
                UnityEngine.Debug.Log("Duplicated Virtual State " + i + ": " + state.ToString());
            }
        }

        return(result);
    }
Esempio n. 2
0
    /// <summary>
    /// Reads the policy file to initialize QValues.
    /// </summary>
    /// <returns>
    /// The matrix representing the Q values.
    /// </returns>
    /// <param name='filename'>
    /// The path to Ftn file. The filename should be level.tmx.ghost/sheep/fiery.0.Ftn.
    /// </param>
    public double[,,] readPolicyFile(string filename)
    {
        string uncompressedStr = ZlibDecompression.decompress(filename);

        string[] strArray = uncompressedStr.Split(' ');

        // 1. read number of virtual states
        int numVirtualStates = int.Parse(strArray[0]);

        double[,,] result = new double[numVirtualStates, GameConstants.NumAgentActions, GameConstants.NumHelperActions];

        int i = 1;

        for (int j = 0; j < numVirtualStates; j++)
        {
            for (int k = 0; k < GameConstants.NumAgentActions; k++)
            {
                for (int l = 0; l < GameConstants.NumHelperActions; l++)
                {
                    result[j, k, l] = double.Parse(strArray[i]);
                    i++;
                }
            }
        }

        return(result);
    }