Exemple #1
0
    // mask some moves as not possible
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        // contains the list of disallowed sheep/moves by number (see: OnActionReceived)
        List <int>  notAllowed           = new List <int>();
        List <bool> perSheepAllowedMoves = new List <bool>();

        for (int i = 0; i < gameManager.sheep.Length; i++)
        {
            // grab the sheep controller
            SheepController  sheep  = gameManager.sheep[i].GetComponent <SheepController>();
            SquareController square = sheep.Square().GetComponent <SquareController>();

            // Get List<bool> of possible moves (true = allowed, false = not allowed)
            // Order is (must match above!):
            // row: 1, col: -1 = 0
            // row: 1, col: 1= 1
            perSheepAllowedMoves = square.PossibleSheepMovesDir();

            // for any false, add the index (or index+1) into notAllowed list
            for (int j = 0; j < perSheepAllowedMoves.Count; j++)
            {
                if (!perSheepAllowedMoves[j])
                {
                    notAllowed.Add(2 * i + j);
                }
            }
        }

        actionMasker.SetMask(0, notAllowed);
    }
        public void MultipleMaskEdit()
        {
            var bp = new BrainParameters();

            bp.vectorActionSpaceType = SpaceType.Discrete;
            bp.vectorActionSize      = new[] { 4, 5, 6 };
            var masker = new DiscreteActionMasker(bp);

            masker.SetMask(0, new[] { 0, 1 });
            masker.SetMask(0, new[] { 3 });
            masker.SetMask(2, new[] { 1 });
            var mask = masker.GetMask();

            for (var i = 0; i < 15; i++)
            {
                if ((i == 0) || (i == 1) || (i == 3) || (i == 10))
                {
                    Assert.IsTrue(mask[i]);
                }
                else
                {
                    Assert.IsFalse(mask[i]);
                }
            }
        }
Exemple #3
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        // Mask the necessary actions if selected by the user.
        if (maskActions)
        {
            // Prevents the agent from picking an action that would make it collide with a wall
            var positionX   = (int)transform.position.x;
            var positionZ   = (int)transform.position.z;
            var maxPosition = (int)m_ResetParams.GetWithDefault("gridSize", 5f) - 1;

            if (positionX == 0)
            {
                actionMasker.SetMask(0, new int[] { k_Left });
            }

            if (positionX == maxPosition)
            {
                actionMasker.SetMask(0, new int[] { k_Right });
            }

            if (positionZ == 0)
            {
                actionMasker.SetMask(0, new int[] { k_Down });
            }

            if (positionZ == maxPosition)
            {
                actionMasker.SetMask(0, new int[] { k_Up });
            }
        }
    }
        public void Contruction()
        {
            var bp     = new BrainParameters();
            var masker = new DiscreteActionMasker(bp);

            Assert.IsNotNull(masker);
        }
Exemple #5
0
        // --------------------------------------------------------------------------------------------------------

        /*public override void Initialize()
         * {
         *      base.Initialize();
         *
         *      if (m_RenderCamera != null)
         *              m_RenderCamera.Render();
         * }*/

        // --------------------------------------------------------------------------------------------------------
        public override void CollectDiscreteActionMasks(DiscreteActionMasker _actionMasker)
        {
            // Mask the necessary actions if selected by the user.
            if (m_MaskActions)
            {
                _actionMasker.SetMask(0, new [] { (int)State.GetNegativeDirection(m_Controller.Direction) });
            }
        }
 // when using discrete actions, we can MASK actions that are impossible,
 // so the agent doesn't try to perform them.
 // when the tank cannon is cooling down, it is not possible for the agent to fire,
 // so we are masking our discrete actions for branch 3, actions 1 and 2 (fire weak, fire strong)
 // this is not critical, but may help with learning
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
 {
     base.CollectDiscreteActionMasks(actionMasker);
     if (shooting && shooting.cooldown)
     {
         int[] firingActions = { 1, 2 };
         actionMasker.SetMask(2, firingActions);
     }
 }
        public void NullMask()
        {
            var bp = new BrainParameters();

            bp.vectorActionSpaceType = SpaceType.Discrete;
            var masker = new DiscreteActionMasker(bp);
            var mask   = masker.GetMask();

            Assert.IsNull(mask);
        }
        public void FailsWithContinuous()
        {
            var bp = new BrainParameters();

            bp.vectorActionSpaceType = SpaceType.Continuous;
            bp.vectorActionSize      = new[] { 4 };
            var masker = new DiscreteActionMasker(bp);

            masker.SetMask(0, new[] { 0 });
            Assert.Catch <UnityAgentsException>(() => masker.GetMask());
        }
