【发布时间】:2011-05-18 04:05:58
【问题描述】:
我有一组必须安排的产品。有 P 个产品,每个产品的索引从 1 到 P。每个产品可以安排在 0 到 T 的时间段内。我需要构造满足以下约束的产品时间表的所有排列:
If p1.Index > p2.Index then p1.Schedule >= p2.Schedule.
我正在努力构建迭代器。当产品数量是已知常量时,我知道如何通过 LINQ 执行此操作,但不确定当产品数量是输入参数时如何生成此查询。
理想情况下,我想使用 yield 语法来构造这个迭代器。
public class PotentialSchedule()
{
public PotentialSchedule(int[] schedulePermutation)
{
_schedulePermutation = schedulePermutation;
}
private readonly int[] _schedulePermutation;
}
private int _numberProducts = ...;
public IEnumerator<PotentialSchedule> GetEnumerator()
{
int[] permutation = new int[_numberProducts];
//Generate all permutation combinations here -- how?
yield return new PotentialSchedule(permutation);
}
编辑:_numberProducts = 2 时的示例
public IEnumerable<PotentialSchedule> GetEnumerator()
{
var query = from p1 in Enumerable.Range(0,T)
from p2 in Enumerable.Range(p2,T)
select new { P1 = p1, P2 = p2};
foreach (var result in query)
yield return new PotentialSchedule(new int[] { result.P1, result.P2 });
}
【问题讨论】:
-
您意识到这会很快变得非常大,对吗?你介意我问为什么你需要生成所有可能的时间表吗?
-
是的——我意识到这将很快变得非常大。这是优化程序的一部分。最棘手的部分是扩展正在考虑的可能性。在最坏的情况下,我的问题大小大约是 P
-
第二个例子的第四行应该是(p1, T),是吗?
-
请注意,此类项目的数量等于 T 边的 P 维金字塔格子中的节点数。(因此,如果 P 为 3,T 为 5,则为点数一个最长边有五个点的三维金字塔。)一个边上有 15 个点的 20 维金字塔有 大量 个点。那个东西的点数和宇宙中基本粒子的数量差不多,(非常)粗略的近似。
标签: c# linq permutation