制作一个LINQ基础的解决方案,以确定是否一组predicates满足一对集合的一组不变量的约束变量、解决方案、基础、LINQ

2023-09-05 03:57:12 作者:你眼角笑意

这是不是一个问题,我觉得我有词汇正确EX preSS,但我也有同样的匿名类型的两个集合(让称之为

This isn't a question I feel I have the vocabulary to properly express, but I have two collections of the same anonymous type (lets call it 'a.)

一个被定义为新{字符串名称,诠释计数}

一的一个我们称之为要求这些藏品。 其中一个一这些集合我们称之为候选人。

One of these collections of 'a we shall call requirements. One of these collections of 'a we shall call candidates.

由于这些藏品,我想确定以下断言成立。

Given these collections, I want to determine if the following assertions hold.

如果存在在要求一些元素的研究的这样 r.Count == 0 ,考生每一个元素的 ç的这样 r.Name == c.Name 必须满足 c.Count == 0 。还有必须在候选人的要求,每个这样的元素存在一个这样的元素。

If there exists some element in requirements r such that r.Count == 0, each element in candidates c such that r.Name == c.Name must satisfy c.Count == 0. There must exist one such element in candidates for each such element in requirements.

有关要求的每个元素的研究的其中 r.Count> 0 ,就必须有考生元素的某个子集的 C 的这样c₁.Name,c₂.Name,...,cₓ.Name= = r.Name C 1 + ... +cₓ> = r.Count 。候选的每个元素用于满足该规则对于在要求某些元件可能不被用于在要求另一元件

For each element of requirements r where r.Count > 0, there must be some subset of elements in candidates c such that c₁.Name, c₂.Name, ..., cₓ.Name == r.Name and that c₁ + ... + cₓ >= r.Count. Each element of candidates used to satisfy this rule for some element in requirements may not be used for another element in requirements.

这方面的一个例子是,鉴于

An example of this would be that given

requirements = {{"A",0}, {"B", 0}, {"C", 9}}
candidates = {{"B", 0},  {"C", 1}, {"A",0}, {"D", 2}, {"C", 4}, {"C", 4}}

这个查询会满意。

That this query would be satisfied.

R = {A,0} R = {B,0} 将根据纳统治#1对 C = {A,0} C = {B,0}

r={"A", 0} and r={"B", 0} would be satisfied according to rule #1 against c={"A", 0} and c={"B", 0}

- 和 -

R = {C,9)根据规则#2通过集合组的 GC 的纳 c.Name 从 {{C,1},{C,4},{C,4}} 为 GC = {C,9}

r={"C", 9) is satisfied according to rule #2 by the group gc on collections c.Name derived from {{"C", 1}, {"C", 4}, {"C", 4}} as gc = {"C", 9}

不过值得一提的是,如果要求包含 {C,6} {C,3} 而不是 {C,9} ,这组特定集合将无法满足predicates。

However it is worth noting that if requirements contained {"C", 6} and {"C", 3} instead of {"C", 9}, this particular set of collections would fail to satisfy the predicates.

现在终于的问题。 什么是形成这种成LINQ EX pression优先次序速度(至少重复)的最佳方法是什么?

未解子已经重新问here

推荐答案

我终于想出了一个可行的解决方案。

I finally came up with a workable solution

        IEnumerable<Glyph> requirements = t.Objectives.Cast<Glyph>().OrderBy(k => k.Name);
        IEnumerable<Glyph> candidates = Resources.Cast<Glyph>().OrderBy(k => k.Name);

        IEnumerable<Glyph> zeroCountCandidates = candidates.Where(c => c.Count == 0);
        IEnumerable<Glyph> zeroCountRequirements = requirements.Where(r => r.Count == 0);

        List<Glyph> remainingCandidates = zeroCountCandidates.ToList();

        if (zeroCountCandidates.Count() < zeroCountRequirements.Count())
        {
            return false;
        }

        foreach (var r in zeroCountRequirements)
        {
            if (!remainingCandidates.Contains(r))
            {
                return false;
            }
            else
            {
                remainingCandidates.Remove(r);
            }
        }

        IEnumerable<Glyph> nonZeroCountCandidates = candidates.Where(c => c.Count > 0);
        IEnumerable<Glyph> nonZeroCountRequirements = requirements.Where(r => r.Count > 0);

        var perms = nonZeroCountCandidates.Permutations();

        foreach (var perm in perms)
        {
            bool isViableSolution = true;

            remainingCandidates = perm.ToList();

            foreach (var requirement in nonZeroCountRequirements)
            {
                int countThreshold = requirement.Count;
                while (countThreshold > 0)
                {
                    if (remainingCandidates.Count() == 0)
                    {
                        isViableSolution = false;
                        break;
                    }

                    var c = remainingCandidates[0];
                    countThreshold -= c.Count;

                    remainingCandidates.Remove(c);
                }
            }

            if (isViableSolution)
            {
                return true;
            }
        }

        return false;

恶心是吧?