Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4695 was 4419, checked in by swagner, 14 years ago

Enabled saving only for some specific items (#1193)

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