Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorCrossover.cs @ 16720

Last change on this file since 16720 was 16720, checked in by ddorfmei, 5 years ago

#2931: Merged revision(s) 16235-16719 from trunk

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HEAL.Attic;
30
31namespace HeuristicLab.Encodings.RealVectorEncoding {
32  /// <summary>
33  /// A base class for operators that perform a crossover of real-valued vectors.
34  /// </summary>
35  [Item("RealVectorCrossover", "A base class for operators that perform a crossover of real-valued vectors.")]
36  [StorableType("4E51D1FF-056B-4A4D-8D90-3EB640EAF44A")]
37  public abstract class RealVectorCrossover : InstrumentedOperator, IRealVectorCrossover, 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<ItemArray<RealVector>> ParentsParameter {
46      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["Parents"]; }
47    }
48    public ILookupParameter<RealVector> ChildParameter {
49      get { return (ILookupParameter<RealVector>)Parameters["Child"]; }
50    }
51    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
52      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
53    }
54    protected OptionalValueParameter<IRealVectorBoundsChecker> BoundsCheckerParameter {
55      get { return (OptionalValueParameter<IRealVectorBoundsChecker>)Parameters["BoundsChecker"]; }
56    }
57    public IRealVectorBoundsChecker BoundsChecker {
58      get { return BoundsCheckerParameter.Value; }
59      set { BoundsCheckerParameter.Value = value; }
60    }
61
62    [StorableConstructor]
63    protected RealVectorCrossover(StorableConstructorFlag _) : base(_) { }
64    protected RealVectorCrossover(RealVectorCrossover original, Cloner cloner)
65      : base(original, cloner) {
66      RegisterEventHandlers();
67    }
68    protected RealVectorCrossover()
69      : base() {
70      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
71      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Parents", "The parent vectors which should be crossed."));
72      ParentsParameter.ActualName = "RealVector";
73      Parameters.Add(new LookupParameter<RealVector>("Child", "The child vector resulting from the crossover."));
74      ChildParameter.ActualName = "RealVector";
75      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds of the real vector."));
76      Parameters.Add(new OptionalValueParameter<IRealVectorBoundsChecker>("BoundsChecker", "The bounds checker that ensures that the values stay within the bounds.", new BoundsChecker()));
77
78      RegisterEventHandlers();
79      ParameterizeBoundsChecker();
80    }
81
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() {
84      // BackwardsCompatibility3.3
85      #region Backwards compatible code (remove with 3.4)
86      if (!Parameters.ContainsKey("BoundsChecker")) {
87        Parameters.Add(new OptionalValueParameter<IRealVectorBoundsChecker>("BoundsChecker", "The bounds checker that ensures that the values stay within the bounds.", new BoundsChecker()));
88        ParameterizeBoundsChecker();
89      }
90      #endregion
91      RegisterEventHandlers();
92    }
93
94    protected virtual void RegisterEventHandlers() {
95      BoundsCheckerParameter.ValueChanged += new System.EventHandler(BoundsCheckerParameter_ValueChanged);
96    }
97
98    public sealed override IOperation InstrumentedApply() {
99      RealVector result = Cross(RandomParameter.ActualValue, ParentsParameter.ActualValue);
100      ChildParameter.ActualValue = result;
101
102      IOperation successor = base.InstrumentedApply();
103      if (BoundsChecker != null) {
104        IOperation checkerOperation = ExecutionContext.CreateChildOperation(BoundsChecker);
105        if (successor == null) return checkerOperation;
106        else return new OperationCollection(checkerOperation, successor);
107      } else return successor;
108    }
109
110    protected abstract RealVector Cross(IRandom random, ItemArray<RealVector> parents);
111
112    protected virtual void BoundsCheckerParameter_ValueChanged(object sender, EventArgs e) {
113      ParameterizeBoundsChecker();
114    }
115
116    protected virtual void ParameterizeBoundsChecker() {
117      if (BoundsChecker != null) {
118        BoundsChecker.BoundsParameter.ActualName = BoundsParameter.Name;
119        BoundsChecker.RealVectorParameter.ActualName = ChildParameter.Name;
120      }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.