Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.OneMax/3.3/OnemaxProblem.cs @ 4128

Last change on this file since 4128 was 4118, checked in by abeham, 14 years ago

#1090

  • Fixed problem plugins reloading their operators on deserialization in following problems (forgot on them in the first commit)
    • SupportVectorRegressionProblem
    • SymbolicTimeSeriesPrognosisProblem
  • Fixed a bug in the FeatureSelectionProblem introduced in r4098
  • Fixed the issues mentioned in the code review of mkommend
File size: 11.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.BinaryVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Problems.OneMax {
36  [Item("OneMax Problem", "Represents a OneMax Problem.")]
37  [Creatable("Problems")]
38  [StorableClass]
39  public sealed class OneMaxProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
40    public override Image ItemImage {
41      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
42    }
43
44    #region Parameter Properties
45    public ValueParameter<BoolValue> MaximizationParameter {
46      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
47    }
48    IParameter ISingleObjectiveProblem.MaximizationParameter {
49      get { return MaximizationParameter; }
50    }
51    public ValueParameter<IntValue> LengthParameter {
52      get { return (ValueParameter<IntValue>)Parameters["Length"]; }
53    }
54    public ValueParameter<IBinaryVectorCreator> SolutionCreatorParameter {
55      get { return (ValueParameter<IBinaryVectorCreator>)Parameters["SolutionCreator"]; }
56    }
57    IParameter IProblem.SolutionCreatorParameter {
58      get { return SolutionCreatorParameter; }
59    }
60    public ValueParameter<IOneMaxEvaluator> EvaluatorParameter {
61      get { return (ValueParameter<IOneMaxEvaluator>)Parameters["Evaluator"]; }
62    }
63    IParameter IProblem.EvaluatorParameter {
64      get { return EvaluatorParameter; }
65    }
66    public ValueParameter<DoubleValue> BestKnownQualityParameter {
67      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
68    }
69    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
70      get { return BestKnownQualityParameter; }
71    }
72    #endregion
73
74    #region Properties
75    public IntValue Length {
76      get { return LengthParameter.Value; }
77      set { LengthParameter.Value = value; }
78    }
79    public IBinaryVectorCreator SolutionCreator {
80      get { return SolutionCreatorParameter.Value; }
81      set { SolutionCreatorParameter.Value = value; }
82    }
83    ISolutionCreator IProblem.SolutionCreator {
84      get { return SolutionCreatorParameter.Value; }
85    }
86    public IOneMaxEvaluator Evaluator {
87      get { return EvaluatorParameter.Value; }
88      set { EvaluatorParameter.Value = value; }
89    }
90    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
91      get { return EvaluatorParameter.Value; }
92    }
93    IEvaluator IProblem.Evaluator {
94      get { return EvaluatorParameter.Value; }
95    }
96    public DoubleValue BestKnownQuality {
97      get { return BestKnownQualityParameter.Value; }
98    }
99    public IEnumerable<IOperator> Operators {
100      get { return operators.Cast<IOperator>(); }
101    }
102    private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
103      get { return operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
104    }
105    #endregion
106
107    [Storable]
108    private List<IOperator> operators;
109
110    [StorableConstructor]
111    private OneMaxProblem(bool deserializing) : base(deserializing) { }
112    public OneMaxProblem()
113      : base() {
114      RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
115      OneMaxEvaluator evaluator = new OneMaxEvaluator();
116
117      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
118      Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
119      Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
120      Parameters.Add(new ValueParameter<IOneMaxEvaluator>("Evaluator", "The operator which should be used to evaluate OneMax solutions.", evaluator));
121      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
122
123      creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
124      evaluator.QualityParameter.ActualName = "NumberOfOnes";
125      ParameterizeSolutionCreator();
126      ParameterizeEvaluator();
127
128      InitializeOperators();
129      AttachEventHandlers();
130    }
131
132    public override IDeepCloneable Clone(Cloner cloner) {
133      OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
134      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
135      clone.AttachEventHandlers();
136      return clone;
137    }
138
139    #region Events
140    public event EventHandler SolutionCreatorChanged;
141    private void OnSolutionCreatorChanged() {
142      EventHandler handler = SolutionCreatorChanged;
143      if (handler != null) handler(this, EventArgs.Empty);
144    }
145    public event EventHandler EvaluatorChanged;
146    private void OnEvaluatorChanged() {
147      EventHandler handler = EvaluatorChanged;
148      if (handler != null) handler(this, EventArgs.Empty);
149    }
150    public event EventHandler OperatorsChanged;
151    private void OnOperatorsChanged() {
152      EventHandler handler = OperatorsChanged;
153      if (handler != null) handler(this, EventArgs.Empty);
154    }
155    public event EventHandler Reset;
156    private void OnReset() {
157      EventHandler handler = Reset;
158      if (handler != null) handler(this, EventArgs.Empty);
159    }
160
161    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
162      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
163      ParameterizeSolutionCreator();
164      ParameterizeEvaluator();
165      ParameterizeAnalyzer();
166      ParameterizeOperators();
167      OnSolutionCreatorChanged();
168    }
169    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
170      ParameterizeEvaluator();
171      ParameterizeAnalyzer();
172      ParameterizeOperators();
173    }
174    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
175      ParameterizeEvaluator();
176      ParameterizeAnalyzer();
177      OnEvaluatorChanged();
178    }
179    void LengthParameter_ValueChanged(object sender, EventArgs e) {
180      ParameterizeSolutionCreator();
181      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
182      BestKnownQualityParameter.Value.Value = Length.Value;
183    }
184    void Length_ValueChanged(object sender, EventArgs e) {
185      BestKnownQualityParameter.Value.Value = Length.Value;
186    }
187    void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
188      BestKnownQualityParameter.Value.Value = Length.Value;
189    }
190    void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
191      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
192      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
193        op.OneBitflipMoveParameter.ActualName = name;
194      }
195    }
196    #endregion
197
198    #region Helpers
199    [StorableHook(HookType.AfterDeserialization)]
200    private void AfterDeserializationHook() {
201      // BackwardsCompatibility3.3
202      #region Backwards compatible code (remove with 3.4)
203      if (operators == null) InitializeOperators();
204      #endregion
205      AttachEventHandlers();
206    }
207
208    private void AttachEventHandlers() {
209      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
210      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
211      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
212      LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
213      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
214      BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
215    }
216
217    private void ParameterizeSolutionCreator() {
218      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
219    }
220    private void ParameterizeEvaluator() {
221      if (Evaluator is OneMaxEvaluator)
222        ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
223    }
224    private void ParameterizeAnalyzer() {
225      BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
226      BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
227      BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
228      BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
229    }
230    private void InitializeOperators() {
231      operators = new List<IOperator>();
232      operators.Add(new BestOneMaxSolutionAnalyzer());
233      ParameterizeAnalyzer();
234      foreach(IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
235        if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
236          operators.Add(op);
237        }
238      }
239      ParameterizeOperators();
240      InitializeMoveGenerators();
241    }
242    private void InitializeMoveGenerators() {
243      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
244        if (op is IMoveGenerator) {
245          op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
246        }
247      }
248    }
249    private void ParameterizeOperators() {
250      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
251        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
252        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
253      }
254      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
255        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
256      }
257      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
258        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
259      }
260    }
261    #endregion
262  }
263}
Note: See TracBrowser for help on using the repository browser.