Changeset 13403 for branches/ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/Improvers/SingleObjectiveTestFunctionImprovementOperator.cs
- Timestamp:
- 11/25/15 23:24:01 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/Improvers/SingleObjectiveTestFunctionImprovementOperator.cs
r12012 r13403 26 26 using HeuristicLab.Encodings.RealVectorEncoding; 27 27 using HeuristicLab.Operators; 28 using HeuristicLab.Optimization;29 28 using HeuristicLab.Parameters; 30 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 40 39 [Item("SingleObjectiveTestFunctionImprovementOperator", "An operator that improves test functions solutions. It is implemented as described in Laguna, M. and Martí, R. (2003). Scatter Search: Methodology and Implementations in C. Operations Research/Computer Science Interfaces Series, Vol. 24. Springer.")] 41 40 [StorableClass] 42 public sealed class SingleObjectiveTestFunctionImprovementOperator : SingleSuccessorOperator, ISingleObjective ImprovementOperator {41 public sealed class SingleObjectiveTestFunctionImprovementOperator : SingleSuccessorOperator, ISingleObjectiveTestFunctionImprovementOperator { 43 42 #region Parameter properties 44 43 public IValueParameter<DoubleValue> AlphaParameter { … … 51 50 get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 52 51 } 53 public ScopeParameter CurrentScopeParameter {54 get { return (ScopeParameter)Parameters["CurrentScope"]; }55 }56 52 public IValueParameter<DoubleValue> DeltaParameter { 57 53 get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; } 58 54 } 59 public IValueLookupParameter<ISingleObjectiveTestFunction ProblemEvaluator> EvaluatorParameter {60 get { return (IValueLookupParameter<ISingleObjectiveTestFunction ProblemEvaluator>)Parameters["Evaluator"]; }55 public IValueLookupParameter<ISingleObjectiveTestFunction> TestFunctionParameter { 56 get { return (IValueLookupParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; } 61 57 } 62 58 public IValueParameter<DoubleValue> GammaParameter { … … 78 74 get { return BetaParameter.Value; } 79 75 } 80 private DoubleMatrix Bounds {81 get { return BoundsParameter.ActualValue; }82 }83 public IScope CurrentScope {84 get { return CurrentScopeParameter.ActualValue; }85 }86 76 private DoubleValue Delta { 87 77 get { return DeltaParameter.Value; } 88 78 } 89 public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {90 get { return EvaluatorParameter.ActualValue; }91 }92 79 private DoubleValue Gamma { 93 80 get { return GammaParameter.Value; } 94 }95 public IntValue ImprovementAttempts {96 get { return ImprovementAttemptsParameter.ActualValue; }97 81 } 98 82 #endregion … … 106 90 Parameters.Add(new ValueParameter<DoubleValue>("Alpha", new DoubleValue(1.0))); 107 91 Parameters.Add(new ValueParameter<DoubleValue>("Beta", new DoubleValue(2.0))); 92 Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5))); 93 Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5))); 94 Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunction>("TestFunction", "The operator used to evaluate solutions.")); 108 95 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.")); 109 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));110 Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5)));111 Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator used to evaluate solutions."));112 Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5)));113 96 Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100))); 114 Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only.")); 97 Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only.")); // TODO: Problematic, this cannot be wired! IImprovementOperators need to be generic 115 98 #endregion 116 99 } … … 121 104 122 105 public override IOperation Apply() { 123 RealVector bestSol = CurrentScope.Variables[SolutionParameter.ActualName].Value as RealVector;106 RealVector bestSol = ExecutionContext.Scope.Variables[SolutionParameter.ActualName].Value as RealVector; 124 107 if (bestSol == null) 125 108 throw new ArgumentException("Cannot improve solution because it has the wrong type."); 126 109 127 var evaluator = Evaluator; 110 var bounds = BoundsParameter.ActualValue; 111 var function = TestFunctionParameter.ActualValue; 112 var maxIterations = ImprovementAttemptsParameter.ActualValue.Value; 128 113 129 double bestSolQuality = evaluator.Evaluate(bestSol);114 double bestSolQuality = function.Evaluate(bestSol); 130 115 131 116 // create perturbed solutions … … 133 118 for (int i = 0; i < simplex.Length; i++) { 134 119 simplex[i] = bestSol.Clone() as RealVector; 135 simplex[i][i] += 0.1 * ( Bounds[0, 1] - Bounds[0, 0]);136 if (simplex[i][i] > Bounds[0, 1]) simplex[i][i] = Bounds[0, 1];137 if (simplex[i][i] < Bounds[0, 0]) simplex[i][i] = Bounds[0, 0];120 simplex[i][i] += 0.1 * (bounds[0, 1] - bounds[0, 0]); 121 if (simplex[i][i] > bounds[0, 1]) simplex[i][i] = bounds[0, 1]; 122 if (simplex[i][i] < bounds[0, 0]) simplex[i][i] = bounds[0, 0]; 138 123 } 139 124 140 125 // improve solutions 141 for (int i = 0; i < ImprovementAttempts.Value; i++) {126 for (int i = 0; i < maxIterations; i++) { 142 127 // order according to their objective function value 143 Array.Sort(simplex, (x, y) => evaluator.Evaluate(x).CompareTo(evaluator.Evaluate(y)));128 Array.Sort(simplex, (x, y) => function.Evaluate(x).CompareTo(function.Evaluate(y))); 144 129 145 130 // calculate centroid … … 155 140 for (int j = 0; j < reflectionPoint.Length; j++) 156 141 reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]); 157 double reflectionPointQuality = evaluator.Evaluate(reflectionPoint);158 if ( evaluator.Evaluate(simplex[0]) <= reflectionPointQuality159 && reflectionPointQuality < evaluator.Evaluate(simplex[simplex.Length - 2]))142 double reflectionPointQuality = function.Evaluate(reflectionPoint); 143 if (function.Evaluate(simplex[0]) <= reflectionPointQuality 144 && reflectionPointQuality < function.Evaluate(simplex[simplex.Length - 2])) 160 145 simplex[simplex.Length - 1] = reflectionPoint; 161 146 162 147 // expansion 163 if (reflectionPointQuality < evaluator.Evaluate(simplex[0])) {148 if (reflectionPointQuality < function.Evaluate(simplex[0])) { 164 149 RealVector expansionPoint = new RealVector(bestSol.Length); 165 150 for (int j = 0; j < expansionPoint.Length; j++) 166 151 expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]); 167 simplex[simplex.Length - 1] = evaluator.Evaluate(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;152 simplex[simplex.Length - 1] = function.Evaluate(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint; 168 153 } 169 154 170 155 // contraction 171 if ( evaluator.Evaluate(simplex[simplex.Length - 2]) <= reflectionPointQuality172 && reflectionPointQuality < evaluator.Evaluate(simplex[simplex.Length - 1])) {156 if (function.Evaluate(simplex[simplex.Length - 2]) <= reflectionPointQuality 157 && reflectionPointQuality < function.Evaluate(simplex[simplex.Length - 1])) { 173 158 RealVector outsideContractionPoint = new RealVector(bestSol.Length); 174 159 for (int j = 0; j < outsideContractionPoint.Length; j++) 175 160 outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]); 176 if ( evaluator.Evaluate(outsideContractionPoint) <= reflectionPointQuality) {161 if (function.Evaluate(outsideContractionPoint) <= reflectionPointQuality) { 177 162 simplex[simplex.Length - 1] = outsideContractionPoint; 178 if ( evaluator.Evaluate(reflectionPoint) >= evaluator.Evaluate(simplex[simplex.Length - 1])) {163 if (function.Evaluate(reflectionPoint) >= function.Evaluate(simplex[simplex.Length - 1])) { 179 164 RealVector insideContractionPoint = new RealVector(bestSol.Length); 180 165 for (int j = 0; j < insideContractionPoint.Length; j++) 181 166 insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]); 182 if ( evaluator.Evaluate(insideContractionPoint) < evaluator.Evaluate(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;167 if (function.Evaluate(insideContractionPoint) < function.Evaluate(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint; 183 168 } 184 169 } … … 192 177 193 178 for (int i = 0; i < simplex[0].Length; i++) { 194 if (simplex[0][i] > Bounds[0, 1]) simplex[0][i] = Bounds[0, 1];195 if (simplex[0][i] < Bounds[0, 0]) simplex[0][i] = Bounds[0, 0];179 if (simplex[0][i] > bounds[0, 1]) simplex[0][i] = bounds[0, 1]; 180 if (simplex[0][i] < bounds[0, 0]) simplex[0][i] = bounds[0, 0]; 196 181 } 197 182 198 CurrentScope.Variables[SolutionParameter.ActualName].Value = simplex[0];199 CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", ImprovementAttempts));183 ExecutionContext.Scope.Variables[SolutionParameter.ActualName].Value = simplex[0]; 184 ExecutionContext.Scope.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(maxIterations))); 200 185 201 186 return base.Apply();
Note: See TracChangeset
for help on using the changeset viewer.