Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.OneMax/3.3/Onemax.cs @ 3067

Last change on this file since 3067 was 3067, checked in by svonolfe, 15 years ago

Updated OneMax problem (#916)

File size: 7.5 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 {
36  [Item("OneMax", "Represents a OneMax Problem.")]
37  [Creatable("Problems")]
38  [StorableClass]
39  public sealed class OneMax : 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    #endregion
67
68    #region Properties
69    public IBinaryVectorCreator SolutionCreator {
70      get { return SolutionCreatorParameter.Value; }
71      set { SolutionCreatorParameter.Value = value; }
72    }
73    ISolutionCreator IProblem.SolutionCreator {
74      get { return SolutionCreatorParameter.Value; }
75    }
76    public IOneMaxEvaluator Evaluator {
77      get { return EvaluatorParameter.Value; }
78      set { EvaluatorParameter.Value = value; }
79    }
80    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
81      get { return EvaluatorParameter.Value; }
82    }
83    IEvaluator IProblem.Evaluator {
84      get { return EvaluatorParameter.Value; }
85    }
86    private List<IBinaryVectorOperator> operators;
87    public IEnumerable<IOperator> Operators {
88      get { return operators.Cast<IOperator>(); }
89    }
90    #endregion
91
92    public OneMax()
93      : base() {
94      RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
95      OneMaxEvaluator evaluator = new OneMaxEvaluator();
96
[3067]97      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
[3065]98      Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
99      Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
100      Parameters.Add(new ValueParameter<IOneMaxEvaluator>("Evaluator", "The operator which should be used to evaluate OneMax solutions.", evaluator));
101
102      creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
103      evaluator.QualityParameter.ActualName = "NumberOfOnes";
104      ParameterizeSolutionCreator();
105      ParameterizeEvaluator();
106
107      Initialize();
108    }
109
110    [StorableConstructor]
111    private OneMax(bool deserializing) : base() { }
112
113    public override IDeepCloneable Clone(Cloner cloner) {
114      OneMax clone = (OneMax)base.Clone(cloner);
115      clone.Initialize();
116      return clone;
117    }
118
119    #region Events
120    public event EventHandler SolutionCreatorChanged;
121    private void OnSolutionCreatorChanged() {
122      if (SolutionCreatorChanged != null)
123        SolutionCreatorChanged(this, EventArgs.Empty);
124    }
125    public event EventHandler EvaluatorChanged;
126    private void OnEvaluatorChanged() {
127      if (EvaluatorChanged != null)
128        EvaluatorChanged(this, EventArgs.Empty);
129    }
130    public event EventHandler OperatorsChanged;
131    private void OnOperatorsChanged() {
132      if (OperatorsChanged != null)
133        OperatorsChanged(this, EventArgs.Empty);
134    }
135
136    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
137      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
138      ParameterizeSolutionCreator();
139      ParameterizeEvaluator();
140      ParameterizeOperators();
141      OnSolutionCreatorChanged();
142    }
143    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
144      ParameterizeEvaluator();
145      ParameterizeOperators();
146    }
147    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
148      ParameterizeEvaluator();
149      OnEvaluatorChanged();
150    }
151    #endregion
152
153    #region Helpers
154    [StorableHook(HookType.AfterDeserialization)]
155    private void Initialize() {
156      InitializeOperators();
157      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
158      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
159      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
160    }
161    private void ParameterizeSolutionCreator() {
162      SolutionCreator.LengthParameter.Value = LengthParameter.Value;
163    }
164    private void ParameterizeEvaluator() {
165      if (Evaluator is OneMaxEvaluator)
166        ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
167    }
168    private void InitializeOperators() {
169      operators = new List<IBinaryVectorOperator>();
170      if (ApplicationManager.Manager != null) {
171        operators.AddRange(ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>());
172        ParameterizeOperators();
173      }
174    }
175    private void ParameterizeOperators() {
176      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
177        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
178        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
179      }
180      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
181        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
182      }
183    }
184    #endregion
185  }
186}
Note: See TracBrowser for help on using the repository browser.