Exemplo n.º 1
0
        public IEnumerable <IStarNode> TravelPath(IStarNode start, IStarNode goal = null)
        {
            _searchSet.Add(start);

            while (_searchSet.Count(x => !x.IsVisited) > 0)
            {
                var current = _searchSet.Where(x => !x.IsVisited)
                              .OrderBy(o => o.TravelScore)
                              .FirstOrDefault();

                current.IsVisited = true;

                var edges = current.GetEdges().Where(e => e.Child.IsVisited == false);

                foreach (var edge in edges)
                {
                    EvaluatePath(current, edge);
                }

                yield return(current);

                if (goal != null && current == goal)
                {
                    break;
                }
            }
        }
Exemplo n.º 2
0
        public IStarNode Search(IStarNode start, IStarNode goal)
        {
            HashSet <IStarNode> closedSet = new HashSet <IStarNode>();
            HashSet <IStarNode> openSet   = new HashSet <IStarNode>();

            openSet.Add(start);
            Dictionary <IStarNode, List <IStarNode> > cameFrom = new Dictionary <IStarNode, List <IStarNode> >();

            _Scores.SetScore(ScoreType.SCORE_OF_DISTANCE, start, 0);
            _Scores.SetScore(ScoreType.SCORE_OF_TOTAL, start, _HDelegate(start, goal));



            while (openSet.Count > 0)
            {
                var current = openSet.OrderBy(o => _Scores[ScoreType.SCORE_OF_TOTAL, o]).FirstOrDefault();

                if (current.Equals(goal))
                {
                    current.PathToMe.Add(current);
                    return(current);
                }

                if (closedSet.Contains(current))
                {
                    continue;
                }

                openSet.Remove(current);
                closedSet.Add(current);
                foreach (var edges in current.GetEdges())
                {
                    if (closedSet.Contains(edges.Child))
                    {
                        continue;
                    }

                    var tgSCore = _Scores[ScoreType.SCORE_OF_DISTANCE, current] + _DistanceEvaluator(edges);
                    if (tgSCore >= _Scores[ScoreType.SCORE_OF_DISTANCE, edges.Child])
                    {
                        continue;
                    }

                    // edges
                    edges.Child.SetSCore(_Scores[ScoreType.SCORE_OF_DISTANCE, edges.Child]
                                         + _HDelegate(edges.Child, goal));
                    edges.Child.PathToMe = (current.PathToMe.ToList());
                    edges.Child.PathToMe.Add(current);
                    openSet.Add(edges.Child);

                    // _Scores
                    _Scores.SetScore(ScoreType.SCORE_OF_DISTANCE, edges.Child, tgSCore);
                    _Scores.SetScore(ScoreType.SCORE_OF_TOTAL, edges.Child, _Scores[ScoreType.SCORE_OF_DISTANCE, edges.Child]
                                     + _HDelegate(edges.Child, goal));
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        public void SetScore(ScoreType type, IStarNode node, float score)
        {
            if (!_Scores.ContainsKey(type))
            {
                _Scores.Add(type, new Dictionary <IStarNode, float>());
            }
            if (_Scores[type].ContainsKey(node))
            {
                _Scores[type].Remove(node);
            }

            _Scores[type].Add(node, score);
        }
Exemplo n.º 4
0
 public float this[ScoreType type, IStarNode node]
 {
     get {
         if (_Scores.ContainsKey(type) && _Scores[type].ContainsKey(node))
         {
             return(_Scores[type][node]);
         }
         else
         {
             return(DefaultValue);
         }
     }
 }
Exemplo n.º 5
0
        private void EvaluatePath(IStarNode from, IStarEdge edges)
        {
            var travelScore = from.TravelScore + _distanceEvaluater(edges);

            if (_evaluateComparer(edges, travelScore))
            {
                return;
            }

            edges.Child.PathToMe = from.PathToMe.ToList();

            from.Depth = edges.Child.PathToMe.Count;

            if (!edges.Child.PathToMe.Contains(from))
            {
                edges.Child.PathToMe.Add(from);
            }

            edges.Child.TravelScore = travelScore;

            _searchSet.Add(edges.Child);
        }
Exemplo n.º 6
0
 public Edge(IStarNode from, IStarNode to, float weight)
 {
     Weight = weight;
     Source = from;
     Child  = to;
 }
Exemplo n.º 7
0
 public int CompareTo(IStarNode other)
 {
     return(_score.CompareTo(other.GetScore()));
 }
Exemplo n.º 8
0
 public bool Equals(IStarNode other) => _Data == ((Node)other)._Data;
Exemplo n.º 9
0
        public IStarNode Search(IStarNode start,
                                IStarNode goal, int numberOfWorkers)
        {
            IStarNode best = null;
            ConcurrentDictionary <int, IStarNode> closedSet = new ConcurrentDictionary <int, IStarNode>();
            ConcurrentDictionary <int, IStarNode> openSet   = new ConcurrentDictionary <int, IStarNode>();

            openSet.TryAdd(start.GetHashCode(), start);
            _Scores.SetScore(ScoreType.SCORE_OF_DISTANCE, start, 0);
            _Scores.SetScore(ScoreType.SCORE_OF_TOTAL, start, _HDelegate(start, goal));
            List <Action> actions = new List <Action>();

            for (int i = 0; i < numberOfWorkers; i++)
            {
                actions.Add(() =>
                {
                    while (openSet.Count > 0)
                    {
                        lock (_Scores)
                        {
                            var current = openSet.Select(x => x.Value)
                                          .OrderBy(o => _Scores[ScoreType.SCORE_OF_TOTAL, o]).FirstOrDefault();

                            if (current == null)
                            {
                                break;
                            }

                            if (current.IsVisited())
                            {
                                openSet.TryRemove(current.GetHashCode(), out IStarNode removed);
                                if (!closedSet.ContainsKey(current.GetHashCode()))
                                {
                                    closedSet.TryAdd(current.GetHashCode(), current);
                                }
                                continue;
                            }
                            else
                            {
                                current.Visit();
                            }

                            if (current.Equals(goal) && best == null)
                            {
                                current.PathToMe.Add(current);
                                best = current;
                                break;
                            }
                            if (closedSet.ContainsKey(current.GetHashCode()))
                            {
                                continue;
                            }
                            openSet.TryRemove(current.GetHashCode(), out IStarNode some);
                            closedSet.TryAdd(current.GetHashCode(), current);

                            foreach (var edges in current.GetEdges())
                            {
                                if (closedSet.ContainsKey(edges.Child.GetHashCode()))
                                {
                                    continue;
                                }
                                var tgSCore = _Scores[ScoreType.SCORE_OF_DISTANCE, current] + _DistanceEvaluator(edges);
                                if (tgSCore >= _Scores[ScoreType.SCORE_OF_DISTANCE, edges.Child])
                                {
                                    continue;
                                }
                                edges.Child.SetSCore(_Scores[ScoreType.SCORE_OF_DISTANCE, edges.Child]
                                                     + _HDelegate(edges.Child, goal));
                                edges.Child.PathToMe = (current.PathToMe.ToList());
                                edges.Child.PathToMe.Add(current);
                                openSet.TryAdd(edges.Child.GetHashCode(), edges.Child);
                                _Scores.SetScore(ScoreType.SCORE_OF_DISTANCE, edges.Child, tgSCore);
                                _Scores.SetScore(ScoreType.SCORE_OF_TOTAL, edges.Child, _Scores[ScoreType.SCORE_OF_DISTANCE, edges.Child]
                                                 + _HDelegate(edges.Child, goal));
                            }
                        }
                    }
                });
            }



            //
            SpawnAndWait(actions);
            return(best);
        }
Exemplo n.º 10
0
 public int CompareTo(IStarNode other)
 {
     return(TravelScore.CompareTo(other.TravelScore));
 }