Search in sources :

Example 1 with TableRow

use of de.neemann.digital.analyse.quinemc.TableRow in project Digital by hneemann.

the class MinimizerQuineMcCluskey method minimize.

@Override
public void minimize(List<Variable> vars, BoolTable boolTable, String resultName, ExpressionListener listener) throws ExpressionException, FormatterException {
    QuineMcCluskey qmc = createQuineMcCluskey(vars).fillTableWith(boolTable);
    PrimeSelector ps = new PrimeSelectorDefault();
    Expression e = qmc.simplify(ps).getExpression();
    if (ps.getAllSolutions() != null) {
        for (ArrayList<TableRow> i : ps.getAllSolutions()) {
            listener.resultFound(resultName, QuineMcCluskey.addAnd(null, i, vars));
        }
    } else {
        listener.resultFound(resultName, e);
    }
}
Also used : QuineMcCluskey(de.neemann.digital.analyse.quinemc.QuineMcCluskey) PrimeSelectorDefault(de.neemann.digital.analyse.quinemc.primeselector.PrimeSelectorDefault) Expression(de.neemann.digital.analyse.expression.Expression) TableRow(de.neemann.digital.analyse.quinemc.TableRow) PrimeSelector(de.neemann.digital.analyse.quinemc.primeselector.PrimeSelector)

Example 2 with TableRow

use of de.neemann.digital.analyse.quinemc.TableRow in project Digital by hneemann.

the class BruteForceGetAllTest method performTestCalculation.

/*
    public void testFull() throws ExpressionException, FormatterException {
        new FullVariantDontCareCreator(4) {
            @Override
            public void handleTable(int n, int[] tab) throws ExpressionException {
                performTestCalculation(n, tab);
            }
        }.create();
    } /**/
private static void performTestCalculation(int n, byte[] tab) throws ExpressionException {
    BruteForceGetAll ps = new BruteForceGetAll();
    ArrayList<Variable> v = vars(n);
    new QuineMcCluskey(v).fillTableWith(new BoolTableByteArray(tab)).simplify(ps);
    ArrayList<ArrayList<TableRow>> solutions = ps.getAllSolutions();
    if (solutions != null) {
        for (ArrayList<TableRow> sol : solutions) {
            Expression e = QuineMcCluskey.addAnd(null, sol, v);
            ContextFiller context = new ContextFiller(v);
            for (int i = 0; i < tab.length; i++) {
                if (tab[i] <= 1) {
                    assertEquals(tab[i] == 1, e.calculate(context.setContextTo(i)));
                }
            }
        }
    }
}
Also used : QuineMcCluskey(de.neemann.digital.analyse.quinemc.QuineMcCluskey) Variable(de.neemann.digital.analyse.expression.Variable) BoolTableByteArray(de.neemann.digital.analyse.quinemc.BoolTableByteArray) Expression(de.neemann.digital.analyse.expression.Expression) ContextFiller(de.neemann.digital.analyse.expression.ContextFiller) TableRow(de.neemann.digital.analyse.quinemc.TableRow) ArrayList(java.util.ArrayList)

Example 3 with TableRow

use of de.neemann.digital.analyse.quinemc.TableRow in project Digital by hneemann.

the class BruteForce method select.

@Override
public void select(ArrayList<TableRow> primes, ArrayList<TableRow> primesAvail, TreeSet<Integer> termIndices) {
    if (primesAvail.size() > 31)
        throw new RuntimeException("to many primes");
    int comb = 1 << primesAvail.size();
    ArrayList<Integer> list = new ArrayList<>(comb);
    for (int i = 1; i < comb; i++) {
        list.add(i);
    }
    Collections.sort(list, (i1, i2) -> Integer.bitCount(i1) - Integer.bitCount(i2));
    ArrayList<Integer> l = new ArrayList<>();
    for (int mask : list) {
        l.addAll(termIndices);
        int m = mask;
        for (TableRow aPrimesAvail : primesAvail) {
            if ((m & 1) > 0) {
                l.removeAll(aPrimesAvail.getSource());
            }
            m >>= 1;
        }
        if (l.isEmpty()) {
            m = mask;
            for (TableRow aPrime : primesAvail) {
                if ((m & 1) > 0) {
                    primes.add(aPrime);
                }
                m >>= 1;
            }
            return;
        } else {
            l.clear();
        }
    }
    throw new RuntimeException("BruteForce Error!");
}
Also used : TableRow(de.neemann.digital.analyse.quinemc.TableRow) ArrayList(java.util.ArrayList)

