Beispiel #1
0
        public void return_null_if_starting_vertex_not_in_graph()
        {
            var a = new Vertex <string>
            {
                Value = "Dog"
            };
            var b = new Vertex <string>
            {
                Value = "Dog"
            };
            var c = new Vertex <string>
            {
                Value = "Dog"
            };

            a.AddEdge(b);
            b.AddEdge(c);
            var graph = new Graph <string>();

            graph.AddVertex(b);
            var dfsSearcher = new DepthFirstSearcher <string>();

            var result = dfsSearcher.Search(graph, a, s => s == "Dog");

            result.Should().Be(null);
        }
Beispiel #2
0
        public void only_search_nodes_on_graph()
        {
            var a = new Vertex <string>
            {
                Value = "Cat"
            };
            var b = new Vertex <string>
            {
                Value = "Rat"
            };
            var c = new Vertex <string>
            {
                Value = "Dog"
            };
            var d = new Vertex <string>
            {
                Value = "Dog"
            };

            a.AddEdge(b);
            b.AddEdge(c);
            a.AddEdge(d);
            var graph = new Graph <string>();

            graph.AddVertex(a);
            graph.AddVertex(b);
            graph.AddVertex(c);
            var dfsSearcher = new DepthFirstSearcher <string>();

            var result = dfsSearcher.Search(graph, a, s => s == "Dog");

            result.Should().Be(c);
        }
Beispiel #3
0
        public bool SearchDepthFirst(T thing)
        {
            if (_root == null)
            {
                return(false);
            }

            var dfs = new DepthFirstSearcher <T>();

            return(dfs.Search(_root, thing));
        }
Beispiel #4
0
        public void search_depth_first()
        {
            var a = new Vertex <string>
            {
                Value = "Cat"
            };
            var b = new Vertex <string>
            {
                Value = "Rat"
            };
            var c = new Vertex <string>
            {
                Value = "Dog"
            };
            var d = new Vertex <string>
            {
                Value = "Turtle"
            };
            var e = new Vertex <string>
            {
                Value = "Parrot"
            };
            var f = new Vertex <string>
            {
                Value = "Raccoon"
            };
            var g = new Vertex <string>
            {
                Value = "Dog"
            };

            a.AddEdge(b);
            b.AddEdge(c);
            a.AddEdge(d);
            d.AddEdge(e);
            e.AddEdge(f);
            f.AddEdge(g);
            var graph = new Graph <string>();

            graph.AddVertex(a);
            graph.AddVertex(b);
            graph.AddVertex(c);
            graph.AddVertex(d);
            graph.AddVertex(e);
            graph.AddVertex(f);
            graph.AddVertex(g);

            var dfsSearcher = new DepthFirstSearcher <string>();

            var result = dfsSearcher.Search(graph, a, s => s == "Dog");

            result.Should().Be(g);
        }
Beispiel #5
0
        public void return_starting_vertex_if_meets_criteria()
        {
            var a = new Vertex <string>
            {
                Value = "Dog"
            };
            var b = new Vertex <string>
            {
                Value = "Dog"
            };

            a.AddEdge(b);
            var graph = new Graph <string>();

            graph.AddVertex(a);
            graph.AddVertex(b);
            var dfsSearcher = new DepthFirstSearcher <string>();

            var result = dfsSearcher.Search(graph, a, s => s == "Dog");

            result.Should().Be(a);
        }