Exemple #9
0
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
 {
     // Set actions masks
     this.moveMasks = Utils.GetMoveMasks(player);
     actionMasker.SetMask(MOVE_BRANCH_INDEX, this.moveMasks);
     this.skillMasks = Utils.GetSkillMasks(player, enemy);
     //UnityEngine.Debug.Log(player.GetName() + " CollectDiscreteActionMasks: " + String.Join(",", this.skillMasks));
     actionMasker.SetMask(SKILL_BRANCH_INDEX, this.skillMasks);
     this.potionMasks = Utils.GetPotionMasks(player);
     actionMasker.SetMask(POTION_BRANCH_INDEX, this.potionMasks);
 }
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker) //make masks on actions
 {
     if (!hasBall)                                                                  //mask shooting and passing if doesnt have ball
     {
         actionMasker.SetMask(3, new int[2] {
             1, 2
         });
     }
     //if (transform.position.y > 1)
     //    actionMasker.SetMask(3, new int[1] { 1 });
 }
Exemple #11
0
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
 {
     /*
      * ZhedBoard zhedBoard = this.gameManager.zhedBoard;
      * for (int i = 0; i < gridSize * gridSize; i++) {
      *  if (!zhedBoard.ValidMove(ToCoords(i))) {
      *      Debug.Log("Masking " + ToCoords(i));
      *      actionMasker.SetMask(1, i);
      *  }
      * }
      */
 }
Exemple #12
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker discreteActionMasker)
    {
        mask = GetActionMask();

        //sometimes the actionMask seems to fail (I found some bug report from 2018 comfirmed by a Unity Dev)
        //this function is called at the wrong moment
        //ignoring the mask if this happens fixes this
        if (mask.Count == vectorActionLength)
        {
            return;
        }

        discreteActionMasker.SetMask(0, mask);
    }
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        List <int> allItems = new List <int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

        allItems.RemoveAll(item => puzzle.getListOfValidTypes().Contains(item));
        actionMasker.SetMask(0, allItems); //Mask for tile types
        //actionMasker.SetMask(1, new int[4] {0,1,2,3}); //Unecessary Probably

        /*
         * for(int i = 0; i < puzzle.getListOfValidTypes().ToArray().Length; i++) {
         *  Debug.Log("Mask: " + puzzle.getListOfValidTypes().ToArray()[i]);
         * }
         */
    }
Exemple #14
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        base.CollectDiscreteActionMasks(actionMasker);
        var list = new List <int>();

        for (int i = 0; i < 7; i++)
        {
            if (!_gameController.CanPlayAtColumn(_playerId, i))
            {
                list.Add(i);
            }
        }
        actionMasker.SetMask(0, list);
    }
        public void MaskReset()
        {
            var bp = new BrainParameters
            {
                vectorActionSpaceType = SpaceType.Discrete,
                vectorActionSize      = new[] { 4, 5, 6 }
            };
            var masker = new DiscreteActionMasker(bp);

            masker.SetMask(1, new[] { 1, 2, 3 });
            masker.ResetMask();
            var mask = masker.GetMask();

            for (var i = 0; i < 15; i++)
            {
                Assert.IsFalse(mask[i]);
            }
        }
Exemple #16
0
    // mask some moves as not possible
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        List <bool> possibleMoves = wolfSquareController.PossibleWolfMovesDir();

        List <int> notAllowed = new List <int>()
        {
        };

        for (int i = 0; i < possibleMoves.Count; i++)
        {
            if (!possibleMoves[i])
            {
                notAllowed.Add(i); // add the movement number that is not allowed
            }
        }

        actionMasker.SetMask(0, notAllowed);
    }
        public void FirstBranchMask()
        {
            var bp = new BrainParameters();

            bp.vectorActionSpaceType = SpaceType.Discrete;
            bp.vectorActionSize      = new[] { 4, 5, 6 };
            var masker = new DiscreteActionMasker(bp);
            var mask   = masker.GetMask();

            Assert.IsNull(mask);
            masker.SetMask(0, new[] { 1, 2, 3 });
            mask = masker.GetMask();
            Assert.IsFalse(mask[0]);
            Assert.IsTrue(mask[1]);
            Assert.IsTrue(mask[2]);
            Assert.IsTrue(mask[3]);
            Assert.IsFalse(mask[4]);
            Assert.AreEqual(mask.Length, 15);
        }
        public void SecondBranchMask()
        {
            var bp = new BrainParameters
            {
                vectorActionSpaceType = SpaceType.Discrete,
                vectorActionSize      = new[] { 4, 5, 6 }
            };
            var masker = new DiscreteActionMasker(bp);

            masker.SetMask(1, new[] { 1, 2, 3 });
            var mask = masker.GetMask();

            Assert.IsFalse(mask[0]);
            Assert.IsFalse(mask[4]);
            Assert.IsTrue(mask[5]);
            Assert.IsTrue(mask[6]);
            Assert.IsTrue(mask[7]);
            Assert.IsFalse(mask[8]);
            Assert.IsFalse(mask[9]);
        }
