Exemple #1
0
        /// <summary>
        /// Check if the move is valid, i.e. not colliding into walls or other agents.
        /// This method is here instead of in ProblemInstance to enable algorithmic tweaks.
        /// </summary>
        /// <param name="possibleMove">The move to check if possible</param>
        /// <returns>true, if the move is possible.</returns>
        protected bool IsValid(TimedMove possibleMove, HashSet <TimedMove> currentMoves, int makespan, int agentNum)
        {
            // Check if the proposed move is reserved in the plan of another agent.
            // This is used in Trevor's IndependenceDetection.
            if (this.illegalMoves != null)
            {
                if (possibleMove.IsColliding(illegalMoves))
                {
                    return(false);
                }
            } // FIXME: Also checked in instance.IsValid later.

            if (this.constraintList != null)
            {
                queryConstraint.Init(agentNum, possibleMove);

                if (this.constraintList.Contains(queryConstraint))
                {
                    return(false);
                }
            }

            if (this.mustConstraints != null && makespan < this.mustConstraints.Length && // There may be a constraint on the timestep of the generated node
                this.mustConstraints[makespan] != null)
            {
                if (this.mustConstraints[makespan].Any <CbsConstraint>(
                        con => con.ViolatesMustConstraint((byte)agentNum, possibleMove)))
                {
                    return(false);
                }
            }

            // If the tile is not free (out of the grid or with an obstacle)
            if (this.instance.IsValid(possibleMove) == false)
            {
                return(false);
            }

            // Check against all the agents that have already moved to see if current move collides with their move
            return(possibleMove.IsColliding(currentMoves) == false);
        }
        /// <summary>
        /// Check if the move is valid, i.e. not colliding into walls or other agents.
        /// This method is here instead of in ProblemInstance to enable algorithmic tweaks.
        /// </summary>
        /// <param name="possibleMove">The move to check if possible</param>
        /// <returns>true, if the move is possible.</returns>
        protected virtual bool IsValid(TimedMove possibleMove, IReadOnlyDictionary <TimedMove, int> currentMoves, int makespan, int agentIndex, WorldState fromNode, WorldState intermediateMode)
        {
            int agentNum = fromNode.allAgentsState[agentIndex].agent.agentNum;

            // Check if the proposed move is reserved in the plan of another agent.
            // This is used in IndependenceDetection's ImprovedID.
            if (this.illegalMoves != null)
            {
                if (possibleMove.IsColliding(illegalMoves))
                {
                    return(false);
                }
            } // FIXME: Also checked in instance.IsValid later.

            if (this.constraints != null)
            {
                this.queryConstraint.Init(agentNum, possibleMove);

                if (this.constraints.Contains(queryConstraint))
                {
                    return(false);
                }
            }


            // If the tile is not free (out of the grid or with an obstacle)
            if (this.instance.IsValid(possibleMove) == false)
            {
                return(false);
            }

            // Check against all the agents that have already moved to see if current move collides with their move
            bool collision;


            collision = possibleMove.IsColliding(currentMoves);

            return(collision == false);
        }
Exemple #3
0
        /// <summary>
        /// Also checks if the move is illegal
        /// </summary>
        /// <param name="toCheck"></param>
        /// <returns></returns>
        public bool IsValid(TimedMove toCheck)
        {
            if (IsValidTile(toCheck.x, toCheck.y) == false)
            {
                return(false);
            }

            if (parameters.ContainsKey(IndependenceDetection.ILLEGAL_MOVES_KEY))
            {
                var reserved = (HashSet <TimedMove>)parameters[IndependenceDetection.ILLEGAL_MOVES_KEY];

                return(toCheck.IsColliding(reserved) == false);
            } // FIXME: Should this be here?

            return(true);
        }
Exemple #4
0
        /// <summary>
        /// Also checks if the move is illegal
        /// </summary>
        /// <param name="toCheck"></param>
        /// <returns></returns>
        public bool IsValid(TimedMove toCheck)
        {
            if (IsValidTile(toCheck.x, toCheck.y) == false)
            {
                return(false);
            }

            if (parameters.ContainsKey("ID-reserved"))
            {
                var reserved = (HashSet <TimedMove>)parameters["ID-reserved"];

                return(toCheck.IsColliding(reserved) == false);
            }

            return(true);
        }
