Example usage for org.apache.commons.math.util MathUtils binomialCoefficient

List of usage examples for org.apache.commons.math.util MathUtils binomialCoefficient

Introduction

In this page you can find the example usage for org.apache.commons.math.util MathUtils binomialCoefficient.

Prototype

public static long binomialCoefficient(final int n, final int k) 

Source Link

Document

Returns an exact representation of the <a href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial Coefficient</a>, "<code>n choose k</code>", the number of <code>k</code>-element subsets that can be selected from an <code>n</code>-element set.

Usage

From source file:exec.csharp.queries.RandomQueryBuilder.java

public int getMaxNumPossible(int numBefore, int numAfter) {
    int maxPossible = (int) MathUtils.binomialCoefficient(numAfter, numBefore);
    return Math.min(maxNumQueries, maxPossible);
}

From source file:com.xiantrimble.combinatorics.CombMathUtilsImpl.java

private static long c(int k, DistinctM dm) {
    long result = 0;

    // create a stack for the calculation.
    FastList<PartialCombinationCount> stack = stackFactory.object();

    // add the initial partial combination.
    stack.addFirst(pccFactory.object().init(k, dm, dm.m, 0, 1));

    while (!stack.isEmpty()) {
        // get the next combination to expand.
        PartialCombinationCount pc = stack.removeFirst();

        //System.out.println(pc);

        // Start the expansion of this partial combination.
        // pc.k = the number of elements that still need to be added to the combination.
        // pc.dm = the next distinct m to consider.
        // pc.dmk = the size of the next combination of elements to add.
        // pc.ldm = the number of distinct unused elements to the left of mdi minus the number of distinct used elements at mdi.
        // pc.size = the number of combinations already in the solution (in k - pc.k)

        // get the current distinct m
        DistinctM cdm = pc.dm;//ww w  . ja v a 2s. co m

        // if there could never be an answer, then bail out.
        if (pc.k > (cdm.count + pc.ldm) * pc.dmk + cdm.rn) {
            pccFactory.recycle(pc);
            continue;
        }

        // for each number of pc.dmk sized sets that we can create, add new partial combinations.
        for (int e = 0; e <= pc.dm.count + pc.ldm && e * pc.dmk <= pc.k; e++) {
            int nextK = pc.k - (e * pc.dmk);
            int nextDmk = pc.dmk - 1;
            long nextSize = pc.size * MathUtils.binomialCoefficient(pc.dm.count + pc.ldm, e);

            // if nextK is zero, then this set of combinations is complete.
            if (nextK == 0) {
                result += nextSize;
                continue;
            }

            // if nextDmk is zero, then we have run out of items to place into k.
            else if (nextDmk == 0)
                continue;

            // if we are on the last distinct m, or the next distinct m is not big enough, stay at dmi.
            else if (pc.dm.next == null || pc.dm.next.m < nextDmk) {
                int nextLdm = pc.ldm - e;
                stack.addFirst(pccFactory.object().init(nextK, pc.dm, nextDmk, nextLdm, nextSize));
            }

            // we need to advance to the next dmi.
            else {
                int nextLdm = pc.ldm - e + cdm.count;
                stack.addFirst(pccFactory.object().init(nextK, pc.dm.next, nextDmk, nextLdm, nextSize));
            }
        }
        pccFactory.recycle(pc);
    }

    stackFactory.recycle(stack);

    return result;
}

From source file:com.xiantrimble.combinatorics.CombMathUtilsBenchmark.java

private static long c(int k, DistinctM... dm) {
    //System.out.print("k:"+k+", dm:[");
    //for( int i = 0; i < dm.length; i++ ) {
    //  System.out.print(dm[i]);
    //  if( i+1 < dm.length ) {
    //    System.out.print(",");
    //  }/*from   w  w  w.  j a v  a 2s .com*/
    //}
    //System.out.println("]");
    long result = 0;

    // create a stack for the calculation.
    FastList<PartialCombinationCount> stack = new FastList<PartialCombinationCount>();

    // add the initial partial combination.
    // 
    stack.addFirst(new PartialCombinationCount(k, 0, dm[0].value, 0, 1));

    while (!stack.isEmpty()) {
        // get the next combination to expand.
        PartialCombinationCount pc = stack.removeFirst();

        //System.out.println(pc);

        // Start the expansion of this partial combination.
        // pc.k = the number of elements that still need to be added to the combination.
        // pc.dmi = the next index in dm to consider.
        // pc.dmk = the size of the next combination of elements to add.
        // pc.ldm = the number of distinct unused elements to the left of mdi minus the number of distinct used elements at mdi.
        // pc.size = the number of combinations already in the solution (in k - pc.k)

        // get the current distinct m
        DistinctM cdm = dm[pc.dmi];

        // for each number of pc.dmk sized sets that we can create, add new partial combinations.
        for (int e = 0; e <= dm[pc.dmi].count + pc.ldm && e * pc.dmk <= pc.k; e++) {
            int nextK = pc.k - (e * pc.dmk);
            int nextDmk = pc.dmk - 1;
            int nextDmi = pc.dmi + 1;
            long nextSize = pc.size * MathUtils.binomialCoefficient(dm[pc.dmi].count + pc.ldm, e);
            //System.out.println("e:"+e+", nextK:"+nextK+", nextDmk:"+nextDmk+", nextDmi:"+nextDmi+", nextSize:"+nextSize);

            // if nextK is zero, then this set of combinations is complete.
            if (nextK == 0) {
                result += nextSize;
                continue;
            }

            // if nextDmk is zero, then we have run out of items to place into k.
            else if (nextDmk == 0)
                continue;

            // if we are on the last distinct m, or the next distinct m is not big enough, stay at dmi.
            else if (nextDmi == dm.length || dm[nextDmi].value < nextDmk) {
                int nextLdm = pc.ldm - e;
                stack.addFirst(new PartialCombinationCount(nextK, pc.dmi, nextDmk, nextLdm, nextSize));
            }

            // we need to advance to the next dmi.
            else {
                int nextLdm = pc.ldm - e + cdm.count;
                stack.addFirst(new PartialCombinationCount(nextK, nextDmi, nextDmk, nextLdm, nextSize));
            }

        }
    }

    //System.out.println("Result: "+result);
    return result;
}

From source file:com.xiantrimble.combinatorics.CombMathUtilsImpl.java

