def espresso_tts(*tts): """Return a tuple of expressions optimized using Espresso.""" for f in tts: if not isinstance(f, TruthTable): raise ValueError("expected a TruthTable instance") support = frozenset.union(*[f.support for f in tts]) inputs = sorted(support) ninputs = len(inputs) noutputs = len(tts) cover = set() for i, point in enumerate(boolfunc.iter_points(inputs)): invec = [2 if point[v] else 1 for v in inputs] outvec = list() for f in tts: val = f.pcdata[i] if val == PC_ZERO: outvec.append(0) elif val == PC_ONE: outvec.append(1) elif val == PC_DC: outvec.append(2) else: raise ValueError("expected truth table entry in {0, 1, -}") cover.add((tuple(invec), tuple(outvec))) set_config(**CONFIG) cover = espresso(ninputs, noutputs, cover, intype=FTYPE|DTYPE|RTYPE) inputs = [exprvar(v.names, v.indices) for v in inputs] return _cover2exprs(inputs, noutputs, cover)
def _do_espresso(fname): fpath = os.path.join('thirdparty', 'espresso', 'test', 'bb_all', fname) with open(fpath) as fin: d = pla.parse(fin.read()) return espresso.espresso(d['ninputs'], d['noutputs'], d['cover'], intype=d['intype'])
def espresso_exprs(*exprs): """Return a tuple of expressions optimized using Espresso. The variadic *exprs* argument is a sequence of expressions. For example:: >>> from pyeda.boolalg.expr import exprvar >>> a, b, c = map(exprvar, 'abc') >>> f1 = ~a & ~b & ~c | ~a & ~b & c | a & ~b & c | a & b & c | a & b & ~c >>> f2 = f2 = ~a & ~b & c | a & ~b & c >>> f1m, f2m = espresso_exprs(f1, f2) >>> f1m Or(And(~a, ~b), And(a, b), And(~b, c)) >>> f2m And(~b, c) """ for f in exprs: if not (isinstance(f, Expression) and f.is_dnf()): raise ValueError("expected a DNF expression") support = frozenset.union(*[f.support for f in exprs]) inputs = sorted(support) # Gather all cubes in the cover of input functions fscover = set() for f in exprs: fscover.update(f.cover) ninputs = len(inputs) noutputs = len(exprs) cover = set() for fscube in fscover: invec = list() for v in inputs: if ~v in fscube: invec.append(1) elif v in fscube: invec.append(2) else: invec.append(3) outvec = list() for f in exprs: for fcube in f.cover: if fcube <= fscube: outvec.append(1) break else: outvec.append(0) cover.add((tuple(invec), tuple(outvec))) set_config(**CONFIG) cover = espresso(ninputs, noutputs, cover, intype=FTYPE) return _cover2exprs(inputs, noutputs, cover)
def espresso_tts(*tts): """Return a tuple of expressions optimized using Espresso. The variadic *tts* argument is a sequence of truth tables. For example:: >>> from pyeda.boolalg.bfarray import exprvars >>> from pyeda.boolalg.table import truthtable >>> X = exprvars('x', 4) >>> f1 = truthtable(X, "0000011111------") >>> f2 = truthtable(X, "0001111100------") >>> f1m, f2m = espresso_tts(f1, f2) >>> f1m.equivalent(X[3] | X[0] & X[2] | X[1] & X[2]) True >>> f2m.equivalent(X[2] | X[0] & X[1]) True """ for f in tts: if not isinstance(f, TruthTable): raise ValueError("expected a TruthTable instance") support = frozenset.union(*[f.support for f in tts]) inputs = sorted(support) ninputs = len(inputs) noutputs = len(tts) cover = set() for i, point in enumerate(boolfunc.iter_points(inputs)): invec = [2 if point[v] else 1 for v in inputs] outvec = list() for f in tts: val = f.pcdata[i] if val == PC_ZERO: outvec.append(0) elif val == PC_ONE: outvec.append(1) elif val == PC_DC: outvec.append(2) else: raise ValueError("expected truth table entry in {0, 1, -}") cover.add((tuple(invec), tuple(outvec))) set_config(**CONFIG) cover = espresso(ninputs, noutputs, cover, intype=FTYPE|DTYPE|RTYPE) inputs = [exprvar(v.names, v.indices) for v in inputs] return _cover2exprs(inputs, noutputs, cover)
def espresso_tts(*tts): """Return a tuple of expressions optimized using Espresso. The variadic *tts* argument is a sequence of truth tables. For example:: >>> from pyeda.boolalg.bfarray import exprvars >>> from pyeda.boolalg.table import truthtable >>> X = exprvars('x', 4) >>> f1 = truthtable(X, "0000011111------") >>> f2 = truthtable(X, "0001111100------") >>> f1m, f2m = espresso_tts(f1, f2) >>> f1m.equivalent(X[3] | X[0] & X[2] | X[1] & X[2]) True >>> f2m.equivalent(X[2] | X[0] & X[1]) True """ for f in tts: if not isinstance(f, TruthTable): raise ValueError("expected a TruthTable instance") support = frozenset.union(*[f.support for f in tts]) inputs = sorted(support) ninputs = len(inputs) noutputs = len(tts) cover = set() for i, point in enumerate(boolfunc.iter_points(inputs)): invec = [2 if point[v] else 1 for v in inputs] outvec = list() for f in tts: val = f.pcdata[i] if val == PC_ZERO: outvec.append(0) elif val == PC_ONE: outvec.append(1) elif val == PC_DC: outvec.append(2) else: raise ValueError("expected truth table entry in {0, 1, -}") cover.add((tuple(invec), tuple(outvec))) set_config(**CONFIG) cover = espresso(ninputs, noutputs, cover, intype=FTYPE | DTYPE | RTYPE) inputs = [exprvar(v.names, v.indices) for v in inputs] return _cover2exprs(inputs, noutputs, cover)
def espresso_exprs(*exprs): """Return a tuple of expressions optimized using Espresso.""" for f in exprs: if not (isinstance(f, Expression) and f.is_dnf()): raise ValueError("expected a DNF expression") support = frozenset.union(*[f.support for f in exprs]) inputs = sorted(support) # Gather all cubes in the cover of input functions fscover = set() for f in exprs: if isinstance(f, ExprLiteral) or isinstance(f, ExprAnd): fscover.add(f.cube) else: fscover.update(f.cover) ninputs = len(inputs) noutputs = len(exprs) cover = set() for fscube in fscover: invec = list() for v in inputs: if ~v in fscube: invec.append(1) elif v in fscube: invec.append(2) else: invec.append(3) outvec = list() for f in exprs: for fcube in f.cover: if fcube <= fscube: outvec.append(1) break else: outvec.append(0) cover.add((tuple(invec), tuple(outvec))) set_config(**CONFIG) cover = espresso(ninputs, noutputs, cover, intype=FTYPE) return _cover2exprs(inputs, noutputs, cover)
def main(fin, fout): opts = PARSER.parse_args() espresso.set_config( single_expand=opts.fast, remove_essential=opts.ess, force_irredundant=opts.irr, unwrap_onset=opts.unwrap, recompute_onset=opts.onset, use_super_gasp=opts.strong, ) try: f = open(fin, "r") d = pla.parse(f.read()) f.close() except pla.Error as exc: print("error parsing file:", opts.file.name) print(exc) return 1 try: cover = espresso.espresso(d['ninputs'], d['noutputs'], d['cover'], intype=d['intype']) except espresso.Error as exc: print("espresso failed:", exc) return 1 f = open(fout, "w") print(".i", d['ninputs'], file=f) print(".o", d['noutputs'], file=f) if d['input_labels']: print(".ilb", " ".join(d['input_labels']), file=f) if d['output_labels']: print(".ob", " ".join(d['output_labels']), file=f) print(".p", len(cover), file=f) for invec, outvec in cover: print("".join({1: '0', 2: '1', 3: '-'}[n] for n in invec), "".join({0: '0', 1: '1', 2: '-'}[n] for n in outvec), file=f) print(".e", file=f) f.close() return 0
def espresso_exprs(*exprs): """Return a tuple of expressions optimized using Espresso.""" support = frozenset.union(*[f.support for f in exprs]) inputs = sorted(support) # Gather all cubes in the cover of input functions fscover = set() for f in exprs: if not (isinstance(f, Expression) and f.is_dnf()): raise ValueError("expected a DNF expression, got " + str(f)) fscover |= f.cover num_inputs = len(inputs) num_outputs = len(exprs) cover = set() for fscube in fscover: invec = list() for v in inputs: if ~v in fscube: invec.append(1) elif v in fscube: invec.append(2) else: invec.append(3) outvec = list() for f in exprs: for fcube in f.cover: if fcube <= fscube: outvec.append(1) break else: outvec.append(0) cover.add((tuple(invec), tuple(outvec))) cover = espresso(num_inputs, num_outputs, cover, intype=FTYPE) return _cover2exprs(inputs, num_outputs, cover)
def espresso_exprs(*exprs): """Return a tuple of expressions optimized using Espresso. The variadic *exprs* argument is a sequence of expressions. For example:: >>> from pyeda.boolalg.expr import exprvar >>> a, b, c = map(exprvar, 'abc') >>> f1 = Or(And(~a, ~b, ~c), And(~a, ~b, c), And(a, ~b, c), And(a, b, c), And(a, b, ~c)) >>> f2 = Or(And(~a, ~b, c), And(a, ~b, c)) >>> f1m, f2m = espresso_exprs(f1, f2) >>> f1.size, f1m.size (21, 10) >>> f1m.equivalent(f1) True >>> f2.size, f2m.size (9, 3) >>> f2m.equivalent(f2) True """ for f in exprs: if not (isinstance(f, Expression) and f.is_dnf()): raise ValueError("expected a DNF expression") support = frozenset.union(*[f.support for f in exprs]) inputs = sorted(support) # Gather all cubes in the cover of input functions fscover = set() for f in exprs: fscover.update(f.cover) ninputs = len(inputs) noutputs = len(exprs) cover = set() for fscube in fscover: invec = list() for v in inputs: if ~v in fscube: invec.append(1) elif v in fscube: invec.append(2) else: invec.append(3) outvec = list() for f in exprs: for fcube in f.cover: if fcube <= fscube: outvec.append(1) break else: outvec.append(0) cover.add((tuple(invec), tuple(outvec))) set_config(**CONFIG) cover = espresso(ninputs, noutputs, cover, intype=FTYPE) return _cover2exprs(inputs, noutputs, cover)
def _do_espresso(fname): fpath = os.path.join('extension', 'espresso', 'test', 'bb_all', fname) with open(fpath) as fin: pla = parse_pla(fin.read()) return espresso.espresso(pla['ninputs'], pla['noutputs'], pla['cover'], intype=pla['intype'])