コード例 #1
0
        public override SearchExitCode Search()
        {
            while (_frontier.Any())
            {
                Iterations++;
                var node = _frontier.Dequeue();
                Explored.Push(node.State);

                if (Problem.GoalTest(node.State))
                {
                    Solution = node.Path();
                    return(SearchExitCode.Success);
                }

                var face = node.Expand(Problem);

                face.ForEach(frontNode =>
                {
                    if (!Explored.Any(s => s.Equals(frontNode.State)) &&
                        !_frontier.Any(n => n.State.Equals(frontNode.State)))
                    {
                        _frontier.Enqueue(frontNode);
                    }
                });
            }

            return(SearchExitCode.Failure);
        }
コード例 #2
0
        public State Solve(State root)
        {
            States.Clear();
            Explored.Clear();
            States.Push(root);
            Visited++;
            while (StatesCount() > 0)
            {
                CurrentState = GetFromStates();

                if (!Explored.ContainsKey(CurrentState.ToString()))
                {
                    Explored.Add(CurrentState.ToString(), CurrentState.DepthLevel);
                    _explored++;
                }
                else if (CurrentState.DepthLevel < Explored[CurrentState.ToString()])
                {
                    Explored[CurrentState.ToString()] = CurrentState.DepthLevel;
                }

                MaxDepth = CurrentState.DepthLevel > MaxDepth ? CurrentState.DepthLevel : MaxDepth;

                //Check if state is solved, if its solved Write info to the file
                if (CurrentState.IsSolved())
                {
                    return(CurrentState);
                }

                AppendWithChildren();
            }

            return(null);
        }
コード例 #3
0
        public override Node Solve()
        {
            Stopwatch.Start();

            while (!CurrentNode.IsSolution())
            {
                Explored.Add(CurrentNode.ToString());
                AddChildNodes(CurrentNode);

                if (CurrentNode.CurrentPathCost > Information.DeepestLevelReached)
                {
                    Information.DeepestLevelReached = CurrentNode.CurrentPathCost;
                }

                if (CurrentNode.IsSolution())
                {
                    break;
                }

                KeyValuePair <Tuple <string, int>, Node> cheapestNode = _frontier.First();
                CurrentNode = cheapestNode.Value;
                _frontier.Remove(cheapestNode.Key);
            }

            Stopwatch.Stop();
            Information.ProcessingTime  = Stopwatch.Elapsed.TotalMilliseconds;
            Information.StatesProcessed = Explored.Count;

            return(CurrentNode);
        }
コード例 #4
0
ファイル: DFS.cs プロジェクト: 210342/GraphTraversal
        private INode Algorithm(INode node, List <IOperator> operatorsSequence, byte[] expectedSolution)
        {
            ExpectedSolution = expectedSolution;
            Explored.Add(node);
            if (CheckIfSolution(node))
            {
                return(node);
            }

            while (true)
            {
                foreach (IOperator op in operatorsSequence)
                {
                    INode kid = FindChild(node, op);
                    if (kid != null)
                    {
                        Depth = kid.Depth;
                        if (CheckIfSolution(kid))
                        {
                            return(kid);
                        }
                        else if (!Explored.Contains(kid) && kid.Depth < _depthLimit)
                        {
                            frontier.Push(kid);
                            Explored.Add(kid);
                        }
                    }
                }
                node = frontier.Pop();
            }
        }
コード例 #5
0
 public void CreepAndVisibility(Observation obs)
 {
     Explored.UpdateImage(obs.RawData.MapState.Visibility);
     Creep.UpdateImage(obs.RawData.MapState.Creep);
     foreach (var ps in obs.RawData.Player.PowerSources)
     {
         UpdatePower(ps);
     }
 }
