Changeset 7786 for branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TestFunctions/TestFunctionsImprovementOperator.cs
- Timestamp:
- 05/09/12 12:22:33 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TestFunctions/TestFunctionsImprovementOperator.cs
r7775 r7786 21 21 22 22 using System; 23 using System.Reflection; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 26 27 using HeuristicLab.Encodings.RealVectorEncoding; 27 28 using HeuristicLab.Operators; 28 using HeuristicLab.Optimization;29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.Knapsack;32 31 using HeuristicLab.Problems.TestFunctions; 33 32 … … 38 37 [Item("TestFunctionsImprovementOperator", "An operator that improves test functions solutions.")] 39 38 [StorableClass] 40 public abstract class TestFunctionsImprovementOperator : SingleSuccessorOperator, ILocalImprovementOperator, IScatterSearchTargetProcessor { 41 #region Problem properties 42 public Type ProblemType { 43 get { return typeof(KnapsackProblem); } 44 } 45 [Storable] 46 private KnapsackProblem problem; 47 public IProblem Problem { 48 get { return problem; } 49 set { problem = (KnapsackProblem)value; } 50 } 51 #endregion 52 39 public sealed class TestFunctionsImprovementOperator : SingleSuccessorOperator, IImprovementOperator { 53 40 #region Parameter properties 41 public IValueParameter<DoubleValue> AlphaParameter { 42 get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; } 43 } 44 public IValueParameter<DoubleValue> BetaParameter { 45 get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; } 46 } 54 47 public ScopeParameter CurrentScopeParameter { 55 48 get { return (ScopeParameter)Parameters["CurrentScope"]; } 56 49 } 57 public IValue LookupParameter<DoubleMatrix> CoordinatesParameter {58 get { return (IValue LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }50 public IValueParameter<DoubleValue> DeltaParameter { 51 get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; } 59 52 } 60 53 public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter { 61 54 get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; } 62 55 } 56 public IValueParameter<DoubleValue> GammaParameter { 57 get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; } 58 } 63 59 public IValueLookupParameter<IntValue> ImprovementAttemptsParameter { 64 60 get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; } … … 70 66 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 71 67 } 72 public IValueParameter<DoubleValue> AlphaParameter {73 get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }74 }75 public IValueParameter<DoubleValue> BetaParameter {76 get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }77 }78 public IValueParameter<DoubleValue> GammaParameter {79 get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }80 }81 public IValueParameter<DoubleValue> DeltaParameter {82 get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; }83 }84 #region ILocalImprovementOperator Parameters85 public IValueLookupParameter<IntValue> MaximumIterationsParameter {86 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }87 }88 public ILookupParameter<IntValue> EvaluatedSolutionsParameter {89 get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }90 }91 public ILookupParameter<ResultCollection> ResultsParameter {92 get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }93 }94 68 #endregion 95 #endregion96 69 97 70 #region Properties 98 private IItem Target {99 get { return TargetParameter.ActualValue; }100 }101 71 private DoubleValue Alpha { 102 72 get { return AlphaParameter.Value; } … … 105 75 get { return BetaParameter.Value; } 106 76 } 107 p rivate DoubleValue Gamma{108 get { return GammaParameter.Value; }77 public IScope CurrentScope { 78 get { return CurrentScopeParameter.ActualValue; } 109 79 } 110 80 private DoubleValue Delta { 111 81 get { return DeltaParameter.Value; } 112 }113 protected Func<RealVector, double> FunctionEvaluator { get; set; }114 public IScope CurrentScope {115 get { return CurrentScopeParameter.ActualValue; }116 }117 public DoubleMatrix Coordinates {118 get { return CoordinatesParameter.ActualValue; }119 set { CoordinatesParameter.ActualValue = value; }120 82 } 121 83 public ISingleObjectiveTestFunctionProblemEvaluator Evaluator { … … 123 85 set { EvaluatorParameter.ActualValue = value; } 124 86 } 87 private DoubleValue Gamma { 88 get { return GammaParameter.Value; } 89 } 125 90 public IntValue ImprovementAttempts { 126 91 get { return ImprovementAttemptsParameter.ActualValue; } … … 131 96 set { RandomParameter.ActualValue = value; } 132 97 } 98 private IItem Target { 99 get { return TargetParameter.ActualValue; } 100 } 133 101 #endregion 134 102 135 103 [StorableConstructor] 136 protected TestFunctionsImprovementOperator(bool deserializing) : base(deserializing) { } 137 protected TestFunctionsImprovementOperator(TestFunctionsImprovementOperator original, Cloner cloner) 138 : base(original, cloner) { 139 this.problem = cloner.Clone(original.problem); 140 } 104 private TestFunctionsImprovementOperator(bool deserializing) : base(deserializing) { } 105 private TestFunctionsImprovementOperator(TestFunctionsImprovementOperator original, Cloner cloner) : base(original, cloner) { } 141 106 public TestFunctionsImprovementOperator() 142 107 : base() { 143 108 #region Create parameters 144 Parameters.Add(new ScopeParameter("CurrentScope"));145 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Coordinates"));146 Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator"));147 Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", new IntValue(100)));148 Parameters.Add(new ValueLookupParameter<IRandom>("Random"));149 Parameters.Add(new ValueLookupParameter<IItem>("Target"));150 109 Parameters.Add(new ValueParameter<DoubleValue>("Alpha", new DoubleValue(1.0))); 151 110 Parameters.Add(new ValueParameter<DoubleValue>("Beta", new DoubleValue(2.0))); 111 Parameters.Add(new ScopeParameter("CurrentScope")); 112 Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5))); 113 Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator")); 152 114 Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5))); 153 Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5))); 115 Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", new IntValue(100))); 116 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 117 Parameters.Add(new ValueLookupParameter<IRandom>("Random")); 154 118 #endregion 155 119 TargetParameter.ActualName = "Point"; // temporary solution for the test functions problem 156 120 } 157 121 122 public override IDeepCloneable Clone(Cloner cloner) { 123 return new TestFunctionsImprovementOperator(this, cloner); 124 } 125 158 126 public override IOperation Apply() { 159 var bestSol = CurrentScope.Variables[TargetParameter.ActualName].Value as RealVector; 160 var bestSolQuality = FunctionEvaluator(bestSol); 127 RealVector bestSol = CurrentScope.Variables[TargetParameter.ActualName].Value as RealVector; 128 MethodInfo evaluationMethod = Evaluator.GetType().GetMethod("Apply", 129 BindingFlags.Public | BindingFlags.Static, 130 null, 131 new Type[] { typeof(RealVector) }, null); 132 Func<RealVector, double> functionEvaluator = x => (double)evaluationMethod.Invoke(Evaluator, new object[] { x }); 133 double bestSolQuality = functionEvaluator(bestSol); 161 134 162 135 // create perturbed solutions 163 varsimplex = new RealVector[bestSol.Length];136 RealVector[] simplex = new RealVector[bestSol.Length]; 164 137 for (int i = 0; i < simplex.Length; i++) { 165 138 simplex[i] = bestSol.Clone() as RealVector; … … 172 145 for (int i = 0; i < ImprovementAttempts.Value; i++) { 173 146 // order according to their objective function value 174 Array.Sort(simplex, (x, y) => FunctionEvaluator(x).CompareTo(FunctionEvaluator(y)));147 Array.Sort(simplex, (x, y) => functionEvaluator(x).CompareTo(functionEvaluator(y))); 175 148 176 149 // calculate centroid 177 var centroid = new RealVector(bestSol.Length);150 RealVector centroid = new RealVector(bestSol.Length); 178 151 foreach (var vector in simplex) 179 152 for (int j = 0; j < centroid.Length; j++) … … 183 156 184 157 // reflection 185 var reflectionPoint = new RealVector(bestSol.Length);158 RealVector reflectionPoint = new RealVector(bestSol.Length); 186 159 for (int j = 0; j < reflectionPoint.Length; j++) 187 160 reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]); 188 double reflectionPointQuality = FunctionEvaluator(reflectionPoint);189 if ( FunctionEvaluator(simplex[0]) <= reflectionPointQuality190 && reflectionPointQuality < FunctionEvaluator(simplex[simplex.Length - 2]))161 double reflectionPointQuality = functionEvaluator(reflectionPoint); 162 if (functionEvaluator(simplex[0]) <= reflectionPointQuality 163 && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 2])) 191 164 simplex[simplex.Length - 1] = reflectionPoint; 192 165 193 166 // expansion 194 if (reflectionPointQuality < FunctionEvaluator(simplex[0])) {195 var expansionPoint = new RealVector(bestSol.Length);167 if (reflectionPointQuality < functionEvaluator(simplex[0])) { 168 RealVector expansionPoint = new RealVector(bestSol.Length); 196 169 for (int j = 0; j < expansionPoint.Length; j++) 197 170 expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]); 198 simplex[simplex.Length - 1] = FunctionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;171 simplex[simplex.Length - 1] = functionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint; 199 172 } 200 173 201 174 // contraction 202 if ( FunctionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality203 && reflectionPointQuality < FunctionEvaluator(simplex[simplex.Length - 1])) {204 var outsideContractionPoint = new RealVector(bestSol.Length);175 if (functionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality 176 && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 1])) { 177 RealVector outsideContractionPoint = new RealVector(bestSol.Length); 205 178 for (int j = 0; j < outsideContractionPoint.Length; j++) 206 179 outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]); 207 if ( FunctionEvaluator(outsideContractionPoint) <= reflectionPointQuality) {180 if (functionEvaluator(outsideContractionPoint) <= reflectionPointQuality) { 208 181 simplex[simplex.Length - 1] = outsideContractionPoint; 209 if ( FunctionEvaluator(reflectionPoint) >= FunctionEvaluator(simplex[simplex.Length - 1])) {210 var insideContractionPoint = new RealVector(bestSol.Length);182 if (functionEvaluator(reflectionPoint) >= functionEvaluator(simplex[simplex.Length - 1])) { 183 RealVector insideContractionPoint = new RealVector(bestSol.Length); 211 184 for (int j = 0; j < insideContractionPoint.Length; j++) 212 185 insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]); 213 if ( FunctionEvaluator(insideContractionPoint) < FunctionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;186 if (functionEvaluator(insideContractionPoint) < functionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint; 214 187 } 215 188 }
Note: See TracChangeset
for help on using the changeset viewer.