1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence;
|
---|
30 |
|
---|
31 | namespace 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 (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.")]
|
---|
37 | [StorableType("16f9e8fc-a9d3-4950-a0a9-272759a2b23c")]
|
---|
38 | public class SASEGASAReunificator : SingleSuccessorOperator {
|
---|
39 |
|
---|
40 | public LookupParameter<IntValue> VillageCountParameter {
|
---|
41 | get { return (LookupParameter<IntValue>)Parameters["VillageCount"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | protected SASEGASAReunificator(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
46 | protected SASEGASAReunificator(SASEGASAReunificator original, Cloner cloner) : base(original, cloner) { }
|
---|
47 | public SASEGASAReunificator()
|
---|
48 | : base() {
|
---|
49 | Parameters.Add(new LookupParameter<IntValue>("VillageCount", "The number of villages left after the reunification."));
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
53 | return new SASEGASAReunificator(this, cloner);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Joins all sub sub scopes of the given <paramref name="scope"/>, reduces the number of sub
|
---|
58 | /// scopes by 1 and uniformly partitions the sub sub scopes again, maintaining the order.
|
---|
59 | /// </summary>
|
---|
60 | /// <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>
|
---|
61 | /// <param name="scope">The current scope whose sub scopes to reduce.</param>
|
---|
62 | /// <returns><c>null</c>.</returns>
|
---|
63 | public override IOperation Apply() {
|
---|
64 | IScope scope = ExecutionContext.Scope;
|
---|
65 | if (VillageCountParameter.ActualValue == null) VillageCountParameter.ActualValue = new IntValue(scope.SubScopes.Count);
|
---|
66 | int villageCount = VillageCountParameter.ActualValue.Value;
|
---|
67 | if (villageCount <= 1)
|
---|
68 | throw new InvalidOperationException(Name + ": Reunification requires 2 or more sub-scopes");
|
---|
69 | if (villageCount != scope.SubScopes.Count)
|
---|
70 | throw new InvalidOperationException(Name + ": VillageCount does not equal the number of sub-scopes");
|
---|
71 |
|
---|
72 | // get all villages
|
---|
73 | List<IScope> population = new List<IScope>();
|
---|
74 | for (int i = 0; i < villageCount; i++) {
|
---|
75 | population.AddRange(scope.SubScopes[i].SubScopes);
|
---|
76 | scope.SubScopes[i].SubScopes.Clear();
|
---|
77 | }
|
---|
78 |
|
---|
79 | // reduce number of villages by 1 and partition the population again
|
---|
80 | scope.SubScopes.RemoveAt(scope.SubScopes.Count - 1);
|
---|
81 | villageCount--;
|
---|
82 | int populationPerVillage = population.Count / villageCount;
|
---|
83 | for (int i = 0; i < villageCount; i++) {
|
---|
84 | scope.SubScopes[i].SubScopes.AddRange(population.GetRange(0, populationPerVillage));
|
---|
85 | population.RemoveRange(0, populationPerVillage);
|
---|
86 | }
|
---|
87 |
|
---|
88 | // add remaining individuals to last village
|
---|
89 | scope.SubScopes[scope.SubScopes.Count - 1].SubScopes.AddRange(population);
|
---|
90 | population.Clear();
|
---|
91 |
|
---|
92 | VillageCountParameter.ActualValue.Value = villageCount;
|
---|
93 |
|
---|
94 | return base.Apply();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|