Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11068 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 3.9 KB
RevLine 
[2794]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2794]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;
[4722]23using HeuristicLab.Common;
[2794]24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
[3021]30namespace HeuristicLab.Optimization.Operators {
[2794]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.")]
[3017]40  [StorableClass]
[2794]41  public sealed class ChildrenCreator : SingleSuccessorOperator {
42    private ScopeParameter CurrentScopeParameter {
43      get { return (ScopeParameter)Parameters["CurrentScope"]; }
44    }
[3048]45    public ValueLookupParameter<IntValue> ParentsPerChildParameter {
46      get { return (ValueLookupParameter<IntValue>)Parameters["ParentsPerChild"]; }
[2794]47    }
48    public IScope CurrentScope {
49      get { return CurrentScopeParameter.ActualValue; }
50    }
[3048]51    public IntValue ParentsPerChild {
[2794]52      get { return ParentsPerChildParameter.Value; }
53      set { ParentsPerChildParameter.Value = value; }
54    }
55
[4722]56    [StorableConstructor]
57    private ChildrenCreator(bool deserializing) : base(deserializing) { }
58    private ChildrenCreator(ChildrenCreator original, Cloner cloner) : base(original, cloner) { }
[2794]59    public ChildrenCreator()
60      : base() {
61      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents."));
[3048]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)));
[2794]63    }
64
[4722]65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new ChildrenCreator(this, cloner);
67    }
68
[2834]69    public override IOperation Apply() {
[2794]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());
[2818]77        for (int j = 0; j < parentsPerChild; j++) {
[2794]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.