コード例 #1
0
        public override GradientMethod.Answer GetAnswer()
        {
            param         = new TemporaryParams();
            param.vectorX = input.vectorX0;

            GradientMethod.Answer answer = new GradientMethod.Answer();
            answer.iterCount     = 0;
            answer.funcCalcCount = 0;

            double[] stepsArray = new double[input.vectorX0.Length];

            for (int i = 0; i < stepsArray.Length; i++)
            {
                stepsArray[i] = input.epsilon * 100;
            }

            DichotomyMethod.Answer answerDM;

            do
            {
                param.oldVectorX = Ext.cloneVector(param.vectorX);

                for (int index = 0; index < param.vectorX.Length; index++)
                {
                    param.currentVectorIndex = index;

                    double varWithNegativeStep = param.vectorX[index] - 3 * input.epsilon;
                    double varWithPositiveStep = param.vectorX[index] + 3 * input.epsilon;

                    double[] copyVector = Ext.cloneVector(param.vectorX);

                    copyVector[index] = varWithNegativeStep;
                    double f1 = input.mainFunc.calculate(copyVector);

                    copyVector[index] = varWithPositiveStep;
                    double f2 = input.mainFunc.calculate(copyVector);
                    param.direction = Math.Sign(f1 - f2);

                    answerDM          = findStep(stepsArray[index]);
                    answer.iterCount += answerDM.iterCount;

                    stepsArray[index]     = answerDM.approxMinX;
                    param.vectorX         = takeAStep(stepsArray[index], param.vectorX, index, param.direction);
                    answer.funcCalcCount += answerDM.funcCalcCount * 2 + 2;
                    answer.approxMinFunc++;
                }
            } while (Ext.normOfVector(Ext.subtructOfVectors(param.vectorX, param.oldVectorX)) >= input.epsilon);

            answer.approxMinFunc    = input.mainFunc.calculate(param.vectorX);
            answer.approxMinVectorX = param.vectorX;

            return(answer);
        }
コード例 #2
0
        private static void showAnswer(string methodName, GradientMethod.Answer answer)
        {
            Console.WriteLine($"Answer for {methodName}");
            Console.WriteLine($"    Approximate minimum of function: {answer.approxMinFunc}");
            string dot = "";

            for (int i = 0; i < answer.approxMinVectorX.Length - 1; i++)
            {
                dot += answer.approxMinVectorX[i] + ",";
            }
            dot += answer.approxMinVectorX[answer.approxMinVectorX.Length - 1];

            Console.WriteLine($"    Dot : x({dot})");
            Console.WriteLine($"    Functionc calculation count : {answer.funcCalcCount}");
            Console.WriteLine($"    Iteration count : {answer.iterCount}\n");
        }
コード例 #3
0
ファイル: ModifiedNutonMethod.cs プロジェクト: SergoSmyk/MMDO
        public override GradientMethod.Answer GetAnswer()
        {
            param = new TemporaryParams();

            GradientMethod.Answer answer = new GradientMethod.Answer();
            answer.iterCount     = 0;
            answer.funcCalcCount = 0;
            param.vectorX        = Ext.cloneVector(input.vectorX0);

            param.firstOrderGradient = Ext.findFirstOrderGradient(input.firstOrderDerivatives, input.vectorX0);

            double[,] secondOrderGradient     = Ext.findSecondOrderGradient(input.secondOrderDerivatives, param.vectorX);
            param.invertedSecondOrderGradient = Ext.findInversedMatrix(secondOrderGradient);;

            answer.funcCalcCount += input.vectorX0.Length + Convert.ToInt32(Math.Pow(param.vectorX.Length, 2));

            double step = 1000 * input.epsilon;

            param.vectorX = Ext.cloneVector(input.vectorX0);

            DichotomyMethod.Answer answerDM;

            if (Ext.normOfVector(param.firstOrderGradient) > input.epsilon)
            {
                do
                {
                    answerDM          = findStep(step);
                    step              = answerDM.approxMinX;
                    answer.iterCount += answerDM.iterCount;

                    param.vectorX            = takeAStep(step, param.vectorX, param.invertedSecondOrderGradient, param.firstOrderGradient);
                    param.firstOrderGradient = Ext.findFirstOrderGradient(input.firstOrderDerivatives, param.vectorX);

                    answer.funcCalcCount += input.vectorX0.Length;
                    answer.iterCount++;
                }while (Ext.normOfVector(param.firstOrderGradient) >= input.epsilon);
            }

            answer.approxMinVectorX = param.vectorX;
            answer.approxMinFunc    = input.mainFunc.calculate(param.vectorX);
            answer.funcCalcCount++;

            return(answer);
        }