Example 4 with TableRow

use of de.neemann.digital.analyse.quinemc.TableRow in project Digital by hneemann.

the class BruteForceGetAll method select.

@Override
public void select(ArrayList<TableRow> primes, ArrayList<TableRow> primesAvail, TreeSet<Integer> termIndices) {
    if (primesAvail.size() > 31)
        throw new RuntimeException("to many primes");
    int comb = 1 << primesAvail.size();
    ArrayList<Integer> list = new ArrayList<>(comb);
    for (int i = 1; i < comb; i++) {
        list.add(i);
    }
    Collections.sort(list, (i1, i2) -> Integer.bitCount(i1) - Integer.bitCount(i2));
    int primesUsed = 0;
    foundSolutions = new ArrayList<>();
    ArrayList<Integer> indicesOpen = new ArrayList<>();
    for (int mask : list) {
        if (primesUsed != 0 && Integer.bitCount(mask) > primesUsed)
            break;
        indicesOpen.clear();
        indicesOpen.addAll(termIndices);
        int m = mask;
        for (TableRow aPrimesAvail : primesAvail) {
            if ((m & 1) > 0) {
                indicesOpen.removeAll(aPrimesAvail.getSource());
            }
            m >>= 1;
        }
        if (indicesOpen.isEmpty()) {
            primesUsed = Integer.bitCount(mask);
            ArrayList<TableRow> singleSolution = new ArrayList<>(primes);
            m = mask;
            for (TableRow aPrime : primesAvail) {
                if ((m & 1) > 0) {
                    singleSolution.add(aPrime);
                }
                m >>= 1;
            }
            foundSolutions.add(singleSolution);
        }
    }
    primes.clear();
    primes.addAll(foundSolutions.get(0));
}
Also used : TableRow(de.neemann.digital.analyse.quinemc.TableRow) ArrayList(java.util.ArrayList)

Example 5 with TableRow

use of de.neemann.digital.analyse.quinemc.TableRow in project Digital by hneemann.

the class LargestFirst method select.

@Override
public void select(ArrayList<TableRow> primes, ArrayList<TableRow> primesAvail, TreeSet<Integer> termIndices) {
    while (!termIndices.isEmpty()) {
        TableRow bestRow = null;
        int maxCount = 0;
        for (TableRow tr : primesAvail) {
            int count = 0;
            for (int i : tr.getSource()) {
                if (termIndices.contains(i))
                    count++;
            }
            if (count > maxCount) {
                maxCount = count;
                bestRow = tr;
            }
        }
        primes.add(bestRow);
        primesAvail.remove(bestRow);
        termIndices.removeAll(bestRow.getSource());
    }
}
Also used : TableRow(de.neemann.digital.analyse.quinemc.TableRow)

Aggregations

TableRow (de.neemann.digital.analyse.quinemc.TableRow)5 ArrayList (java.util.ArrayList)3 Expression (de.neemann.digital.analyse.expression.Expression)2 QuineMcCluskey (de.neemann.digital.analyse.quinemc.QuineMcCluskey)2 ContextFiller (de.neemann.digital.analyse.expression.ContextFiller)1 Variable (de.neemann.digital.analyse.expression.Variable)1 BoolTableByteArray (de.neemann.digital.analyse.quinemc.BoolTableByteArray)1 PrimeSelector (de.neemann.digital.analyse.quinemc.primeselector.PrimeSelector)1 PrimeSelectorDefault (de.neemann.digital.analyse.quinemc.primeselector.PrimeSelectorDefault)1