1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Optimization.Operators {
|
---|
30 | /// <summary>
|
---|
31 | /// An operator which is used to prepare crossover.
|
---|
32 | /// </summary>
|
---|
33 | /// <remarks>
|
---|
34 | /// The sub-scopes of the current scope the operator is applied on represent the parents. The operator creates
|
---|
35 | /// new and empty scopes for each child, adds the scopes that represent the child's parents as sub-scopes to
|
---|
36 | /// the child and adds the child as sub-scope to the current scope.
|
---|
37 | /// </remarks>
|
---|
38 | [Item("ChildrenCreator", "An operator which is used to prepare crossover. The sub-scopes of the current scope the operator is applied on represent the parents. The operator creates new and empty scopes for each child, adds the scopes that represent the child's parents as sub-scopes to the child and adds the child as sub-scope to the current scope.")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class ChildrenCreator : SingleSuccessorOperator {
|
---|
41 | private ScopeParameter CurrentScopeParameter {
|
---|
42 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
43 | }
|
---|
44 | public ValueLookupParameter<IntValue> ParentsPerChildParameter {
|
---|
45 | get { return (ValueLookupParameter<IntValue>)Parameters["ParentsPerChild"]; }
|
---|
46 | }
|
---|
47 | public IScope CurrentScope {
|
---|
48 | get { return CurrentScopeParameter.ActualValue; }
|
---|
49 | }
|
---|
50 | public IntValue ParentsPerChild {
|
---|
51 | get { return ParentsPerChildParameter.Value; }
|
---|
52 | set { ParentsPerChildParameter.Value = value; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public ChildrenCreator()
|
---|
56 | : base() {
|
---|
57 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents."));
|
---|
58 | Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", "The number of parents that should be crossed per child. Note that some of the typical crossover operators require exactly two parents.", new IntValue(2)));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IOperation Apply() {
|
---|
62 | int parentsPerChild = ParentsPerChildParameter.ActualValue.Value;
|
---|
63 | int parents = CurrentScope.SubScopes.Count;
|
---|
64 | if (parents % parentsPerChild > 0) throw new InvalidOperationException("Number of parents is not an integral multiple of ParentsPerChild.");
|
---|
65 | int children = parents / parentsPerChild;
|
---|
66 |
|
---|
67 | for (int i = 0; i < children; i++) {
|
---|
68 | IScope child = new Scope(i.ToString());
|
---|
69 | for (int j = 0; j < parentsPerChild; j++) {
|
---|
70 | IScope parent = CurrentScope.SubScopes[0];
|
---|
71 | CurrentScope.SubScopes.RemoveAt(0);
|
---|
72 | child.SubScopes.Add(parent);
|
---|
73 | }
|
---|
74 | CurrentScope.SubScopes.Add(child);
|
---|
75 | }
|
---|
76 | return base.Apply();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|