コード例 #4
0
        public override GradientMethod.Answer GetAnswer()
        {
            param = new TemporaryParams();

            GradientMethod.Answer answer = new GradientMethod.Answer();
            answer.iterCount     = 0;
            answer.funcCalcCount = 0;

            double[] firstOrderGradient = Ext.findFirstOrderGradient(input.firstOrderDerivatives, input.vectorX0);
            double[,] secondOrderGradient, invertedSecondOrderGradient;
            answer.funcCalcCount += input.vectorX0.Length;

            double[] sums = new double[input.vectorX0.Length];
            param.vectorX = Ext.cloneVector(input.vectorX0);

            if (Ext.normOfVector(firstOrderGradient) > input.epsilon)
            {
                do
                {
                    secondOrderGradient         = Ext.findSecondOrderGradient(input.secondOrderDerivatives, param.vectorX);
                    answer.funcCalcCount       += Convert.ToInt32(Math.Pow(param.vectorX.Length, 2));
                    invertedSecondOrderGradient = Ext.findInversedMatrix(secondOrderGradient);

                    for (int i = 0; i < param.vectorX.Length; i++)
                    {
                        sums[i] = 0;
                        for (int j = 0; j < param.vectorX.Length; j++)
                        {
                            sums[i] += invertedSecondOrderGradient[i, j] * firstOrderGradient[j];
                        }
                    }
                    param.vectorX         = Ext.subtructOfVectors(param.vectorX, sums);
                    firstOrderGradient    = Ext.findFirstOrderGradient(input.firstOrderDerivatives, param.vectorX);
                    answer.funcCalcCount += input.vectorX0.Length;
                    answer.iterCount++;
                }while (Ext.normOfVector(firstOrderGradient) >= input.epsilon);
            }

            answer.approxMinVectorX = param.vectorX;
            answer.approxMinFunc    = input.mainFunc.calculate(param.vectorX);
            answer.funcCalcCount++;

            return(answer);
        }
コード例 #5
0
        public override GradientMethod.Answer GetAnswer()
        {
            param = new TemporaryParams();

            GradientMethod.Answer answer = new GradientMethod.Answer();
            answer.iterCount     = 0;
            answer.funcCalcCount = 0;

            param.vectorX  = Ext.cloneVector(input.vectorX0);
            param.gradient = Ext.findFirstOrderGradient(input.firstOrderDerivatives, param.vectorX);

            answer.funcCalcCount += input.vectorX0.Length;

            if (Ext.normOfVector(param.gradient) > input.epsilon)
            {
                double step = 100 * input.epsilon;
                DichotomyMethod.Answer answerDM;
                do
                {
                    answerDM = findStep(step);
                    step     = answerDM.approxMinX;

                    answer.funcCalcCount += answerDM.funcCalcCount;
                    answer.iterCount     += answerDM.iterCount;

                    param.oldVectorX = param.vectorX;
                    param.vectorX    = takeAStep(step, param.vectorX, param.gradient);
                    param.gradient   = Ext.findFirstOrderGradient(input.firstOrderDerivatives, param.vectorX);

                    answer.funcCalcCount += input.vectorX0.Length;
                    answer.iterCount++;
                }while (Ext.normOfVector(Ext.subtructOfVectors(param.vectorX, param.oldVectorX)) >= input.epsilon &&
                        Ext.normOfVector(param.gradient) >= input.epsilon);
            }

            answer.approxMinVectorX = param.vectorX;
            answer.approxMinFunc    = input.mainFunc.calculate(param.vectorX);
            answer.funcCalcCount++;

            return(answer);
        }