Free cookie consent management tool by TermsFeed Policy Generator

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

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

added first draft of SASEGASA #839

File size: 4.1 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;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Optimization.Operators {
32  /// <summary>
33  /// Joins all sub sub scopes of a specified scope, reduces the number of sub
34  /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order.
35  /// </summary>
36  [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.")]
37  [StorableClass]
38  public class SASEGASAReunificator : SingleSuccessorOperator, IMigrator {
39
40    public LookupParameter<IntValue> VillageCountParameter {
41      get { return (LookupParameter<IntValue>)Parameters["VillageCount"]; }
42    }
43
44    public SASEGASAReunificator()
45      : base() {
46      Parameters.Add(new LookupParameter<IntValue>("VillageCount", "The number of villages left after the reunification."));
47    }
48    /// <summary>
49    /// Joins all sub sub scopes of the given <paramref name="scope"/>, reduces the number of sub
50    /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order.
51    /// </summary>
52    /// <exception cref="InvalidOperationException">Thrown when there are less than 2 sub-scopes available or when VillageCount does not equal the number of sub-scopes.</exception>
53    /// <param name="scope">The current scope whose sub scopes to reduce.</param>
54    /// <returns><c>null</c>.</returns>
55    public override IOperation Apply() {
56      IScope scope = ExecutionContext.Scope;
57      if (VillageCountParameter.ActualValue == null) VillageCountParameter.ActualValue = new IntValue(scope.SubScopes.Count);
58      int villageCount = VillageCountParameter.ActualValue.Value;
59      if (villageCount <= 1)
60        throw new InvalidOperationException(Name + ": Reunification requires 2 or more sub-scopes");
61      if (villageCount != scope.SubScopes.Count)
62        throw new InvalidOperationException(Name + ": VillageCount does not equal the number of sub-scopes");
63
64      // get all villages
65      List<IScope> population = new List<IScope>();
66      for (int i = 0; i < villageCount; i++) {
67        population.AddRange(scope.SubScopes[i].SubScopes);
68        scope.SubScopes[i].SubScopes.Clear();
69      }
70
71      // reduce number of villages by 1 and partition the population again
72      scope.SubScopes.Remove(scope.SubScopes[scope.SubScopes.Count - 1]);
73      villageCount--;
74      int populationPerVillage = population.Count / villageCount;
75      for (int i = 0; i < villageCount; i++) {
76        scope.SubScopes[i].SubScopes.AddRange(population.GetRange(0, populationPerVillage));
77        population.RemoveRange(0, populationPerVillage);
78      }
79
80      // add remaining sub-sub-scopes to last sub-scope
81      scope.SubScopes[scope.SubScopes.Count - 1].SubScopes.AddRange(population);
82      population.Clear();
83
84      VillageCountParameter.ActualValue.Value = villageCount;
85
86      return base.Apply();
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.