Ejemplo n.º 1
0
        public Optimization(string function, double calculationStep, List <OptimizationVariable> startVariables, List <OptimizationVariable> endVariables, List <OptimizationVariable> constants = null)
        {
            if (Optimization.CheckInputParametersName(startVariables, endVariables))
            {
                List <OptimizationVariable> allVariables = new List <OptimizationVariable>();

                this.StartVariables = startVariables;
                this.EndVariables   = endVariables;
                allVariables.AddRange(startVariables);

                if (constants != null && constants.Count > 0)
                {
                    this.Constants = constants;
                    allVariables.AddRange(constants);
                }

                this.Function = new Expression(function, OptimizationVariable.ConvertOptimizationVariablesToVariables(allVariables));

                this.CalculationStep = calculationStep;
            }
            else
            {
                throw new ArgumentException("Parameters 'startValue' and 'endVariable' are incorrect.");
            }
        }
        public List <OptimizationVariable> CalculateCoordinateSearch(out double functionResult, double epselon)
        {
            List <Variable> currentVariables = new List <Variable>();

            double[] shifts = new double[this.StartVariables.Count * 2];

            for (int i = 0; i < this.StartVariables.Count; i++)
            {
                currentVariables.Add(new Variable(this.StartVariables[i].Name, this.StartVariables[i].Value));
                shifts[i] = 0;
            }

            int    currenIteration = 0;
            double oldValue, newValue;

            do
            {
                oldValue = this.Function.GetResultValue(currentVariables);
                for (int i = 0; i < this.StartVariables.Count; i++)
                {
                    currentVariables[i].Value += this.CalculationStep;
                    shifts[i * 2]              = this.Function.GetResultValue(currentVariables);

                    currentVariables[i].Value -= 2 * this.CalculationStep;
                    shifts[i * 2 + 1]          = this.Function.GetResultValue(currentVariables);
                    currentVariables[i].Value += this.CalculationStep;
                }

                int minimalShiftIndex = this.GetMinIndex(shifts);
                int parameterIndex    = minimalShiftIndex / 2;
                if (minimalShiftIndex % 2 == 0)
                {
                    currentVariables[parameterIndex].Value += this.CalculationStep;
                }
                else
                {
                    currentVariables[parameterIndex].Value -= this.CalculationStep;
                }

                newValue = this.Function.GetResultValue(currentVariables);
                currenIteration++;
            } while (Math.Abs(newValue - oldValue) > epselon && currenIteration < this.MaxIterationCount);

            List <OptimizationVariable> result = OptimizationVariable.ConvertVariablesToOptimizationVariables(currentVariables);

            functionResult = newValue;
            return(result);
        }
Ejemplo n.º 3
0
        public List <OptimizationVariable> CalculateBrouteForce(out double functionResult)
        {
            List <OptimizationVariable> result = new List <OptimizationVariable>();

            this.broutForceMin                = double.MaxValue;
            this.brouteForceResult            = new List <Variable>();
            this.currentBrouteForceParameters = new List <Variable>();

            foreach (OptimizationVariable variable in this.StartVariables)
            {
                this.brouteForceResult.Add(new Variable(variable.Name, variable.Value));
                this.currentBrouteForceParameters.Add(new Variable(variable.Name, variable.Value));
            }

            this.BustOptions(0);
            result = OptimizationVariable.ConvertVariablesToOptimizationVariables(this.brouteForceResult);

            functionResult = this.broutForceMin;
            return(result);
        }
Ejemplo n.º 4
0
        private static bool CheckInputParametersName(List <OptimizationVariable> startVariables, List <OptimizationVariable> endVariables)
        {
            if (startVariables != null && endVariables != null &&
                startVariables.Count > 0 && endVariables.Count > 0 && startVariables.Count == endVariables.Count)
            {
                for (int i = 0; i < startVariables.Count; i++)
                {
                    OptimizationVariable variable = (from g in endVariables
                                                     where g.Name.Equals(startVariables[i].Name)
                                                     select g).FirstOrDefault();

                    if (variable == null)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OptimizationVariable" /> class.
 /// </summary>
 /// <param name="integralVariable">Initial variable which is supposed to be copied to the current one</param>
 public OptimizationVariable(OptimizationVariable integralVariable)
 {
     this.Name  = integralVariable.Name;
     this.Value = integralVariable.Value;
 }