1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
10 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
11 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
12 | using HeuristicLab.Collections;
|
---|
13 | using HeuristicLab.Data;
|
---|
14 | using HeuristicLab.Optimization;
|
---|
15 | using HeuristicLab.Common;
|
---|
16 |
|
---|
17 | namespace 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 | }
|
---|