Assembla home | Assembla project page
 

Changeset 145

Show
Ignore:
Timestamp:
05/05/08 07:41:57 (2 months ago)
Author:
ToyinA
Message:

Added ArmijoLineSearch?, Simplex, ConjugateGradient? and SteepestDescent? Optimization objects.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Math/Optimization/EndCriteria.cs

    r117 r145  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    3    
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     4 *  
    45 This file is part of QLNet Project http://www.qlnet.org 
    56 
     
    2223using System.Text; 
    2324 
    24 namespace QLNet { 
    25     //! Criteria to end optimization process: 
    26     /*! - maximum number of iterations AND minimum number of iterations around stationary point 
    27         - x (independent variable) stationary point 
    28         - y=f(x) (dependent variable) stationary point 
    29         - stationary gradient 
    30     */ 
    31     public class EndCriteria { 
    32         public enum Type {None, 
    33                           MaxIterations, 
    34                           StationaryPoint, 
    35                           StationaryFunctionValue, 
    36                           StationaryFunctionAccuracy, 
    37                           ZeroGradientNorm, 
    38                           Unknown}; 
    39     } 
     25namespace QLNet 
     26
     27 
     28        //! Criteria to end optimization process: 
     29//    ! - maximum number of iterations AND minimum number of iterations around stationary point 
     30//        - x (independent variable) stationary point 
     31//        - y=f(x) (dependent variable) stationary point 
     32//        - stationary gradient 
     33//     
     34        public class EndCriteria 
     35        { 
     36                public enum Type 
     37                { 
     38               None, 
     39                   MaxIterations, 
     40                   StationaryPoint, 
     41                   StationaryFunctionValue, 
     42                   StationaryFunctionAccuracy, 
     43                   ZeroGradientNorm, 
     44                   Unknown 
     45                } 
     46 
     47                //! Initialization constructor 
     48                public EndCriteria(int maxIterations, int? maxStationaryStateIterations, double rootEpsilon, double functionEpsilon, double? gradientNormEpsilon) 
     49                { 
     50                        maxIterations_ = maxIterations; 
     51                        maxStationaryStateIterations_ = maxStationaryStateIterations; 
     52                        rootEpsilon_ = rootEpsilon; 
     53                        functionEpsilon_ = functionEpsilon; 
     54                        gradientNormEpsilon_ = gradientNormEpsilon; 
     55         
     56                        if (maxStationaryStateIterations_ == null) 
     57                                maxStationaryStateIterations_ = Math.Min((int)(maxIterations/2), (int)(100)); 
     58 
     59            if (!(maxStationaryStateIterations_ > 1)) 
     60                throw new ApplicationException("maxStationaryStateIterations_ (" + maxStationaryStateIterations_ + ") must be greater than one"); 
     61 
     62            if (!(maxStationaryStateIterations_ < maxIterations_)) 
     63                throw new ApplicationException("maxStationaryStateIterations_ (" + maxStationaryStateIterations_ + ") must be less than maxIterations_ (" + maxIterations_ + ")"); 
     64 
     65                        if (gradientNormEpsilon_ == null) 
     66                                gradientNormEpsilon_ = functionEpsilon_; 
     67                } 
     68 
     69                // Inspectors 
     70 
     71                // Inspectors 
     72                public int maxIterations() 
     73                { 
     74                        return maxIterations_; 
     75                } 
     76                public int maxStationaryStateIterations() 
     77                { 
     78            return maxStationaryStateIterations_.GetValueOrDefault(); 
     79                } 
     80                public double rootEpsilon() 
     81                { 
     82                        return rootEpsilon_; 
     83                } 
     84                public double functionEpsilon() 
     85                { 
     86                        return functionEpsilon_; 
     87                } 
     88                public double gradientNormEpsilon() 
     89                { 
     90            return gradientNormEpsilon_.GetValueOrDefault(); 
     91                } 
     92 
     93//        ! Test if the number of iterations is not too big 
     94//            and if a minimum point is not reached  
     95                public bool value(int iteration, ref int statStateIterations, bool positiveOptimization, double fold, double UnnamedParameter1, double fnew, double normgnew, ref EndCriteria.Type ecType) 
     96                { 
     97                        return checkMaxIterations(iteration, ref ecType) || checkStationaryFunctionValue(fold, fnew, ref statStateIterations, ref ecType) || checkStationaryFunctionAccuracy(fnew, positiveOptimization, ref ecType) || checkZeroGradientNorm(normgnew, ref ecType); 
     98                } 
     99 
     100                //! Test if the number of iteration is below MaxIterations  
     101                public bool checkMaxIterations(int iteration, ref EndCriteria.Type ecType) 
     102                { 
     103                        if (iteration < maxIterations_) 
     104                                return false; 
     105                        ecType = Type.MaxIterations; 
     106                        return true; 
     107                } 
     108                //! Test if the root variation is below rootEpsilon  
     109                public bool checkStationaryPoint(double xOld, double xNew, ref int statStateIterations, ref EndCriteria.Type ecType) 
     110                { 
     111                        if (Math.Abs(xNew-xOld) >= rootEpsilon_) 
     112                        { 
     113                                statStateIterations = 0; 
     114                                return false; 
     115                        } 
     116                        ++statStateIterations; 
     117                        if (statStateIterations <= maxStationaryStateIterations_) 
     118                                return false; 
     119                        ecType = Type.StationaryPoint; 
     120                        return true; 
     121                } 
     122                //! Test if the function variation is below functionEpsilon  
     123                public bool checkStationaryFunctionValue(double fxOld, double fxNew, ref int statStateIterations, ref EndCriteria.Type ecType) 
     124                { 
     125                        if (Math.Abs(fxNew-fxOld) >= functionEpsilon_) 
     126                        { 
     127                                statStateIterations = 0; 
     128                                return false; 
     129                        } 
     130                        ++statStateIterations; 
     131                        if (statStateIterations <= maxStationaryStateIterations_) 
     132                                return false; 
     133                        ecType = Type.StationaryFunctionValue; 
     134                        return true; 
     135                } 
     136                //! Test if the function value is below functionEpsilon  
     137                public bool checkStationaryFunctionAccuracy(double f, bool positiveOptimization, ref EndCriteria.Type ecType) 
     138                { 
     139                        if (!positiveOptimization) 
     140                                return false; 
     141                        if (f >= functionEpsilon_) 
     142                                return false; 
     143                        ecType = Type.StationaryFunctionAccuracy; 
     144                        return true; 
     145                } 
     146         
     147                public bool checkZeroGradientNorm(double gradientNorm, ref EndCriteria.Type ecType) 
     148                { 
     149                        if (gradientNorm >= gradientNormEpsilon_) 
     150                                return false; 
     151                        ecType = Type.ZeroGradientNorm; 
     152                        return true; 
     153                } 
     154 
     155                //! Maximum number of iterations 
     156                protected int maxIterations_; 
     157                //! Maximun number of iterations in stationary state 
     158                protected int? maxStationaryStateIterations_; 
     159                //! root, function and gradient epsilons 
     160                protected double rootEpsilon_; 
     161                protected double functionEpsilon_; 
     162                protected double? gradientNormEpsilon_; 
     163 
     164        } 
    40165} 
  • trunk/QLNet/QLNet/Math/Optimization/problem.cs

    r117 r145  
    11/* 
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    3    
     3 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     4 *  
    45 This file is part of QLNet Project http://www.qlnet.org 
    56 
     
    5859        /*! \warning it does not reset the current minumum to any initial value 
    5960        */ 
    60         void reset() { 
     61        public void reset() 
     62        { 
    6163            functionEvaluation_ = gradientEvaluation_ = 0; 
    6264            functionValue_ = squaredNorm_ = null; 
     
    9395        } 
    9496 
    95         void setFunctionValue(double functionValue) { 
     97        public void setFunctionValue(double functionValue) 
     98        { 
    9699            functionValue_=functionValue; 
    97100        } 
    98101 
    99         void setGradientNormValue(double squaredNorm) { 
     102        public void setGradientNormValue(double squaredNorm) 
     103        { 
    100104            squaredNorm_=squaredNorm; 
    101105        } 
  • trunk/QLNet/QLNet/Math/Vector.cs

    r121 r145  
    22 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    33 Copyright (C) 2008 Andrea Maggiulli 
    4    
     4 Copyright (C) 2008 Toyin Akin (toyin_akin@hotmail.com) 
     5 *  
    56 This file is part of QLNet Project http://www.qlnet.org 
    67 
     
    9697        public static Vector operator /(Vector v1, double value) { return operValue(v1, value, (x, y) => x / y); } 
    9798 
    98         private static Vector operVector(Vector v1, Vector v2, Func<double, double, double> func) { 
     99        internal static Vector operVector(Vector v1, Vector v2, Func<double, double, double> func) { 
    99100            if (v1.Count != v2.Count) 
    100101                throw new ApplicationException("operation on vectors with different sizes (" + v1.Count + ", " + v2.Count); 
     
    130131            return v1 * v2; 
    131132        } 
     133 
     134        public static Vector DirectMultiply(Vector v1, Vector v2) 
     135        { 
     136            return Vector.operVector(v1, v2, (x, y) => x * y); 
     137        } 
     138 
    132139    } 
    133140} 
  • trunk/QLNet/QLNet/QLNet.csproj

    r143 r145  
    146146    <Compile Include="Math\Interpolations\Loginterpolation.cs" /> 
    147147    <Compile Include="Math\Matrix.cs" /> 
     148    <Compile Include="Math\Optimization\ArmijoLineSearch.cs" /> 
     149    <Compile Include="Math\Optimization\ConjugateGradient.cs" /> 
    148150    <Compile Include="Math\Optimization\Constraint.cs" /> 
    149151    <Compile Include="Math\Optimization\EndCriteria.cs" /> 
     152    <Compile Include="Math\Optimization\LeastSquareProblem.cs" /> 
     153    <Compile Include="Math\Optimization\LineSearch.cs" /> 
     154    <Compile Include="Math\Optimization\LineSearchBasedMethod.cs" /> 
    150155    <Compile Include="Math\Optimization\method.cs" /> 
    151156    <Compile Include="Math\Optimization\problem.cs" /> 
     157    <Compile Include="Math\Optimization\ProjectedCostFunction.cs" /> 
     158    <Compile Include="Math\Optimization\Simplex.cs" /> 
     159    <Compile Include="Math\Optimization\SteepestDescent.cs" /> 
    152160    <Compile Include="Math\SampledCurve.cs" /> 
    153161    <Compile Include="Math\Solver1d.cs" />