Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/Crossovers/ParameterSetCrossover.cs @ 4539

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

implemented basic crossover operator for ParameterSets. MetaOptimization is now functional on a basic level (Configuration and Crossing only works for IntValue Parameters) (#1215)

File size: 4.3 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;
15
16namespace 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}
Note: See TracBrowser for help on using the repository browser.