[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[3451] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
[4068] | 25 | using HeuristicLab.Data;
|
---|
[3451] | 26 | using HeuristicLab.Operators;
|
---|
[4068] | 27 | using HeuristicLab.Parameters;
|
---|
[3451] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 29 |
|
---|
[3451] | 30 | namespace HeuristicLab.Optimization.Operators {
|
---|
[881] | 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>
|
---|
[3489] | 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.")]
|
---|
[3451] | 36 | [StorableClass]
|
---|
[3489] | 37 | public class SASEGASAReunificator : SingleSuccessorOperator {
|
---|
[2] | 38 |
|
---|
[3479] | 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 | }
|
---|
[881] | 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>
|
---|
[3479] | 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>
|
---|
[881] | 52 | /// <param name="scope">The current scope whose sub scopes to reduce.</param>
|
---|
| 53 | /// <returns><c>null</c>.</returns>
|
---|
[3451] | 54 | public override IOperation Apply() {
|
---|
| 55 | IScope scope = ExecutionContext.Scope;
|
---|
[3479] | 56 | if (VillageCountParameter.ActualValue == null) VillageCountParameter.ActualValue = new IntValue(scope.SubScopes.Count);
|
---|
| 57 | int villageCount = VillageCountParameter.ActualValue.Value;
|
---|
[3451] | 58 | if (villageCount <= 1)
|
---|
[3479] | 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");
|
---|
[2] | 62 |
|
---|
[3451] | 63 | // get all villages
|
---|
[3479] | 64 | List<IScope> population = new List<IScope>();
|
---|
[3451] | 65 | for (int i = 0; i < villageCount; i++) {
|
---|
[3479] | 66 | population.AddRange(scope.SubScopes[i].SubScopes);
|
---|
[3451] | 67 | scope.SubScopes[i].SubScopes.Clear();
|
---|
[2] | 68 | }
|
---|
| 69 |
|
---|
[3451] | 70 | // reduce number of villages by 1 and partition the population again
|
---|
[4489] | 71 | scope.SubScopes.RemoveAt(scope.SubScopes.Count - 1);
|
---|
[3451] | 72 | villageCount--;
|
---|
| 73 | int populationPerVillage = population.Count / villageCount;
|
---|
| 74 | for (int i = 0; i < villageCount; i++) {
|
---|
[3479] | 75 | scope.SubScopes[i].SubScopes.AddRange(population.GetRange(0, populationPerVillage));
|
---|
| 76 | population.RemoveRange(0, populationPerVillage);
|
---|
[2] | 77 | }
|
---|
| 78 |
|
---|
[3489] | 79 | // add remaining individuals to last village
|
---|
[3479] | 80 | scope.SubScopes[scope.SubScopes.Count - 1].SubScopes.AddRange(population);
|
---|
| 81 | population.Clear();
|
---|
[2] | 82 |
|
---|
[3479] | 83 | VillageCountParameter.ActualValue.Value = villageCount;
|
---|
| 84 |
|
---|
[3451] | 85 | return base.Apply();
|
---|
[2] | 86 | }
|
---|
[3479] | 87 | }
|
---|
[2] | 88 | }
|
---|