コード例 #6
0
        /// <summary>
        /// Solve puzzle with selected function
        /// </summary>
        public virtual void Solve()
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();

            //States visited

            while (StatesCount() > 0)
            {
                CurrentState = GetFromStates();

                if (!Explored.ContainsKey(CurrentState.ToString()))
                {
                    Explored.Add(CurrentState.ToString(), CurrentState.DepthLevel);
                }
                else if (CurrentState.DepthLevel < Explored[CurrentState.ToString()])
                {
                    Explored[CurrentState.ToString()] = CurrentState.DepthLevel;
                }

                MaxDepth = CurrentState.DepthLevel > MaxDepth ? CurrentState.DepthLevel : MaxDepth;

                //Check if state is solved, if its solved Write info to the file
                if (CurrentState.IsSolved())
                {
                    timer.Stop();
                    SolvedState = CurrentState;
                    Console.WriteLine("Done!");
                    break;
                }

                AppendWithChildren();
            }
            if (timer.IsRunning)
            {
                timer.Stop();
            }

            DataWriter.WriteSolutionToFile(new InformationDataPack
            {
                SizeOfSolvedPuzzle = SolvedState?.DepthLevel ?? -1,
                Solution           = SolvedState?.GetPath() ?? "",
            }, SolutionPath);

            DataWriter.WriteInfoToFile(new InformationDataPack
            {
                DepthSize          = MaxDepth,
                SizeOfSolvedPuzzle = SolvedState?.DepthLevel ?? -1,
                StatesVisited      = Visited,
                StatesProcessed    = Explored.Count,
                Time = timer.Elapsed.TotalMilliseconds
            }, InfoPath);
        }
コード例 #7
0
ファイル: World.cs プロジェクト: pfarrel/TiberianStrike
 public void DrawFog(SpriteBatch spriteBatch)
 {
     for (int y = 0; y < Explored.GetLength(0); y++)
     {
         for (int x = 0; x < Explored.GetLength(1); x++)
         {
             if (!Explored[y, x])
             {
                 spriteBatch.Draw(Sprites.WhitePixel.Texture, new Rectangle(x * CellSize, y * CellSize, CellSize, CellSize), null, Color.Black, 0, Vector2.Zero, SpriteEffects.None, ZOrder.Fog);
             }
         }
     }
 }
コード例 #8
0
ファイル: World.cs プロジェクト: pfarrel/TiberianStrike
 public void Explore(Vector2 center, float range)
 {
     for (int y = 0; y < Explored.GetLength(0); y++)
     {
         for (int x = 0; x < Explored.GetLength(1); x++)
         {
             Vector2 cell = new Vector2(x * CellSize + CellSize / 2, y * CellSize + CellSize / 2);
             if (Vector2.Distance(center, cell) < range)
             {
                 Explored[y, x] = true;
             }
         }
     }
 }
コード例 #9
0
        public void RenderImageToDesktop(string filename)
        {
            var height = PlacementGrid.Height;
            var width  = PlacementGrid.Width;
            var bm     = new Bitmap(width, height);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if (BlockedGrid.IsSet(x, y))
                    {
                        bm.SetPixel(x, height - y - 1, Color.Red);
                    }
                    else if (Creep.IsSet(x, y))
                    {
                        bm.SetPixel(x, height - y - 1, Color.Purple);
                    }
                    else if (Explored.IsSet(x, y))
                    {
                        if (PlacementGrid.IsSet(x, y))
                        {
                            bm.SetPixel(x, height - y - 1, Color.LightBlue);
                        }
                        else
                        {
                            bm.SetPixel(x, height - y - 1, Color.DarkBlue);
                        }
                    }
                    else if (PlacementGrid.IsSet(x, y))
                    {
                        if (NaturalGrid.IsSet(x, y))
                        {
                            bm.SetPixel(x, height - y - 1, Color.Pink);
                        }
                        else
                        {
                            bm.SetPixel(x, height - y - 1, Color.Green);
                        }
                    }
                    else
                    {
                        bm.SetPixel(x, height - y - 1, Color.Black);
                    }
                }
            }
            bm.Save($"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\{filename}.BMP");
        }
