Changeset 6938 for trunk/sources/HeuristicLab.Problems.TestFunctions
- Timestamp:
- 10/26/11 23:36:10 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs
r6051 r6938 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing;25 24 using System.Linq; 26 25 using HeuristicLab.Common; … … 37 36 [StorableClass] 38 37 [Creatable("Problems")] 39 public sealed class SingleObjectiveTestFunctionProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {38 public sealed class SingleObjectiveTestFunctionProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveTestFunctionProblemEvaluator, IRealVectorCreator>, IStorableContent { 40 39 public string Filename { get; set; } 41 40 … … 47 46 private StdDevStrategyVectorManipulator strategyVectorManipulator; 48 47 49 public override Image ItemImage {50 get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }51 }52 53 48 #region Parameter Properties 54 public ValueParameter<BoolValue> MaximizationParameter {55 get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }56 }57 IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {58 get { return MaximizationParameter; }59 }60 49 public ValueParameter<DoubleMatrix> BoundsParameter { 61 50 get { return (ValueParameter<DoubleMatrix>)Parameters["Bounds"]; } … … 64 53 get { return (ValueParameter<IntValue>)Parameters["ProblemSize"]; } 65 54 } 66 public ValueParameter<IRealVectorCreator> SolutionCreatorParameter {67 get { return (ValueParameter<IRealVectorCreator>)Parameters["SolutionCreator"]; }68 }69 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {70 get { return SolutionCreatorParameter; }71 }72 public ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {73 get { return (ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }74 }75 IParameter IHeuristicOptimizationProblem.EvaluatorParameter {76 get { return EvaluatorParameter; }77 }78 public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {79 get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }80 }81 IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {82 get { return BestKnownQualityParameter; }83 }84 55 public OptionalValueParameter<RealVector> BestKnownSolutionParameter { 85 56 get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; } … … 88 59 89 60 #region Properties 90 public BoolValue Maximization {91 get { return MaximizationParameter.Value; }92 set { MaximizationParameter.Value = value; }93 }94 61 public DoubleMatrix Bounds { 95 62 get { return BoundsParameter.Value; } … … 100 67 set { ProblemSizeParameter.Value = value; } 101 68 } 102 public IRealVectorCreator SolutionCreator {103 get { return SolutionCreatorParameter.Value; }104 set { SolutionCreatorParameter.Value = value; }105 }106 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {107 get { return SolutionCreatorParameter.Value; }108 }109 public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {110 get { return EvaluatorParameter.Value; }111 set { EvaluatorParameter.Value = value; }112 }113 ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {114 get { return EvaluatorParameter.Value; }115 }116 IEvaluator IHeuristicOptimizationProblem.Evaluator {117 get { return EvaluatorParameter.Value; }118 }119 public DoubleValue BestKnownQuality {120 get { return BestKnownQualityParameter.Value; }121 set { BestKnownQualityParameter.Value = value; }122 }123 public IEnumerable<IOperator> Operators {124 get { return operators; }125 }126 69 private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer { 127 get { return operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); } 128 } 129 #endregion 130 131 [Storable] 132 private List<IOperator> operators; 70 get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); } 71 } 72 #endregion 73 74 // BackwardsCompatibility3.3 75 #region Backwards compatible code, remove with 3.4 76 [Obsolete] 77 [Storable(Name = "operators")] 78 private IEnumerable<IOperator> oldOperators { 79 get { return null; } 80 set { 81 if (value != null && value.Any()) 82 Operators.AddRange(value); 83 } 84 } 85 #endregion 133 86 134 87 [StorableConstructor] … … 136 89 private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner) 137 90 : base(original, cloner) { 138 operators = original.operators.Where(x => original.IsNotFieldReferenced(x)).Select(x => cloner.Clone(x)).ToList();139 91 strategyVectorCreator = cloner.Clone(original.strategyVectorCreator); 140 operators.Add(strategyVectorCreator);141 92 strategyVectorCrossover = cloner.Clone(original.strategyVectorCrossover); 142 operators.Add(strategyVectorCrossover);143 93 strategyVectorManipulator = cloner.Clone(original.strategyVectorManipulator); 144 operators.Add(strategyVectorManipulator);145 94 AttachEventHandlers(); 146 95 } 147 96 public SingleObjectiveTestFunctionProblem() 148 : base() { 149 UniformRandomRealVectorCreator creator = new UniformRandomRealVectorCreator(); 150 AckleyEvaluator evaluator = new AckleyEvaluator(); 151 152 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(evaluator.Maximization))); 153 Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", evaluator.Bounds)); 97 : base(new AckleyEvaluator(), new UniformRandomRealVectorCreator()) { 98 Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.", Evaluator.Bounds)); 154 99 Parameters.Add(new ValueParameter<IntValue>("ProblemSize", "The dimension of the problem.", new IntValue(2))); 155 Parameters.Add(new ValueParameter<IRealVectorCreator>("SolutionCreator", "The operator which should be used to create new test function solutions.", creator));156 Parameters.Add(new ValueParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator", "The operator which should be used to evaluate test function solutions.", evaluator));157 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this test function.", new DoubleValue(evaluator.BestKnownQuality)));158 100 Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance.")); 159 101 … … 165 107 strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5); 166 108 167 creator.RealVectorParameter.ActualName = "Point";109 SolutionCreator.RealVectorParameter.ActualName = "Point"; 168 110 ParameterizeSolutionCreator(); 169 111 ParameterizeEvaluator(); … … 185 127 186 128 #region Events 187 public event EventHandler SolutionCreatorChanged; 188 private void OnSolutionCreatorChanged() { 189 EventHandler handler = SolutionCreatorChanged; 190 if (handler != null) handler(this, EventArgs.Empty); 191 } 192 public event EventHandler EvaluatorChanged; 193 private void OnEvaluatorChanged() { 194 EventHandler handler = EvaluatorChanged; 195 if (handler != null) handler(this, EventArgs.Empty); 196 } 197 public event EventHandler OperatorsChanged; 198 private void OnOperatorsChanged() { 199 EventHandler handler = OperatorsChanged; 200 if (handler != null) handler(this, EventArgs.Empty); 201 } 202 public event EventHandler Reset; 203 private void OnReset() { 204 EventHandler handler = Reset; 205 if (handler != null) handler(this, EventArgs.Empty); 206 } 207 208 private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) { 209 ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged); 210 ProblemSize_ValueChanged(null, EventArgs.Empty); 211 } 212 private void ProblemSize_ValueChanged(object sender, EventArgs e) { 213 if (ProblemSize.Value < 1) ProblemSize.Value = 1; 214 ParameterizeSolutionCreator(); 215 ParameterizeEvaluator(); 216 strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value)); 217 strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value))); 218 OnReset(); 219 } 220 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 129 protected override void OnSolutionCreatorChanged() { 130 base.OnSolutionCreatorChanged(); 221 131 ParameterizeSolutionCreator(); 222 132 ParameterizeAnalyzers(); 133 SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged); 223 134 SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty); 224 OnSolutionCreatorChanged(); 225 } 226 private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) { 227 ParameterizeEvaluator(); 228 ParameterizeOperators(); 229 ParameterizeAnalyzers(); 230 } 231 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 135 } 136 protected override void OnEvaluatorChanged() { 137 base.OnEvaluatorChanged(); 232 138 ParameterizeEvaluator(); 233 139 UpdateMoveEvaluators(); … … 242 148 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 243 149 Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty); 244 OnEvaluatorChanged();245 150 OnReset(); 151 } 152 private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) { 153 ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged); 154 ProblemSize_ValueChanged(null, EventArgs.Empty); 155 } 156 private void ProblemSize_ValueChanged(object sender, EventArgs e) { 157 if (ProblemSize.Value < 1) ProblemSize.Value = 1; 158 ParameterizeSolutionCreator(); 159 ParameterizeEvaluator(); 160 strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value)); 161 strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value))); 162 OnReset(); 163 } 164 private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) { 165 ParameterizeEvaluator(); 166 ParameterizeOperators(); 167 ParameterizeAnalyzers(); 246 168 } 247 169 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { … … 305 227 // BackwardsCompatibility3.3 306 228 #region Backwards compatible code (remove with 3.4) 307 if ( operators == null) InitializeOperators();229 if (Operators.Count == 0) InitializeOperators(); 308 230 #endregion 309 231 AttachEventHandlers(); … … 316 238 Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged); 317 239 Bounds.ItemChanged += new EventHandler<EventArgs<int, int>>(Bounds_ItemChanged); 318 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);319 240 SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged); 320 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);321 241 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 322 242 strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged); … … 334 254 } 335 255 private void InitializeOperators() { 336 operators = new List<IOperator>(); 337 operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer()); 256 Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer()); 338 257 ParameterizeAnalyzers(); 339 operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>());340 operators.Add(strategyVectorCreator);341 operators.Add(strategyVectorCrossover);342 operators.Add(strategyVectorManipulator);258 Operators.AddRange(ApplicationManager.Manager.GetInstances<IRealVectorOperator>().Cast<IOperator>()); 259 Operators.Add(strategyVectorCreator); 260 Operators.Add(strategyVectorCrossover); 261 Operators.Add(strategyVectorManipulator); 343 262 UpdateMoveEvaluators(); 344 263 ParameterizeOperators(); … … 354 273 private void UpdateMoveEvaluators() { 355 274 foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType<ISingleObjectiveTestFunctionMoveEvaluator>().ToList()) 356 operators.Remove(op);275 Operators.Remove(op); 357 276 foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances<ISingleObjectiveTestFunctionMoveEvaluator>()) 358 277 if (op.EvaluatorType == Evaluator.GetType()) { 359 operators.Add(op);278 Operators.Add(op); 360 279 #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators 361 280 if (op is ISphereMoveEvaluator) {
Note: See TracChangeset
for help on using the changeset viewer.