Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/Crossovers/ParameterVectorCrossover.cs @ 4830

Last change on this file since 4830 was 4830, checked in by cneumuel, 14 years ago

#1215 worked on metaoptimization

File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Operators;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Core;
8using HeuristicLab.Parameters;
9using HeuristicLab.Encodings.RealVectorEncoding;
10using HeuristicLab.Encodings.BinaryVectorEncoding;
11using HeuristicLab.Encodings.IntegerVectorEncoding;
12using HeuristicLab.Collections;
13using HeuristicLab.Data;
14using HeuristicLab.Optimization;
15using HeuristicLab.Common;
16
17namespace HeuristicLab.Problems.MetaOptimization.Encodings.Crossovers {
18  /// <summary>
19  ///
20  /// </summary>
21  [Item("ParameterVectorCrossover", "TODO")]
22  [StorableClass]
23  public class ParameterVectorCrossover : SingleSuccessorOperator, IParameterConfigurationOperator, ICrossover {
24    public override bool CanChangeName {
25      get { return false; }
26    }
27
28    public ILookupParameter<IRandom> RandomParameter {
29      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
30    }
31    public ILookupParameter<ItemArray<IParameterConfiguration>> ParentsParameter {
32      get { return (ScopeTreeLookupParameter<IParameterConfiguration>)Parameters["Parents"]; }
33    }
34    public ILookupParameter<IParameterConfiguration> ChildParameter {
35      get { return (ILookupParameter<IParameterConfiguration>)Parameters["Child"]; }
36    }
37
38    private IIntegerVectorCrossover integerVectorCrossover;
39    public IIntegerVectorCrossover IntegerVectorCrossover {
40      get { return integerVectorCrossover; }
41      set { integerVectorCrossover = value; }
42    }
43    private IRealVectorCrossover realVectorCrossover;
44    public IRealVectorCrossover RealVectorCrossover {
45      get { return realVectorCrossover; }
46      set { realVectorCrossover = value; }
47    }
48
49    [StorableConstructor]
50    protected ParameterVectorCrossover(bool deserializing) : base(deserializing) { }
51    protected ParameterVectorCrossover(ParameterVectorCrossover original, Cloner cloner) : base(original, cloner) { }
52    public ParameterVectorCrossover()
53      : base() {
54      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
55      Parameters.Add(new ScopeTreeLookupParameter<IParameterConfiguration>("Parents", "The parent vectors which should be crossed."));
56      ParentsParameter.ActualName = "ParameterConfiguration";
57      Parameters.Add(new LookupParameter<IParameterConfiguration>("Child", "The child vector resulting from the crossover."));
58      ChildParameter.ActualName = "ParameterConfiguration";
59    }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new ParameterVectorCrossover(this, cloner);
62    }
63
64    public override IOperation Apply() {
65
66      IParameterConfiguration child = (IParameterConfiguration)ParentsParameter.ActualValue[0].Clone();
67      // todo
68      //for (int i = 0; i < child.Parameters.Count; i++) {
69      //  if (child.Parameters[i].Parameter.DataType == typeof(IntValue)) {
70      //    child.Parameters[i].Parameter.ActualValue = CrossInteger(ParentsParameter.ActualValue[0].Parameters[i], ParentsParameter.ActualValue[1].Parameters[i]);
71      //  }
72      //}
73
74      this.ChildParameter.ActualValue = child;
75      return base.Apply();
76    }
77
78    private IntValue CrossInteger(IParameterConfiguration parameter1, IParameterConfiguration parameter2) {
79      IntegerVector integerChild = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(
80        RandomParameter.ActualValue,
81        new IntegerVector(new IntArray(new int[] { ((IntValue)parameter1.Parameter.ActualValue).Value })),
82        new IntegerVector(new IntArray(new int[] { ((IntValue)parameter2.Parameter.ActualValue).Value })));
83      return new IntValue(integerChild[0]);
84    }
85
86  }
87}
Note: See TracBrowser for help on using the repository browser.