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);
}
}
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)));
}
}
}
}
}
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!");
}
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));
}
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());
}
}
Aggregations