Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorManipulator.cs @ 5947

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

Updated year of copyrights (#1406)

File size: 5.0 KB
RevLine 
[2900]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2900]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
[5434]22using System;
[4722]23using HeuristicLab.Common;
[2900]24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
[3053]31namespace HeuristicLab.Encodings.RealVectorEncoding {
[2900]32  /// <summary>
33  /// A base class for operators that manipulate real-valued vectors.
34  /// </summary>
35  [Item("RealVectorManipulator", "A base class for operators that manipulate real-valued vectors.")]
[3017]36  [StorableClass]
[2900]37  public abstract class RealVectorManipulator : SingleSuccessorOperator, IRealVectorManipulator, IStochasticOperator {
[3034]38    public override bool CanChangeName {
39      get { return false; }
40    }
41
[2900]42    public ILookupParameter<IRandom> RandomParameter {
43      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
44    }
[3060]45    public ILookupParameter<RealVector> RealVectorParameter {
46      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
[2900]47    }
[3182]48    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
49      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
50    }
[5434]51    protected OptionalValueParameter<IRealVectorBoundsChecker> BoundsCheckerParameter {
[5381]52      get { return (OptionalValueParameter<IRealVectorBoundsChecker>)Parameters["BoundsChecker"]; }
53    }
[5434]54    public IRealVectorBoundsChecker BoundsChecker {
55      get { return BoundsCheckerParameter.Value; }
56      set { BoundsCheckerParameter.Value = value; }
57    }
[2900]58
[4722]59    [StorableConstructor]
60    protected RealVectorManipulator(bool deserializing) : base(deserializing) { }
[5434]61    protected RealVectorManipulator(RealVectorManipulator original, Cloner cloner)
62      : base(original, cloner) {
63      RegisterEventHandlers();
64    }
[2900]65    protected RealVectorManipulator()
66      : base() {
67      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
[3060]68      Parameters.Add(new LookupParameter<RealVector>("RealVector", "The vector which should be manipulated."));
[3182]69      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds of the real vector."));
[5381]70      Parameters.Add(new OptionalValueParameter<IRealVectorBoundsChecker>("BoundsChecker", "The bounds checker that ensures that the values stay within the bounds.", new BoundsChecker()));
[5434]71
72      RegisterEventHandlers();
73      ParameterizeBoundsChecker();
[2900]74    }
75
[5381]76    [StorableHook(HookType.AfterDeserialization)]
77    private void AfterDeserialization() {
[5434]78      // BackwardsCompatibility3.3
79      #region Backwards compatible code (remove with 3.4)
80      if (!Parameters.ContainsKey("BoundsChecker")) {
[5381]81        Parameters.Add(new OptionalValueParameter<IRealVectorBoundsChecker>("BoundsChecker", "The bounds checker that ensures that the values stay within the bounds.", new BoundsChecker()));
[5434]82        ParameterizeBoundsChecker();
83      }
84      #endregion
85      RegisterEventHandlers();
[5381]86    }
87
[5434]88    protected virtual void RegisterEventHandlers() {
89      BoundsCheckerParameter.ValueChanged += new System.EventHandler(BoundsCheckerParameter_ValueChanged);
90    }
91
[2900]92    public sealed override IOperation Apply() {
[3182]93      RealVector vector = RealVectorParameter.ActualValue;
94      Manipulate(RandomParameter.ActualValue, vector);
[5381]95
96      IOperation successor = base.Apply();
[5434]97      if (BoundsChecker != null) {
98        IOperation checkerOperation = ExecutionContext.CreateChildOperation(BoundsChecker);
[5381]99        if (successor == null) return checkerOperation;
100        else return new OperationCollection(checkerOperation, successor);
101      } else return successor;
[2900]102    }
103
[3060]104    protected abstract void Manipulate(IRandom random, RealVector realVector);
[5434]105
106    protected virtual void BoundsCheckerParameter_ValueChanged(object sender, EventArgs e) {
107      ParameterizeBoundsChecker();
108    }
109
110    protected virtual void ParameterizeBoundsChecker() {
111      if (BoundsChecker != null) {
112        BoundsChecker.BoundsParameter.ActualName = BoundsParameter.Name;
113        BoundsChecker.RealVectorParameter.ActualName = RealVectorParameter.Name;
114      }
115    }
[2900]116  }
117}
Note: See TracBrowser for help on using the repository browser.