Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4477 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

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