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 |
|
---|
16 | namespace HeuristicLab.Problems.MetaOptimization.Encodings.Crossovers {
|
---|
17 | /// <summary>
|
---|
18 | ///
|
---|
19 | /// </summary>
|
---|
20 | [Item("ParameterSetCrossover", "TODO")]
|
---|
21 | [StorableClass]
|
---|
22 | public class ParameterSetCrossover : SingleSuccessorOperator, IParameterSetOperator, ICrossover {
|
---|
23 | public override bool CanChangeName {
|
---|
24 | get { return false; }
|
---|
25 | }
|
---|
26 |
|
---|
27 | public ILookupParameter<IRandom> RandomParameter {
|
---|
28 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
29 | }
|
---|
30 | public ILookupParameter<ItemArray<IParameterSet>> ParentsParameter {
|
---|
31 | get { return (ScopeTreeLookupParameter<IParameterSet>)Parameters["Parents"]; }
|
---|
32 | }
|
---|
33 | public ILookupParameter<IParameterSet> ChildParameter {
|
---|
34 | get { return (ILookupParameter<IParameterSet>)Parameters["Child"]; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public IValueParameter<IBinaryVectorCrossover> BinaryVectorCrossoverParameter {
|
---|
38 | get { return (IValueParameter<IBinaryVectorCrossover>)Parameters["BinaryVectorCrossover"]; }
|
---|
39 | }
|
---|
40 | public IValueParameter<IIntegerVectorCrossover> IntegerVectorCrossoverParameter {
|
---|
41 | get { return (IValueParameter<IIntegerVectorCrossover>)Parameters["IntegerVectorCrossover"]; }
|
---|
42 | }
|
---|
43 | public IValueParameter<IRealVectorCrossover> RealVectorCrossoverParameter {
|
---|
44 | get { return (IValueParameter<IRealVectorCrossover>)Parameters["RealVectorCrossover"]; }
|
---|
45 | }
|
---|
46 | public ParameterSetCrossover()
|
---|
47 | : base() {
|
---|
48 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
|
---|
49 | Parameters.Add(new ScopeTreeLookupParameter<IParameterSet>("Parents", "The parent vectors which should be crossed."));
|
---|
50 | ParentsParameter.ActualName = "ParameterSet";
|
---|
51 | Parameters.Add(new LookupParameter<IParameterSet>("Child", "The child vector resulting from the crossover."));
|
---|
52 | ChildParameter.ActualName = "ParameterSet";
|
---|
53 | Parameters.Add(new ValueParameter<IRealVectorCrossover>("BinaryVectorCrossover", "Crossover operator used for crossing binary parameters"));
|
---|
54 | Parameters.Add(new ValueParameter<IRealVectorCrossover>("IntegerVectorCrossover", "Crossover operator used for crossing integer parameters"));
|
---|
55 | Parameters.Add(new ValueParameter<IRealVectorCrossover>("RealVectorCrossover", "Crossover operator used for crossing real parameters"));
|
---|
56 | }
|
---|
57 | public override IOperation Apply() {
|
---|
58 | IEnumerable<IndexedItem<IParameterConfiguration>> integerParameters1 = GetIntegerParameters(ParentsParameter.ActualValue[0]);
|
---|
59 | IEnumerable<IndexedItem<IParameterConfiguration>> integerParameters2 = GetIntegerParameters(ParentsParameter.ActualValue[1]);
|
---|
60 | IParameterSet child = (IParameterSet)ParentsParameter.ActualValue[0].Clone();
|
---|
61 |
|
---|
62 | // TODO: use specific crossover operators to do the crossing
|
---|
63 | // for now: just swap the int-values
|
---|
64 | foreach (var par in integerParameters1) {
|
---|
65 | ParentsParameter.ActualValue[1].Parameters.ElementAt(par.Index).Parameter.ActualValue = par.Value.Parameter.ActualValue;
|
---|
66 | }
|
---|
67 | foreach (var par in integerParameters2) {
|
---|
68 | ParentsParameter.ActualValue[0].Parameters.ElementAt(par.Index).Parameter.ActualValue = par.Value.Parameter.ActualValue;
|
---|
69 | }
|
---|
70 |
|
---|
71 | ChildParameter.ActualValue = child;
|
---|
72 | return base.Apply();
|
---|
73 | }
|
---|
74 |
|
---|
75 | private IEnumerable<IndexedItem<IParameterConfiguration>> GetIntegerParameters(IParameterSet parameterSet) {
|
---|
76 | List<IndexedItem<IParameterConfiguration>> integerParameters = new List<IndexedItem<IParameterConfiguration>>();
|
---|
77 | int i = 0;
|
---|
78 | foreach (var item in parameterSet.Parameters) {
|
---|
79 | if (item.Parameter.DataType == typeof(IntValue)) {
|
---|
80 | integerParameters.Add(new IndexedItem<IParameterConfiguration>(i, (IParameterConfiguration)item.Clone()));
|
---|
81 | }
|
---|
82 | i++;
|
---|
83 | }
|
---|
84 | return integerParameters;
|
---|
85 | }
|
---|
86 |
|
---|
87 | }
|
---|
88 | }
|
---|