Changeset 11309


Ignore:
Timestamp:
08/27/14 10:14:54 (5 years ago)
Author:
bburlacu
Message:

#2234: Added CartesianProduct extension method to HeuristicLab.Common/3.3/EnumerableExtensions.cs, used to generate all possible combinations of parameters for the grid search.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Common/3.3/EnumerableExtensions.cs

    r11171 r11309  
    8787      return result;
    8888    }
     89
     90    /// <summary>
     91    /// Compute the n-ary cartesian product of arbitrarily many sequences: http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
     92    /// </summary>
     93    /// <typeparam name="T">The type of the elements inside each sequence</typeparam>
     94    /// <param name="sequences">The collection of sequences</param>
     95    /// <returns>An enumerable sequence of all the possible combinations of elements</returns>
     96    public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) {
     97      IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
     98      return sequences.Aggregate(result, (current, s) => (from seq in current from item in s select seq.Concat(new[] { item })));
     99    }
    89100  }
    90101}
Note: See TracChangeset for help on using the changeset viewer.