コード例 #1
0
        public void TestSubsetIterator()
        {
            SubsetIterator <int> it = new SubsetIterator <int>(2, new List <int> {
                1, 6, 7, 9
            });

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 6
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                6, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                6, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                7, 9
            }, it.Current);

            Assert.IsFalse(it.MoveNext());
        }
コード例 #2
0
        public void TestSubsetIteratorThatCrashed()
        {
            SubsetIterator <int> it = new SubsetIterator <int>(2, new List <int> {
                1, 4, 7
            });

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 4
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                4, 7
            }, it.Current);

            Assert.IsFalse(it.MoveNext());
        }
コード例 #3
0
        public void TestBiggerSubsetIterator()
        {
            SubsetIterator <int> it = new SubsetIterator <int>(3, new List <int> {
                1, 3, 4, 6, 7, 9
            });

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 3, 4
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 3, 6
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 3, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 3, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 4, 6
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 4, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 4, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 6, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 6, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 7, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 4, 6
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 4, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 4, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 6, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 6, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 7, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                4, 6, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                4, 6, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                4, 7, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                6, 7, 9
            }, it.Current);

            Assert.IsFalse(it.MoveNext());
        }
コード例 #4
0
        protected bool SolveForCells(int groupSize, ref Board board, CellData cellData)
        {
            bool ruleSucceeded = false;

            var unsolvedValues = cellData.GetUnsolvedValues();
            var unsolvedCells  = cellData.GetUnsolvedCells();

            if (groupSize >= unsolvedCells.Count)
            {
                return(false);
            }

            var valueSetGenerator = new SubsetIterator <int>(groupSize, unsolvedValues);

            while (valueSetGenerator.MoveNext())
            {
                List <int> setOfValues = valueSetGenerator.Current;

                HashSet <KeyValuePair <int, List <int> > > cellsWithValues = new HashSet <KeyValuePair <int, List <int> > >();

                foreach (int value in setOfValues)
                {
                    cellsWithValues.AddRange(unsolvedCells.Where(kv => kv.Value.Contains(value)).ToList());
                }

                if (cellsWithValues.Count == groupSize)
                {
                    foreach (KeyValuePair <int, List <int> > cell in cellsWithValues)
                    {
                        foreach (int value in unsolvedValues)
                        {
                            if (!setOfValues.Contains(value) && cell.Value.Contains(value))
                            {
                                board.RemoveValueFromCell(cell.Key, value);
                                ruleSucceeded = true;
                            }
                        }
                    }
                }
            }

            var cellSetGenerator = new SubsetIterator <KeyValuePair <int, List <int> > >(groupSize, unsolvedCells.ToList());

            while (cellSetGenerator.MoveNext())
            {
                List <KeyValuePair <int, List <int> > > setOfCells = cellSetGenerator.Current;
                HashSet <int> valuesInCells = new HashSet <int>();

                foreach (KeyValuePair <int, List <int> > cell in setOfCells)
                {
                    valuesInCells.AddRange(cell.Value);
                }

                if (valuesInCells.Count == groupSize)
                {
                    foreach (KeyValuePair <int, List <int> > cell in unsolvedCells)
                    {
                        if (!setOfCells.Select(c => c.Key).Contains(cell.Key))
                        {
                            foreach (int valueInCell in valuesInCells)
                            {
                                if (cell.Value.Contains(valueInCell))
                                {
                                    board.RemoveValueFromCell(cell.Key, valueInCell);
                                    ruleSucceeded = true;
                                }
                            }
                        }
                    }
                }
            }

            return(ruleSucceeded);
        }