private long p(int k, DistinctM dm) {
    long result = 0;

    // create a stack for the calculation.
    FastList<PartialCombinationCount> stack = stackFactory.object();

    // add the initial partial combination.
    // /*from ww  w .  j a v a2  s.  c o m*/
    stack.addFirst(pccFactory.object().init(k, dm, dm.m, 0, 1, 1));

    while (!stack.isEmpty()) {
        // get the next combination to expand.
        PartialCombinationCount pc = stack.removeFirst();

        //System.out.println(pc);

        // Start the expansion of this partial combination.
        // pc.k = the number of elements that still need to be added to the combination.
        // pc.dm = the next distinct m to consider.
        // pc.dmk = the size of the next combination of elements to add.
        // pc.ldm = the number of distinct unused elements to the left of mdi minus the number of distinct used elements at mdi.
        // pc.size = the number of combinations already in the solution (in k - pc.k)
        // pc.pd = the permutation count denominator.

        // get the current distinct m
        DistinctM cdm = pc.dm;
        //System.out.println(cdm);

        // if there could never be an answer, then bail out.
        if (pc.k > (cdm.count + pc.ldm) * pc.dmk + cdm.rn) {
            //System.out.println("OPTIMIZED DUE TO LACK OF ELEMENTS.");
            pccFactory.recycle(pc);
            continue;
        }

        // for each number of pc.dmk sized sets that we can create, add new partial combinations.
        for (int e = 0; e <= pc.dm.count + pc.ldm && e * pc.dmk <= pc.k; e++) {
            int nextK = pc.k - (e * pc.dmk);
            int nextDmk = pc.dmk - 1;
            long nextSize = pc.size * MathUtils.binomialCoefficient(pc.dm.count + pc.ldm, e);
            long nextPd = pc.pd * MathUtils.pow(MathUtils.factorial(pc.dmk), e);

            //System.out.println("e:"+e+", nextK:"+nextK+", nextDmk:"+nextDmk+", nextDmi:"+nextDmi+", nextSize:"+nextSize);

            // if nextK is zero, then this set of combinations is complete.
            if (nextK == 0) {
                result += (nextSize * (MathUtils.factorial(k) / nextPd));
                continue;
            }

            // if nextDmk is zero, then we have run out of items to place into k.
            else if (nextDmk == 0)
                continue;

            // if we are on the last distinct m, or the next distinct m is not big enough, stay at dmi.
            else if (pc.dm.next == null || pc.dm.next.m < nextDmk) {
                int nextLdm = pc.ldm - e;
                stack.addFirst(pccFactory.object().init(nextK, pc.dm, nextDmk, nextLdm, nextSize, nextPd));
            }

            // we need to advance to the next dmi.
            else {
                int nextLdm = pc.ldm - e + cdm.count;
                stack.addFirst(pccFactory.object().init(nextK, pc.dm.next, nextDmk, nextLdm, nextSize, nextPd));
            }
        }
        pccFactory.recycle(pc);
    }

    stackFactory.recycle(stack);

    //System.out.println("Result: "+result);
    return result;
}

From source file:no.sintef.ict.splcatool.CoveringArrayAlgICPL.java

private void generate1(int coverLimit, Integer sizelimit)
        throws TimeoutException, org.sat4j.specs.TimeoutException {
    System.out.println("--- 1-wise ---");

    List<BooleanVariableInterface> vars = new ArrayList<BooleanVariableInterface>(cnf.getFocusVariables());
    List<List<Integer>> solutions = new ArrayList<List<Integer>>(initial);

    int nrVars = vars.size();

    // Find mandatory and dead features
    Set<BooleanVariableInterface> mandatory = new HashSet<BooleanVariableInterface>();
    Set<BooleanVariableInterface> dead = new HashSet<BooleanVariableInterface>();
    MandatoryAndDeadDetection mdd = new MandatoryAndDeadDetection(cnf, nrid);
    mdd.findMandatoryAndDeadFeatures(vars, mandatory, dead);
    vars.removeAll(mandatory);/*from  w  ww  .j  av  a 2s.c o  m*/
    vars.removeAll(dead);

    // Fill invalid1w
    invalid1w = new HashSet<Pair>();
    if (coverZerosOnly && !coverOnlyOnes) {
        for (BooleanVariableInterface v : mandatory) {
            Pair p = new Pair();
            p.v = v;
            p.b = false;
            invalid1w.add(p);
        }
    }

    for (BooleanVariableInterface v : dead) {
        Pair p = new Pair();
        p.v = v;
        p.b = true;
        invalid1w.add(p);
    }

    // Cover the uncovered
    // Check uncovered
    List<Pair> uncovered = new ArrayList<Pair>();
    long alreadyCovered = 0;
    for (BooleanVariableInterface v : vars) {
        Pair p;
        if (coverZerosOnly && !coverOnlyOnes) {
            p = new Pair();
            p.b = false;
            p.v = v;
            if (!CALib.isCovered1(idnr, p, solutions))
                uncovered.add(p);
            else
                alreadyCovered++;
        }
        p = new Pair();
        p.b = true;
        p.v = v;
        if (!CALib.isCovered1(idnr, p, solutions))
            uncovered.add(p);
        else
            alreadyCovered++;
    }
    //int totaluncTotal = uncovered.size();
    /*      System.out.print("Uncovered: [");
          for(Pair p : uncovered)
             System.out.print("" + cnf.idnr.get(p.v.getID()) + ":" +p.b + ", ");
          System.out.println("]");
    */

    System.out.println("Total uncovered: " + uncovered.size());

    // Check
    if (coverZerosOnly && !coverOnlyOnes) {
        if (uncovered.size() + alreadyCovered + invalid1w.size() + mandatory.size() + dead.size() != 2
                * MathUtils.binomialCoefficient(nrVars, 1)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.out.println(
                    uncovered.size() + alreadyCovered + invalid1w.size() + mandatory.size() + dead.size());
            System.out.println(2 * MathUtils.binomialCoefficient(nrVars, 1));
            System.exit(-1);
        }
    } else {
        if (uncovered.size() + alreadyCovered + invalid1w.size() + mandatory.size() != MathUtils
                .binomialCoefficient(nrVars, 1)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.out.println(uncovered.size() + alreadyCovered + invalid1w.size() + mandatory.size());
            System.out.println(MathUtils.binomialCoefficient(nrVars, 1));
            System.exit(-1);
        }
    }

    // Find two solutions and remove covered
    {
        SAT4JSolver satSolver = null;
        try {
            satSolver = cnf.getSAT4JSolver();
            satSolver.solver.isSatisfiable();
        } catch (ContradictionException e1) {
        } catch (org.sat4j.specs.TimeoutException e) {
        }
        int[] s1 = satSolver.solver.model();
        List<Integer> solution1 = new ArrayList<Integer>();
        for (int z : s1)
            solution1.add(z);
        try {
            satSolver = cnf.getSAT4JSolverInverse();
            satSolver.solver.isSatisfiable();
        } catch (ContradictionException e1) {
        } catch (org.sat4j.specs.TimeoutException e) {
        }
        int[] s2 = satSolver.solver.model();
        List<Integer> solution2 = new ArrayList<Integer>();
        for (int z : s2)
            solution2.add(-z); // Must be inverted since sat is inverted

        solutions.add(solution1);
        solutions.add(solution2);

        List<Pair> covered = new ArrayList<Pair>();
        covered.addAll(getCovered(solution1, vars));
        covered.addAll(getCovered(solution2, vars));
        uncovered.removeAll(covered);
    }

    System.out.println("Total uncovered after first removal: " + uncovered.size());

    // Cover
    int totalunc = uncovered.size();
    List<Pair> uncSplit = new ArrayList<Pair>();
    for (int i = 0; i < uncovered.size(); i++) {
        uncSplit.add(uncovered.get(i));
    }

    C1Thread fmt = new C1Thread(cnf, uncSplit, sizelimit);
    Thread t = new Thread(fmt);

    t.start();

    // Start monitoring thread
    List<C1Thread> fmts = new ArrayList<C1Thread>();
    fmts.add(fmt);
    List<ProgressReporter> prs = new ArrayList<ProgressReporter>(fmts);
    ProgressThread pt = new ProgressThread("Cover", prs, totalunc);
    Thread ptt = new Thread(pt);
    ptt.start();

    // Wait for all threads to finish
    try {
        t.join();
    } catch (InterruptedException e) {
    }

    // Stop monitoring
    pt.stop();

    // Collect
    solutions.addAll(fmt.getSolutions());

    // For the special cases where the sizelimit is less than 3 (due to how the algorithm for 1w starts with adding 3)
    if (sizelimit == 2) {
        if (solutions.size() == 3) {
            solutions.remove(2);
        }
    }
    if (sizelimit == 1) {
        if (solutions.size() == 3) {
            solutions.remove(2);
        }
        if (solutions.size() == 2) {
            solutions.remove(1);
        }
    }

    // Done
    result = solutions;

    System.out.println("1-wise done, solutions: " + solutions.size() + ", invalid: " + invalid1w.size());

    // Write to cache
    try {
        writeToFile(fmdir.getAbsoluteFile() + "/ca1.csv");
    } catch (Exception e) {
    }
}

