Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.Optimization.Operators/3.3/ChildrenCreator.cs @ 14015

Last change on this file since 14015 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.9 KB
Line 
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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace 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]
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}
Note: See TracBrowser for help on using the repository browser.