Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3797 was 3797, checked in by mkommend, 14 years ago

corrected namespaces and removed resources files in HL 3.3 solution (ticket #893)

File size: 10.9 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    private List<IOperator> operators;
100    public IEnumerable<IOperator> Operators {
101      get { return operators.Cast<IOperator>(); }
102    }
103    private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
104      get { return operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
105    }
106    #endregion
107
108    public OneMaxProblem()
109      : base() {
110      RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
111      OneMaxEvaluator evaluator = new OneMaxEvaluator();
112
113      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
114      Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
115      Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
116      Parameters.Add(new ValueParameter<IOneMaxEvaluator>("Evaluator", "The operator which should be used to evaluate OneMax solutions.", evaluator));
117      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
118
119      creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
120      evaluator.QualityParameter.ActualName = "NumberOfOnes";
121      ParameterizeSolutionCreator();
122      ParameterizeEvaluator();
123
124      Initialize();
125    }
126
127    [StorableConstructor]
128    private OneMaxProblem(bool deserializing) : base() { }
129
130    public override IDeepCloneable Clone(Cloner cloner) {
131      OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
132      clone.Initialize();
133      return clone;
134    }
135
136    #region Events
137    public event EventHandler SolutionCreatorChanged;
138    private void OnSolutionCreatorChanged() {
139      EventHandler handler = SolutionCreatorChanged;
140      if (handler != null) handler(this, EventArgs.Empty);
141    }
142    public event EventHandler EvaluatorChanged;
143    private void OnEvaluatorChanged() {
144      EventHandler handler = EvaluatorChanged;
145      if (handler != null) handler(this, EventArgs.Empty);
146    }
147    public event EventHandler OperatorsChanged;
148    private void OnOperatorsChanged() {
149      EventHandler handler = OperatorsChanged;
150      if (handler != null) handler(this, EventArgs.Empty);
151    }
152    public event EventHandler Reset;
153    private void OnReset() {
154      EventHandler handler = Reset;
155      if (handler != null) handler(this, EventArgs.Empty);
156    }
157
158    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
159      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
160      ParameterizeSolutionCreator();
161      ParameterizeEvaluator();
162      ParameterizeAnalyzer();
163      ParameterizeOperators();
164      OnSolutionCreatorChanged();
165    }
166    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
167      ParameterizeEvaluator();
168      ParameterizeAnalyzer();
169      ParameterizeOperators();
170    }
171    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
172      ParameterizeEvaluator();
173      ParameterizeAnalyzer();
174      OnEvaluatorChanged();
175    }
176    void LengthParameter_ValueChanged(object sender, EventArgs e) {
177      ParameterizeSolutionCreator();
178      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
179      BestKnownQualityParameter.Value.Value = Length.Value;
180    }
181    void Length_ValueChanged(object sender, EventArgs e) {
182      BestKnownQualityParameter.Value.Value = Length.Value;
183    }
184    void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
185      BestKnownQualityParameter.Value.Value = Length.Value;
186    }
187    void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
188      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
189      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
190        op.OneBitflipMoveParameter.ActualName = name;
191      }
192    }
193    #endregion
194
195    #region Helpers
196    [StorableHook(HookType.AfterDeserialization)]
197    private void Initialize() {
198      InitializeOperators();
199      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
200      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
201      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
202      LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
203      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
204      BestKnownQualityParameter.Value.Value = Length.Value;
205      BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
206    }
207    private void ParameterizeSolutionCreator() {
208      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
209    }
210    private void ParameterizeEvaluator() {
211      if (Evaluator is OneMaxEvaluator)
212        ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
213    }
214    private void ParameterizeAnalyzer() {
215      BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
216      BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
217      BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
218      BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
219    }
220    private void InitializeOperators() {
221      operators = new List<IOperator>();
222      operators.Add(new BestOneMaxSolutionAnalyzer());
223      ParameterizeAnalyzer();
224      foreach(IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
225        if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
226          operators.Add(op);
227        }
228      }
229      ParameterizeOperators();
230      InitializeMoveGenerators();
231    }
232    private void InitializeMoveGenerators() {
233      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
234        if (op is IMoveGenerator) {
235          op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
236        }
237      }
238    }
239    private void ParameterizeOperators() {
240      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
241        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
242        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
243      }
244      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
245        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
246      }
247      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
248        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
249      }
250    }
251    #endregion
252  }
253}
Note: See TracBrowser for help on using the repository browser.