Beispiel #1
0
        /// <summary>
        /// http://en.wikipedia.org/wiki/Adaptive_Simpson%27s_method
        /// </summary>
        /// <param name="f"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private static double SimpsonsRule(IFunction f, double a, double b)
        {
            double c  = (a + b) / 2.0;
            double h3 = System.Math.Abs(b - a) / 6.0;

            return(h3 * (f.Eval(a) + 4.0 * f.Eval(c) + f.Eval(b)));
        }
Beispiel #2
0
        private static double SafeSimpsonsRule(IFunction f, double a, double b, double minRange, double maxRange, ref bool encounteredDiscontinuity)
        {
            double result = 0.0;
            double c      = (a + b) / 2.0;
            double f_a    = f.Eval(a);
            double f_b    = f.Eval(b);
            double f_c    = f.Eval(c);

            if (Tools.Maths.IsIn(f_a, minRange, maxRange) && Tools.Maths.IsIn(f_b, minRange, maxRange) && Tools.Maths.IsIn(f_c, minRange, maxRange))
            {
                encounteredDiscontinuity = false;
                var h3 = Maths.Abs(b - a) / 6.0;
                result = h3 * (f_a + 4.0 * f_c + f_b);
            }
            else
            {
                encounteredDiscontinuity = true;
            }
            return(result);
        }
Beispiel #3
0
        double[] Mutation_improved(double[] x, IFunction f)
        {
            double r = 1;
            double s = random.NextDouble();
            int    b = 5;
            double dg, gg;

            r -= Math.Pow(s, (Math.Pow(1 - (f.Eval() / evaluation), b)));
            dg = Math.Max(lowerBound, (upperBound - lowerBound) * r);
            gg = Math.Min(upperBound, (upperBound - lowerBound) * r);


            if (random.NextDouble() < pMut)
            {
                for (int i = 0; i < x.Count(); i++)
                {
                    x[i] = random.NextDouble() * (gg - (dg)) + (dg);
                }
            }
            return(x);
        }
        private Property parsePrimaryExpr()
        {
            Property prop;

            switch (currentToken)
            {
            case TOK_LPAR:
                next();
                prop = parseAdditiveExpr();
                expectRpar();
                return(prop);

            case TOK_LITERAL:
                prop = new StringProperty(currentTokenValue);
                break;

            case TOK_NCNAME:
                prop = new NCnameProperty(currentTokenValue);
                break;

            case TOK_FLOAT:
                prop = new NumberProperty(ParseDouble(currentTokenValue));
                break;

            case TOK_INTEGER:
                prop = new NumberProperty(Int32.Parse(currentTokenValue));
                break;

            case TOK_PERCENT:
                double pcval = ParseDouble(
                    currentTokenValue.Substring(0, currentTokenValue.Length - 1)) / 100.0;
                IPercentBase pcBase = this.propInfo.GetPercentBase();
                if (pcBase != null)
                {
                    if (pcBase.GetDimension() == 0)
                    {
                        prop = new NumberProperty(pcval * pcBase.GetBaseValue());
                    }
                    else if (pcBase.GetDimension() == 1)
                    {
                        prop = new LengthProperty(new PercentLength(pcval,
                                                                    pcBase));
                    }
                    else
                    {
                        throw new PropertyException("Illegal percent dimension value");
                    }
                }
                else
                {
                    prop = new NumberProperty(pcval);
                }
                break;

            case TOK_NUMERIC:
                int    numLen   = currentTokenValue.Length - currentUnitLength;
                string unitPart = currentTokenValue.Substring(numLen);
                double numPart  = ParseDouble(currentTokenValue.Substring(0, numLen));
                Length length   = null;
                if (unitPart.Equals(RELUNIT))
                {
                    length = new FixedLength(numPart, propInfo.currentFontSize());
                }
                else
                {
                    length = new FixedLength(numPart, unitPart);
                }
                if (length == null)
                {
                    throw new PropertyException("unrecognized unit name: " + currentTokenValue);
                }
                else
                {
                    prop = new LengthProperty(length);
                }
                break;

            case TOK_COLORSPEC:
                prop = new ColorTypeProperty(new ColorType(currentTokenValue));
                break;

            case TOK_FUNCTION_LPAR:
            {
                IFunction function =
                    (IFunction)functionTable[currentTokenValue];
                if (function == null)
                {
                    throw new PropertyException("no such function: "
                                                + currentTokenValue);
                }
                next();
                propInfo.pushFunction(function);
                prop = function.Eval(parseArgs(function.NumArgs), propInfo);
                propInfo.popFunction();
                return(prop);
            }

            default:
                throw new PropertyException("syntax error");
            }
            next();
            return(prop);
        }
Beispiel #5
0
 public double Eval(double x)
 {
     return(System.Math.Sqrt(-1 + System.Math.Pow(_derivativeFunction.Eval(x), 2)));
 }
Beispiel #6
0
 public static double Derivative(IFunction f, double x)
 {
     return((f.Eval(x + Tools.Maths.EPSILON_D) - f.Eval(x)) / Tools.Maths.EPSILON_D);
 }
Beispiel #7
0
        public double StartGA(IFunction f, int variableSize)
        {
            random     = new Random();
            population = MakePopulation(variableSize);
            double[] xworst, xbest;
            double   fxbest, fxworst;

            fi = new double[N];

            //calculate best and worst chromosome
            xworst = (double[])population[0].Clone();
            xbest  = (double[])population[0].Clone();

            for (int i = 1; i < N; i++)
            {
                if (f.Value(population[i]) < f.Value(xbest))
                {
                    xbest = population[i];
                }
                else if (f.Value(population[i]) > f.Value(xworst))
                {
                    xworst = population[i];
                }
            }

            //calculate fitness for chromosomes
            fxbest = f.Value(xbest);
            Console.WriteLine("Trenutni fmin = {0}", fxbest);
            fxworst = f.Value(xworst);
            CalculateFitness(f, fxworst, fxbest);

            int  indeksW;
            bool change;

            do
            {
                iteration++;
                change  = false;
                indeksW = Tournament(f, population);

                double trenutnaVR = f.Value(population[indeksW]);

                if (trenutnaVR < fxbest)
                {
                    xbest  = (double[])population[indeksW].Clone();
                    fxbest = trenutnaVR;
                    CalculateFitness(f, fxworst, fxbest);
                    change = true;
                }
                else if (trenutnaVR > fxworst)
                {
                    xworst  = (double[])population[indeksW].Clone();
                    fxworst = trenutnaVR;
                    CalculateFitness(f, fxworst, fxbest);
                }
                else
                {
                    fi[indeksW] = Fitness(f, population[indeksW], fxworst, fxbest);
                }

                if (change)
                {
                    Console.WriteLine("Trenutni fmin = {0} || broj evaluacija {1}", fxbest, f.Eval());
                }

                if (fxbest < 1e-6 && change)
                {
                    hits++;
                }
            } while (f.Eval() < evaluation);
            //  } while (fxbest > 1e-6);


            return(fxbest);
        }
Beispiel #8
0
 public double Eval(double x)
 {
     return(Math.Sqrt(1 + Math.Pow(_derivativeFunction.Eval(x), 2)));
 }