Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/EnumerableExtensions.cs @ 15529

Last change on this file since 15529 was 12422, checked in by mkommend, 10 years ago

#2320: Merged the encoding class and all accompanying changes in the trunk.

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Core;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
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.")]
32  public static class EnumerableExtensions {
33    [Obsolete("This method should not be used anymore. Use the extensions provided by HeuristicLab.Random.RandomEnumberable instead.")]
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    }
43    [Obsolete("This method should not be used anymore. Use the extensions provided by HeuristicLab.Random.RandomEnumberable instead.")]
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    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.