Exemple #19
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        List <int> impossibleMoves = new List <int>();

        int[] possibleMoves;

        for (Direction i = Direction.Up; i < Direction.DirectionsCount; i++)
        {
            if (!CheckMove(i, out possibleMoves))
            {
                impossibleMoves.Add((int)i);
            }
        }

        actionMasker.SetMask(0, impossibleMoves);

        if (impossibleMoves.Count == (int)Direction.DirectionsCount)
        {
            MarkGameOver();
        }
    }
        public void ThrowsError()
        {
            var bp = new BrainParameters
            {
                vectorActionSpaceType = SpaceType.Discrete,
                vectorActionSize      = new[] { 4, 5, 6 }
            };
            var masker = new DiscreteActionMasker(bp);

            Assert.Catch <UnityAgentsException>(
                () => masker.SetMask(0, new[] { 5 }));
            Assert.Catch <UnityAgentsException>(
                () => masker.SetMask(1, new[] { 5 }));
            masker.SetMask(2, new[] { 5 });
            Assert.Catch <UnityAgentsException>(
                () => masker.SetMask(3, new[] { 1 }));
            masker.GetMask();
            masker.ResetMask();
            masker.SetMask(0, new[] { 0, 1, 2, 3 });
            Assert.Catch <UnityAgentsException>(
                () => masker.GetMask());
        }
Exemple #21
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        switch (gms.gamePhase)
        {
        case 0:
            List <int> zeroFields = new List <int>();
            for (int i = 0; i < 34; i++)
            {
                if (playerHand[i] == 0)
                {
                    zeroFields.Add(i);
                }
            }
            zeroFields.ToArray();
            actionMasker.SetMask(0, zeroFields);
            Debug.Log("Action received: " + string.Join(", ", playerHand));
            break;

        case -1:
            break;

        default:
            List <int> nullChoices  = new List <int>();
            GameObject overlayPanel = GameObject.Find("OverlayPanel(Clone)");

            if (overlayPanel)
            {
                for (int i = overlayPanel.transform.childCount - 1; i < 4; i++)
                {
                    nullChoices.Add(i);
                }
                nullChoices.ToArray();
                actionMasker.SetMask(1, nullChoices);
            }
            break;
        }
    }
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        //x : -5 ~ 4, y = -5 ~ 5

        var posX = transform.position.x;
        var posY = transform.position.y;

        if (posX <= -5)
        {
            actionMasker.SetMask(0, new[] { left });
        }
        if (posX >= 4)
        {
            actionMasker.SetMask(0, new[] { right });
        }
        if (posY <= -5)
        {
            actionMasker.SetMask(0, new[] { down });
        }
        if (posY >= 5)
        {
            actionMasker.SetMask(0, new[] { up });
        }
    }
Exemple #23
0
 /// <summary>
 /// Disables unavailable actions before making a decision request
 /// </summary>
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
 {
     actionMasker.SetMask(0, MaskedActions);
 }
Exemple #24
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        // action order: b, c, x, y, z
        if (restrictionCollision && !(collVectorAxisY == Vector3.zero && collVectorCutter == Vector3.zero))
        {
            actionMasker.SetMask(0, new int[2] {
                0, 2
            });
            actionMasker.SetMask(1, new int[2] {
                0, 2
            });
            actionMasker.SetMask(2, new int[2] {
                0, 2
            });
            actionMasker.SetMask(3, new int[2] {
                0, 1
            });
            actionMasker.SetMask(4, new int[2] {
                0, 2
            });
        }
        else if (restrictionMovingRange)
        {
            if (cutterPotential == 0)
            {
                if (toKeepVectorCutter == Vector3.zero)
                {
                    if (traX > 0.5f)
                    {
                        actionMasker.SetMask(2, new int[2] {
                            1, 2
                        });
                    }
                    if (traX < 0.5f)
                    {
                        actionMasker.SetMask(2, new int[2] {
                            0, 1
                        });
                    }
                    if (traZ > 0.5f)
                    {
                        actionMasker.SetMask(4, new int[2] {
                            1, 2
                        });
                    }
                    if (traZ < 0.5f)
                    {
                        actionMasker.SetMask(4, new int[2] {
                            0, 1
                        });
                    }
                    actionMasker.SetMask(3, new int[2] {
                        0, 2
                    });

                    actionMasker.SetMask(7, new int[4] {
                        0, 1, 2, 3
                    });
                    actionMasker.SetMask(9, new int[4] {
                        0, 1, 2, 3
                    });
                }
                else
                {
                    actionMasker.SetMask(0, new int[2] {
                        0, 2
                    });
                    actionMasker.SetMask(1, new int[2] {
                        0, 2
                    });
                    actionMasker.SetMask(2, new int[2] {
                        0, 2
                    });
                    actionMasker.SetMask(3, new int[2] {
                        0, 1
                    });
                    actionMasker.SetMask(4, new int[2] {
                        0, 2
                    });
                }
            }
            else if (Math.Abs(cutterPotential) <= cutterPotentialLimitHigh)
            {
                actionMasker.SetMask(3, new int[2] {
                    1, 2
                });

                actionMasker.SetMask(8, new int[4] {
                    0, 1, 2, 3
                });
            }
        }
    }
