Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Operators/3.3/SASEGASAReunificator.cs @ 3451

Last change on this file since 3451 was 3451, checked in by abeham, 14 years ago

Added commented version of SASEGASAReunificator #839

File size: 3.7 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 System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization.Operators {
30  /// <summary>
31  /// Joins all sub sub scopes of a specified scope, reduces the number of sub
32  /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order.
33  /// </summary>
34  /*[Item("SASEGASAReunificator", "This operator merges the villages in a migration phase and redistributes the individuals. It is implemented as described in Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications, CRC Press.")]
35  [StorableClass]
36  public class SASEGASAReunificator : SingleSuccessorOperator, IMigrator {
37
38    /// <summary>
39    /// Joins all sub sub scopes of the given <paramref name="scope"/>, reduces the number of sub
40    /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order.
41    /// </summary>
42    /// <exception cref="InvalidOperationException">Thrown when only 0 or 1 sub scope is available.</exception>
43    /// <param name="scope">The current scope whose sub scopes to reduce.</param>
44    /// <returns><c>null</c>.</returns>
45    public override IOperation Apply() {
46      IScope scope = ExecutionContext.Scope;
47      int villageCount = scope.SubScopes.Count;
48      if (villageCount <= 1)
49        throw new InvalidOperationException("SASEGASA reunification requires 2 or more sub-scopes");
50
51      // get all villages
52      IList<IScope> population = new List<IScope>();
53      for (int i = 0; i < villageCount; i++) {
54        while (scope.SubScopes[i].SubScopes[0].SubScopes.Count > 0) {
55          population.Add(scope.SubScopes[i].SubScopes[0].SubScopes[0]);
56          scope.SubScopes[i].SubScopes.Remove(scope.SubScopes[i].SubScopes[0].SubScopes[0]);
57        }
58        scope.SubScopes[i].SubScopes.Clear();
59      }
60
61      // reduce number of villages by 1 and partition the population again
62      scope.SubScopes.Remove(scope.SubScopes[scope.SubScopes.Count - 1]);
63      villageCount--;
64      int populationPerVillage = population.Count / villageCount;
65      for (int i = 0; i < villageCount; i++) {
66        scope.SubScopes[i].SubScopes.Add(new Scope());
67        scope.SubScopes[i].SubScopes.Add(new Scope());
68        for (int j = 0; j < populationPerVillage; j++) {
69          scope.SubScopes[i].SubScopes[1].SubScopes.Add(population[0]);
70          population.RemoveAt(0);
71        }
72      }
73
74      // add remaining sub-sub-scopes to last sub-scope
75      while (population.Count > 0) {
76        scope.SubScopes[scope.SubScopes.Count - 1].SubScopes[1].SubScopes.Add(population[0]);
77        population.RemoveAt(0);
78      }
79
80      return base.Apply();
81    }
82  }*/
83}
Note: See TracBrowser for help on using the repository browser.