Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/EnumerableExtensions.cs @ 14424

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

#2526: Updated year of copyrights in license headers

File size: 2.8 KB
RevLine 
[3338]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3338]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
[12422]22using System;
[3338]23using System.Collections.Generic;
[4068]24using System.Linq;
[3338]25using HeuristicLab.Core;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[12422]28
29  //This class should not be used anymore. Use HeuristicLab.Random.RandomEnumberable instead
30  //This could not be fixed right now, because the algorithm behavior would be modified => version increment
31  [Obsolete("This class will be removed in the future, because the functionality is provided in HeuristicLab.Random.RandomEnumerable.")]
[3338]32  public static class EnumerableExtensions {
[12422]33    [Obsolete("This method should not be used anymore. Use the extensions provided by HeuristicLab.Random.RandomEnumberable instead.")]
[3338]34    public static T SelectRandom<T>(this IEnumerable<T> xs, IRandom random) {
35      var list = xs as IList<T>;
36      if (list != null) {
37        return list[random.Next(list.Count)];
38      } else {
39        list = xs.ToList();
40        return list[random.Next(list.Count)];
41      }
42    }
[12422]43    [Obsolete("This method should not be used anymore. Use the extensions provided by HeuristicLab.Random.RandomEnumberable instead.")]
[5411]44    public static T SelectRandom<T>(this IEnumerable<T> xs, IEnumerable<double> weights, IRandom random) {
45      var list = xs as IList<T>;
46      var weightsList = weights as IList<double>;
47      if (list == null) {
48        list = xs.ToList();
49      }
50      if (weightsList == null) {
51        weightsList = weights.ToList();
52      }
53      if (list.Count != weightsList.Count) throw new ArgumentException("Number of elements in enumerations doesn't match.");
54      if (list.Count == 0) throw new ArgumentException("Enumeration is empty", "xs");
55
56      double sum = weightsList.Sum();
57      double r = random.NextDouble() * sum;
58      double agg = 0;
59      for (int i = 0; i < list.Count; i++) {
60        agg += weightsList[i];
61        if (agg > r) return list[i];
62      }
63      // should never happen
64      throw new InvalidOperationException();
65    }
[3338]66  }
67}
Note: See TracBrowser for help on using the repository browser.