Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Selection/3.3/RightChildReducer.cs @ 17035

Last change on this file since 17035 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Selection {
29  /// <summary>
30  /// Reduces the sub-scopes, so that the selected sub-scope contains all selected leaves (1) and (2)
31  /// and the remaining sub-scope contains the sub-scopes of the bottom-most remaining scope (3).
32  /// <pre>                                                     
33  ///                  scope            scope 
34  ///                   / \             /   \     
35  ///                  R   S(1)   =>   R     S                 
36  ///                 / \     \       /|\   /|\       
37  ///               R(3) S(2)  C     ABCDEF  CDEF     
38  ///               /|\  /|\                             
39  ///             ABCDEF DEF                             
40  /// </pre>
41  /// </summary>
42  [Item("RightChildReducer", "Merges all sub-scopes generated by successively selecting sub-scopes of the remaining part.")]
43  [StorableClass]
44  public class RightChildReducer : Reducer, IReducer {
45    [StorableConstructor]
46    protected RightChildReducer(bool deserializing) : base(deserializing) { }
47    protected RightChildReducer(RightChildReducer original, Cloner cloner) : base(original, cloner) { }
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new RightChildReducer(this, cloner);
50    }
51    public RightChildReducer() : base() { }
52    /// <summary>
53    ///  Reduces the sub-scopes, so that the selected sub-scope contains all selected leaves
54    /// and the remaining sub-scope contains the sub-scopes of the bottom-most remaining scope.
55    /// </summary>
56    /// <param name="scope">The current scope to reduce.</param>
57    /// <returns>A list of the new reduced sub scopes.</returns>
58    protected override List<IScope> Reduce(List<IScope> scopes) {
59      IScope rightChild = scopes[scopes.Count - 1];
60      IScope leftChild = scopes[0];
61      while (leftChild.SubScopes.Count > 1 && leftChild.SubScopes[0].SubScopes.Count > 0) {
62        IScope leftRightChild = leftChild.SubScopes[leftChild.SubScopes.Count - 1];
63
64        // merge right children
65        while (leftRightChild.SubScopes.Count > 0) {
66          IScope leftRightChildChild = leftRightChild.SubScopes[0];
67          leftRightChild.SubScopes.RemoveAt(0);
68          rightChild.SubScopes.Add(leftRightChildChild);
69        }
70
71        leftChild = leftChild.SubScopes[0];
72      }
73      List<IScope> subScopes = new List<IScope>();
74      subScopes.Add(leftChild);
75      subScopes.Add(rightChild);
76      return subScopes;
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.