コード例 #5
0
    public HashSet<GeneratorNode> spawn(FOForAll op)
    {
        HashSet<GeneratorNode> spawnedSet, outSet = new HashSet<GeneratorNode>();
        Atom x = op.getQuantifiedVariable();
        string qualifier = op.getQualifier();

        // Iterate over domain
        HashSet<Constant> oplus_domain = getOPlusDomain(qualifier);
        HashSet<Constant> domain = op.getDomain();
        SubsetIterator<Constant> it;

        if (!m_encounteredQualifiers.Contains(qualifier))
        {
            // We haven't decomposed a For All in the past, so we can
          		// add elements to the message
            it = new SubsetIterator<Constant>(domain, oplus_domain);
        }

        else
        {
            // Otherwise, we stick to the elements we already have to
          		// evaluate this quantifier
            it = new SubsetIterator<Constant>(oplus_domain);
        }

        m_encounteredQualifiers.Add(op.getQualifier());
        m_decomposedAForAll = true;

        if (op.isPathNegation())
        {
            // The quantifier asserts the absence of a path
            GeneratorNode wn = new GeneratorNode(this);
            OPlus opl = new OPlus();

            opl.setQualifier(op.getQualifier());
            opl.setOperand(Operator.m_falseAtom);

            if (!wn.addToOPluses(opl))
            {
                // We can't add this OPlus to the current set. Contradiction! Return the empty set
                return new HashSet<GeneratorNode>();
            }

            return wn.spawn();
        }

        if (op.isPathAssertion())
        {
            // In negated form, the quantifier may assert the existence of a path
            GeneratorNode wn = new GeneratorNode(this);
            OPlus opl = new OPlus();

            opl.setQualifier(op.getQualifier());
            opl.setOperand(Operator.m_trueAtom);

            if (!wn.addToOPluses(opl))
            {
                // We can't add this OPlus to the current set. Contradiction! Return the empty set
                return new HashSet<GeneratorNode>();
            }

            return wn.spawn();
        }

        while (it.hasNext())
        {
            GeneratorNode wn = new GeneratorNode(this);
            HashSet<Constant> subset = it.next();

            foreach (Atom v in subset)
            {
                Operator o2 = op.getOperand();
                Operator o3 = o2.evaluate(x, v);
                OPlus opl = new OPlus(qualifier, v);

                wn.addToGamma(o3);

                if (!wn.addToOPluses(opl))
                {
                    // Contradiction! Skip that branch
                    continue;
                }
            }

            spawnedSet = wn.spawn();

            if (spawnedSet != null)
            {
                foreach (GeneratorNode gn in spawnedSet)
                {
                    outSet.Add(gn);
                }
            }
        }

        return outSet;
    }
コード例 #6
0
    public HashSet<GeneratorNode> spawn(FOExists op)
    {
        HashSet<GeneratorNode> outSet = new HashSet<GeneratorNode>();
        Atom x = op.getQuantifiedVariable();
        string qualifier = op.getQualifier();

        if (m_encounteredQualifiers.Contains(qualifier))
        {
            // We add something along a path where a ForAll has already
          		// been evaluated: soundness is no longer guaranteed for this node
            m_sound = false;
        }

        if (op.isAnOPlus())
        {
            // This is an OPlus; return a node with op transferred to the OPlus set
            GeneratorNode wn = new GeneratorNode(this);

            if (!wn.addToOPluses(op.toOPlus()))
            {
                // We can't add this OPlus to the current set. Contradiction! Return the empty set
                return new HashSet<GeneratorNode>();
            }

            return wn.spawn();
        }

        if (op.isPathAssertion())
        {
            GeneratorNode wn = new GeneratorNode();
            OPlus opl = new OPlus();

            opl.setQualifier(op.getQualifier());
            opl.setOperand(Operator.m_trueAtom);

            if (!wn.addToOPluses(opl))
            {
                // We can't add this OPlus to the current set. Contradiction! Return the empty set
                return new HashSet<GeneratorNode>();
            }

            return wn.spawn();
        }

        // Iterate over domain
        //HashSet<Constant> oplus_domain = getOPlusDomain(qualifier);
        HashSet<Constant> domain = op.getDomain();
        SubsetIterator<Constant> it = new SubsetIterator<Constant>(domain); //,oplus_domain

        while (it.hasNext())
        {
            GeneratorNode wn = new GeneratorNode(this);
            HashSet<Constant> subset = it.next();

            foreach (Atom v in subset)
            {
                Operator o2 = op .getOperand();
                Operator o3 = o2.evaluate(x, v);

                if (!op.isPathAssertion())
                {
                    wn.addToGamma(o3);
                }

                OPlus opl = new OPlus(qualifier, v);

                if (!wn.addToOPluses(opl))
                {
                    // We can't add this OPlus to the current set. Contradiction! Skip that branch
                    continue;
                }
            }

            HashSet<GeneratorNode> spawnedSet = wn.spawn();

            if (spawnedSet != null)
            {
                foreach (GeneratorNode gn in spawnedSet)
                {
                    outSet.Add(gn);
                }
            }
        }

        return outSet;
    }