| 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 | | } |
|---|
| | 25 | namespace 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 | } |
|---|