Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ALPS/3.3/AgingScheme.cs @ 14625

Last change on this file since 14625 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 2.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Data;
26
27namespace HeuristicLab.Algorithms.ALPS {
28  /// <summary>
29  /// Defines the growth of age limits for the layers.
30  /// </summary>
31  public enum AgingScheme {
32    Linear,
33    Fibonacci,
34    Polynomial,
35    Exponential
36  }
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  }
80}
Note: See TracBrowser for help on using the repository browser.