Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.OneMax/3.3/OneMaxProblem.cs @ 12417

Last change on this file since 12417 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 8.2 KB
RevLine 
[3065]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3065]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 {
[3528]35  [Item("OneMax Problem", "Represents a OneMax Problem.")]
[3065]36  [StorableClass]
[12004]37  [NonDiscoverableType]
[6938]38  public sealed class OneMaxProblem : SingleObjectiveHeuristicOptimizationProblem<IOneMaxEvaluator, IBinaryVectorCreator>, IStorableContent {
[4419]39    public string Filename { get; set; }
40
[3065]41    #region Parameter Properties
42    public ValueParameter<IntValue> LengthParameter {
43      get { return (ValueParameter<IntValue>)Parameters["Length"]; }
44    }
45    #endregion
46
47    #region Properties
[3108]48    public IntValue Length {
49      get { return LengthParameter.Value; }
50      set { LengthParameter.Value = value; }
51    }
[3667]52    private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
[6938]53      get { return Operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
[3642]54    }
[3065]55    #endregion
56
[6938]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
[4098]69
70    [StorableConstructor]
[4118]71    private OneMaxProblem(bool deserializing) : base(deserializing) { }
[4722]72    private OneMaxProblem(OneMaxProblem original, Cloner cloner)
73      : base(original, cloner) {
[7351]74      RegisterEventHandlers();
[4722]75    }
[3164]76    public OneMaxProblem()
[6938]77      : base(new OneMaxEvaluator(), new RandomBinaryVectorCreator()) {
[3065]78      Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
[6939]79
80      Maximization.Value = true;
81      MaximizationParameter.Hidden = true;
[7519]82      BestKnownQuality = new DoubleValue(5);
[3065]83
[6938]84      SolutionCreator.BinaryVectorParameter.ActualName = "OneMaxSolution";
85      Evaluator.QualityParameter.ActualName = "NumberOfOnes";
[3065]86      ParameterizeSolutionCreator();
87      ParameterizeEvaluator();
88
[4098]89      InitializeOperators();
[7351]90      RegisterEventHandlers();
[3065]91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
[4722]94      return new OneMaxProblem(this, cloner);
[3065]95    }
96
97    #region Events
[6938]98    protected override void OnSolutionCreatorChanged() {
99      base.OnSolutionCreatorChanged();
[3642]100      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
[3065]101      ParameterizeSolutionCreator();
102      ParameterizeEvaluator();
[3667]103      ParameterizeAnalyzer();
[3065]104      ParameterizeOperators();
105    }
[6938]106    protected override void OnEvaluatorChanged() {
107      base.OnEvaluatorChanged();
[3065]108      ParameterizeEvaluator();
[3667]109      ParameterizeAnalyzer();
[3065]110    }
[6938]111    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
[3065]112      ParameterizeEvaluator();
[3667]113      ParameterizeAnalyzer();
[6938]114      ParameterizeOperators();
[3065]115    }
[6938]116    private void LengthParameter_ValueChanged(object sender, EventArgs e) {
[3108]117      ParameterizeSolutionCreator();
118      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
[3115]119      BestKnownQualityParameter.Value.Value = Length.Value;
[3108]120    }
[6938]121    private void Length_ValueChanged(object sender, EventArgs e) {
[3108]122      BestKnownQualityParameter.Value.Value = Length.Value;
123    }
[6938]124    private void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
[3108]125      BestKnownQualityParameter.Value.Value = Length.Value;
126    }
[6938]127    private void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
[3121]128      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
129      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
130        op.OneBitflipMoveParameter.ActualName = name;
131      }
132    }
[3065]133    #endregion
134
135    #region Helpers
136    [StorableHook(HookType.AfterDeserialization)]
[4722]137    private void AfterDeserialization() {
[4118]138      // BackwardsCompatibility3.3
139      #region Backwards compatible code (remove with 3.4)
[6938]140      if (Operators.Count == 0) InitializeOperators();
[4118]141      #endregion
[7351]142      RegisterEventHandlers();
[4118]143    }
144
[7351]145    private void RegisterEventHandlers() {
[3642]146      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
[3108]147      LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
[3115]148      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
[3108]149      BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
[3065]150    }
[4118]151
[3065]152    private void ParameterizeSolutionCreator() {
[3108]153      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
[3065]154    }
155    private void ParameterizeEvaluator() {
156      if (Evaluator is OneMaxEvaluator)
157        ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
158    }
[3667]159    private void ParameterizeAnalyzer() {
[3789]160      BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
161      BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
[3667]162      BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
163      BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
[3642]164    }
[3065]165    private void InitializeOperators() {
[6938]166      Operators.Add(new BestOneMaxSolutionAnalyzer());
[3667]167      ParameterizeAnalyzer();
[5287]168      foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
[3303]169        if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
[6938]170          Operators.Add(op);
[3126]171        }
[3065]172      }
[3303]173      ParameterizeOperators();
[3121]174      InitializeMoveGenerators();
[3065]175    }
[3121]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    }
[3065]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      }
[3121]191      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
192        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
193      }
[6042]194      foreach (var op in Operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>())
195        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
[3065]196    }
197    #endregion
198  }
199}
Note: See TracBrowser for help on using the repository browser.