1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HEAL.Attic;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
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.")]
|
---|
36 | [StorableType("E1C6331C-E650-4160-8C54-407DE0253277")]
|
---|
37 | public abstract class RealVectorManipulator : InstrumentedOperator, IRealVectorManipulator, IStochasticOperator {
|
---|
38 | public override bool CanChangeName {
|
---|
39 | get { return false; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ILookupParameter<IRandom> RandomParameter {
|
---|
43 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
44 | }
|
---|
45 | public ILookupParameter<RealVector> RealVectorParameter {
|
---|
46 | get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
47 | }
|
---|
48 | public IValueLookupParameter<DoubleMatrix> BoundsParameter {
|
---|
49 | get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
50 | }
|
---|
51 | protected OptionalValueParameter<IRealVectorBoundsChecker> BoundsCheckerParameter {
|
---|
52 | get { return (OptionalValueParameter<IRealVectorBoundsChecker>)Parameters["BoundsChecker"]; }
|
---|
53 | }
|
---|
54 | public IRealVectorBoundsChecker BoundsChecker {
|
---|
55 | get { return BoundsCheckerParameter.Value; }
|
---|
56 | set { BoundsCheckerParameter.Value = value; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | [StorableConstructor]
|
---|
60 | protected RealVectorManipulator(StorableConstructorFlag _) : base(_) { }
|
---|
61 | protected RealVectorManipulator(RealVectorManipulator original, Cloner cloner)
|
---|
62 | : base(original, cloner) {
|
---|
63 | RegisterEventHandlers();
|
---|
64 | }
|
---|
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."));
|
---|
68 | Parameters.Add(new LookupParameter<RealVector>("RealVector", "The vector which should be manipulated."));
|
---|
69 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds of the real vector."));
|
---|
70 | Parameters.Add(new OptionalValueParameter<IRealVectorBoundsChecker>("BoundsChecker", "The bounds checker that ensures that the values stay within the bounds.", new BoundsChecker()));
|
---|
71 |
|
---|
72 | RegisterEventHandlers();
|
---|
73 | ParameterizeBoundsChecker();
|
---|
74 | }
|
---|
75 |
|
---|
76 | [StorableHook(HookType.AfterDeserialization)]
|
---|
77 | private void AfterDeserialization() {
|
---|
78 | // BackwardsCompatibility3.3
|
---|
79 | #region Backwards compatible code (remove with 3.4)
|
---|
80 | if (!Parameters.ContainsKey("BoundsChecker")) {
|
---|
81 | Parameters.Add(new OptionalValueParameter<IRealVectorBoundsChecker>("BoundsChecker", "The bounds checker that ensures that the values stay within the bounds.", new BoundsChecker()));
|
---|
82 | ParameterizeBoundsChecker();
|
---|
83 | }
|
---|
84 | #endregion
|
---|
85 | RegisterEventHandlers();
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected virtual void RegisterEventHandlers() {
|
---|
89 | BoundsCheckerParameter.ValueChanged += new System.EventHandler(BoundsCheckerParameter_ValueChanged);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public sealed override IOperation InstrumentedApply() {
|
---|
93 | RealVector vector = RealVectorParameter.ActualValue;
|
---|
94 | Manipulate(RandomParameter.ActualValue, vector);
|
---|
95 |
|
---|
96 | IOperation successor = base.InstrumentedApply();
|
---|
97 | if (BoundsChecker != null) {
|
---|
98 | IOperation checkerOperation = ExecutionContext.CreateChildOperation(BoundsChecker);
|
---|
99 | if (successor == null) return checkerOperation;
|
---|
100 | else return new OperationCollection(checkerOperation, successor);
|
---|
101 | } else return successor;
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected abstract void Manipulate(IRandom random, RealVector realVector);
|
---|
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 | }
|
---|
116 | }
|
---|
117 | }
|
---|