Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Operators/3.3/ChildrenCreator.cs @ 3048

Last change on this file since 3048 was 3048, checked in by swagner, 14 years ago

Renamed classes of HeuristicLab.Data (#909)

File size: 3.6 KB
Line 
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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace 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  [Creatable("Test")]
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    public ChildrenCreator()
57      : base() {
58      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents."));
59      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)));
60    }
61
62    public override IOperation Apply() {
63      int parentsPerChild = ParentsPerChildParameter.ActualValue.Value;
64      int parents = CurrentScope.SubScopes.Count;
65      if (parents % parentsPerChild > 0) throw new InvalidOperationException("Number of parents is not an integral multiple of ParentsPerChild.");
66      int children = parents / parentsPerChild;
67
68      for (int i = 0; i < children; i++) {
69        IScope child = new Scope(i.ToString());
70        for (int j = 0; j < parentsPerChild; j++) {
71          IScope parent = CurrentScope.SubScopes[0];
72          CurrentScope.SubScopes.RemoveAt(0);
73          child.SubScopes.Add(parent);
74        }
75        CurrentScope.SubScopes.Add(child);
76      }
77      return base.Apply();
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.