Exemplo n.º 1
0
 /// <summary>
 /// Called by Unity when the PathPlanner object is created.
 /// </summary>
 public void Start()
 {
     heap = new TileHeap(Map);
     this.UpdateOverlayVisibility();
 }
Exemplo n.º 2
0
 protected internal Node(TilePosition position, TileHeap heap)
 {
     this.Position = position;
     this.heap = heap;
 }
Exemplo n.º 3
0
    ///// <summary>
    ///// Find the shortest path using Dijkstra's algorithm
    ///// </summary>
    //public TilePath Dijkstra(TilePosition start, TileRect end)
    //{
    //    heap.Clear();
    //    searchedTilesOverlay.Clear();
    //    var startNode = heap.NodeAt(start);
    //    startNode.Predecessor = null;
    //    heap.DecreaseKey(startNode, 0, 0);
    //    var currentNode = startNode;
    //    while (!heap.IsEmpty && !end.Contains((currentNode = heap.ExtractMin()).Position))
    //    {
    //        searchedTilesOverlay.Add(currentNode.Position);
    //        foreach (var n in currentNode.Neighbors)
    //        {
    //            float newDistanceFromStart = currentNode.DistanceFromStart
    //                             + TilePosition.EuclideanDistance(currentNode.Position, n.Position);
    //            if (n.DistanceFromStart > newDistanceFromStart)
    //            {
    //                n.Predecessor = currentNode;
    //                heap.DecreaseKey(n, newDistanceFromStart, newDistanceFromStart);
    //            }
    //        }
    //    }
    //    heap.SetOverlayToComputedPath(this.computedPathOverlay, currentNode);

    //    if (!end.Contains(currentNode.Position))
    //        return null;

    //    return this.MakePath(currentNode);
    //}

    /// <summary>
    /// Make a path object from the nodes from the search.
    /// Starts at the end node and tracks backward, following predecessor links, until it finds the start node.
    /// </summary>
    /// <param name="endNode">The ending node of the path</param>
    /// <returns>The reconstructed path</returns>
    private TilePath MakePath(TileHeap.Node endNode)
    {
        // Reconstruct the path to from the start to the end.
        var path = new TilePath(endNode.Position);
        while (endNode != null)
        {
            path.AddBefore(endNode.Position.TileCenter);
            endNode = endNode.Predecessor;
        }
        return path;
    }
Exemplo n.º 4
0
 public void Before()
 {
     th = new TileHeap(10, 10);
 }
Exemplo n.º 5
0
 protected internal Node(TilePosition position, TileHeap heap)
 {
     this.Position = position;
     this.heap     = heap;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Called by Unity when the PathPlanner object is created.
 /// </summary>
 public void Start()
 {
     heap = new TileHeap(Map);
     this.UpdateOverlayVisibility();
 }
Exemplo n.º 7
0
        /// <summary>
        /// Runs the SerialSolver
        /// </summary>
        /// <param name="ng">Nonogram to solve</param>
        /// <returns>Number of solver tiles or -1 if a contradiction was encountered</returns>
        public int Run(Nonogram ng)
        {
            _ng        = ng.Copy();
            _benchTime = TimeSpan.Zero;
            _results   = new List <Result>();
            LineSolver ls = new LineSolver();

            if (ls.Run(_ng) > 0)
            {
                if (ls.Solved())
                {
                    _results   = ls.Results();
                    _benchTime = ls.BenchTime();
                    _solved    = true;
                    return(_results.Count);
                }
            }
            _benchTime = ls.BenchTime();
            Stopwatch sw = Stopwatch.StartNew();

            _th = new TileHeap(_ng.Width, ng.Height);
            Update(ls.Results());
            for (int i = 0; i < _ng.Width; i++)
            {
                _th.Add(0, i, _ng.GetPrio(0, i));
                _th.Add(_ng.Height - 1, i, _ng.GetPrio(_ng.Height - 1, i));
            }
            for (int i = 1; i < _ng.Height - 1; i++)
            {
                _th.Add(i, 0, _ng.GetPrio(i, 0));
                _th.Add(i, _ng.Width - 1, _ng.GetPrio(i, _ng.Width - 1));
            }
            _benchTime = _benchTime.Add(sw.Elapsed);
            TrialSolver trS = new TrialSolver();

            while (!_th.IsEmpty)
            {
                Coordinate c = _th.Poll();
                if (!_ng.Resolved(c.Row, c.Column))
                {
                    int res = trS.Run(_ng, c.Row, c.Column);
                    if (res == -1)
                    {
                        return(_results.Count);
                    }
                    _benchTime = _benchTime.Add(trS.BenchTime());
                    sw         = Stopwatch.StartNew();
                    Update(trS.Results());
                    _benchTime = _benchTime.Add(sw.Elapsed);
                    if (_ng.LeftToClear == 0)
                    {
                        _solved = true;
                        return(_results.Count);
                    }
                }
            }
            if (_ng.LeftToClear < 201 || !_smalltree)
            {
                ISolver ts = new TreeSolver();
                ts.Run(_ng);
                _benchTime = _benchTime.Add(ts.BenchTime());
                Update(ts.Results());
                _solved = true;
            }
            return(_results.Count);
        }