[11568] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15973] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11568] | 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 |
|
---|
[12271] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 |
|
---|
[11568] | 27 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
[12271] | 28 | /// <summary>
|
---|
| 29 | /// Defines the growth of age limits for the layers.
|
---|
| 30 | /// </summary>
|
---|
[12149] | 31 | public enum AgingScheme {
|
---|
[11568] | 32 | Linear,
|
---|
| 33 | Fibonacci,
|
---|
| 34 | Polynomial,
|
---|
| 35 | Exponential
|
---|
| 36 | }
|
---|
[12271] | 37 |
|
---|
| 38 | /// <summary>
|
---|
| 39 | /// Helper for calculating the age limits for a AgingScheme and a given AgeGap.
|
---|
| 40 | /// </summary>
|
---|
| 41 | public static class AgingSchemeCalculator {
|
---|
| 42 | public static IntArray CalculateAgeLimits(this AgingScheme scheme, int ageGap, int numberOfLayers) {
|
---|
| 43 | IEnumerable<int> schemeGenerator;
|
---|
| 44 | switch (scheme) {
|
---|
| 45 | case AgingScheme.Linear: schemeGenerator = LinearAgingScheme(); break;
|
---|
| 46 | case AgingScheme.Fibonacci: schemeGenerator = FibonacciAgingScheme(); break;
|
---|
| 47 | case AgingScheme.Polynomial: schemeGenerator = PolynomialAgingScheme(2); break;
|
---|
| 48 | case AgingScheme.Exponential: schemeGenerator = ExponentialAgingScheme(2); break;
|
---|
| 49 | default: throw new NotSupportedException("Aging Scheme " + scheme + " is not supported.");
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | return new IntArray(schemeGenerator.Select(a => a * ageGap).Take(numberOfLayers).ToArray());
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | #region Scheme definitions
|
---|
| 56 | // 1 2 3 4 5 6 7 ...
|
---|
| 57 | private static IEnumerable<int> LinearAgingScheme() {
|
---|
| 58 | for (int i = 0; ; i++)
|
---|
| 59 | yield return i + 1;
|
---|
| 60 | }
|
---|
| 61 | // 1 2 3 5 8 13 21 ...
|
---|
| 62 | private static IEnumerable<int> FibonacciAgingScheme() {
|
---|
| 63 | for (int i = 1, next = 2, temp; ; temp = next, next = i + next, i = temp)
|
---|
| 64 | yield return i;
|
---|
| 65 | }
|
---|
| 66 | // (n^2): 1 2 4 9 16 25 36 ...
|
---|
| 67 | private static IEnumerable<int> PolynomialAgingScheme(double exp) {
|
---|
| 68 | yield return 1;
|
---|
| 69 | yield return 2;
|
---|
| 70 | for (int i = 2; ; i++)
|
---|
| 71 | yield return (int)Math.Pow(i, exp);
|
---|
| 72 | }
|
---|
| 73 | // 1 2 4 8 16 32 64 ...
|
---|
| 74 | private static IEnumerable<int> ExponentialAgingScheme(double @base) {
|
---|
| 75 | for (int i = 0; ; i++)
|
---|
| 76 | yield return (int)Math.Pow(@base, i);
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 | }
|
---|
[11568] | 80 | } |
---|