Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5551 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 11.4 KB
RevLine 
[3065]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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.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]
[4419]39  public sealed class OneMaxProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
40    public string Filename { get; set; }
41
[3065]42    public override Image ItemImage {
[5287]43      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
[3065]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    }
[3080]68    public ValueParameter<DoubleValue> BestKnownQualityParameter {
69      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
70    }
71    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
72      get { return BestKnownQualityParameter; }
73    }
[3065]74    #endregion
75
76    #region Properties
[3108]77    public IntValue Length {
78      get { return LengthParameter.Value; }
79      set { LengthParameter.Value = value; }
80    }
[3065]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    }
[3080]98    public DoubleValue BestKnownQuality {
99      get { return BestKnownQualityParameter.Value; }
100    }
[3065]101    public IEnumerable<IOperator> Operators {
102      get { return operators.Cast<IOperator>(); }
103    }
[3667]104    private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
105      get { return operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
[3642]106    }
[3065]107    #endregion
108
[4098]109    [Storable]
110    private List<IOperator> operators;
111
112    [StorableConstructor]
[4118]113    private OneMaxProblem(bool deserializing) : base(deserializing) { }
[4722]114    private OneMaxProblem(OneMaxProblem original, Cloner cloner)
115      : base(original, cloner) {
116      operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
117      AttachEventHandlers();
118    }
[3164]119    public OneMaxProblem()
[3065]120      : base() {
121      RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
122      OneMaxEvaluator evaluator = new OneMaxEvaluator();
123
[3067]124      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
[3065]125      Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
126      Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
127      Parameters.Add(new ValueParameter<IOneMaxEvaluator>("Evaluator", "The operator which should be used to evaluate OneMax solutions.", evaluator));
[3080]128      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
[3065]129
130      creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
131      evaluator.QualityParameter.ActualName = "NumberOfOnes";
132      ParameterizeSolutionCreator();
133      ParameterizeEvaluator();
134
[4098]135      InitializeOperators();
136      AttachEventHandlers();
[3065]137    }
138
139    public override IDeepCloneable Clone(Cloner cloner) {
[4722]140      return new OneMaxProblem(this, cloner);
[3065]141    }
142
143    #region Events
144    public event EventHandler SolutionCreatorChanged;
145    private void OnSolutionCreatorChanged() {
[3739]146      EventHandler handler = SolutionCreatorChanged;
147      if (handler != null) handler(this, EventArgs.Empty);
[3065]148    }
149    public event EventHandler EvaluatorChanged;
150    private void OnEvaluatorChanged() {
[3739]151      EventHandler handler = EvaluatorChanged;
152      if (handler != null) handler(this, EventArgs.Empty);
[3065]153    }
154    public event EventHandler OperatorsChanged;
155    private void OnOperatorsChanged() {
[3739]156      EventHandler handler = OperatorsChanged;
157      if (handler != null) handler(this, EventArgs.Empty);
[3065]158    }
[3739]159    public event EventHandler Reset;
160    private void OnReset() {
161      EventHandler handler = Reset;
162      if (handler != null) handler(this, EventArgs.Empty);
163    }
[3065]164
165    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
[3642]166      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
[3065]167      ParameterizeSolutionCreator();
168      ParameterizeEvaluator();
[3667]169      ParameterizeAnalyzer();
[3065]170      ParameterizeOperators();
171      OnSolutionCreatorChanged();
172    }
[3642]173    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
[3065]174      ParameterizeEvaluator();
[3667]175      ParameterizeAnalyzer();
[3065]176      ParameterizeOperators();
177    }
178    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
179      ParameterizeEvaluator();
[3667]180      ParameterizeAnalyzer();
[3065]181      OnEvaluatorChanged();
182    }
[3108]183    void LengthParameter_ValueChanged(object sender, EventArgs e) {
184      ParameterizeSolutionCreator();
185      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
[3115]186      BestKnownQualityParameter.Value.Value = Length.Value;
[3108]187    }
188    void Length_ValueChanged(object sender, EventArgs e) {
189      BestKnownQualityParameter.Value.Value = Length.Value;
190    }
191    void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
192      BestKnownQualityParameter.Value.Value = Length.Value;
193    }
[3121]194    void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
195      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
196      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
197        op.OneBitflipMoveParameter.ActualName = name;
198      }
199    }
[3065]200    #endregion
201
202    #region Helpers
203    [StorableHook(HookType.AfterDeserialization)]
[4722]204    private void AfterDeserialization() {
[4118]205      // BackwardsCompatibility3.3
206      #region Backwards compatible code (remove with 3.4)
207      if (operators == null) InitializeOperators();
208      #endregion
209      AttachEventHandlers();
210    }
211
[4098]212    private void AttachEventHandlers() {
[3065]213      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
[3642]214      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
[3065]215      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
[3108]216      LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
[3115]217      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
[3108]218      BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
[3065]219    }
[4118]220
[3065]221    private void ParameterizeSolutionCreator() {
[3108]222      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
[3065]223    }
224    private void ParameterizeEvaluator() {
225      if (Evaluator is OneMaxEvaluator)
226        ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
227    }
[3667]228    private void ParameterizeAnalyzer() {
[3789]229      BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
230      BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
[3667]231      BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
232      BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
[3642]233    }
[3065]234    private void InitializeOperators() {
[3642]235      operators = new List<IOperator>();
236      operators.Add(new BestOneMaxSolutionAnalyzer());
[3667]237      ParameterizeAnalyzer();
[5287]238      foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
[3303]239        if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
240          operators.Add(op);
[3126]241        }
[3065]242      }
[3303]243      ParameterizeOperators();
[3121]244      InitializeMoveGenerators();
[3065]245    }
[3121]246    private void InitializeMoveGenerators() {
247      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
248        if (op is IMoveGenerator) {
249          op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
250        }
251      }
252    }
[3065]253    private void ParameterizeOperators() {
254      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
255        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
256        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
257      }
258      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
259        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
260      }
[3121]261      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
262        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
263      }
[3065]264    }
265    #endregion
266  }
267}
Note: See TracBrowser for help on using the repository browser.