Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.ALPS/3.3/AgingScheme.cs @ 17197

Last change on this file since 17197 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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