Hi all,
I’m using Julia 1.5.2 and Combinatorics v1.0.2.
I tried the following combinatorics exercise.
First level:
Get all combinations selecting 4 elements out of 8 available elements, without repetition (meaning each element occurs only once).
using Combinatorics
julia> a = combinations(1:8,4)
Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{UnitRange{Int64}}}(Combinatorics.var"#reorder#10"{UnitRange{Int64}}(1:8), Combinatorics.Combinations(8, 4))
julia> a_list = [x for x in a]
70-element Array{Array{Int64,1},1}:
[1, 2, 3, 4]
...
[5, 6, 7, 8]
Up to here, everything is fine.
Now let’s add a second level of complexity.
Just select n (e.g. 6) elements of these previously generated 70 elements and “process” them.
julia> b = combinations(combinations(1:8, 4), 6)
Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{UnitRange{Int64}}}}}(Combinatorics.var"#reorder#10"{Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{UnitRange{Int64}}}}(Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{UnitRange{Int64}}}(Combinatorics.var"#reorder#10"{UnitRange{Int64}}(1:8), Combinatorics.Combinations(8, 4))), Combinatorics.Combinations(70, 6))
julia> b_list = [x for x in b]
ERROR: MethodError: no method matching getindex(::Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{UnitRange{Int64}}}, ::Int64)
Stacktrace:
[1] (::Combinatorics.var"#9#11"{Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{UnitRange{Int64}}}})(::Int64) at .\none:0
[2] iterate at .\generator.jl:47 [inlined]
[3] collect at .\array.jl:686 [inlined]
[4] reorder at C:\Users\mac\.julia\packages\Combinatorics\Udg6X\src\combinations.jl:48 [inlined]
[5] iterate at .\generator.jl:47 [inlined]
[6] iterate at .\generator.jl:44 [inlined]
[7] collect(::Base.Generator{Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{Base.Generator{Combinatorics.Combinations,Combinatorics.var"#reorder#10"{UnitRange{Int64}}}}},typeof(identity)}) at .\array.jl:686
[8] top-level scope at REPL[11]:1
Background:
In this example, there are 70 possible 4 element long vectors. I want to evaluate all combination of each e.g. 6 of these 4 element vectors selected from the 70 possible ones, by a function qualifying each arrangement (with a sort of costfunction). This sort of list comprehension above it just to show my basic problem. Nearly the same issue occurs, when calling the costfunction for each b element in a for loop consuming the generator data.
Obviously I did not yet understand the generator concept fully.
I did not find any examples in the Web pointing me to a solution for this exercise.
The reported error it-self, does not guide me enough to a solution. Why is it working in the first step, but not in the more complex one.
Has anybody an example and much better, also an explanation on how to consume such generated data.
Thanks a lot in advance,
Mike