Beispiel #6
0
        private void DfsButton_Click(object sender, EventArgs e)
        {
            // Without any I/D or heuristics this is the same as random search

            var tree = new SearchTree <Problems.SlidingTilesPuzzle.State>(_currentState);
            var dfs  = new DepthFirstSearcher <Problems.SlidingTilesPuzzle.State, int>(
                tree,
                x => x.GetActions().RandomizeOrder(),
                (x, a) => x.ApplyAction(a), // Don't need to clone because DFS is one way
                (x, a) => 1,
                x => x.IsSolution);

            var           updateCount  = 0;
            var           start        = DateTime.Now;
            Action <bool> updateCounts = (force) =>
            {
                if (!force && ++updateCount % 10000 > 0)
                {
                    return;
                }
                NodesGeneratedLabel.Text = "Generated = " + dfs.NodesGeneratedCount + " ";
                ExpandedNodesLabel.Text  = "Expanded = " + dfs.NodesExpandedCount + " ";
                RetainedNodesLabel.Text  = "Retained = " + dfs.NodesRetainedCount + " ";
                SecondsLabel.Text        = DateTime.Now.Subtract(start).TotalSeconds.ToString();
                _currentState            = dfs.CurrentNode.Data;
                panel1.Invalidate();
                Application.DoEvents();
            };

            dfs.NodeExpanded  += (s, a) => updateCounts(false);
            dfs.NodeGenerated += (s, a) => updateCounts(false);

            var solution = dfs.Search();

            updateCounts(true);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            var initialState = Enumerable.Range(0, _problem.Size).Select(x => - 1).ToArray();
            var tree         = new SearchTree <Problems.QueensPuzzle.State>(_problem.CreateState(initialState));
            var dfs          = new DepthFirstSearcher <Problems.QueensPuzzle.State, int>(
                tree,
                GetPossibleActions,
                ApplyAction,
                (x, a) => 1,
                x => x.IsSolution);


            var           updateCount  = 0;
            var           start        = DateTime.Now;
            Action <bool> updateCounts = (force) =>
            {
                if (!force && ++updateCount % 10000 > 0)
                {
                    return;
                }
                NodesGeneratedLabel.Text = "Generated = " + dfs.NodesGeneratedCount + " ";
                ExpandedNodesLabel.Text  = "Expanded = " + dfs.NodesExpandedCount + " ";
                RetainedNodesLabel.Text  = "Retained = " + dfs.NodesRetainedCount + " ";
                SecondsLabel.Text        = DateTime.Now.Subtract(start).TotalSeconds.ToString();
                _currentState            = dfs.CurrentNode.Data;
                panel1.Invalidate();
                Application.DoEvents();
            };

            dfs.NodeExpanded  += (s, a) => updateCounts(false);
            dfs.NodeGenerated += (s, a) => updateCounts(false);

            var solution = dfs.Search();

            updateCounts(true);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            var initialState = Enumerable.Range(0, _problem.Size).Select(x => -1).ToArray();
            var tree = new SearchTree<Problems.QueensPuzzle.State>(_problem.CreateState(initialState));
            var dfs = new DepthFirstSearcher<Problems.QueensPuzzle.State, int>(
                tree,
                GetPossibleActions,
                ApplyAction,
                (x, a) => 1,
                x => x.IsSolution);

            var updateCount = 0;
            var start = DateTime.Now;
            Action<bool> updateCounts = (force) =>
            {
                if (!force && ++updateCount % 10000 > 0) return;
                NodesGeneratedLabel.Text = "Generated = " + dfs.NodesGeneratedCount + " ";
                ExpandedNodesLabel.Text = "Expanded = " + dfs.NodesExpandedCount + " ";
                RetainedNodesLabel.Text = "Retained = " + dfs.NodesRetainedCount + " ";
                SecondsLabel.Text = DateTime.Now.Subtract(start).TotalSeconds.ToString();
                _currentState = dfs.CurrentNode.Data;
                panel1.Invalidate();
                Application.DoEvents();
            };

            dfs.NodeExpanded += (s,a) => updateCounts(false);
            dfs.NodeGenerated += (s, a) => updateCounts(false);

            var solution = dfs.Search();

            updateCounts(true);
        }
        private void DfsButton_Click(object sender, EventArgs e)
        {
            // Without any I/D or heuristics this is the same as random search

            var tree = new SearchTree<Problems.SlidingTilesPuzzle.State>(_currentState);
            var dfs = new DepthFirstSearcher<Problems.SlidingTilesPuzzle.State, int>(
                tree,
                x => x.GetActions().RandomizeOrder(),
                (x, a) => x.ApplyAction(a), // Don't need to clone because DFS is one way
                (x, a) => 1,
                x => x.IsSolution);

            var updateCount = 0;
            var start = DateTime.Now;
            Action<bool> updateCounts = (force) =>
            {
                if (!force && ++updateCount % 10000 > 0) return;
                NodesGeneratedLabel.Text = "Generated = " + dfs.NodesGeneratedCount + " ";
                ExpandedNodesLabel.Text = "Expanded = " + dfs.NodesExpandedCount + " ";
                RetainedNodesLabel.Text = "Retained = " + dfs.NodesRetainedCount + " ";
                SecondsLabel.Text = DateTime.Now.Subtract(start).TotalSeconds.ToString();
                _currentState = dfs.CurrentNode.Data;
                panel1.Invalidate();
                Application.DoEvents();
            };

            dfs.NodeExpanded += (s,a) => updateCounts(false);
            dfs.NodeGenerated += (s, a) => updateCounts(false);

            var solution = dfs.Search();

            updateCounts(true);
        }