Ejemplo n.º 1
0
        public void FindPathTree(IVertex root)
        {
            if (root == null)
            {
                return;
            }

            // reset path tree
            foreach (IVertex node in fVerticesList)
            {
                node.Dist    = int.MaxValue;
                node.Visited = false;
                node.EdgeIn  = null;
            }

            // init root
            root.Dist    = 0;
            root.Visited = true;
            root.EdgeIn  = null;

            PathCandidate topCandidate = new PathCandidate(root, null);

            // processing
            while (topCandidate != null)
            {
                IVertex topNode = topCandidate.Node;
                topCandidate = topCandidate.Next;

                int nodeDist = topNode.Dist;
                topNode.Visited = false;

                foreach (IEdge link in topNode.EdgesOut)
                {
                    IVertex target  = link.Target;
                    int     newDist = nodeDist + link.Cost;

                    if (newDist < target.Dist)
                    {
                        target.Dist   = newDist;
                        target.EdgeIn = link;

                        if (!target.Visited)
                        {
                            target.Visited = true;
                            topCandidate   = new PathCandidate(target, topCandidate);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public IMovementPath GetPath(ICharacter user, IWorldObject targetObject)
        {
            Tile          playerTile    = user.Tile;
            Vector2Ushort targetTilePos = targetObject.TilePosition;

            if (playerTile.Position == targetTilePos) // User activated the script while already standing near to the target
            {
                return(new StraightMovementPath(user.Position, GeometryHelper.TileCenter(playerTile), targetObject));
            }

            SortedSet <PathCandidate> candidates = new SortedSet <PathCandidate>();
            HashSet <Tile>            visited    = new HashSet <Tile>();

            candidates.Add(PathCandidate.Init(playerTile, targetTilePos));

            // Api.Logger.Dev($"Automaton: finding path from {playerTile.Position} to {targetTilePos}");

            while (candidates.Count != 0)
            {
                PathCandidate withLeastWeight = candidates.Min();
                candidates.Remove(withLeastWeight);
                var undiscovered = new HashSet <Tile>(withLeastWeight.Neighbours).Except(visited);
                visited.Add(withLeastWeight.Head);

                foreach (Tile next in undiscovered)
                {
                    if (next.Position == targetTilePos)
                    {
                        return(new WaypointMovementPath(withLeastWeight.Fork(next).AsTilesCenters(), targetObject));
                    }

                    if (CanTravel(user, withLeastWeight.Head, next) && withLeastWeight.Length < MAX_PATH_TILES)
                    {
                        candidates.Add(withLeastWeight.Fork(next));
                    }
                }
            }

            // Api.Logger.Error($"Automaton: failed to find proper path from {playerTile.Position} to {targetTilePos}.");
            return(new NoPathPath(user));
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="precursorMass">If you dont wish to use precursor information, set the value to -1</param>
        /// <param name="precursorTolerance"></param>
        /// <param name="theRegexes"></param>
        /// <param name="scanNumber"></param>
        /// <param name="measuredPrecursorMZ"></param>
        /// <param name="estimatedPrecursorCharge"></param>
        /// <returns></returns>
        ///
        public SearchResult Search(double searchLowerBound, double searchUpperBound, List <PathCandidate> pathCandidates, int maxRegexes, int scanNumber, double measuredPrecursorMZ, double estimatedPrecursorCharge)
        {
            if (fastaParser.MyItems.Count == 0)
            {
                throw (new Exception("Please load a FASTA DB prior to search"));
            }
            //Now lets score the sequences
            //First lets set all the counters to zero



            foreach (FastaItem seq in fastaParser.MyItems)
            {
                seq.ExtraDouble = 0;

                //We will use this to store the matching regexes
                seq.Objects.Clear();
            }

            //Prepare the regexes
            if (pathCandidates.Count > maxRegexes)
            {
                pathCandidates.RemoveRange(maxRegexes - 1, pathCandidates.Count - maxRegexes);
            }


            //The search is done in 2 steps, we first rank a regex for specificity, then we do the search with the score.

            Parallel.ForEach(pathCandidates, p =>
                             //foreach (PathCandidate p in pathCandidates)
            {
                Regex r = new Regex(@p.StringRegexForwardReverse, RegexOptions.Compiled);
                List <FastaItem> theMatches = new List <FastaItem>();
                foreach (FastaItem seq in fastaParser.MyItems)
                {
                    if (seq.MonoisotopicMass >= searchLowerBound)
                    {
                        if (seq.MonoisotopicMass <= searchUpperBound || searchUpperBound == -1)
                        {
                            if (r.IsMatch(seq.Sequence))
                            {
                                theMatches.Add(seq);
                                seq.Objects.Add(p);
                            }
                        }
                    }
                }


                if (theMatches.Count > 0)
                {
                    theMatches.Sort((a, b) => a.ExtraDouble.CompareTo(b.ExtraDouble));
                    double points = 1 / (double)theMatches.Count;

                    for (int i = 0; i < theMatches.Count; i++)
                    {
                        double rankingCorrectionFactor = (double)(i + 1) / (double)theMatches.Count;
                        if (rankingCorrectionFactor < 0.2)
                        {
                            rankingCorrectionFactor = 0.2;
                        }

                        double sequenceCorrectionFactor = 1 / (double)theMatches[i].Sequence.Length;

                        theMatches[i].ExtraDouble += points * sequenceCorrectionFactor * rankingCorrectionFactor;
                    }
                }
            });
            //}

            SearchResult result = new SearchResult(scanNumber, measuredPrecursorMZ, estimatedPrecursorCharge);

            //System.Threading.Parallel.ForEach(fastaParser.MyItems, fi =>
            foreach (FastaItem fi in fastaParser.MyItems)
            {
                if (fi.ExtraDouble > 0)
                {
                    SearchResult.SearchResultCandidate sr = new SearchResult.SearchResultCandidate();
                    sr.Name            = fi.SequenceIdentifier;
                    sr.Sequence        = fi.Sequence;
                    sr.Score           = fi.ExtraDouble;
                    sr.TheoreticalMass = fi.MonoisotopicMass;

                    foreach (object o in fi.Objects)
                    {
                        PathCandidate pc = (PathCandidate)o;
                        PatternTools.T_ReXS.RegexGenerationTools.SearchResult.RegexAndPath rp = new PatternTools.T_ReXS.RegexGenerationTools.SearchResult.RegexAndPath(pc.NodePath, pc.StringRegexForwardReverse);
                        sr.RegexAndPaths.Add(rp);
                    }
                    result.TheResults.Add(sr);
                }
            }
            //);
            return(result);
        }
Ejemplo n.º 4
0
 public PathCandidate(IVertex node, PathCandidate next)
 {
     Node = node;
     Next = next;
 }