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
    public void AIMove()
    {
        InitialState = getCurrentState();
        List <Vector3> m = (GenerateListOfMoves(InitialState, 0));

        lastMove = m[0];

        lastMoves.Add(lastMove);
        if (lastMoves.Count > 10)
        {
            lastMoves.RemoveAt(0);
        }
        if (lastMoves.Count == 10 && Glitch(lastMoves))
        {
            if (TreeDeeplimit < 8)
            {
                TreeDeeplimit++;
            }
            lastMoves.RemoveRange(0, 3);
            Debug.Log("Glitch detected TreeDeeplimit rised");
        }
        else
        {
            if (lastMoves.Count == 10 && !Glitch(lastMoves))
            {
                if (TreeDeeplimit > 7)
                {
                    TreeDeeplimit--;
                }
            }
        }
        transform.position += m[0];
    }
Esempio n. 3
0
    public static List <Vector3> possibleMoves(VirtualState state, Vector3 moveableObject)
    {//Tested seems ok.
        List <Vector3> possibleMoves = new List <Vector3>()
        {
            new Vector3(1, 0, 0), new Vector3(-1, 0, 0), new Vector3(0, -1, 0), new Vector3(0, 1, 0)
        };
        List <Vector3> returnMoves = new List <Vector3>();

        foreach (Vector3 move in possibleMoves)
        {
            if (PlayerController.MoveIsPossible(moveableObject, moveableObject + move, state.Boxes))
            {
                returnMoves.Add(move);
            }
        }
        if (possibleMoves.Count == 0)
        {
            throw new System.ArgumentException("There are not possible moves");
        }
        if (possibleMoves.Count < 4)
        {
            Debug.Log("Less than 4 possible moves");
        }
        return(returnMoves);
    }
Esempio n. 4
0
    int CreateSearchTree(VirtualState state, int n, ref List <Vector3> moves)
    {
        List <VirtualState> PossibleStates = GeneratePossibleStates(state);
        VirtualState        best           = new VirtualState();

        foreach (VirtualState v in PossibleStates)
        {
            if (n + 1 < TreeDeeplimit)
            {
                //  1'st optimalization
                //  if (state.move == -v.move)
                //      v.EvaluationValue += int.MaxValue;
                //  else
                v.EvaluationValue += CreateSearchTree(v, n + 1, ref moves);
            }
            if (v.EvaluationValue < best.EvaluationValue)
            {
                best = new VirtualState(v, v.EvaluationValue);
            }
        }
        //Debug.Log("N: " + n + " , " + best.EvaluationValue + ", " + best.move);

        if (n == 0)
        {
            moves.Add(best.Move);
        }

        return(best.EvaluationValue);
    }
Esempio n. 5
0
    List <Vector3> GenerateListOfMoves(VirtualState state, int n)
    {
        List <Vector3> moves = new List <Vector3>();

        CreateSearchTree(state, n, ref moves);
        //Debug.Log(moves[0]);
        return(moves);
    }
Esempio n. 6
0
    VirtualState getCurrentState()
    {
        VirtualState CurrentState = new VirtualState(
            CastFromGameObjectListToPositionVector(GameObject.FindGameObjectsWithTag("Box")),
            CastFromGameObjectListToPositionVector(GameObject.FindGameObjectsWithTag("Cross")),
            CastFromGameObjectListToPositionVector(GameObject.FindGameObjectsWithTag("Wall")),
            GameObject.FindGameObjectWithTag("Player").transform.position,
            new Vector3(0, 0, 0)
            );

        //Debug.Log("Initial position: " + GameObject.FindGameObjectWithTag("Player").transform.position);

        return(CurrentState);
    }
Esempio n. 7
0
    List <VirtualState> GeneratePossibleStates(VirtualState state)
    {//Tested ok.
        List <VirtualState> newStates           = new List <VirtualState>();
        List <Vector3>      listOfPossibleMoves = possibleMoves(state, state.Player);

        foreach (Vector3 move in listOfPossibleMoves)
        {
            VirtualState temporaryState = new VirtualState(state.Boxes, state.Crosses, state.Walls, state.Player, move);
            temporaryState.Player += move;
            if (PlayerController.BoxAtPos(temporaryState.Boxes, temporaryState.Player))
            {
                temporaryState.Boxes[PlayerController.iBoxAtPos(temporaryState.Boxes, temporaryState.Player)] += move;
            }
            newStates.Add(temporaryState);
        }
        return(newStates);
    }
