internal static void Check(DistanceNode sourcePerson, DistanceNode targetPerson, Steps steps, double nodeDistance)
        {
            Assert.AreEqual(nodeDistance, steps.Degree);

            var stepList = steps.GetSteps().ToList();

            Assert.AreEqual(nodeDistance, stepList.Count);

            if (stepList.Count == 0)
            {
                return;
            }

            if (!stepList[0].Left.Name.Equals(targetPerson.Name))
            {
                Assert.Fail("Chain starts incorrectly.");
            }

            if (!stepList[stepList.Count - 1].Right.Name.Equals(sourcePerson.Name))
            {
                Assert.Fail("Chain ends incorrectly.");
            }

            CheckTitles(stepList);

            CheckAllButLastElementForSourcePerson(sourcePerson, stepList);

            CheckAllButFirstElementForTargetPerson(targetPerson, stepList);

            CheckForDuplicateNodes(stepList);
        }
Esempio n. 2
0
        internal static void CheckSteps(DistanceNode sourceNode, DistanceNode targetNode, List <Steps> stepsList, double nodeDistance)
        {
            StepsChecker.Check(stepsList);

            foreach (var steps in stepsList)
            {
                StepsChecker.Check(sourceNode, targetNode, steps, nodeDistance);
            }
        }
        public void TestCompareTo_GreaterThan()
        {
            DistanceNode thisNode  = new DistanceNode(new Point(RandomVector()), new Point(RandomVector()), 0.5f);
            DistanceNode otherNode = new DistanceNode(new Point(RandomVector()), new Point(RandomVector()), 0.2f);

            int expected = 1;
            int actual   = thisNode.CompareTo(otherNode);

            Assert.That(actual, Is.EqualTo(expected));
        }
        public void Handle(string instruction)
        {
            AbstractNode left = null, right = null;
            AbstractNode direction = null, action = null, distance = null;
            // 声明一个栈用以存储抽象语法树
            Stack <AbstractNode> nodeStack = new Stack <AbstractNode>();

            // 以空格分隔指令字符串
            string[] words = instruction.Split(' ');

            for (int i = 0; i < words.Length; i++)
            {
                // 这里采用栈的方式来处理指令,如果遇到"and",
                // 则将其后的3个单词作为3个终结符表达式连成一个简单句子
                // SentenseNode作为"and"的右表达式,而将从栈顶弹出的表达式作为"and"的左表达式,
                // 最后将"and"表达式压栈
                if (words[i].Equals("and", StringComparison.OrdinalIgnoreCase))
                {
                    // 弹出栈顶表达式作为左表达式
                    left = nodeStack.Pop();
                    // 构造右表达式
                    string word1 = words[++i];
                    direction = new DirectionNode(word1);
                    string word2 = words[++i];
                    action = new ActionNode(word2);
                    string word3 = words[++i];
                    distance = new DistanceNode(word3);

                    right = new SentenseNode(direction, action, distance);
                    // 新表达式压栈
                    AndNode newNode = new AndNode(left, right);
                    nodeStack.Push(newNode);
                }
                // 如果是从头开始进行解释,则将前3个单词组成一个简单句子SentenseNode并将该句子压栈
                else
                {
                    // 构造左表达式
                    string word1 = words[i];
                    direction = new DirectionNode(word1);
                    string word2 = words[++i];
                    action = new ActionNode(word2);
                    string word3 = words[++i];
                    distance = new DistanceNode(word3);

                    left = new SentenseNode(direction, action, distance);

                    nodeStack.Push(left);
                }
            }
            // 全部表达式出栈
            this.node = nodeStack.Pop();
        }
        private static void CheckAllButFirstElementForTargetPerson(DistanceNode targetPerson, List <Step> steps)
        {
            for (int stepIndex = 2; stepIndex < steps.Count - 1; stepIndex += 2)
            {
                var personNode = steps[stepIndex].Left;

                if (!(personNode.Tag is PersonNode))
                {
                    Assert.Fail("Node is not a person node.");
                }

                if (personNode.Name.Equals(targetPerson.Name))
                {
                    Assert.Fail("Target person can be found after start.");
                }
            }
        }
        private static void CheckAllButLastElementForSourcePerson(DistanceNode sourcePerson, List <Step> steps)
        {
            for (int stepIndex = 0; stepIndex < steps.Count - 1; stepIndex += 2)
            {
                var personNode = steps[stepIndex].Left;

                if (!(personNode.Tag is PersonNode))
                {
                    Assert.Fail("Node is not a person node.");
                }

                if (personNode.Name.Equals(sourcePerson.Name))
                {
                    Assert.Fail("Source person can be found before end.");
                }
            }
        }
        private static void AddPersonNodes(Graph graph, DistanceNode profileNode, IEnumerable <IPerson> people, HashSet <string> duplicateChecker)
        {
            foreach (var person in people)
            {
                if (!graph.TryGetDistanceNode(PersonNode.BuildNodeName(person), out var personNode))
                {
                    personNode = new PersonNode(person);

                    graph.AddNode(personNode);
                }

                if (duplicateChecker.Add(personNode.Name)) //a person can have multiple jobs in the same profiles
                {
                    graph.AddEdge(profileNode, personNode, 1, true);
                }

                ((PersonNode)personNode).AddJob((ProfileNode)profileNode, person);
            }
        }
        public void TestCompareTo_EqualToDistanceAndStaticEqual()
        {
            float        distance = Random.value;
            DistanceNode thisNode = new DistanceNode(
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                distance
                );
            DistanceNode otherNode = new DistanceNode(
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                new Point(new Vector3(100.0f, 200.0f, 300.0f)),
                distance
                );

            int actual   = thisNode.CompareTo(otherNode);
            int expected = -1;

            Assert.That(actual, Is.EqualTo(expected));
        }
        public void TestEquals_DistanceNotEqual()
        {
            DistanceNode thisNode = new DistanceNode(
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                new Point(new Vector3(4.0f, 5.0f, 6.0f)),
                7.0f
                );
            DistanceNode otherNode = new DistanceNode(
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                new Point(new Vector3(4.0f, 5.0f, 6.0f)),
                3.5f
                );

            bool expected = false;
            bool actual1  = thisNode.Equals(otherNode);
            bool actual2  = thisNode.Equals(otherNode);

            Assert.That(expected, Is.EqualTo(actual1));
            Assert.That(expected, Is.EqualTo(actual2));
        }
        internal ResultForm(Graph resultGraph, DistanceNode resultGraphTargetNode)
        {
            _resultGraph = resultGraph;

            _results = GraphHelper.GetPaths(resultGraphTargetNode);;

            InitializeComponent();

            Icon = Properties.Resource.djdsoft;

            ShowPeoplesJobInImageToolStripMenuItem.Checked = Properties.Settings.Default.ShowJobs;

            var rows = _results.Select(r => CreateRow(r)).ToArray();

            ResultListView.Items.AddRange(rows);

            if (ResultListView.Items.Count > 0)
            {
                ResultListView.Items[0].Selected = true;
            }
        }
        public void TestCompareTo_EqualTo()
        {
            float        distance = Random.value;
            DistanceNode thisNode = new DistanceNode(
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                new Point(new Vector3(4.0f, 2.0f, 5.0f)),
                distance
                );
            DistanceNode otherNode = new DistanceNode(
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                new Point(new Vector3(4.0f, 2.0f, 5.0f)),
                distance
                );

            int actual1  = thisNode.CompareTo(otherNode);
            int actual2  = otherNode.CompareTo(thisNode);
            int expected = 0;

            Assert.That(actual1, Is.EqualTo(expected));
            Assert.That(actual2, Is.EqualTo(expected));
        }
        public void TestEquals_ModelVectorNotEqual()
        {
            float        distance = Random.value;
            DistanceNode thisNode = new DistanceNode(
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                new Point(new Vector3(4.0f, 5.0f, 6.0f)),
                distance
                );
            DistanceNode otherNode = new DistanceNode(
                new Point(new Vector3(1.0f, 2.0f, 3.0f)),
                new Point(new Vector3(4.0f, 6.0f, 6.0f)),
                distance
                );

            bool expected = false;
            bool actual1  = thisNode.Equals(otherNode);
            bool actual2  = thisNode.Equals(otherNode);

            Assert.That(expected, Is.EqualTo(actual1));
            Assert.That(expected, Is.EqualTo(actual2));
        }
