Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.SolutionCaching/3.3/RealVectorEncoding/RealVectorCrossoverPointcut.cs @ 10096

Last change on this file since 10096 was 10096, checked in by ascheibe, 11 years ago

#1886 started working on crossover wrappers that can execute analyzers for crossovers

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Analysis.SolutionCaching.RealVectorEncoding {
35  [Item("RealVectorCrossoverPointcut", "Wraps a crossover and applies after-crossover actions.")]
36  [StorableClass]
37  public class RealVectorCrossoverPointcut : SingleSuccessorOperator, IRealVectorCrossover, IStochasticOperator, IRealVectorPointcut {
38    private const string AdviceParameterName = "Advice";
39    private const string CrossoversParameterName = "Crossovers";
40
41    public ConstrainedValueParameter<IRealVectorCrossover> CrossoversParameter {
42      get { return (ConstrainedValueParameter<IRealVectorCrossover>)Parameters[CrossoversParameterName]; }
43    }
44    public ConstrainedValueParameter<IRealVectorAdvice> AdviceParameter {
45      get { return (ConstrainedValueParameter<IRealVectorAdvice>)Parameters[AdviceParameterName]; }
46    }
47    public ILookupParameter<ItemArray<RealVector>> ParentsParameter {
48      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["Parents"]; }
49    }
50    public ILookupParameter<RealVector> ChildParameter {
51      get { return (ILookupParameter<RealVector>)Parameters["Child"]; }
52    }
53    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
54      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
55    }
56    public ILookupParameter<IRandom> RandomParameter {
57      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
58    }
59
60    [StorableConstructor]
61    protected RealVectorCrossoverPointcut(bool deserializing) : base(deserializing) { }
62    protected RealVectorCrossoverPointcut(RealVectorCrossoverPointcut original, Cloner cloner) : base(original, cloner) { }
63
64    public RealVectorCrossoverPointcut()
65      : base() {
66      var cxs = new ConstrainedValueParameter<IRealVectorCrossover>(CrossoversParameterName, "The crossovers.");
67      //Would be nicer if it comes from operator collection, but MultiRealVector Crossover does the same thing.
68      //Also: MultiRealVectorCrossover cannot be used here as it would generate a cyclic recursion
69      //MultiRealVectorCrossover discovers RealVectorCrossoverPointcut which discovers Multi....
70      foreach (Type t in ApplicationManager.Manager.GetTypes(typeof(IRealVectorCrossover))) {
71        if ((!typeof(RealVectorCrossoverPointcut).IsAssignableFrom(t)) &&
72          (!typeof(MultiOperator<IRealVectorCrossover>).IsAssignableFrom(t))) {
73          cxs.ValidValues.Add((IRealVectorCrossover)Activator.CreateInstance(t));
74        }
75      }
76      Parameters.Add(cxs);
77
78      var advices = new ConstrainedValueParameter<IRealVectorAdvice>(AdviceParameterName,
79        "Advice to introduce after crossover");
80      foreach (var a in ApplicationManager.Manager.GetInstances<IRealVectorAdvice>()) {
81        advices.ValidValues.Add(a);
82      }
83      Parameters.Add(advices);
84
85      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Parents", "The parent vectors which should be crossed."));
86      ParentsParameter.ActualName = "RealVector";
87      Parameters.Add(new LookupParameter<RealVector>("Child", "The child vector resulting from the crossover."));
88      ChildParameter.ActualName = "RealVector";
89      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds of the real vector."));
90      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
91
92      ParameterizeCrossovers();
93    }
94
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new RealVectorCrossoverPointcut(this, cloner);
97    }
98
99    public override IOperation Apply() {
100      IOperation successor = base.Apply();
101      IOperation crossoverOperation = ExecutionContext.CreateChildOperation(CrossoversParameter.Value);
102      IOperation adviceOperation = ExecutionContext.CreateChildOperation(AdviceParameter.Value);
103
104      var opCol = new OperationCollection();
105      opCol.Add(crossoverOperation);
106      opCol.Add(adviceOperation);
107      if (successor != null)
108        opCol.Add(successor);
109
110      return opCol;
111    }
112
113    private void ParameterizeCrossovers() {
114      foreach (IRealVectorCrossover crossover in CrossoversParameter.ValidValues.OfType<IRealVectorCrossover>()) {
115        crossover.ChildParameter.ActualName = ChildParameter.Name;
116        crossover.ParentsParameter.ActualName = ParentsParameter.Name;
117        crossover.BoundsParameter.ActualName = BoundsParameter.Name;
118      }
119      foreach (IStochasticOperator crossover in CrossoversParameter.ValidValues.OfType<IStochasticOperator>()) {
120        crossover.RandomParameter.ActualName = RandomParameter.Name;
121      }
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.