Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3732 was 3667, checked in by svonolfe, 14 years ago
  • Updated OneMax analyzer
  • Updated Knapsack analyzer
  • Fixed bug in OneMax and TF analyzer views

(#999)

File size: 10.6 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;
34using HeuristicLab.Problems.OneMax.Analyzers;
35
36namespace HeuristicLab.Problems.OneMax {
37  [Item("OneMax Problem", "Represents a OneMax Problem.")]
38  [Creatable("Problems")]
39  [StorableClass]
40  public sealed class OneMaxProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
43    }
44
45    #region Parameter Properties
46    public ValueParameter<BoolValue> MaximizationParameter {
47      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
48    }
49    IParameter ISingleObjectiveProblem.MaximizationParameter {
50      get { return MaximizationParameter; }
51    }
52    public ValueParameter<IntValue> LengthParameter {
53      get { return (ValueParameter<IntValue>)Parameters["Length"]; }
54    }
55    public ValueParameter<IBinaryVectorCreator> SolutionCreatorParameter {
56      get { return (ValueParameter<IBinaryVectorCreator>)Parameters["SolutionCreator"]; }
57    }
58    IParameter IProblem.SolutionCreatorParameter {
59      get { return SolutionCreatorParameter; }
60    }
61    public ValueParameter<IOneMaxEvaluator> EvaluatorParameter {
62      get { return (ValueParameter<IOneMaxEvaluator>)Parameters["Evaluator"]; }
63    }
64    IParameter IProblem.EvaluatorParameter {
65      get { return EvaluatorParameter; }
66    }
67    public ValueParameter<DoubleValue> BestKnownQualityParameter {
68      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
69    }
70    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
71      get { return BestKnownQualityParameter; }
72    }
73    #endregion
74
75    #region Properties
76    public IntValue Length {
77      get { return LengthParameter.Value; }
78      set { LengthParameter.Value = value; }
79    }
80    public IBinaryVectorCreator SolutionCreator {
81      get { return SolutionCreatorParameter.Value; }
82      set { SolutionCreatorParameter.Value = value; }
83    }
84    ISolutionCreator IProblem.SolutionCreator {
85      get { return SolutionCreatorParameter.Value; }
86    }
87    public IOneMaxEvaluator Evaluator {
88      get { return EvaluatorParameter.Value; }
89      set { EvaluatorParameter.Value = value; }
90    }
91    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
92      get { return EvaluatorParameter.Value; }
93    }
94    IEvaluator IProblem.Evaluator {
95      get { return EvaluatorParameter.Value; }
96    }
97    public DoubleValue BestKnownQuality {
98      get { return BestKnownQualityParameter.Value; }
99    }
100    private List<IOperator> operators;
101    public IEnumerable<IOperator> Operators {
102      get { return operators.Cast<IOperator>(); }
103    }
104    private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
105      get { return operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
106    }
107    #endregion
108
109    public OneMaxProblem()
110      : base() {
111      RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
112      OneMaxEvaluator evaluator = new OneMaxEvaluator();
113
114      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
115      Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
116      Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
117      Parameters.Add(new ValueParameter<IOneMaxEvaluator>("Evaluator", "The operator which should be used to evaluate OneMax solutions.", evaluator));
118      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
119
120      creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
121      evaluator.QualityParameter.ActualName = "NumberOfOnes";
122      ParameterizeSolutionCreator();
123      ParameterizeEvaluator();
124
125      Initialize();
126    }
127
128    [StorableConstructor]
129    private OneMaxProblem(bool deserializing) : base() { }
130
131    public override IDeepCloneable Clone(Cloner cloner) {
132      OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
133      clone.Initialize();
134      return clone;
135    }
136
137    #region Events
138    public event EventHandler SolutionCreatorChanged;
139    private void OnSolutionCreatorChanged() {
140      if (SolutionCreatorChanged != null)
141        SolutionCreatorChanged(this, EventArgs.Empty);
142    }
143    public event EventHandler EvaluatorChanged;
144    private void OnEvaluatorChanged() {
145      if (EvaluatorChanged != null)
146        EvaluatorChanged(this, EventArgs.Empty);
147    }
148
149    public event EventHandler OperatorsChanged;
150    private void OnOperatorsChanged() {
151      if (OperatorsChanged != null)
152        OperatorsChanged(this, EventArgs.Empty);
153    }
154
155    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
156      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
157      ParameterizeSolutionCreator();
158      ParameterizeEvaluator();
159      ParameterizeAnalyzer();
160      ParameterizeOperators();
161      OnSolutionCreatorChanged();
162    }
163    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
164      ParameterizeEvaluator();
165      ParameterizeAnalyzer();
166      ParameterizeOperators();
167    }
168    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
169      ParameterizeEvaluator();
170      ParameterizeAnalyzer();
171      OnEvaluatorChanged();
172    }
173    void LengthParameter_ValueChanged(object sender, EventArgs e) {
174      ParameterizeSolutionCreator();
175      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
176      BestKnownQualityParameter.Value.Value = Length.Value;
177    }
178    void Length_ValueChanged(object sender, EventArgs e) {
179      BestKnownQualityParameter.Value.Value = Length.Value;
180    }
181    void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
182      BestKnownQualityParameter.Value.Value = Length.Value;
183    }
184    void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
185      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
186      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
187        op.OneBitflipMoveParameter.ActualName = name;
188      }
189    }
190    #endregion
191
192    #region Helpers
193    [StorableHook(HookType.AfterDeserialization)]
194    private void Initialize() {
195      InitializeOperators();
196      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
197      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
198      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
199      LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
200      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
201      BestKnownQualityParameter.Value.Value = Length.Value;
202      BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
203    }
204    private void ParameterizeSolutionCreator() {
205      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
206    }
207    private void ParameterizeEvaluator() {
208      if (Evaluator is OneMaxEvaluator)
209        ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
210    }
211    private void ParameterizeAnalyzer() {
212      BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
213      BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
214    }
215    private void InitializeOperators() {
216      operators = new List<IOperator>();
217      operators.Add(new BestOneMaxSolutionAnalyzer());
218      ParameterizeAnalyzer();
219      foreach(IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
220        if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
221          operators.Add(op);
222        }
223      }
224      ParameterizeOperators();
225      InitializeMoveGenerators();
226    }
227    private void InitializeMoveGenerators() {
228      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
229        if (op is IMoveGenerator) {
230          op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
231        }
232      }
233    }
234    private void ParameterizeOperators() {
235      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
236        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
237        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
238      }
239      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
240        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
241      }
242      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
243        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
244      }
245    }
246    #endregion
247  }
248}
Note: See TracBrowser for help on using the repository browser.