Пример #1
0
        public void ExactCoverProblemsWithSingleSolutionTest()
        {
            Func <int, string> makeLabel = numSolutions => string.Format(
                "Expected exactly one solution but got {0}",
                numSolutions);

            var arbMatrix = Arb.From(GenMatrixOfIntWithSingleSolution());

            var property = Prop.ForAll(arbMatrix, matrix =>
            {
                var solutions = new Dlx().Solve(matrix).ToList();
                var p1        = (solutions.Count() == 1).Label(makeLabel(solutions.Count()));
                var p2        = CheckSolutions(solutions, matrix);
                return(FsCheckUtils.And(p1, p2));
            });

            Check.One(Config, property);
        }
Пример #2
0
        private static Property CheckSolution(Solution solution, int[,] matrix)
        {
            var numCols                   = matrix.GetLength(1);
            var numSolutionRows           = solution.RowIndexes.Count();
            var expectedNumZerosPerColumn = numSolutionRows - 1;
            var colProperties             = new List <Property>();

            Func <int, int, string> makeLabel1 = (colIndex, numOnes) => string.Format(
                "Expected column {0} to contain a single 1 but it contains {1}",
                colIndex,
                numOnes);

            Func <int, int, string> makeLabel2 = (colIndex, numZeros) => string.Format(
                "Expected column {0} to contain exactly {1} 0s but it contains {2}",
                colIndex,
                expectedNumZerosPerColumn,
                numZeros);

            for (var colIndex = 0; colIndex < numCols; colIndex++)
            {
                var numZeros = 0;
                var numOnes  = 0;

                foreach (var rowIndex in solution.RowIndexes)
                {
                    if (matrix[rowIndex, colIndex] == 0)
                    {
                        numZeros++;
                    }
                    if (matrix[rowIndex, colIndex] == 1)
                    {
                        numOnes++;
                    }
                }

                var p1 = (numOnes == 1).Label(makeLabel1(colIndex, numOnes));
                var p2 = (numZeros == expectedNumZerosPerColumn).Label(makeLabel2(colIndex, numZeros));

                colProperties.Add(p1);
                colProperties.Add(p2);
            }

            return(FsCheckUtils.And(colProperties));
        }
Пример #3
0
        public void ExactCoverProblemsWithMultipleSolutionsTest()
        {
            var arbNumSolutions = Arb.From(Gen.Choose(2, 5));

            var property = Prop.ForAll(arbNumSolutions, numSolutions =>
            {
                Func <int, string> makeLabel = actualNumSolutions => string.Format(
                    "Expected exactly {0} solutions but got {1}",
                    numSolutions, actualNumSolutions);

                var arbMatrix = Arb.From(GenMatrixOfIntWithMultipleSolutions(numSolutions));

                return(Prop.ForAll(arbMatrix, matrix =>
                {
                    var solutions = new Dlx().Solve(matrix).ToList();
                    var actualNumSolutions = solutions.Count();
                    var p1 = (actualNumSolutions == numSolutions).Label(makeLabel(actualNumSolutions));
                    var p2 = CheckSolutions(solutions, matrix);
                    return FsCheckUtils.And(p1, p2);
                }));
            });

            Check.One(Config, property);
        }
Пример #4
0
 private static Property CheckSolutions(IEnumerable <Solution> solutions, int[,] matrix)
 {
     return(FsCheckUtils.And(solutions.Select(solution => CheckSolution(solution, matrix))));
 }
Пример #5
0
 private static Gen <IList <int> > PickRandomRowIdxs(int numSolutionsRows, int numRows)
 {
     return(FsCheckUtils.PickValues(numSolutionsRows, Enumerable.Range(0, numRows)));
 }