From source file:no.sintef.ict.splcatool.CoveringArrayAlgICPL.java

private void generate3(int coverLimit, Integer sizelimit)
        throws TimeoutException, org.sat4j.specs.TimeoutException {
    // Get a list of vars
    List<BooleanVariableInterface> vars = new ArrayList<BooleanVariableInterface>(cnf.getFocusVariables());

    // Get invalid 2-tuples
    generate2(100, sizelimit);//w w w  .jav a 2s. c  o m

    // 3-wise
    System.out.println("--- 3-wise ---");

    // Set of invalid 3-tuples
    invalid3w = new HashSet<Pair3>();

    // Solutions
    List<List<Integer>> solutions = new ArrayList<List<Integer>>(initial);
    int coveredInitially = 0;

    /* Calculate uncovered tuples */
    long invalid = 0;
    long ignored = 0;
    List<Pair3> uncovered = new ArrayList<Pair3>();

    {
        int f = vars.size();
        long total = MathUtils.binomialCoefficient(f, 3);
        if (coverOnlyOnes) {
        } else if (!coverZerosOnly) {
            total *= (2 * 2 * 2 - 1);
        } else if (firstHalfOnly || secondHalfOnly) {
            total *= 4;
        } else if (firstFourthOnly || thirdFourthOnly) {
            total *= 2;
        } else if (coverEight != 0) {
        } else {
            total *= 2 * 2 * 2;
        }
        int threads = Runtime.getRuntime().availableProcessors();
        List<CalcUncovered3Thread> cuts = new ArrayList<CalcUncovered3Thread>();
        for (int i = 0; i < threads; i++) {
            int begin = i * vars.size() / threads;
            int end = ((i + 1) * vars.size() / threads);

            CalcUncovered3Thread cut = new CalcUncovered3Thread(begin, end, vars, coverOnlyOnes, coverZerosOnly,
                    invalid2w, idnr, solutions, new HashSet<Pair3>(), firstHalfOnly, secondHalfOnly,
                    firstFourthOnly, thirdFourthOnly, coverEight);
            cuts.add(cut);
        }
        List<Thread> cutts = new ArrayList<Thread>();
        for (int i = 0; i < threads; i++) {
            cutts.add(new Thread(cuts.get(i)));
        }

        // Start threads
        for (int i = 0; i < threads; i++) {
            cutts.get(i).start();
        }

        // Monitor progress
        List<ProgressReporter> xprs = new ArrayList<ProgressReporter>();
        xprs.addAll(cuts);
        ProgressThread xpt = new ProgressThread("Calculate uncovered triples", xprs, total);
        Thread xptt = new Thread(xpt);
        xptt.start();

        // Wait
        for (int i = 0; i < threads; i++) {
            try {
                cutts.get(i).join();
            } catch (InterruptedException e1) {
            }
        }

        // Stop monitoring
        xpt.stop();

        // Gather
        for (int i = 0; i < threads; i++) {
            invalid += cuts.get(i).getInvalidCount();
            uncovered.addAll(cuts.get(i).getUncovered());
            invalid3w.addAll(cuts.get(i).getInvalid());
        }
    }

    // Done
    System.out.println(
            "Uncovered triples left: " + uncovered.size() + " invalid: " + invalid + " ignored: " + ignored);

    // Cover
    long grandTotal = uncovered.size() + invalid;
    boolean invalidRemoved = false;
    int oldcovered = uncovered.size();
    while (!uncovered.isEmpty()) {
        List<List<Integer>> sols = new ArrayList<List<Integer>>();
        int uncTotal = coveredInitially + uncovered.size();

        // Start threads
        {
            List<Pair3> uncSplit = new ArrayList<Pair3>();
            for (int i = 0; i < uncovered.size(); i++) {
                uncSplit.add(uncovered.get(i));
            }
            uncovered.clear();

            C3SplitThread fmt = new C3SplitThread(cnf, uncSplit, idnr);
            Thread t = new Thread(fmt);

            t.start();

            // Start monitoring thread
            List<C3SplitThread> fmts = new ArrayList<C3SplitThread>();
            fmts.add(fmt);
            List<ProgressReporter> prs = new ArrayList<ProgressReporter>(fmts);
            ProgressThread pt = new ProgressThread("Cover triples", prs, uncTotal);
            Thread ptt = new Thread(pt);
            ptt.start();

            // Wait for thread to finish
            try {
                t.join();
            } catch (InterruptedException e) {
            }

            // Stop monitoring
            pt.stop();

            // Round complete
            System.out.println("Round complete");
            uncovered.addAll(fmt.getUncovered());
            sols.addAll(fmt.getSolutions());

            if (saveAfterEachRound) {
                try {
                    solutions.addAll(sols);
                    result = solutions;
                    writeToFile(tmp_save_filename, Type.horizontal, tmpSave_hideUnderscoreVariables);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        // Remove covered
        int newcovered;
        Set<Pair3> cov;
        {
            cov = getCovInv3(sols, uncovered);
            System.out.println("Additionally covered " + cov.size());
            newcovered = uncovered.size();
            Set<Pair3> uncovSet = new HashSet<Pair3>(uncovered);
            uncovered.clear();
            uncovSet.removeAll(cov);
            uncovered.addAll(uncovSet);
            uncovSet.clear();
        }

        newcovered = newcovered - uncovered.size();

        // Remove invalid at some round
        if (!invalidRemoved) {
            if ((int) Math.log10(cov.size()) <= (int) Math.log10(cnf.getFocusVariables().size())) {
                System.out.println("Removing invalid");
                int diff = uncovered.size();
                uncovered = getInvalid3(coveredInitially, uncovered);
                diff -= uncovered.size();
                uncTotal -= diff;
                System.out.println("Invalid: " + diff);
                invalidRemoved = true;
            }
        }

        // Store
        solutions.addAll(sols);

        // Report progress
        System.out.println("Uncovered: " + uncovered.size() + ", progress: "
                + (grandTotal - uncovered.size()) * 100 / grandTotal + "% with solutions: " + solutions.size());

        // Stop at limit
        if (coverLimit <= (grandTotal - uncovered.size()) * 100 / grandTotal)
            break;

        // Stop at limit
        if (solutions.size() >= sizelimit)
            break;

        // Done if no more covered
        if (oldcovered == uncovered.size()) {
            System.out.println("Unable to cover valid tuples: " + uncovered.size());
            System.exit(-1);
        }
        oldcovered = uncovered.size();
    }

    result = solutions;
}

From source file:no.sintef.ict.splcatool.CoveringArrayChvatal.java

private void generate2(int coverLimit, Integer sizelimit) {
    // Get a list of vars
    List<BooleanVariableInterface> vars = new ArrayList<BooleanVariableInterface>(cnf.getVariables());
    List<List<Integer>> solutions = new ArrayList<List<Integer>>(initial);

    // Calculate uncovered tuples
    List<Pair2> uncovered = new ArrayList<Pair2>();
    List<BooleanVariableInterface> vars2 = new ArrayList<BooleanVariableInterface>(vars);
    long ignored = 0;
    long alreadyCovered = 0;
    for (BooleanVariableInterface var1 : vars) {
        vars2.remove(var1);
        for (BooleanVariableInterface var2 : vars2) {

            // Set pairs
            Pair2 unc;//from  www.ja v a2s  .  c  o  m
            if (!coverOnlyOnes) {
                if (coverZerosOnly) {
                    unc = new Pair2(idnr);
                    unc.v1 = var1;
                    unc.b1 = false;
                    unc.v2 = var2;
                    unc.b2 = false;
                    if (!CALib.isCovered(idnr, unc, solutions)) {
                        uncovered.add(unc);
                    } else {
                        alreadyCovered++;
                    }
                }
                unc = new Pair2(idnr);
                unc.v1 = var1;
                unc.b1 = false;
                unc.v2 = var2;
                unc.b2 = true;
                if (!CALib.isCovered(idnr, unc, solutions)) {
                    uncovered.add(unc);
                } else {
                    alreadyCovered++;
                }
                unc = new Pair2(idnr);
                unc.v1 = var1;
                unc.b1 = true;
                unc.v2 = var2;
                unc.b2 = false;
                if (!CALib.isCovered(idnr, unc, solutions)) {
                    uncovered.add(unc);
                } else {
                    alreadyCovered++;
                }
            }
            unc = new Pair2(idnr);
            unc.v1 = var1;
            unc.b1 = true;
            unc.v2 = var2;
            unc.b2 = true;
            if (!CALib.isCovered(idnr, unc, solutions)) {
                uncovered.add(unc);
            } else {
                alreadyCovered++;
            }
        }
    }

    System.out.println("Uncovered pairs left: " + uncovered.size());

    // If starting from a covering array or doing a cover limit, start by finding invalids
    boolean invalidRemoved = false;
    long invalids = 0;
    if (coverLimit != 100 || initial.size() > 0) {
        System.out.println(
                "Removing invalid first when given a cover limit or a size limit or an initial covering array");
        int diff = uncovered.size();
        uncovered = getInvalid(0, uncovered);
        diff -= uncovered.size();
        System.out.println("Invalid: " + diff);
        invalids = diff;
        invalidRemoved = true;
    }

    // Check
    if (coverOnlyOnes) {
        if (uncovered.size() + alreadyCovered + invalids != MathUtils.binomialCoefficient(vars.size(), 2)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    } else if (!coverZerosOnly) {
        if (uncovered.size() + alreadyCovered + invalids != 3 * MathUtils.binomialCoefficient(vars.size(), 2)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    } else {
        if (uncovered.size() + alreadyCovered + invalids != 4 * MathUtils.binomialCoefficient(vars.size(), 2)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    }

    // Get solver
    SAT4JSolver satSolver = null;
    try {
        satSolver = cnf.getSAT4JSolver();
    } catch (ContradictionException e) {
    }

    // Cover
    long grandTotal = uncovered.size() + alreadyCovered;
    while (true) {
        // Calculate coverage
        coverage = (grandTotal - uncovered.size()) * 100 / grandTotal;

        // Stop at limit
        if (invalidRemoved && coverLimit <= coverage)
            break;

        // Check for limit
        if (solutions.size() >= sizelimit)
            break;

        // Mix
        Set<Pair2> mix = new HashSet<Pair2>(uncovered);

        List<Pair2> canBeSet = new ArrayList<Pair2>();
        List<Pair2> x = new ArrayList<Pair2>(uncovered);

        // assumptions
        Set<Integer> sol = new HashSet<Integer>();

        //System.out.println("Uncovered: " +uncovered.size());
        for (int i = 0; i < x.size(); i++) {
            if (i % 1000 == 0)
                System.out.println(i + "/" + x.size());

            // Get the two pairs
            boolean b1 = x.get(i).b1;
            boolean b2 = x.get(i).b2;
            BooleanVariableInterface v1 = x.get(i).v1;
            BooleanVariableInterface v2 = x.get(i).v2;
            Pair p1 = new Pair();
            p1.v = v1;
            p1.b = b1;
            Pair p2 = new Pair();
            p2.v = v2;
            p2.b = b2;

            // Set it
            int var1nr, var2nr;
            var1nr = (b1 ? 1 : -1) * idnr.get(v1.getID());
            var2nr = (b2 ? 1 : -1) * idnr.get(v2.getID());

            // Check
            try {
                // List
                List<Integer> assumpsList = new ArrayList<Integer>();
                for (int a : sol) {
                    assumpsList.add(a);
                }
                if (assumpsList.contains(-var1nr))
                    continue;
                if (assumpsList.contains(-var2nr))
                    continue;

                assumpsList.add(var1nr);
                assumpsList.add(var2nr);

                // Convert
                int assumpsArray[] = new int[assumpsList.size()];
                int c = 0;
                for (int a : assumpsList) {
                    assumpsArray[c] = a;
                    c++;
                }
                IVecInt assumps = new VecInt(assumpsArray);

                // Check
                if (satSolver.solver.isSatisfiable(assumps)) {
                    sol.add(var1nr);
                    sol.add(var2nr);
                    canBeSet.add(x.get(i));
                    mix.remove(x.get(i));
                }
            } catch (org.sat4j.specs.TimeoutException e1) {
            }
        }

        uncovered = new ArrayList<Pair2>(mix);

        // Convert
        int asssumpsArray[] = new int[sol.size()];
        int c = 0;
        for (int a : sol) {
            asssumpsArray[c] = a;
            c++;
        }
        IVecInt assumps = new VecInt(asssumpsArray);

        try {
            satSolver.solver.isSatisfiable(assumps);
        } catch (org.sat4j.specs.TimeoutException e1) {
        }
        int[] s = satSolver.solver.model();
        List<Integer> solution = new ArrayList<Integer>();
        for (int z : s)
            solution.add(z);

        // Remove invalid at some round
        if (!invalidRemoved) {
            if ((int) Math.log10(canBeSet.size()) <= (int) Math.log10(cnf.getVariables().size())) {
                System.out.println("Removing invalid");
                int diff = uncovered.size();
                uncovered = getInvalid(0, uncovered);
                diff -= uncovered.size();
                System.out.println("Invalid: " + diff);
                invalidRemoved = true;
            }
        }

        // Calculate coverage
        coverage = (grandTotal - uncovered.size()) * 100 / grandTotal;

        // Check if done
        if (canBeSet.size() == 0) {
            System.out.println("Breaking at " + uncovered.size() + " invalids");
            break;
        } else {
            System.out.println("Covered at " + (uncovered.size() + canBeSet.size()) + ", " + canBeSet.size()
                    + ", progress: " + coverage + "%");
            //System.out.println(canBeSet);
        }

        // Return
        solutions.add(solution);
    }

    // Done
    result = solutions;
}

From source file:no.sintef.ict.splcatool.CoveringArrayChvatal.java

private void generate1(int coverLimit, Integer sizelimit)
        throws TimeoutException, org.sat4j.specs.TimeoutException {
    List<BooleanVariableInterface> vars = new ArrayList<BooleanVariableInterface>(cnf.getVariables());
    List<List<Integer>> solutions = new ArrayList<List<Integer>>(initial);

    // Cover the uncovered
    // Check uncovered
    List<Pair> uncovered = new ArrayList<Pair>();
    long alreadyCovered = 0;
    for (BooleanVariableInterface v : vars) {
        Pair p;// www  . j  a v a 2 s. c  om
        if (coverZerosOnly && !coverOnlyOnes) {
            p = new Pair();
            p.b = false;
            p.v = v;
            if (!CALib.isCovered1(idnr, p, solutions))
                uncovered.add(p);
            else
                alreadyCovered++;
        }
        p = new Pair();
        p.b = true;
        p.v = v;
        if (!CALib.isCovered1(idnr, p, solutions))
            uncovered.add(p);
        else
            alreadyCovered++;
    }

    // If starting from a covering array or doing a cover limit, start by finding invalids
    List<Pair> invalid = new ArrayList<Pair>();
    if (coverLimit != 100 || initial.size() > 0) {
        getInvalid1(uncovered, invalid);

        // Remove from uncovered
        uncovered.removeAll(invalid);
    }

    // Check
    if (coverZerosOnly && !coverOnlyOnes) {
        if (uncovered.size() + alreadyCovered + invalid.size() != 2
                * MathUtils.binomialCoefficient(vars.size(), 1)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.out.println(uncovered.size() + alreadyCovered + invalid.size());
            System.out.println(2 * MathUtils.binomialCoefficient(vars.size(), 1));
            return;
        }
    } else {
        if (uncovered.size() + alreadyCovered + invalid.size() != MathUtils.binomialCoefficient(vars.size(),
                1)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.out.println(uncovered.size() + alreadyCovered + invalid.size());
            System.out.println(MathUtils.binomialCoefficient(vars.size(), 1));
            return;
        }
    }

    // Start
    Set<Pair> mix = new HashSet<Pair>(uncovered);
    uncovered = new ArrayList<Pair>(mix);

    long grandTotal = uncovered.size() + invalid.size() + alreadyCovered;
    while (true) {
        // Calculate coverage
        coverage = (grandTotal - uncovered.size()) * 100 / grandTotal;
        // Stop at limit
        if (coverLimit <= coverage)
            break;
        // Check for limit
        if (solutions.size() >= sizelimit)
            break;

        // Mix
        mix = new HashSet<Pair>(uncovered);
        uncovered = new ArrayList<Pair>(mix);

        // Cover as many as possible
        List<Pair> canBeSet = new ArrayList<Pair>();
        List<Pair> x = new ArrayList<Pair>(uncovered);
        Set<Integer> sol = new HashSet<Integer>();

        for (int i = 0; i < x.size(); i++) {
            SAT4JSolver satSolver = null;
            try {
                satSolver = cnf.getSAT4JSolver();
            } catch (ContradictionException e) {
            }

            Pair p = x.get(i);

            // Set it
            int var1nr = (p.b ? 1 : -1) * cnf.getNr(p.v.getID());

            // Check
            try {
                // List
                List<Integer> assumpsList = new ArrayList<Integer>();
                for (int a : sol) {
                    assumpsList.add(a);
                }

                if (assumpsList.contains(-var1nr))
                    continue;

                assumpsList.add(var1nr);

                // Convert
                int assumpsArray[] = new int[assumpsList.size()];
                int c = 0;
                for (int a : assumpsList) {
                    assumpsArray[c] = a;
                    c++;
                }
                IVecInt assumps = new VecInt(assumpsArray);

                //System.out.println(assumps);

                // Check
                if (satSolver.solver.isSatisfiable(assumps)) {
                    sol.add(var1nr);
                    canBeSet.add(p);
                    uncovered.remove(p);
                    //System.out.println("sat: " + varnr);
                } else {
                    //System.out.println("unsat: " + varnr + ", " + p.v.getID());
                }

                /*if(varnr == -101){
                   System.exit(-1);
                }*/
            } catch (org.sat4j.specs.TimeoutException e1) {
            }
        }

        SAT4JSolver satSolver = null;
        try {
            satSolver = cnf.getSAT4JSolver();
        } catch (ContradictionException e) {
        }

        // Break if no more covered
        if (canBeSet.size() == 0) {
            System.out.println("Breaking at " + uncovered.size() + " invalids");
            break;
        } else {
            System.out.println("Covered at " + (uncovered.size() + canBeSet.size()) + ", " + canBeSet.size());
            //System.out.println(canBeSet);
        }

        // Convert
        int assumpsArray[] = new int[sol.size()];
        int c = 0;
        for (int a : sol) {
            assumpsArray[c] = a;
            c++;
        }
        IVecInt assumps = new VecInt(assumpsArray);

        // Solve
        try {
            satSolver.solver.isSatisfiable(assumps);
        } catch (org.sat4j.specs.TimeoutException e1) {
        }
        int[] s = satSolver.solver.model();

        List<Integer> solution = new ArrayList<Integer>();
        for (int i : s)
            solution.add(i);

        // Add solution
        solutions.add(solution);
    }

    // Done
    result = solutions;
}

From source file:no.sintef.ict.splcatool.CoveringArrayChvatal.java

private void generate3(int coverLimit, Integer sizelimit) {
    // Get a list of vars
    List<BooleanVariableInterface> vars = new ArrayList<BooleanVariableInterface>(cnf.getVariables());
    List<List<Integer>> solutions = new ArrayList<List<Integer>>(initial);

    // Calculate uncovered tuples
    List<Pair3> uncovered = new ArrayList<Pair3>();
    List<BooleanVariableInterface> vars2 = new ArrayList<BooleanVariableInterface>(vars);
    List<BooleanVariableInterface> vars3 = new ArrayList<BooleanVariableInterface>(vars);
    long ignored = 0;
    long alreadyCovered = 0;
    for (int i = 0; i < vars.size(); i++) {
        BooleanVariableInterface var1 = vars.get(i);
        for (int j = i + 1; j < vars2.size(); j++) {
            BooleanVariableInterface var2 = vars2.get(j);
            for (int k = j + 1; k < vars3.size(); k++) {
                BooleanVariableInterface var3 = vars3.get(k);

                // Set triples
                Pair3 unc;/* w w  w .  j  ava  2 s  .c o m*/
                if (!coverOnlyOnes) {
                    if (coverZerosOnly) {
                        unc = new Pair3(idnr);
                        unc.v1 = var1;
                        unc.b1 = false;
                        unc.v2 = var2;
                        unc.b2 = false;
                        unc.v3 = var3;
                        unc.b3 = false;
                        if (!CALib.isCovered3(idnr, unc, solutions))
                            uncovered.add(unc);
                        else
                            alreadyCovered++;
                    }
                    unc = new Pair3(idnr);
                    unc.v1 = var1;
                    unc.b1 = false;
                    unc.v2 = var2;
                    unc.b2 = false;
                    unc.v3 = var3;
                    unc.b3 = true;
                    if (!CALib.isCovered3(idnr, unc, solutions))
                        uncovered.add(unc);
                    else
                        alreadyCovered++;
                    unc = new Pair3(idnr);
                    unc.v1 = var1;
                    unc.b1 = false;
                    unc.v2 = var2;
                    unc.b2 = true;
                    unc.v3 = var3;
                    unc.b3 = false;
                    if (!CALib.isCovered3(idnr, unc, solutions))
                        uncovered.add(unc);
                    else
                        alreadyCovered++;
                    unc = new Pair3(idnr);
                    unc.v1 = var1;
                    unc.b1 = false;
                    unc.v2 = var2;
                    unc.b2 = true;
                    unc.v3 = var3;
                    unc.b3 = true;
                    if (!CALib.isCovered3(idnr, unc, solutions))
                        uncovered.add(unc);
                    else
                        alreadyCovered++;
                    unc = new Pair3(idnr);
                    unc.v1 = var1;
                    unc.b1 = true;
                    unc.v2 = var2;
                    unc.b2 = false;
                    unc.v3 = var3;
                    unc.b3 = false;
                    if (!CALib.isCovered3(idnr, unc, solutions))
                        uncovered.add(unc);
                    else
                        alreadyCovered++;
                    unc = new Pair3(idnr);
                    unc.v1 = var1;
                    unc.b1 = true;
                    unc.v2 = var2;
                    unc.b2 = false;
                    unc.v3 = var3;
                    unc.b3 = true;
                    if (!CALib.isCovered3(idnr, unc, solutions))
                        uncovered.add(unc);
                    else
                        alreadyCovered++;
                    unc = new Pair3(idnr);
                    unc.v1 = var1;
                    unc.b1 = true;
                    unc.v2 = var2;
                    unc.b2 = true;
                    unc.v3 = var3;
                    unc.b3 = false;
                    if (!CALib.isCovered3(idnr, unc, solutions))
                        uncovered.add(unc);
                    else
                        alreadyCovered++;
                }
                unc = new Pair3(idnr);
                unc.v1 = var1;
                unc.b1 = true;
                unc.v2 = var2;
                unc.b2 = true;
                unc.v3 = var3;
                unc.b3 = true;
                if (!CALib.isCovered3(idnr, unc, solutions))
                    uncovered.add(unc);
                else
                    alreadyCovered++;
            }
        }
    }

    System.out.println("Uncovered triples left: " + uncovered.size());

    System.out.println("alreadyCovered: " + alreadyCovered);

    System.out.println("expected: " + 8 * MathUtils.binomialCoefficient(vars.size(), 3));

    // Check
    if (coverOnlyOnes) {
        if (uncovered.size() + alreadyCovered != MathUtils.binomialCoefficient(vars.size(), 3)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    } else if (!coverZerosOnly) {
        if (uncovered.size() + alreadyCovered != 7 * MathUtils.binomialCoefficient(vars.size(), 3)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    } else {
        if (uncovered.size() + alreadyCovered != 8 * MathUtils.binomialCoefficient(vars.size(), 3)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    }

    // If starting from a covering array or doing a cover limit, start by finding invalids
    boolean invalidRemoved = false;
    long invalids = 0;
    if (coverLimit != 100 || initial.size() > 0) {
        System.out.println(
                "Removing invalid first when given a cover limit or a size limit or an initial covering array");
        int diff = uncovered.size();
        uncovered = getInvalid3(0, uncovered);
        diff -= uncovered.size();
        System.out.println("Invalid: " + diff);
        invalids = diff;
        invalidRemoved = true;
    }

    // Get solver
    SAT4JSolver satSolver = null;
    try {
        satSolver = cnf.getSAT4JSolver();
    } catch (ContradictionException e) {
    }

    // Cover
    long grandTotal = uncovered.size() + alreadyCovered;
    while (true) {
        // Calculate coverage
        coverage = (grandTotal - uncovered.size()) * 100 / grandTotal;
        // Stop at limit
        if (invalidRemoved && coverLimit <= coverage)
            break;
        // Check for limit
        if (solutions.size() >= sizelimit)
            break;

        // Mix
        Set<Pair3> mix = new HashSet<Pair3>(uncovered);

        List<Pair3> canBeSet = new ArrayList<Pair3>();
        List<Pair3> x = new ArrayList<Pair3>(uncovered);

        // assumptions
        Set<Integer> sol = new HashSet<Integer>();

        //System.out.println("Uncovered: " +uncovered.size());
        for (int i = 0; i < x.size(); i++) {
            if (i % 1000 == 0)
                System.out.println(i + "/" + x.size());

            // Get the two pairs
            boolean b1 = x.get(i).b1;
            boolean b2 = x.get(i).b2;
            boolean b3 = x.get(i).b3;
            BooleanVariableInterface v1 = x.get(i).v1;
            BooleanVariableInterface v2 = x.get(i).v2;
            BooleanVariableInterface v3 = x.get(i).v3;
            Pair p1 = new Pair();
            p1.v = v1;
            p1.b = b1;
            Pair p2 = new Pair();
            p2.v = v2;
            p2.b = b2;
            Pair p3 = new Pair();
            p3.v = v3;
            p3.b = b3;

            // Set it
            int var1nr, var2nr, var3nr;
            var1nr = (b1 ? 1 : -1) * idnr.get(v1.getID());
            var2nr = (b2 ? 1 : -1) * idnr.get(v2.getID());
            var3nr = (b3 ? 1 : -1) * idnr.get(v3.getID());

            // Check
            try {
                // List
                List<Integer> assumpsList = new ArrayList<Integer>();
                for (int a : sol) {
                    assumpsList.add(a);
                }
                if (assumpsList.contains(-var1nr))
                    continue;
                if (assumpsList.contains(-var2nr))
                    continue;
                if (assumpsList.contains(-var3nr))
                    continue;

                assumpsList.add(var1nr);
                assumpsList.add(var2nr);
                assumpsList.add(var3nr);

                // Convert
                int assumpsArray[] = new int[assumpsList.size()];
                int c = 0;
                for (int a : assumpsList) {
                    assumpsArray[c] = a;
                    c++;
                }
                IVecInt assumps = new VecInt(assumpsArray);

                // Check
                if (satSolver.solver.isSatisfiable(assumps)) {
                    sol.add(var1nr);
                    sol.add(var2nr);
                    sol.add(var3nr);
                    canBeSet.add(x.get(i));
                    mix.remove(x.get(i));
                }
            } catch (org.sat4j.specs.TimeoutException e1) {
            }
        }

        uncovered = new ArrayList<Pair3>(mix);

        // Convert
        int asssumpsArray[] = new int[sol.size()];
        int c = 0;
        for (int a : sol) {
            asssumpsArray[c] = a;
            c++;
        }
        IVecInt assumps = new VecInt(asssumpsArray);

        try {
            satSolver.solver.isSatisfiable(assumps);
        } catch (org.sat4j.specs.TimeoutException e1) {
        }
        int[] s = satSolver.solver.model();
        List<Integer> solution = new ArrayList<Integer>();
        for (int z : s)
            solution.add(z);

        // Remove invalid at some round
        if (!invalidRemoved) {
            if ((int) Math.log10(canBeSet.size()) <= (int) Math.log10(cnf.getVariables().size())) {
                System.out.println("Removing invalid");
                int diff = uncovered.size();
                uncovered = getInvalid3(0, uncovered);
                diff -= uncovered.size();
                System.out.println("Invalid: " + diff);
                invalidRemoved = true;
            }
        }

        // Check if done
        if (canBeSet.size() == 0) {
            System.out.println("Breaking at " + uncovered.size() + " invalids");
            break;
        } else {
            System.out.println("Covered at " + (uncovered.size() + canBeSet.size()) + ", " + canBeSet.size());
            //System.out.println(canBeSet);
        }

        // Return
        solutions.add(solution);
    }

    // Done
    result = solutions;
}

From source file:no.sintef.ict.splcatool.CoveringArrayChvatal.java

private void generate4(int coverLimit, Integer sizelimit) {
    // Get a list of vars
    List<BooleanVariableInterface> vars = new ArrayList<BooleanVariableInterface>(cnf.getVariables());
    List<List<Integer>> solutions = new ArrayList<List<Integer>>(initial);

    // Calculate uncovered tuples
    List<Pair4> uncovered = new ArrayList<Pair4>();
    List<BooleanVariableInterface> vars2 = new ArrayList<BooleanVariableInterface>(vars);
    List<BooleanVariableInterface> vars3 = new ArrayList<BooleanVariableInterface>(vars);
    List<BooleanVariableInterface> vars4 = new ArrayList<BooleanVariableInterface>(vars);
    long ignored = 0;
    long alreadyCovered = 0;
    for (int i = 0; i < vars.size(); i++) {
        BooleanVariableInterface var1 = vars.get(i);
        for (int j = i + 1; j < vars2.size(); j++) {
            BooleanVariableInterface var2 = vars2.get(j);
            for (int k = j + 1; k < vars3.size(); k++) {
                BooleanVariableInterface var3 = vars3.get(k);
                for (int l = k + 1; l < vars4.size(); l++) {
                    BooleanVariableInterface var4 = vars4.get(l);

                    // Set pair
                    boolean tf[] = new boolean[] { false, true };
                    for (boolean a : tf)
                        for (boolean b : tf)
                            for (boolean c : tf)
                                for (boolean d : tf) {
                                    Pair4 unc = new Pair4(idnr);
                                    unc.v1 = var1;
                                    unc.b1 = a;
                                    unc.v2 = var2;
                                    unc.b2 = b;
                                    unc.v3 = var3;
                                    unc.b3 = c;
                                    unc.v4 = var4;
                                    unc.b4 = d;
                                    if (coverOnlyOnes && (a != true || b != true || c != true || d != true))
                                        continue;
                                    if (!coverZerosOnly
                                            && (a == false && b == false && c == false && d == false))
                                        continue;
                                    if (!CALib.isCovered4(idnr, unc, solutions))
                                        uncovered.add(unc);
                                    else
                                        alreadyCovered++;
                                }/*w  ww.ja v  a  2  s  . co m*/
                }
            }
        }
    }

    System.out.println("Uncovered tuples left: " + uncovered.size());

    // Check
    if (coverOnlyOnes) {
        if (uncovered.size() + alreadyCovered != MathUtils.binomialCoefficient(vars.size(), 4)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    } else if (!coverZerosOnly) {
        if (uncovered.size() + alreadyCovered != 15 * MathUtils.binomialCoefficient(vars.size(), 4)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    } else {
        if (uncovered.size() + alreadyCovered != 16 * MathUtils.binomialCoefficient(vars.size(), 4)) {
            System.out.println("Internal error: Wrong number of tuples");
            System.exit(-1);
        }
    }

    // If starting from a covering array or doing a cover limit, start by finding invalids
    boolean invalidRemoved = false;
    List<Pair4> invalid = new ArrayList<Pair4>();
    if (coverLimit != 100 || initial.size() > 0) {
        System.out.println(
                "Removing invalid first when given a cover limit or a size limit or an initial covering array");
        getInvalid4(uncovered, invalid);
        System.out.println("Invalid: " + invalid.size());
        uncovered.removeAll(invalid);
        invalidRemoved = true;
    }

    // Get solver
    SAT4JSolver satSolver = null;
    try {
        satSolver = cnf.getSAT4JSolver();
    } catch (ContradictionException e) {
    }

    // Cover
    long grandTotal = uncovered.size() + alreadyCovered;
    while (true) {
        // Calculate coverage
        coverage = (grandTotal - uncovered.size()) * 100 / grandTotal;
        // Stop at limit
        if (invalidRemoved && coverLimit <= coverage)
            break;
        // Check for limit
        if (solutions.size() >= sizelimit)
            break;

        // Mix
        Set<Pair4> mix = new HashSet<Pair4>(uncovered);

        List<Pair4> canBeSet = new ArrayList<Pair4>();
        List<Pair4> x = new ArrayList<Pair4>(uncovered);

        // assumptions
        Set<Integer> sol = new HashSet<Integer>();

        //System.out.println("Uncovered: " +uncovered.size());
        for (int i = 0; i < x.size(); i++) {
            if (i % 1000 == 0)
                System.out.println(i + "/" + x.size());

            // Get the two pairs
            boolean b1 = x.get(i).b1;
            boolean b2 = x.get(i).b2;
            boolean b3 = x.get(i).b3;
            boolean b4 = x.get(i).b4;
            BooleanVariableInterface v1 = x.get(i).v1;
            BooleanVariableInterface v2 = x.get(i).v2;
            BooleanVariableInterface v3 = x.get(i).v3;
            BooleanVariableInterface v4 = x.get(i).v4;
            Pair p1 = new Pair();
            p1.v = v1;
            p1.b = b1;
            Pair p2 = new Pair();
            p2.v = v2;
            p2.b = b2;
            Pair p3 = new Pair();
            p3.v = v3;
            p3.b = b3;
            Pair p4 = new Pair();
            p4.v = v4;
            p4.b = b4;

            // Set it
            int var1nr, var2nr, var3nr, var4nr;
            var1nr = (b1 ? 1 : -1) * idnr.get(v1.getID());
            var2nr = (b2 ? 1 : -1) * idnr.get(v2.getID());
            var3nr = (b3 ? 1 : -1) * idnr.get(v3.getID());
            var4nr = (b4 ? 1 : -1) * idnr.get(v4.getID());

            // Check
            try {
                // List
                List<Integer> assumpsList = new ArrayList<Integer>();
                for (int a : sol) {
                    assumpsList.add(a);
                }
                if (assumpsList.contains(-var1nr))
                    continue;
                if (assumpsList.contains(-var2nr))
                    continue;
                if (assumpsList.contains(-var3nr))
                    continue;
                if (assumpsList.contains(-var4nr))
                    continue;

                if (!assumpsList.contains(var1nr))
                    assumpsList.add(var1nr);
                if (!assumpsList.contains(var2nr))
                    assumpsList.add(var2nr);
                if (!assumpsList.contains(var3nr))
                    assumpsList.add(var3nr);
                if (!assumpsList.contains(var4nr))
                    assumpsList.add(var4nr);

                // Convert
                int assumpsArray[] = new int[assumpsList.size()];
                int c = 0;
                for (int a : assumpsList) {
                    assumpsArray[c] = a;
                    c++;
                }
                IVecInt assumps = new VecInt(assumpsArray);

                //System.out.println(assumps);

                // Check
                if (satSolver.solver.isSatisfiable(assumps)) {
                    sol.add(var1nr);
                    sol.add(var2nr);
                    sol.add(var3nr);
                    sol.add(var4nr);
                    canBeSet.add(x.get(i));
                    mix.remove(x.get(i));
                }
            } catch (org.sat4j.specs.TimeoutException e1) {
            }
        }

        uncovered = new ArrayList<Pair4>(mix);

        // Convert
        int asssumpsArray[] = new int[sol.size()];
        int c = 0;
        for (int a : sol) {
            asssumpsArray[c] = a;
            c++;
        }
        IVecInt assumps = new VecInt(asssumpsArray);

        try {
            satSolver.solver.isSatisfiable(assumps);
        } catch (org.sat4j.specs.TimeoutException e1) {
        }
        int[] s = satSolver.solver.model();
        List<Integer> solution = new ArrayList<Integer>();
        for (int z : s)
            solution.add(z);

        // Check if done
        if (canBeSet.size() == 0) {
            System.out.println("Breaking at " + uncovered.size() + " invalids");
            break;
        } else {
            System.out.println("Covered at " + (uncovered.size() + canBeSet.size()) + ", " + canBeSet.size());
            //System.out.println(canBeSet);
        }

        // Return
        solutions.add(solution);
    }

    // Done
    result = solutions;
}