Exemple #25
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        List <int> illegalActions = new List <int>();

        for (int i = 0; i < 6; i++)
        {
            //can't play cards that are not in hand
            if (!Player.CardsInHand.Contains(i))
            {
                illegalActions.Add(i);
            }
        }

        //can't play last played card
        if (!illegalActions.Contains(DeckHandler.instance.lastPlayed))
        {
            illegalActions.Add(DeckHandler.instance.lastPlayed);
        }

        //6 represents drawing a new card
        //can't have 5 cards in hand
        if (Player.CardsInHand.Count == 4)
        {
            illegalActions.Add(6);
        }

        //if forced to play and able to play cards in hand - can't draw card
        if (Player.ForcedToPlay && !illegalActions.Contains(6))
        {
            bool canPlay;

            if (Player.CardsInHand.Count == 1 && Player.CardsInHand[0] == DeckHandler.instance.lastPlayed ||
                Player.CardsInHand.Count == 2 && Player.CardsInHand[0] == DeckHandler.instance.lastPlayed &&
                Player.CardsInHand[1] == DeckHandler.instance.lastPlayed)
            {
                canPlay = false;
            }
            else
            {
                canPlay = true;
            }

            if (canPlay)
            {
                illegalActions.Add(6);
            }
        }

        //set main action (play card 0-5, 6 - draw card)
        actionMasker.SetMask(0, illegalActions);

        //can't sacrifice last token to block/force enemy to play
        if (int.Parse(Player.Tokens.text) < 2)
        {
            actionMasker.SetMask(1, new [] { 1 });
            actionMasker.SetMask(2, new [] { 1 });
        }

        //cant force enemy to play if he has no cards / is blocking
        if (Opponent.CardsInHand.Count == 0 || Opponent.CardsInHand.Count == 4 || Opponent.Blocking)
        {
            actionMasker.SetMask(2, new [] { 1 });
        }
    }
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
 {
     actionMasker.SetMask(0, board.GetOccupiedFields());
 }
Exemple #27
0
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
 {
     actionMasker.SetMask(0, xMask);
     actionMasker.SetMask(1, yMask);
 }
Exemple #28
0
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
 {
     actionMasker.SetMask(0, GetImpossiblePlays());
 }
Exemple #29
0
 //Set a mask describing what output actions are valid for the next decision
 public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
 {
     //Mask out any actions not in the AllMoves returned from GetValidMoves()
 }
Exemple #30
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        List <AgentAction> disableActions = new List <AgentAction>()
        {
            AgentAction.Up,
            AgentAction.Right,
            AgentAction.Down,
            AgentAction.Left,
            AgentAction.Bomb
        };

        if (!_player.IsDead)
        {
            if (_isMoving)
            {
                // We only allow the same action (so direction) than the previous one
                disableActions.Remove(_previousAction);
                disableActions.Add(AgentAction.Nothing);
            }
            else if (_canTakeDecision)
            {
                // Check accessible cells around
                var cellPosition = _map.CellPosition(transform.position);
                var neighbours   = AIUtils.GetNeighbours(cellPosition, _map, true, true, false);

                foreach (var neighbourDirection in neighbours.Keys)
                {
                    switch (neighbourDirection)
                    {
                    case EDirection.None:
                        break;

                    case EDirection.Up:
                        disableActions.Remove(AgentAction.Up);
                        break;

                    case EDirection.Right:
                        disableActions.Remove(AgentAction.Right);
                        break;

                    case EDirection.Down:
                        disableActions.Remove(AgentAction.Down);
                        break;

                    case EDirection.Left:
                        disableActions.Remove(AgentAction.Left);
                        break;
                    }
                }

                // Only allow the agent to plant a bomb if he can
                if (_player.BombCount > 0)
                {
                    disableActions.Remove(AgentAction.Bomb);
                }
            }
        }

        int[] actionsMask = new int[disableActions.Count];

        for (int i = 0; i < disableActions.Count; i++)
        {
            actionsMask[i] = (int)disableActions[i];
        }

        actionMasker.SetMask(0, actionsMask);
    }