Esempio n. 13
0
        private static IEnumerable <Steps> GetSteps(DistanceNode targetNode, Steps steps)
        {
            if (targetNode.Distance == 0)
            {
                yield return(steps);
            }
            else
            {
                var predecessorNodes = targetNode.Predecessors.Cast <DistanceNode>();

                foreach (var predecessorNode in predecessorNodes)
                {
                    var nextStep = steps.Add(targetNode, predecessorNode);

                    var stepsList = GetSteps(predecessorNode, nextStep);

                    foreach (var stepItem in stepsList)
                    {
                        yield return(stepItem);
                    }
                }
            }
        }
        public void Test_Add_DistanceNode()
        {
            Point staticPoint = Auxilaries.RandomPoint();
            Point modelPoint  = Auxilaries.RandomPoint();
            float distance    = 3;

            DistanceNode node = new DistanceNode(
                modelPoint: modelPoint,
                staticPoint: staticPoint,
                distance: distance
                );

            CorrespondenceCollection actual = correspondences;

            correspondences.Add(node);

            modelPointList.Add(modelPoint);
            staticPointList.Add(staticPoint);
            correspondenceList.Add(new Correspondence(staticPoint: staticPoint, modelPoint: modelPoint));
            CorrespondenceCollection expected = new CorrespondenceCollection(modelPointList, staticPointList, correspondenceList);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 15
0
        public override Direction Think(GameState gs)
        {
            bool chased = false;

            foreach (Ghost ghost in gs.Ghosts)
            {
                if (ghost.Node == null || !ghost.Chasing || !ghost.Entered)
                {
                    continue;
                }
                Node.PathInfo path = ghost.Node.ShortestPath[gs.Pacman.Node.X, gs.Pacman.Node.Y, (int)Direction.None];
                if (path != null && path.Distance < 6 && path.Direction == ghost.Direction)
                {
                    chased = true;
                }
            }
            // powerpill taker
            foreach (Node node in gs.Map.Nodes)
            {
                if (node.Type == Node.NodeType.PowerPill)
                {
                    Node.PathInfo path = gs.Pacman.Node.ShortestPath[node.X, node.Y, (int)Direction.None];
                    if (path != null)
                    {
                        if (path.Distance < 3 || (chased && path.Distance < 7))
                        {
                            return(path.Direction);
                        }
                    }
                }
            }
            // danger
            double[] dist = { Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity };
            // calculate danger for each direction
            foreach (Ghost ghost in gs.Ghosts)
            {
                if (ghost.Node == null)
                {
                    continue;
                }
                if (ghost.Node.IsSame(gs.Pacman.Node) && gs.Pacman.PossibleDirection(gs.Pacman.Direction))
                {
                    return(gs.Pacman.Direction);
                }
                Node.PathInfo path = gs.Pacman.Node.ShortestPath[ghost.Node.X, ghost.Node.Y, (int)Direction.None];
                if (path != null)
                {
                    if ((ghost.Node.ShortestPath[gs.Pacman.Node.X, gs.Pacman.Node.Y, (int)Direction.None].Direction != ghost.InverseDirection(ghost.Direction) || path.Distance < 2) && // heading towards pacman
                        path.Distance < dist[(int)path.Direction] &&
                        path.Distance < 4 &&                                                                                                                                            // magic number
                        (ghost.Chasing || ghost.RemainingFlee < 200 || ghost.Entering))
                    {
                        dist[(int)path.Direction] = path.Distance;
                    }
                }
            }
            // calculate best
            List <Direction> possibleDirections = new List <Direction>();

            for (int i = 0; i < dist.Length; i++)
            {
                if (gs.Pacman.PossibleDirection((Direction)i) && dist[i] == Double.PositiveInfinity)
                {
                    possibleDirections.Add((Direction)i);
                }
            }
            // 0 => choose least dangerous // still needs more intelligence
            if (possibleDirections.Count == 0)
            {
                Direction bestDirection = Direction.None;
                double    bestDistance  = 0.0;
                for (int i = 0; i < dist.Length; i++)
                {
                    if (gs.Pacman.PossibleDirection((Direction)i) && dist[i] > bestDistance)
                    {
                        bestDirection = (Direction)i;
                        bestDistance  = dist[i];
                    }
                }
                if (debug)
                {
                    Console.WriteLine("0 options: " + bestDirection);
                }
                return(bestDirection);
            }
            // 1 => choose just that
            else if (possibleDirections.Count == 1)
            {
                if (debug)
                {
                    Console.WriteLine("1 option: " + possibleDirections[0]);
                }
                return(possibleDirections[0]);
            }
            // 2+ => choose ... ?
            else
            {
                if (debug)
                {
                    Console.Write(possibleDirections.Count + " options: ");
                }
                if (debug)
                {
                    foreach (Direction d in possibleDirections)
                    {
                        Console.Write(d + ", ");
                    }
                }
                if (debug)
                {
                    Console.WriteLine("");
                }
                Node.PathInfo bestPath = null;
                Node          bestNode = null;
                // dijkstra avoidance
                StateInfo.PillPath        avoidancePoint = null;
                LinkedList <DistanceNode> possibles;
                Prediction prediction = new Prediction(gs, 7);
                // find new target (pill furthest away)
                StateInfo.PillPath pillPath = StateInfo.FurthestPill(gs.Pacman.Node, gs);
                if (pillPath.PathInfo != null)
                {
                    avoidancePoint = pillPath;                     // maybe nearest powerpill (if exists) would be a good target?
                }
                List <DirectionDanger> directionDangers = new List <DirectionDanger>();
                if (avoidancePoint != null)
                {
                    // run dijkstra to find path
                    foreach (Direction d in gs.Pacman.PossibleDirections())
                    {
                        // find best direction
                        Direction bestDirection = Direction.None;
                        float     shortestRoute = float.MaxValue;
                        int       longestTime   = int.MaxValue;
                        possibles = new LinkedList <DistanceNode>();
                        possibles.AddFirst(new LinkedListNode <DistanceNode>(new DistanceNode(gs.Pacman.Node.GetNode(d), 0.0f, d, 1)));
                        while (possibles.Count > 0)
                        {
                            // find next lowest node
                            DistanceNode node = possibles.First.Value;
                            // found node
                            if (node.Node == avoidancePoint.Target)
                            {
                                if (node.Distance < shortestRoute)
                                {
                                    shortestRoute = node.Distance;
                                    longestTime   = node.Time;
                                    bestDirection = d;
                                }
                                break;
                            }
                            // pop node
                            possibles.Remove(node);
                            // add adjacents
                            foreach (Node gn in node.Node.GhostPossibles[(int)node.Direction])
                            {
                                // find danger
                                float danger = 0.0f;
                                if (node.Time < prediction.Iterations - 1)
                                {
                                    danger = prediction.DangerMaps[node.Time + 1].Danger[node.Node.X, node.Node.Y] +
                                             prediction.DangerMaps[node.Time + 1].Danger[gn.X, gn.Y];
                                    if (danger > 1.0f)
                                    {
                                        danger = 1.0f;
                                    }
                                }
                                // create new node
                                DistanceNode newNode = new DistanceNode(gn, node.Distance + danger, node.Node.GetDirection(gn), node.Time + 1);
                                // past iterations
                                if (newNode.Time > prediction.Iterations || gn.Type == Node.NodeType.PowerPill || newNode.Distance == 1.0)
                                {
                                    if (newNode.Distance < shortestRoute || (newNode.Distance == shortestRoute && newNode.Time > longestTime))
                                    {
                                        shortestRoute = newNode.Distance;
                                        longestTime   = newNode.Time;
                                        bestDirection = d;
                                    }
                                    continue;
                                }
                                LinkedListNode <DistanceNode> curNode = possibles.First;
                                while (curNode != null && curNode.Value.Distance < newNode.Distance)
                                {
                                    curNode = curNode.Next;
                                }
                                if (curNode == null)
                                {
                                    possibles.AddLast(newNode);
                                }
                                else
                                {
                                    possibles.AddAfter(curNode, newNode);
                                }
                            }
                        }
                        directionDangers.Add(new DirectionDanger(d, shortestRoute));
                    }
                }
                if (directionDangers.Count > 0)
                {
                    List <Direction> newPossibleDirections = new List <Direction>();
                    directionDangers.Sort(new Comparison <DirectionDanger>(delegate(DirectionDanger dd1, DirectionDanger dd2) {
                        if (dd1.Danger == dd2.Danger)
                        {
                            return(0);
                        }
                        if (dd1.Danger > dd2.Danger)
                        {
                            return(1);
                        }
                        return(-1);
                    }));
                    foreach (Direction possible in possibleDirections)
                    {
                        foreach (DirectionDanger dd in directionDangers)
                        {
                            if (dd.Direction == possible)
                            {
                                if (dd.Danger < 0.2)
                                {
                                    newPossibleDirections.Add(possible);
                                }
                                break;
                            }
                        }
                    }
                    if (newPossibleDirections.Count == 1)
                    {
                        return(newPossibleDirections[0]);
                    }
                    // 0 - return previous best
                    if (newPossibleDirections.Count == 0)
                    {
                        foreach (DirectionDanger dd in directionDangers)
                        {
                            foreach (Direction possible in possibleDirections)
                            {
                                if (dd.Direction == possible)
                                {
                                    return(possible);
                                }
                            }
                        }
                    }
                    possibleDirections = newPossibleDirections;
                }
                // hunt ghosts
                foreach (Ghost ghost in gs.Ghosts)
                {
                    if (ghost.Node == null || ghost.Chasing || !ghost.Entered)
                    {
                        continue;
                    }
                    Node.PathInfo ghostPath = ghost.Node.ShortestPath[gs.Pacman.Node.X, gs.Pacman.Node.Y, (int)Direction.None];
                    Node.PathInfo path      = gs.Pacman.Node.ShortestPath[ghost.Node.X, ghost.Node.Y, (int)Direction.None];
                    if (path != null && (path.Distance < 4 || (path.Distance < 7 && ghostPath.Direction == ghost.Direction)))
                    {
                        if (bestPath == null || path.Distance < bestPath.Distance)
                        {
                            bestPath = path;
                        }
                    }
                }
                // hunt pills
                if (bestPath == null)
                {
                    foreach (Node node in gs.Map.Nodes)
                    {
                        if (node.Type != Node.NodeType.Wall)
                        {
                            if (node.Type == Node.NodeType.Pill || node.Type == Node.NodeType.PowerPill)
                            {
                                Node.PathInfo curPath = gs.Pacman.Node.ShortestPath[node.X, node.Y, (int)Direction.None];
                                if (curPath != null)
                                {
                                    curPath = curPath.Clone();
                                }
                                else
                                {
                                    continue;
                                }
                                if (curPath.Direction == gs.Pacman.InverseDirection(gs.Pacman.Direction))
                                {
                                    curPath = new Node.PathInfo(curPath.Direction, curPath.Distance + 10);
                                }
                                List <Node> route = gs.Map.GetRoute(gs.Pacman.Node, node);
                                foreach (Node routeNode in route)
                                {
                                    if (routeNode.X == 0 && gs.Map.Tunnels[routeNode.Y])
                                    {
                                        curPath = new Node.PathInfo(curPath.Direction, curPath.Distance - 15);
                                        break;
                                    }
                                }
                                if (curPath != null && (bestPath == null || curPath.Distance <= bestPath.Distance))
                                {
                                    foreach (Direction d in possibleDirections)
                                    {
                                        if (d == curPath.Direction)
                                        {
                                            //if( bestPath == null || curPath.Distance < bestPath.Distance || (bestPath.Distance == curPath.Distance && GameState.Random.NextDouble() < 0.5) ) {
                                            bestNode = node;
                                            bestPath = curPath;
                                            if (debug)
                                            {
                                                Console.WriteLine("best pill: " + bestPath.Direction);
                                            }
                                            break;
                                            //}
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // return if found
                if (bestPath != null)
                {
                    if (debug)
                    {
                        Console.WriteLine("best: " + bestPath.Direction);
                    }
                    return(bestPath.Direction);
                }
                // follow same direction if it is in possibles
                foreach (Direction d in possibleDirections)
                {
                    if (d == gs.Pacman.Direction)
                    {
                        if (debug)
                        {
                            Console.WriteLine("follow direction: " + d);
                        }
                        return(d);
                    }
                }
                // otherwise choose randomly (?)
                int chosen = GameState.Random.Next(0, possibleDirections.Count - 1);
                if (debug)
                {
                    Console.WriteLine("random: " + possibleDirections[chosen]);
                }
                return(possibleDirections[chosen]);
            }
        }
Esempio n. 16
0
        public static IEnumerable <Steps> GetPaths(DistanceNode targetNode)
        {
            var result = GetSteps(targetNode, new Steps());

            return(result);
        }
Esempio n. 17
0
        /// <summary>
        /// 距離程の評価
        /// </summary>
        /// <param name="node">距離程ノード</param>
        /// <returns>null</returns>
        public override object Visit(DistanceNode node)
        {
            nowDistance = (double)Visit(node.Value);

            return(null);
        }
Esempio n. 18
0
 public abstract T Visit(DistanceNode node);
Esempio n. 19
0
 internal Steps Add(DistanceNode left, DistanceNode right) => new Steps(this._steps, new Step(left, right));
Esempio n. 20
0
        internal Step(DistanceNode left, DistanceNode right)
        {
            this.Left = left;

            this.Right = right;
        }
Esempio n. 21
0
 //Hier werden die Filme rausgefiltert
 internal static double GetRealMovieDistance(DistanceNode targetNode)
 {
     return(targetNode.Distance / 2);
 }