Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 10.9 KB
RevLine 
[3065]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 {
[3528]36  [Item("OneMax Problem", "Represents a OneMax Problem.")]
[3065]37  [Creatable("Problems")]
38  [StorableClass]
[3164]39  public sealed class OneMaxProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
[3065]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    }
[3080]66    public ValueParameter<DoubleValue> BestKnownQualityParameter {
67      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
68    }
69    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
70      get { return BestKnownQualityParameter; }
71    }
[3065]72    #endregion
73
74    #region Properties
[3108]75    public IntValue Length {
76      get { return LengthParameter.Value; }
77      set { LengthParameter.Value = value; }
78    }
[3065]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    }
[3080]96    public DoubleValue BestKnownQuality {
97      get { return BestKnownQualityParameter.Value; }
98    }
[3642]99    private List<IOperator> operators;
[3065]100    public IEnumerable<IOperator> Operators {
101      get { return operators.Cast<IOperator>(); }
102    }
[3667]103    private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
104      get { return operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
[3642]105    }
[3065]106    #endregion
107
[3164]108    public OneMaxProblem()
[3065]109      : base() {
110      RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
111      OneMaxEvaluator evaluator = new OneMaxEvaluator();
112
[3067]113      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
[3065]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));
[3080]117      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
[3065]118
119      creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
120      evaluator.QualityParameter.ActualName = "NumberOfOnes";
121      ParameterizeSolutionCreator();
122      ParameterizeEvaluator();
123
124      Initialize();
125    }
126
127    [StorableConstructor]
[3164]128    private OneMaxProblem(bool deserializing) : base() { }
[3065]129
130    public override IDeepCloneable Clone(Cloner cloner) {
[3164]131      OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
[3065]132      clone.Initialize();
133      return clone;
134    }
135
136    #region Events
137    public event EventHandler SolutionCreatorChanged;
138    private void OnSolutionCreatorChanged() {
[3739]139      EventHandler handler = SolutionCreatorChanged;
140      if (handler != null) handler(this, EventArgs.Empty);
[3065]141    }
142    public event EventHandler EvaluatorChanged;
143    private void OnEvaluatorChanged() {
[3739]144      EventHandler handler = EvaluatorChanged;
145      if (handler != null) handler(this, EventArgs.Empty);
[3065]146    }
147    public event EventHandler OperatorsChanged;
148    private void OnOperatorsChanged() {
[3739]149      EventHandler handler = OperatorsChanged;
150      if (handler != null) handler(this, EventArgs.Empty);
[3065]151    }
[3739]152    public event EventHandler Reset;
153    private void OnReset() {
154      EventHandler handler = Reset;
155      if (handler != null) handler(this, EventArgs.Empty);
156    }
[3065]157
158    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
[3642]159      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
[3065]160      ParameterizeSolutionCreator();
161      ParameterizeEvaluator();
[3667]162      ParameterizeAnalyzer();
[3065]163      ParameterizeOperators();
164      OnSolutionCreatorChanged();
165    }
[3642]166    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
[3065]167      ParameterizeEvaluator();
[3667]168      ParameterizeAnalyzer();
[3065]169      ParameterizeOperators();
170    }
171    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
172      ParameterizeEvaluator();
[3667]173      ParameterizeAnalyzer();
[3065]174      OnEvaluatorChanged();
175    }
[3108]176    void LengthParameter_ValueChanged(object sender, EventArgs e) {
177      ParameterizeSolutionCreator();
178      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
[3115]179      BestKnownQualityParameter.Value.Value = Length.Value;
[3108]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    }
[3121]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    }
[3065]193    #endregion
194
195    #region Helpers
196    [StorableHook(HookType.AfterDeserialization)]
197    private void Initialize() {
198      InitializeOperators();
199      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
[3642]200      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
[3065]201      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
[3108]202      LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
[3115]203      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
[3108]204      BestKnownQualityParameter.Value.Value = Length.Value;
205      BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
[3065]206    }
207    private void ParameterizeSolutionCreator() {
[3108]208      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
[3065]209    }
210    private void ParameterizeEvaluator() {
211      if (Evaluator is OneMaxEvaluator)
212        ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
213    }
[3667]214    private void ParameterizeAnalyzer() {
[3789]215      BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
216      BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
[3667]217      BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
218      BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
[3642]219    }
[3065]220    private void InitializeOperators() {
[3642]221      operators = new List<IOperator>();
222      operators.Add(new BestOneMaxSolutionAnalyzer());
[3667]223      ParameterizeAnalyzer();
[3303]224      foreach(IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
225        if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
226          operators.Add(op);
[3126]227        }
[3065]228      }
[3303]229      ParameterizeOperators();
[3121]230      InitializeMoveGenerators();
[3065]231    }
[3121]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    }
[3065]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      }
[3121]247      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
248        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
249      }
[3065]250    }
251    #endregion
252  }
253}
Note: See TracBrowser for help on using the repository browser.