Esempio n. 1
0
        /// <summary>
        /// Znajduje najlepsze dziecko wg formuly mf
        /// Gdy gs==null to szuka tylko wśród dzieci w drzewie
        /// </summary>
        /// <param name="gs"></param>
        /// <param name="toLook"></param>
        /// <param name="mf"></param>
        /// <param name="n"></param>
        /// <param name="move"></param>
        private void FindMaximizedNode(GameState gs, Node toLook, MaximumFormula mf, out Node n, out int move, List <Move> moves)
        {
            double maxVal = double.NegativeInfinity, currVal = double.NegativeInfinity;

            n    = null;
            move = 0;
            if (toLook.Children != null)
            {
                foreach (var d in toLook.Children)
                {
                    currVal = mf(d.Value, d.Key, gs);
                    if (currVal > maxVal)
                    {
                        maxVal = currVal;
                        n      = d.Value;
                        move   = d.Key;
                    }
                }
            }
            if (gs != null) //&& maxVal<0) //jak nie jest ujemne to i tak nic nie znajdzie
            {
                if (moves == null)
                {
                    heuristics.SortHand(gs);
                    moves = GameRules.GetMoves(gs);
                    heuristics.SortMoves(gs, moves);
                }
                if (toLook.Children != null && toLook.Children.Count == moves.Count)
                {
                    return;
                }
                foreach (Move m in moves)
                {
                    if (toLook.Children == null || !toLook.Children.ContainsKey(m.SerializedMove)) //jeśli nie zawiera
                    {
                        Node nd = new Node();                                                      //toLook);
                        currVal = mf(nd, m.SerializedMove, gs);                                    // i tak zwraca zero
                        if (currVal > maxVal)
                        {
                            maxVal = currVal;
                            n      = nd;
                            move   = m.SerializedMove;
                            //  break; //i tak nie znajdzie niczego większego
                        }
                    }
                }
            }
        }
 public void RemoveConstraint(Isotope isotope)
 {
     MinimumFormula.Remove(isotope);
     MaximumFormula.Remove(isotope);
 }
 public void AddConstraint(ChemicalFormula minimumChemicalFormula, ChemicalFormula maximumChemicalFormula)
 {
     MinimumFormula.Add(minimumChemicalFormula);
     MaximumFormula.Add(maximumChemicalFormula);
 }
 public void AddConstraint(Isotope isotope, int min, int max)
 {
     MinimumFormula.Add(isotope, min);
     MaximumFormula.Add(isotope, max);
 }
        public IEnumerable <ChemicalFormula> FromMass(double lowMass, double highMass, int maxNumberOfResults = int.MaxValue, bool sort = true)
        {
            if (highMass <= lowMass)
            {
                throw new ArgumentException("The high mass must be greater than the low mass");
            }

            if (!MaximumFormula.Contains(MinimumFormula))
            {
                throw new ArgumentException("The maximum formula must include the minimum formula");
            }

            List <ChemicalFormula> returnFormulas = new List <ChemicalFormula>();

            // The minimum formula required for any return formulas

            double correctedLowMass  = lowMass;
            double correctedHighMass = highMass;

            bool minFormulaExists = MinimumFormula.IsotopeCount != 0;

            int[]  minValues      = null;
            double minFormulaMass = 0;

            if (minFormulaExists)
            {
                minValues      = MinimumFormula.GetIsotopes();
                minFormulaMass = MinimumFormula.MonoisotopicMass;

                correctedLowMass  -= minFormulaMass;
                correctedHighMass -= minFormulaMass;

                // Add the minimum formula itself if it is within the bounds
            }

            // The maximum formula allowed, represented in number of isotopes
            int[] maxValues = MaximumFormula.GetIsotopes();

            // The current formula represented in isotopes
            int[] currentFormula = new int[maxValues.Length];

            // A list of all the isotopes masses
            double[] masses = new double[maxValues.Length];

            int length = maxValues.Length;

            for (int j = 0; j < length; j++)
            {
                if (minFormulaExists && j < minValues.Length)
                {
                    maxValues[j] -= minValues[j];
                }
                if (maxValues[j] == 0)
                {
                    continue;
                }
                masses[j] = PeriodicTable.GetIsotope(j).AtomicMass;
            }
            masses[0] = PeriodicTable.GetIsotope(0).AtomicMass;

            GenerateFormulaHelper(correctedLowMass, correctedHighMass, masses, maxValues, length - 1, currentFormula, returnFormulas);

            if (minFormulaExists)
            {
                foreach (ChemicalFormula formula in returnFormulas)
                {
                    formula.Add(MinimumFormula);
                }
                if (minFormulaMass >= lowMass && minFormulaMass <= highMass)
                {
                    returnFormulas.Add(new ChemicalFormula(MinimumFormula));
                }
            }

            if (!sort)
            {
                return(returnFormulas);
            }
            double meanValue = (highMass + lowMass) / 2.0;

            return(returnFormulas.OrderBy(formula => Math.Abs(formula.MonoisotopicMass - meanValue)).Take(maxNumberOfResults));
        }