Esempio n. 8
0
    public void PrepareNewState(int state)
    {
        if (!loadingInProgress)
        {
            string tourImgsPath = this.tourPath;

            VirtualState currState = vt.states[state];

            if (!string.IsNullOrEmpty(Path.Combine(tourImgsPath, currState.img)))
            {
                loadingInProgress = true;
                StartCoroutine(
                    FadeInOutNewState(
                        LoadImageAndMarkers(
                            Path.Combine(tourImgsPath, currState.img),
                            Path.Combine(tourImgsPath, currState.img2),
                            currState.markers
                            )
                        )
                    );
            }
        }
    }
        public async Task <DashboardState> GetState(
            DateTimeOffset date, UserId user, DashboardId dashboardId, ReadOnlyState state, VirtualState virtualState)
        {
            var configs = await _repository.GetUserDashboardConfigs(user);

            var dashboard = configs.FirstOrDefault(c => c.Id == dashboardId);
            var tagState  = await _tagManager.GetTags(user);

            var assetNames    = CollectAssetNames(state.Brokers.SelectMany(b => b.Inventory).ToArray(), user);
            var assetTags     = CollectAssetTags(tagState);
            var virtualAssets = CollectVirtualAssets(virtualState);
            var dashboardTags = dashboard?.Tags ?? new List <DashboardConfigTagModel>();

            _logger.LogTrace($"Dashboard {dashboardId} contains {dashboardTags.Count} tags");
            var tags = dashboardTags
                       .Select(t => {
                if (!assetTags.TryGetValue(new(t.Tag), out var assetIsins))
                {
                    assetIsins = Array.Empty <AssetISIN>();
                }
                var assets = assetIsins
                             .Where(isin => virtualAssets.Any(a => a.Isin == isin))
                             .Select(isin => {
                    var name = assetNames.GetValueOrDefault(isin) ?? string.Empty;
                    var sums = CalculateAssetSums(isin, virtualAssets, date);
                    return(new DashboardAsset(isin, name, sums));
                })
                             .ToArray();
                var assetSums = AggregateSums(assets.Select(a => a.Sums));
                return(new DashboardStateTag(t.Tag, assets, assetSums));
            })
                       .ToArray();
            var tagSums = AggregateSums(tags.Select(t => t.Sums));

            return(new(tags, tagSums));
        }
Esempio n. 10
0
 IReadOnlyCollection <VirtualAsset> CollectVirtualAssets(VirtualState virtualState) =>
 virtualState.Balances
 .SelectMany(b => b.Inventory)
 .Where(a => a.Count > 0)
 .ToArray();
Esempio n. 11
0
    /// <summary>
    /// Deprecated! Generates the state map.
    /// </summary>
    /// <returns>
    /// The Dictionary representing the state map.
    /// </returns>
    /// <param name='npcType'>
    /// Npc type.
    /// </param>
    public Dictionary <VirtualState, int> generateStateMap(int npcType)
    {
        //UnityEngine.Debug.Log("generateStateMap");
        VirtualState state;
        Dictionary <VirtualState, int> result = new Dictionary <VirtualState, int>();

        int sizeMonster = GameConstants.numProperties[npcType];
        //state = new VirtualState(sizeMonster);
        // stateIndex 0 is termState, no need to store.
        int stateIndex = 1;

        // 1. construct a state
        // TODO: may need to change to ySize first, xSize next, also the coordinates is i, ySize - j -1
        for (int p1x = 1; p1x < parentScript.levelData.xSize - 1; p1x++)
        {
            for (int p1y = 1; p1y < parentScript.levelData.ySize - 1; p1y++)
            {
                if (parentScript.levelData.map[p1x, parentScript.levelData.ySize - p1y - 1] != GameConstants.Wall)
                {
                    for (int p2x = 1; p2x < parentScript.levelData.xSize - 1; p2x++)
                    {
                        for (int p2y = 1; p2y < parentScript.levelData.ySize - 1; p2y++)
                        {
                            if (parentScript.levelData.map[p2x, parentScript.levelData.ySize - p2y - 1] != GameConstants.Wall)
                            {
                                for (int mx = 1; mx < parentScript.levelData.xSize - 1; mx++)
                                {
                                    for (int my = 1; my < parentScript.levelData.ySize - 1; my++)
                                    {
                                        if (parentScript.levelData.map[mx, parentScript.levelData.ySize - my - 1] != GameConstants.Wall)
                                        {
                                            state = new VirtualState(sizeMonster);

                                            state.playerProperties[0, 0] = p1x;
                                            state.playerProperties[0, 1] = p1y;
                                            state.playerProperties[1, 0] = p2x;
                                            state.playerProperties[1, 1] = p2y;

                                            state.monsterProperty[0] = mx;
                                            state.monsterProperty[1] = my;

                                            // if NPC is ghost, there is HP.
                                            if (npcType == GameConstants.GhostType)
                                            {
                                                for (int hp = 0; hp <= parentScript.ghostMaxHP; hp++)
                                                {
                                                    state.monsterProperty[2] = hp;
                                                    try{
                                                        result.Add(state, stateIndex);
//														if (stateIndex <= 10)
//															UnityEngine.Debug.Log("Virtual State " + stateIndex +
//																": " + state.ToString());
                                                        stateIndex++;
                                                    }
                                                    catch
                                                    {
                                                        UnityEngine.Debug.Log("Duplicated Virtual State " +
                                                                              stateIndex + ": " + state.ToString());
                                                    }
                                                }
                                            }
                                            // Otherwise, no additional property
                                            else
                                            {
                                                try{
                                                    result.Add(state, stateIndex);
//													if (stateIndex <= 10)
//															UnityEngine.Debug.Log("Virtual State " + stateIndex +
//																": " + state.ToString());
                                                    stateIndex++;
                                                }
                                                catch
                                                {
                                                    UnityEngine.Debug.Log("Duplicated Virtual State " +
                                                                          stateIndex + ": " + state.ToString());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        UnityEngine.Debug.Log("generate state map for " + GameConstants.NPCTypeStr[npcType] +
                              ", num Virtual States =" + stateIndex);

        return(result);
    }
Esempio n. 12
0
 public VirtualState(VirtualState state, int ev)
     : this(state.Boxes, state.Crosses, state.Walls, state.Player, state.Move)
 {
     EvaluationValue = ev;
 }
Esempio n. 13
0
 public VirtualState(VirtualState state)
     : this(state.Boxes, state.Crosses, state.Walls, state.Player, state.Move)
 {
     EvaluationValue = EvaluationFuction(Boxes, Crosses, Walls, Player);
 }