Exemple #1
0
        private void WidaButton_Click(object sender, EventArgs e)
        {
            var astar = new IteratedDeepeningAStarSearcher <SlidingTilesPuzzle.State, int>(config =>
            {
                config.ActionsListFunc = x =>
                {
                    return(x.Data.GetActions().Where(a => x.Parent == null || x.Parent.Data.Blank != a));
                };
                config.ActionApplierFunc           = (x, a) => x.Data.Clone().ApplyAction(a);
                config.ActionCostFunc              = (x, a) => 1;
                config.HeuristicFunc               = SlidingTilePuzzleHeuristics.GetManhattanDistance;
                config.IsGoalFunc                  = x => x.Data.IsSolution;
                config.HeuristicWeighting          = float.Parse(WidaValueText.Text);
                config.RoundEstimatedRemainingCost = true;
            });

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

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

            var solution = astar.Search(_currentState);

            updateCounts(true);
        }
        private void WidaButton_Click(object sender, EventArgs e)
        {
            var astar = new IteratedDeepeningAStarSearcher<SlidingTilesPuzzle.State, int>(config =>
            {
                config.ActionsListFunc = x =>
                {
                    return x.Data.GetActions().Where(a => x.Parent == null || x.Parent.Data.Blank != a);
                };
                config.ActionApplierFunc = (x, a) => x.Data.Clone().ApplyAction(a);
                config.ActionCostFunc = (x, a) => 1;
                config.HeuristicFunc = SlidingTilePuzzleHeuristics.GetManhattanDistance;
                config.IsGoalFunc = x => x.Data.IsSolution;
                config.HeuristicWeighting = float.Parse(WidaValueText.Text);
                config.RoundEstimatedRemainingCost = true;
            });

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

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

            var solution = astar.Search(_currentState);

            updateCounts(true);
        }