Exemple #5
0
 private bool isValidMove(TimedMove move)
 {
     if (this.problem.IsValid(move))
     {
         if (move.IsColliding(RT) == false)
         {
             Move key = new Move(move.x, move.y, Move.Direction.NO_DIRECTION);
             if (!parked.Contains(key) || (int)(parked[key]) > move.time)
             {
                 return(true);
             }
         }
     }
     //switch (step.direction)
     //{
     //    case 0:
     //        return true;
     //    case 1:
     //        return safeFromCollision(step.pos_X + 1, step.pos_Y, step.step, 3);
     //    case 2:
     //        return safeFromCollision(step.pos_X, step.pos_Y-1, step.step, 4);
     //    case 3:
     //        return safeFromCollision(step.pos_X - 1, step.pos_Y, step.step, 1);
     //    case 4:
     //        return safeFromCollision(step.pos_X , step.pos_Y+1, step.step, 2);
     //    case 5:
     //        return safeFromCollision(step.pos_X + 1, step.pos_Y-1, step.step, 7);
     //    case 6:
     //        return safeFromCollision(step.pos_X - 1, step.pos_Y-1, step.step, 8);
     //    case 7:
     //        return safeFromCollision(step.pos_X - 1, step.pos_Y+1, step.step, 5);
     //    case 8:
     //        return safeFromCollision(step.pos_X + 1, step.pos_Y+1, step.step, 6);
     //}
     return(false);
 }
        /// <summary>
        /// Expands a single agent in the nodes.
        /// This includes:
        /// - Generating the children
        /// - Inserting them into OPEN
        /// - Insert node into CLOSED
        /// Returns the child nodes
        /// </summary>
        protected virtual List <WorldState> ExpandOneAgent(List <WorldState> intermediateNodes, int agentIndex)
        {
            /*if (Program.TO_EXECUTE && Run.replanStopwath != null && Run.replanStopwath.ElapsedMilliseconds > Run.TIMEOUT)
             *  throw new Exception("3");*/
            var        GeneratedNodes = new List <WorldState>();
            WorldState childNode;

            foreach (var currentNode in intermediateNodes)
            {
                if (runner.ElapsedMilliseconds() > Constants.MAX_TIME)
                {
                    break;
                }

                // Try all legal moves of the agents
                foreach (TimedMove agentLocation in currentNode.allAgentsState[agentIndex].lastMove.GetNextMoves())
                {
                    WorldState origNode    = agentIndex == 0? currentNode : currentNode.prevStep;
                    bool       moveIsValid = true;
                    //moveIsValid = this.IsValid(agentLocation, currentNode.currentMoves, currentNode.makespan + 1, agentIndex, origNode, currentNode);
                    //if (moveIsValid == false)
                    //    continue;

                    //----------------Begin pasting isValid method
                    TimedMove possibleMove = agentLocation;
                    IReadOnlyDictionary <TimedMove, int> currentMoves = currentNode.currentMoves;
                    int        makespan         = currentNode.makespan + 1;
                    WorldState fromNode         = origNode;
                    WorldState intermediateMode = currentNode;
                    int        agentNum         = fromNode.allAgentsState[agentIndex].agent.agentNum;

                    // Check if the proposed move is reserved in the plan of another agent.
                    // This is used in IndependenceDetection's ImprovedID.
                    if (this.illegalMoves != null)
                    {
                        if (possibleMove.IsColliding(illegalMoves))
                        {
                            continue;
                        }
                    } // FIXME: Also checked in instance.IsValid later.

                    if (this.constraints != null)
                    {
                        queryConstraint.Init(agentNum, possibleMove);

                        if (this.constraints.Contains(queryConstraint))
                        {
                            continue;
                        }
                    }



                    // If the tile is not free (out of the grid or with an obstacle)
                    if (this.instance.IsValid(possibleMove) == false)
                    {
                        continue;
                    }

                    // Check against all the agents that have already moved to see if current move collides with their move
                    bool collision;

                    collision = possibleMove.IsColliding(currentMoves);

                    if (collision)
                    {
                        continue;
                    }
                    //----------------end paste from isValid

                    childNode = CreateSearchNode(currentNode);
                    childNode.allAgentsState[agentIndex].MoveTo(agentLocation);

                    childNode.prevStep = currentNode.prevStep; // Skip temporary node objects used during expansion process.
                    if (agentIndex == 0)
                    {
                        childNode.prevStep = currentNode;
                    }
                    if (agentIndex < currentNode.allAgentsState.Length - 1) // More agents need to move
                    {
                        childNode.currentMoves.Add(agentLocation, agentIndex);
                    }
                    else // Moved the last agent
                    {
                        childNode.currentMoves.Clear(); // To reduce memory load and lookup times, even though it's correct to leave the old moves since they're timed.
                    }
                    GeneratedNodes.Add(childNode);
                }
            }
            return(GeneratedNodes);
        }