private void ViewPort_Loaded(object sender, RoutedEventArgs e) { width = (int)this.ViewPortContainer.ActualWidth; height = (int)this.ViewPortContainer.ActualHeight; writeableBitmap = BitmapFactory.New(width, height); ViewPort.Source = writeableBitmap; PrintTheMap(); CompositionTarget.Rendering += CompositionTarget_Rendering; MazeState state = new MazeState(); if (ALGORITHM_CHOOSER == 0) { var dfs = new DeepFirstSearcher(state); new Thread(() => dfs.Search()).Start(); } if (ALGORITHM_CHOOSER == 1) { var bfs = new BreadthFirstSearcher(state); new Thread(() => bfs.Search()).Start(); } if (ALGORITHM_CHOOSER == 2) { var ast = new AStarSearcher(state); new Thread(() => ast.Search()).Start(); } }
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); }
// Start is called before the first frame update void Start() { // Rigidbody2Dを取得する Rigidbody2D rd = GetComponent <Rigidbody2D>(); //目的地の設定 //餌の位置 targetFeed = GameObject.Find("feed").transform.position; //巣の位置 targetNest = GameObject.Find("nest").transform.position; //現在置 nowPlace = GameObject.Find("workingAnt").transform.position; //経路探索のプログラムにぶち込むためのnode作成 nodeGraph = new NodeGraph(); nodeNest = nodeGraph.CreateNode(targetNest); nodeFeed = nodeGraph.CreateNode(targetFeed); //現在は曲がり角を生成していないため、nodeは2つだけ //試しにtargetNestEntrance(巣の出入り口)なるものを曲がり角として用意してみる targetNestEntrance = GameObject.Find("nestEntrance").transform.position; nodeNestEntrance = nodeGraph.CreateNode(targetNestEntrance); // nodeGraph.ConnectNodes(new []{nodeNest, nodeNestEntrance,nodeFeed}); nodeGraph.ConnectNodes(new [] { nodeNest, nodeNestEntrance }); nodeGraph.ConnectNodes(new [] { nodeNestEntrance, nodeFeed }); //探索ごとにAStarSearcherインスタンスを作る searcher = new AStarSearcher(nodeGraph); //経路探索をする searcher.SearchRoute(nodeNest, nodeFeed); Debug.Log(searcher.Route); //結果はsearcher.Routeに入る foreach (var node in searcher.Route) { //vにnode.Posで値を取得する v.Add(node.Pos); Debug.Log(node.ToString()); } //ありの状態の取得 //初期位置 nowPlace = GameObject.Find("workingAnt").transform.position; //対象までにかかる時間 time = 250; //node間の移動を計算し足していく値 //最初は1番目の目的地へと向かう i = 1; target = v[i]; way = explore(target); float x = way.x; float y = way.y; Debug.Log(x); Debug.Log(y); //運搬能力 //ability = 1; }
//運搬能力 //public int ability = 1; // Start is called before the first frame update void Start() { antMovement = new AntMovement(); //目的地の設定 //餌と巣の位置情報はテスト用(後で削除または変更する可能性あり) //餌の位置 targetFeed = GameObject.Find("feed").transform.position; //巣の位置 targetNest = GameObject.Find("nest").transform.position; //現在置 //自身の座標を取得 antMovement.nowPlace = transform.position; //経路探索のプログラムにぶち込むためのnode作成 nodeGraph = new NodeGraph(); nodeNest = nodeGraph.CreateNode(targetNest); nodeFeed = nodeGraph.CreateNode(targetFeed); //現在は曲がり角を生成していないため、nodeは2つだけ //試しにtargetNestEntrance(巣の出入り口)なるものを曲がり角として用意してみる targetNestEntrance = GameObject.Find("nestEntrance").transform.position; nodeNestEntrance = nodeGraph.CreateNode(targetNestEntrance); //nodeをそれぞれつなげる作業 //nodeGraph.ConnectNodes(new []{from,to});→ fromからtoへnodeをつなげる //nodeGraph.ConnectNodes(new []{A, B,C});→ABCの循環円を作成 nodeGraph.ConnectNodes(new [] { nodeNest, nodeNestEntrance }); nodeGraph.ConnectNodes(new [] { nodeNestEntrance, nodeFeed }); //探索ごとにAStarSearcherインスタンスを作る searcher = new AStarSearcher(nodeGraph); //経路探索をする searcher.SearchRoute(nodeNest, nodeFeed); Debug.Log(searcher.Route); //結果はsearcher.Routeに入る foreach (var node in searcher.Route) { //vにnode.Posで値を取得する v.Add(node.WorldPosition); //Debug.Log(node.ToString()); I++; //vの大きさ } //ありの状態の取得 //初期位置 antMovement.nowPlace = transform.position; //アリのスピード取得(未取得) //public float speed = hogehoge.speed; //試験用スピード //これは後で削除 antMovement.speed = 0.01f; //node間の移動を計算し足していく値 //最初は1番目の目的地へと向かう i = 1; target = v[i]; antMovement.way = antMovement.Explore(target); }
// Start is called before the first frame update void Start() { Debug.Log("testStart"); //経路グラフはNodeGraphで表現する NodeGraph nodeGraph = new NodeGraph(); Node nodeA = nodeGraph.CreateNode(new Vector2(0, 0)); Node nodeB = nodeGraph.CreateNode(new Vector2(-5, 10)); Node nodeC = nodeGraph.CreateNode(new Vector2(0, 5)); Node nodeD = nodeGraph.CreateNode(new Vector2(10, 10)); Node nodeE = nodeGraph.CreateNode(new Vector2(10, 0)); //それぞれをつなげる nodeGraph.ConnectNodes(new [] { nodeA, nodeB, nodeC }); nodeGraph.ConnectNodes(new [] { nodeB, nodeC, nodeD }); nodeGraph.ConnectNodes(new [] { nodeA, nodeE }); nodeGraph.ConnectNodes(new [] { nodeD, nodeE }); Debug.Log(nodeA.ToString()); Debug.Log(nodeB.ToString()); Debug.Log(nodeC.ToString()); Debug.Log(nodeD.ToString()); Debug.Log(nodeE.ToString()); Debug.Log("graphOk"); Debug.Log("AStarSearchStart"); var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); //探索ごとにAStarSearcherインスタンスを作る AStarSearcher searcher = new AStarSearcher(nodeGraph); //経路探索をする searcher.SearchRoute(nodeB, nodeE); stopWatch.Stop(); Debug.Log(stopWatch.ElapsedMilliseconds + "ms"); Debug.Log("Route:"); //結果はsearcher.Routeに入る foreach (var node in searcher.Route) { Debug.Log(node.ToString()); } // Debug.Log(); }
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); }