예제 #1
0
파일: Library.cs 프로젝트: Hengle/unitytool
        // Checks for collision between two nodes and their children
        public static bool CheckCollision(Node n1, Node n2, NodeProvider provider, SpaceState state, bool noisy = false, int deep = 0)
        {
            if (deep > 5)
            {
                return(false);
            }
            int  x  = (n1.x + n2.x) / 2;
            int  y  = (n1.y + n2.y) / 2;
            int  t  = (n1.t + n2.t) / 2;
            Node n3 = provider.GetNode(t, x, y);

            // Noisy calculation
            if (state.enemies != null && ((Cell)n3.cell).noisy)
            {
                foreach (Enemy enemy in state.enemies)
                {
                    Vector3 dupe = enemy.positions [t];
                    dupe.x = (dupe.x - state.floorMin.x) / state.tileSize.x;
                    dupe.y = n3.t;
                    dupe.z = (dupe.z - state.floorMin.z) / state.tileSize.y;

                    // This distance is in number of cells size radius i.e. a 10 tilesize circle around the point
                    if (Vector3.Distance(dupe, n3.GetVector3()) < 10)
                    {
                        return(true);
                    }
                }
            }

            return(!n3.cell.IsWalkable() || CheckCollision(n1, n3, provider, state, noisy, deep + 1) || CheckCollision(n2, n3, provider, state, noisy, deep + 1));
        }
        /// <summary>
        /// Validate given solution
        /// </summary>
        /// <param name="tour">Tour to check</param>
        private void ValidateTour(ITour tour)
        {
            if (tour == null)
            {
                throw new ArgumentNullException("tour");
            }

            if (tour.Dimension != tour.Nodes.Count)
            {
                throw new TourInvalidException("Tour dimension does not match number of nodes on a list");
            }

            HashSet <int> identifiers = new HashSet <int>();

            foreach (int nodeId in tour.Nodes)
            {
                if (identifiers.Contains(nodeId))
                {
                    throw new TourInvalidException("Tour is invalid, has a node " + nodeId + " multiple times");
                }

                if (null == NodeProvider.GetNode(nodeId))
                {
                    throw new TourInvalidException("Tour is invalid, has a node " + nodeId + " which is not present in a problem");
                }

                identifiers.Add(nodeId);
            }
        }
예제 #3
0
파일: Library.cs 프로젝트: muntac/unitytool
        // Checks for collision between two nodes and their children
        public static bool CheckCollision(Node n1, Node n2, NodeProvider provider, SpaceState state, bool noisy = false, int deep = 0)
        {
            if (deep > 5)
                return false;
            int x = (n1.x + n2.x) / 2;
            int y = (n1.y + n2.y) / 2;
            int t = (n1.t + n2.t) / 2;
            Node n3 = provider.GetNode (t, x, y);

            // Noisy calculation
            if (state.enemies != null && ((Cell)n3.cell).noisy) {
                foreach (Enemy enemy in state.enemies) {
                    Vector3 dupe = enemy.positions [t];
                    dupe.x = (dupe.x - state.floorMin.x) / state.tileSize.x;
                    dupe.y = n3.t;
                    dupe.z = (dupe.z - state.floorMin.z) / state.tileSize.y;

                    // This distance is in number of cells size radius i.e. a 10 tilesize circle around the point
                    if (Vector3.Distance (dupe, n3.GetVector3 ()) < 10)
                        return true;
                }
            }

            return !(n3.cell.safe || !(n3.cell.blocked || n3.cell.seen)) || CheckCollision (n1, n3, provider, state, noisy, deep + 1) || CheckCollision (n2, n3, provider, state, noisy, deep + 1);
        }
        /// <summary>
        /// Gets tour distance for a given problem
        /// </summary>
        /// <param name="tour">Tour to check</param>
        /// <returns>Tour distance</returns>
        public override double TourDistance(ITour tour)
        {
            ValidateTour(tour);
            double distance = 0;

            for (int i = -1; i + 1 < tour.Nodes.Count; i++)
            {
                INode  first  = i == -1 ? NodeProvider.GetNode(tour.Nodes.Last()) : NodeProvider.GetNode(tour.Nodes[i]);
                INode  second = NodeProvider.GetNode(tour.Nodes[i + 1]);
                double weight = EdgeWeightsProvider.GetWeight(first, second);
                distance += weight;
            }

            return(distance);
        }
예제 #5
0
        /// <summary>
        /// Validate given solution
        /// </summary>
        /// <param name="tour">Tour to check</param>
        private void ValidateTour(ITour tour)
        {
            if (tour == null)
            {
                throw new ArgumentNullException("tour");
            }

            if (tour.Dimension != tour.Nodes.Count)
            {
                throw new TourInvalidException("Tour dimension does not match number of nodes on a list");
            }

            // Fast check for the sake of speed.
            var set = new HashSet <int>(tour.Nodes);

            set.UnionWith(NodeProvider.GetNodes().Select(n => n.Id));

            if (set.Count == tour.Nodes.Count)
            {
                return;
            }

            // Slow check for a more detailed exception message.
            var identifiers = new HashSet <int>();

            foreach (var nodeId in tour.Nodes)
            {
                if (identifiers.Contains(nodeId))
                {
                    throw new TourInvalidException("Tour is invalid, has a node " + nodeId + " multiple times");
                }

                if (null == NodeProvider.GetNode(nodeId))
                {
                    throw new TourInvalidException("Tour is invalid, has a node " + nodeId + " which is not present in a problem");
                }

                identifiers.Add(nodeId);
            }
        }