예제 #1
0
 /// Constructor method.
 /// @param func IManyVariableFunction	function to project
 /// @param x DhbVector	origin of projected function
 /// @param d DhbVector	direction of projection
 public VectorProjectedFunction(IManyVariableFunction func,
                                DhbVector x, DhbVector d)
 {
     _f             = func;
     this.Origin    = x;
     this.Direction = d;
 }
예제 #2
0
        /// @param x double[]	origin of projected function
        /// @exception DhbIllegalDimension
        ///						if dimension of x is not that of the origin.
        public DhbVector ArgumentAt(double x)
        {
            DhbVector v = _direction * x;

            v.Accumulate(_origin);
            return(v);
        }
예제 #3
0
        /// @return boolean	true if a better point was found
        /// @exception DhbIllegalDimension if dimension of initial value is 0.
        private bool AddReflection(DhbVector centerOfgravity)
        {
            DhbVector reflectedVector = centerOfgravity * 2;

            reflectedVector.AccumulateNegated(
                _simplex[_result.Length].Position);
            OptimizingVector reflectedPoint =
                _pointFactory.CreateVector(reflectedVector, _f);

            if (reflectedPoint.BetterThan(_simplex[0]))
            {
                reflectedVector.ScaledBy(2);
                reflectedVector.AccumulateNegated(centerOfgravity);
                OptimizingVector expandedPoint =
                    _pointFactory.CreateVector(reflectedVector, _f);
                if (expandedPoint.BetterThan(reflectedPoint))
                {
                    AddBestPoint(expandedPoint);
                }
                else
                {
                    AddBestPoint(reflectedPoint);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #4
0
        /// @param x DhbVector
        public override void AddMutationOf(object x)
        {
            double[] v = ((DhbVector)x).ToComponents();
            int      i = (int)(this.NextDouble() * _origin.Dimension);

            v[i] = RandomComponent(i);
            try { _population[_fillIndex++] = new DhbVector(v); }
            catch (OverflowException) { };
        }
예제 #5
0
 public override void AddRandomChromosome()
 {
     double[] v = new double[_origin.Dimension];
     for (int i = 0; i < _origin.Dimension; i++)
     {
         v[i] = RandomComponent(i);
     }
     try { _population[_fillIndex++] = new DhbVector(v); }
     catch (OverflowException) { };
 }
예제 #6
0
        private void RotateDirections()
        {
            DhbVector firstDirection = _projections[0].Direction;
            int       n = _projections.Length;

            for (int i = 1; i < n; i++)
            {
                _projections[i - 1].Direction = _projections[i].Direction;
            }
            _projections[n - 1].Direction = firstDirection;
        }
예제 #7
0
        /// @return DhbVector	center of gravity of best points of simplex,
        ///													except worst one
        private DhbVector CenterOfGravity()
        {
            DhbVector g = new DhbVector(_result.Length);

            for (int i = 0; i < _result.Length; i++)
            {
                g.Accumulate(_simplex[i].Position);
            }
            g.ScaledBy(1.0 / _result.Length);
            return(g);
        }
 /// Iterate matrix product in eigenvalue information.
 public override double EvaluateIteration()
 {
     double oldEigenvalue = _eigenvalue;
     _transposedEigenvector = _transposedEigenvector.SecureProduct(_matrix);
     _transposedEigenvector *= (1.0 / _transposedEigenvector[0]);
     _eigenvector = _matrix.SecureProduct(_eigenvector);
     _eigenvalue = _eigenvector[0];
     _eigenvector *= (1.0 / _eigenvalue);
     return double.IsNaN(oldEigenvalue)
                     ? 10 * this.DesiredPrecision
                     : System.Math.Abs(_eigenvalue - oldEigenvalue);
 }
예제 #9
0
        /// @return boolean	true if a better point was found
        /// @param g DhbVector	summit whose median is contracted
        /// @exception DhbIllegalDimension if dimension of initial value is 0.
        private bool AddContraction(DhbVector g)
        {
            g.Accumulate(_simplex[_result.Length].Position);
            g.ScaledBy(0.5);
            OptimizingVector contractedPoint = _pointFactory.CreateVector(g, _f);

            if (contractedPoint.BetterThan(_simplex[0]))
            {
                AddBestPoint(contractedPoint);
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #10
0
/**
 * Here precision is the largest extent of the simplex.
 */
        public override double EvaluateIteration()
        {
            try
            {
                double    bestValue = _simplex[0].Value;
                DhbVector g         = CenterOfGravity();
                if (!AddReflection(g))
                {
                    if (!AddContraction(g))
                    {
                        contractSimplex();
                    }
                }
                _result = _simplex[0].Position;
                return(ComputePrecision());
            }
            catch (DhbIllegalDimension) { return(1); };
        }
예제 #11
0
        /// @param x DhbVector
        /// @param y DhbVector
        public override void AddCrossoversOf(object x, object y)
        {
            double[] v = ((DhbVector)x).ToComponents();
            double[] w = ((DhbVector)x).ToComponents();
            int      n = (int)(this.NextDouble() * (_origin.Dimension - 1));

            for (int i = 0; i < n; i++)
            {
                double temp = v[i];
                v[i] = w[i];
                w[i] = temp;
            }
            try
            {
                _population[_fillIndex++] = new DhbVector(v);
                _population[_fillIndex++] = new DhbVector(w);
            }
            catch (OverflowException) { };
        }
예제 #12
0
        /// @return double
        /// @param x double
        public double Error(double x)
        {
            int n = this.Degree + 1;

            double[] errors = new double[n];
            errors[0] = 1;
            for (int i = 1; i < n; i++)
            {
                errors[i] = errors[i - 1] * x;
            }
            DhbVector errorVector = new DhbVector(errors);
            double    answer;

            try
            {
                answer = errorVector * (_errorMatrix * errorVector);
            }
            catch (DhbIllegalDimension) { answer = double.NaN; };
            return(Math.Sqrt(answer));
        }
예제 #13
0
 private void AdjustLastDirection(DhbVector start)
 {
     try
     {
         int n = _projections.Length - 1;
         _projections[n].SetOrigin(_result);
         DhbVector newDirection = _projections[n].Origin - start;
         double    norm         = newDirection.Norm;
         if (norm > this.DesiredPrecision)
         {
             newDirection.ScaledBy(1 / norm);
             _projections[n].Direction          = newDirection;
             _unidimensionalFinder.Function     = _projections[n];
             _unidimensionalFinder.InitialValue = 0;
             _unidimensionalFinder.Evaluate();
             _result = _projections[n].ArgumentAt(
                 _unidimensionalFinder.Result).ToComponents();
         }
     }
     catch (DhbIllegalDimension) { };
 }
예제 #14
0
 public override double EvaluateIteration()
 {
     try
     {
         DhbVector start;
         start = new DhbVector(_result);
         int n = _projections.Length;
         for (int i = 0; i < n; i++)
         {
             _projections[i].SetOrigin(_result);
             _unidimensionalFinder.Function     = _projections[i];
             _unidimensionalFinder.InitialValue = 0;
             _unidimensionalFinder.Evaluate();
             _result = _projections[i].ArgumentAt(
                 _unidimensionalFinder.Result).ToComponents();
         }
         RotateDirections();
         AdjustLastDirection(start);
         return(ComputePrecision(start.ToComponents()));
     }
     catch (OverflowException) { return(double.NaN); }
     catch (DhbIllegalDimension) { return(double.NaN); };
 }
예제 #15
0
 /// @param v DhbMatrixAlgebra.DhbVector
 /// @exception OverflowException if dimension of v is 0.
 public void SetDirection(double[] v)
 {
     _direction = new DhbVector(v);
 }
 /// Set result to undefined.
 public override void InitializeIterations()
 {
     _eigenvalue = double.NaN;
     int n = _matrix.Columns;
     double[] eigenvectorComponents = new double[n];
     for (int i = 0; i < n; i++)
         eigenvectorComponents[i] = 1.0;
     _eigenvector = new DhbVector(eigenvectorComponents);
     n = _matrix.Rows;
     eigenvectorComponents = new double[n];
     for (int i = 0; i < n; i++)
         eigenvectorComponents[i] = 1.0;
     _transposedEigenvector = new DhbVector(eigenvectorComponents);
 }
예제 #17
0
 /// @param x double	component of the origin of the hypercube
 ///	constraining the domain of definition of the function to optimize
 /// @exception OverflowException
 ///						when the size of the array is 0
 public void SetOrigin(double[] x)
 {
     this._origin = new DhbVector(x);
 }
예제 #18
0
 /// @param x DhbVector
 public override void AddCloneOf(object x)
 {
     double[] v = ((DhbVector)x).ToComponents();
     try { _population[_fillIndex++] = new DhbVector(v); }
     catch (OverflowException) { };
 }
예제 #19
0
 /// @param v DhbMatrixAlgebra.DhbVector
 /// @exception OverflowException if dimension of v is 0.
 public void SetOrigin(double[] v)
 {
     _origin = new DhbVector(v);
 }
예제 #20
0
 /// @return DhbOptimizing.OptimizingVector
 /// @param v DhbVector
 /// @param f IManyVariableFunction
 public OptimizingVector CreateVector(DhbVector v, IManyVariableFunction f)
 {
     return(CreateVector(v.ToComponents(), f));
 }