Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.OneMax/3.3/OneMaxProblem.cs @ 11868

Last change on this file since 11868 was 11185, checked in by pfleck, 10 years ago

#2208 merged trunk and updated version info

File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.OneMax {
35  [Item("OneMax Problem", "Represents a OneMax Problem.")]
36  [Creatable("Problems")]
37  [StorableClass]
38  public sealed class OneMaxProblem : SingleObjectiveHeuristicOptimizationProblem<IOneMaxEvaluator, IBinaryVectorCreator>, IStorableContent {
39    public string Filename { get; set; }
40
41    #region Parameter Properties
42    public ValueParameter<IntValue> LengthParameter {
43      get { return (ValueParameter<IntValue>)Parameters["Length"]; }
44    }
45    #endregion
46
47    #region Properties
48    public IntValue Length {
49      get { return LengthParameter.Value; }
50      set { LengthParameter.Value = value; }
51    }
52    private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
53      get { return Operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
54    }
55    #endregion
56
57    // BackwardsCompatibility3.3
58    #region Backwards compatible code, remove with 3.4
59    [Obsolete]
60    [Storable(Name = "operators")]
61    private IEnumerable<IOperator> oldOperators {
62      get { return null; }
63      set {
64        if (value != null && value.Any())
65          Operators.AddRange(value);
66      }
67    }
68    #endregion
69
70    [StorableConstructor]
71    private OneMaxProblem(bool deserializing) : base(deserializing) { }
72    private OneMaxProblem(OneMaxProblem original, Cloner cloner)
73      : base(original, cloner) {
74      RegisterEventHandlers();
75    }
76    public OneMaxProblem()
77      : base(new OneMaxEvaluator(), new RandomBinaryVectorCreator()) {
78      Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
79
80      Maximization.Value = true;
81      MaximizationParameter.Hidden = true;
82      BestKnownQuality = new DoubleValue(5);
83
84      SolutionCreator.BinaryVectorParameter.ActualName = "OneMaxSolution";
85      Evaluator.QualityParameter.ActualName = "NumberOfOnes";
86      ParameterizeSolutionCreator();
87      ParameterizeEvaluator();
88
89      InitializeOperators();
90      RegisterEventHandlers();
91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new OneMaxProblem(this, cloner);
95    }
96
97    #region Events
98    protected override void OnSolutionCreatorChanged() {
99      base.OnSolutionCreatorChanged();
100      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
101      ParameterizeSolutionCreator();
102      ParameterizeEvaluator();
103      ParameterizeAnalyzer();
104      ParameterizeOperators();
105    }
106    protected override void OnEvaluatorChanged() {
107      base.OnEvaluatorChanged();
108      ParameterizeEvaluator();
109      ParameterizeAnalyzer();
110    }
111    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
112      ParameterizeEvaluator();
113      ParameterizeAnalyzer();
114      ParameterizeOperators();
115    }
116    private void LengthParameter_ValueChanged(object sender, EventArgs e) {
117      ParameterizeSolutionCreator();
118      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
119      BestKnownQualityParameter.Value.Value = Length.Value;
120    }
121    private void Length_ValueChanged(object sender, EventArgs e) {
122      BestKnownQualityParameter.Value.Value = Length.Value;
123    }
124    private void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
125      BestKnownQualityParameter.Value.Value = Length.Value;
126    }
127    private void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
128      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
129      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
130        op.OneBitflipMoveParameter.ActualName = name;
131      }
132    }
133    #endregion
134
135    #region Helpers
136    [StorableHook(HookType.AfterDeserialization)]
137    private void AfterDeserialization() {
138      // BackwardsCompatibility3.3
139      #region Backwards compatible code (remove with 3.4)
140      if (Operators.Count == 0) InitializeOperators();
141      #endregion
142      RegisterEventHandlers();
143    }
144
145    private void RegisterEventHandlers() {
146      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
147      LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
148      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
149      BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
150    }
151
152    private void ParameterizeSolutionCreator() {
153      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
154    }
155    private void ParameterizeEvaluator() {
156      if (Evaluator is OneMaxEvaluator)
157        ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
158    }
159    private void ParameterizeAnalyzer() {
160      BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
161      BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
162      BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
163      BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
164    }
165    private void InitializeOperators() {
166      Operators.Add(new BestOneMaxSolutionAnalyzer());
167      ParameterizeAnalyzer();
168      foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
169        if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
170          Operators.Add(op);
171        }
172      }
173      ParameterizeOperators();
174      InitializeMoveGenerators();
175    }
176    private void InitializeMoveGenerators() {
177      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
178        if (op is IMoveGenerator) {
179          op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
180        }
181      }
182    }
183    private void ParameterizeOperators() {
184      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
185        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
186        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
187      }
188      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
189        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
190      }
191      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
192        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
193      }
194      foreach (var op in Operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>())
195        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
196    }
197    #endregion
198  }
199}
Note: See TracBrowser for help on using the repository browser.