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