julia中se的所有子集

julia中se的所有子集,julia,julia-jump,Julia,Julia Jump,你能帮帮我吗? 我如何做一个代码,可以找到一个集合的所有子集 比如说 我想用Julia编写这个约束。这是我们的限制。但我不知道如何才能找到S集的所有子集 约束(ILRP, c7[k表示总k,t表示总h], sum(对于i=1:totalS,x[i,j,k,t],j=1:totalS)您可以使用combinations.jl软件包中的powerset函数获得它,例如: julia> using Combinatorics julia> x = [1:5;] 5-element Ar

你能帮帮我吗? 我如何做一个代码,可以找到一个集合的所有子集

比如说

我想用Julia编写这个约束。这是我们的限制。但我不知道如何才能找到S集的所有子集

约束(ILRP, c7[k表示总k,t表示总h],
sum(对于i=1:totalS,x[i,j,k,t],j=1:totalS)您可以使用combinations.jl软件包中的
powerset
函数获得它,例如:

julia> using Combinatorics

julia> x = [1:5;]
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> powerset(x)
Base.Iterators.Flatten{Array{Combinatorics.Combinations{Array{Int64,1}},1}}(Combinatorics.Combinations{Array{Int64,1}}[Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 0), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 1), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 2), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 3), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 4), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 5)])

julia> collect(powerset(x))
32-element Array{Array{Int64,1},1}:
 []
 [1]
 [2]
 [3]
 [4]
 [5]
 [1, 2]
 [1, 3]
 [1, 4]
 [1, 5]
 [2, 3]
 [2, 4]
 [2, 5]
 [3, 4]
 [3, 5]
 [4, 5]
 [1, 2, 3]
 [1, 2, 4]
 [1, 2, 5]
 [1, 3, 4]
 [1, 3, 5]
 [1, 4, 5]
 [2, 3, 4]
 [2, 3, 5]
 [2, 4, 5]
 [3, 4, 5]
 [1, 2, 3, 4]
 [1, 2, 3, 5]
 [1, 2, 4, 5]
 [1, 3, 4, 5]
 [2, 3, 4, 5]
 [1, 2, 3, 4, 5]
请注意,默认情况下,
powerset
返回迭代器以避免分配所有子集。 您还可以将第二个和第三个位置参数传递给
powerset
,以限制返回子集的最小和最大大小,例如:

julia> collect(powerset(x, 2, 3))
20-element Array{Array{Int64,1},1}:
 [1, 2]
 [1, 3]
 [1, 4]
 [1, 5]
 [2, 3]
 [2, 4]
 [2, 5]
 [3, 4]
 [3, 5]
 [4, 5]
 [1, 2, 3]
 [1, 2, 4]
 [1, 2, 5]
 [1, 3, 4]
 [1, 3, 5]
 [1, 4, 5]
 [2, 3, 4]
 [2, 3, 5]
 [2, 4, 5]
 [3, 4, 5]

不知道这是否是你想要的:

使用组合数学

   function subsets(A::AbstractArray,r::Union{AbstractArray,Integer})
  o= Array{Array{eltype(A),1},1}(undef,0)         
  if typeof(r)<:Integer
      r>length(A) && (r=[length(A)])
      r=[r...]
  elseif typeof(r)<:UnitRange
      r[end]>length(A) && (r=1:r[length(A)])
  else
      !issubset(r,1:length(A)) && (r=intersect(r,1:length(A)))
  end
  for n = r
      a=combinations(A,n)
      for i in a     
          push!(o,i)
      end
  end
  return o
end

subsets(A::AbstractArray) = subsets(A,1:length(A))

非常感谢。我使用了你宝贵的帮助。但是当我在约束中使用时,它会让我出错。非常感谢。我使用了你宝贵的帮助。但是当我在约束中使用时,它会让我出错。“totalI=3;s=[1:totalI;]s=collect(powerset(s)),因为I=2:size(s,1)@constraint(ILRP,c7[k in totalK,t in totalH,s in size(s[I],1)],sum(x[I,j,k,t]对于i=1:size(S[i],1),j=1:size(S[i],1),您将重复使用
i
变量两次。一次在外部循环中,一次在约束内部。您应该使用唯一的变量名称,例如,对于i=2:size(S,1)将
更改为
对于外部i=2:size(S,1)
并更正内部代码以适当地使用
外部。\i
如果您不介意,请再次帮助我。我可以使用powerset(x,2:5)吗?它只提供包含2,3,4和5个元素的子集。
powerset(x,2,5)
   function subsets(A::AbstractArray,r::Union{AbstractArray,Integer})
  o= Array{Array{eltype(A),1},1}(undef,0)         
  if typeof(r)<:Integer
      r>length(A) && (r=[length(A)])
      r=[r...]
  elseif typeof(r)<:UnitRange
      r[end]>length(A) && (r=1:r[length(A)])
  else
      !issubset(r,1:length(A)) && (r=intersect(r,1:length(A)))
  end
  for n = r
      a=combinations(A,n)
      for i in a     
          push!(o,i)
      end
  end
  return o
end

subsets(A::AbstractArray) = subsets(A,1:length(A))
 julia> subsets(1:3)
 7-element Array{Array{Int64,1},1}:
  [1]
  [2]
  [3]
  [1, 2]
  [1, 3]
  [2, 3]
  [1, 2, 3]
  julia> subsets(1:3,2)
  3-element Array{Array{Int64,1},1}:
  [1, 2]
  [1, 3]
  [2, 3]