コード例 #10
0
ファイル: AStar.cs プロジェクト: 210342/GraphTraversal
        private INode Algorithm(INode node, List <IOperator> operatorsSequence, byte[] expectedSolution)
        {
            ExpectedSolution = expectedSolution;
            frontier.AddFirst(node);
            Explored.Add(node);
            if (CheckIfSolution(node))
            {
                return(node);
            }

            while (true)
            {
                foreach (IOperator op in operatorsSequence)
                {
                    INode kid = FindChild(node, op);
                    if (kid != null)
                    {
                        Depth = kid.Depth;
                        if (CheckIfSolution(kid))
                        {
                            frontier.RemoveFirst();
                            return(kid);
                        }
                        else if (!Explored.Contains(kid))
                        {
                            kid.SummedCost = node.SummedCost + HeuristicFunction(kid);
                            if (kid.SummedCost < frontier.First.Value.SummedCost)
                            {
                                frontier.AddFirst(kid);
                            }
                            else
                            {
                                LinkedListNode <INode> InsertAfter = frontier.First;
                                while (InsertAfter.Next != null && kid.SummedCost > InsertAfter.Next.Value.SummedCost)
                                {
                                    InsertAfter = InsertAfter.Next;
                                }
                                frontier.AddAfter(InsertAfter, kid);
                            }
                            Explored.Add(kid);
                        }
                    }
                }
                frontier.RemoveFirst();
                node = frontier.First.Value;
            }
        }
コード例 #11
0
        public override SearchExitCode Search()
        {
            while (_frontier.Any())
            {
                Iterations++;
                var node = _frontier.First();
                _frontier.Remove(node);

                Explored.Push(node.State);

                if (Problem.GoalTest(node.State))
                {
                    Solution = node.Path();
                    return(SearchExitCode.Success);
                }

                var face = node.Expand(Problem).Select(HeuristicNode <TState, TAction> .From).ToList();

                face.ForEach(frontNode =>
                {
                    if (!Explored.Any(s => s.Equals(frontNode.State)) &&
                        !_frontier.Any(n => n.State.Equals(frontNode.State)))
                    {
                        AddToFrontier(frontNode);
                    }
                    else
                    {
                        _frontier.FirstOrNone(n =>
                                              n.State.Equals(frontNode.State) &&
                                              n.PathCost + _heuristic(n.State.Name, Problem.Goal.Name) >
                                              frontNode.PathCost + _heuristic(frontNode.State.Name, Problem.Goal.Name))
                        .IfNotNull(n =>
                        {
                            _frontier.Remove(n);
                            AddToFrontier(frontNode);
                        });
                    }
                });
            }

            return(SearchExitCode.Failure);
        }
コード例 #12
0
        /// <summary>
        /// Method that appends priority queue with new states from allowed moves for current state.
        /// </summary>
        protected void AppendWithChildren()
        {
            if (!CanMove())
            {
                return;
            }

            List <DirectionEnum> allowedMoves = GetAllMoves();

            foreach (var move in allowedMoves)
            {
                State newPuzzle = new State(DimensionX, DimensionY, CurrentState.Move(move), move, CurrentState.DepthLevel + 1, CurrentState);
                if (!Explored.ContainsKey(newPuzzle.ToString()) ||
                    Explored[newPuzzle.ToString()] > newPuzzle.DepthLevel)
                {
                    AddToStates(newPuzzle);
                    Visited++;
                }
            }
        }
コード例 #13
0
        public override SearchExitCode Search()
        {
            while (_frontier.Any())
            {
                Iterations++;
                var node = _frontier.First();
                _frontier.Remove(node);

                Explored.Push(node.State);

                if (Problem.GoalTest(node.State))
                {
                    Solution = node.Path();
                    return(SearchExitCode.Success);
                }

                var face = node.Expand(Problem);

                face.ForEach(frontNode =>
                {
                    if (!Explored.Any(s => s.Equals(frontNode.State)) &&
                        !_frontier.Any(n => n.State.Equals(frontNode.State)))
                    {
                        _frontier.Add(frontNode);
                        _frontier.Sort();
                    }
                    else
                    {
                        _frontier.FirstOrNone(n => n.State.Equals(frontNode.State) && n.PathCost > frontNode.PathCost)
                        .IfNotNull(n =>
                        {
                            _frontier.Remove(n);
                            _frontier.Add(frontNode);
                            _frontier.Sort();
                        });
                    }
                });
            }

            return(SearchExitCode.Failure);
        }
