private void RecreatePuzzle() { Text = "Size: " + trackBar1.Value + ", moves: " + trackBar2.Value; _problem = new Problems.SlidingTilesPuzzle(trackBar1.Value); _currentState = _problem.CreateJumbledState(trackBar2.Value); panel1.Invalidate(); }
private void IDNaiveButton_Click(object sender, EventArgs e) { var tree = new SearchTree <Problems.SlidingTilesPuzzle.State>(_currentState); var dfs = new DepthFirstSearcher <Problems.SlidingTilesPuzzle.State, int>( tree, x => x.GetActions(), (x, a) => x.Clone().ApplyAction(a), (x, a) => 1, x => x.IsSolution); var id = new IterativeDeepeningDepthFirstSearch <Problems.SlidingTilesPuzzle.State, int>(dfs); 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() + " (next depth = " + dfs.NextCostThreshhold + ")"; _currentState = dfs.CurrentNode.Data; panel1.Invalidate(); Application.DoEvents(); }; dfs.NodeExpanded += (s, a) => updateCounts(false); dfs.NodeGenerated += (s, a) => updateCounts(false); var solution = id.Search(); updateCounts(true); }
public static float GetManhattanDistance(SlidingTilesPuzzle.State state) { Cell[] coords; if (!_cache.TryGetValue(state.Size * state.Size, out coords)) { coords = new Cell[state.Size * state.Size]; for (var i = 0; i < state.Size * state.Size; i++) { coords[i] = new Cell { Row = (int)(i / (state.Size)), Col = i % (state.Size) }; } _cache.Add(state.Size * state.Size, coords); } var result = 0; for (var i = 0; i < state.Size * state.Size; i++) { if (i == state.Blank) { continue; } var v1 = coords[state.Tiles[i]].Row - coords[i].Row; var v2 = coords[state.Tiles[i]].Col - coords[i].Col; result += (v1 < 0 ? -v1 : v1) + (v2 < 0 ? -v2 : v2); } return(result); }
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 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 AStarButton_Click(object sender, EventArgs e) { var astar = new AStarSearcher<Problems.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; }); var updateCount = 0; var start = DateTime.Now; Action<bool> updateCounts = (force) => { if (!force && ++updateCount % 10000 > 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() + " (next depth = " + astar.NextCostLimitThreshhold + ")"; _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 RecreatePuzzle() { Text = "Size: " + trackBar1.Value + ", moves: " + trackBar2.Value; _problem = new Problems.SlidingTilesPuzzle(trackBar1.Value); _currentState = _problem.CreateJumbledState(trackBar2.Value); panel1.Invalidate(); }
private void IDNaiveButton_Click(object sender, EventArgs e) { var tree = new SearchTree<Problems.SlidingTilesPuzzle.State>(_currentState); var dfs = new DepthFirstSearcher<Problems.SlidingTilesPuzzle.State, int>( tree, x => x.GetActions(), (x, a) => x.Clone().ApplyAction(a), (x, a) => 1, x => x.IsSolution); var id = new IterativeDeepeningDepthFirstSearch<Problems.SlidingTilesPuzzle.State, int>(dfs); 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() + " (next depth = " + dfs.NextCostThreshhold + ")"; _currentState = dfs.CurrentNode.Data; panel1.Invalidate(); Application.DoEvents(); }; dfs.NodeExpanded += (s, a) => updateCounts(false); dfs.NodeGenerated += (s, a) => updateCounts(false); var solution = id.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); }