コード例 #14
0
ファイル: DFSSolver.cs プロジェクト: Buk1m/FifteenPuzzle
        public override Node Solve()
        {
            Stopwatch.Start();
            _frontier.Push(CurrentNode);
            while (!CurrentNode.IsSolution())
            {
                CurrentNode = _frontier.Pop();
                Explored.Add(CurrentNode.ToString() + CurrentNode.CurrentPathCost);
                AddChildNodes(CurrentNode);

                if (Information.DeepestLevelReached < CurrentNode.CurrentPathCost)
                {
                    Information.DeepestLevelReached = CurrentNode.CurrentPathCost;
                }
            }

            Stopwatch.Stop();
            Information.ProcessingTime  = Stopwatch.Elapsed.TotalMilliseconds;
            Information.StatesProcessed = Explored.Count;

            return(CurrentNode);
        }
コード例 #15
0
ファイル: BFSSolver.cs プロジェクト: Buk1m/FifteenPuzzle
        public override Node Solve()
        {
            Stopwatch.Start();
            while (!CurrentNode.IsSolution())
            {
                Explored.Add(CurrentNode.ToString());

                AddChildNodes(CurrentNode);
                if (CurrentNode.IsSolution())
                {
                    break;
                }

                CurrentNode = _queue.Dequeue();
            }

            Stopwatch.Stop();

            Information.DeepestLevelReached = CurrentNode.CurrentPathCost;
            Information.ProcessingTime      = Stopwatch.Elapsed.TotalMilliseconds;
            Information.StatesProcessed     = Explored.Count;

            return(CurrentNode);
        }
コード例 #16
0
        public override bool Search(World world)
        {
            // Starting Node in the world
            Node currentNode = world.Start;
            // Starting path that contains only the node
            Path        currentPath = new Path();
            List <Node> children;

            // If the goal node is the current Node, then return true with path of only that node
            if (world.Goal == currentNode)
            {
                CompletedSearchPath.NodePath.Add(currentNode);
                return(true);
            }
            // Add Node to the frontier
            FrontierNode.Enqueue(currentNode);
            // Add initial Path to the frontier
            FrontierPath.Enqueue(currentPath);

            // while frontier is not empty
            while (FrontierNode.Count > 0 && FrontierPath.Count > 0)
            {
                // get next node
                currentNode = FrontierNode.Dequeue();
                // get next Path
                currentPath = FrontierPath.Dequeue();

                // add this node to explored list
                Explored.Add(currentNode);
                // add this node to the path
                currentPath.Add(currentNode);
                // Get children of the current node
                children = currentNode.GetChildren();

                foreach (Node n in children)
                {
                    if (!FrontierNode.Contains(n) && !Explored.Contains(n))
                    {
                        if (n is GoalNode)
                        {
                            currentPath.Add(n);
                            CompletedSearchPath = currentPath;
                            Console.Clear();
                            return(true);
                        }
                        else
                        {
                            if (!(n is WallNode))
                            {
                                // Add
                                FrontierNode.Enqueue(n);
                                FrontierPath.Enqueue(new Path(currentPath));
                            }
                        }
                    }
                }
                Console.Clear();
                world.PrintPath(currentPath);
                // 0.5s between each loop to help visualise.
                System.Threading.Thread.Sleep(300);
            }
            // if program gets there then there is no solution
            return(false);
        }
コード例 #17
0
ファイル: DFSSolver.cs プロジェクト: Buk1m/FifteenPuzzle
 private bool ExploredNotContainsNode(Node nextNode)
 {
     return(!Explored.Contains(nextNode.ToString() + (CurrentNode.CurrentPathCost - 1)));
 }