项目作者: pubkey

项目描述 :
A library to create, minimize and optimize binary decision diagrams
高级语言: TypeScript
项目地址: git://github.com/pubkey/binary-decision-diagram.git
创建时间: 2020-02-16T15:57:51Z
项目社区:https://github.com/pubkey/binary-decision-diagram

开源协议:Apache License 2.0

下载


binary decision diagram

A library to create, minimize and optimize binary decision diagrams in JavaScript.

A binary decision diagram is a data structure that represents a set of boolean function in an efficient way. To learn more about it, follow these links:

Installation

  1. npm install binary-decision-diagram --save

createBddFromTruthTable()

Creates a BDD from a truth table.
The Truth-Table is a Map<string, number> where the string is a truth-set like 1101 and the number is the value.

  1. const truthTable = new Map();
  2. truthTable.add('00', 1);
  3. truthTable.add('01', 3);
  4. truthTable.add('10', 2);
  5. truthTable.add('11', 1);
  6. const bdd = createBddFromTruthTable(
  7. truthTable
  8. );

minimize()

Reduces the nodes of a BDD by applying the reduction- and elimination rules.

  1. bdd.minimize(
  2. false // if true, logs stuff (optional)
  3. );

countNodes()

Returns the amount of nodes of the BDD.

  1. bdd.countNodes(); // returns a number

removeIrrelevantLeafNodes()

Removes all irrelevant leaf-nodes with the given value.

  1. // this will remove all leaf-nodes with the value of 5
  2. bdd.removeIrrelevantLeafNodes(5);

resolve()

Resolves a state by calling the boolean functions through the nodes.

The resolve-functions is an object with the truth-table-value as key and a boolean function as value.

  1. const resolvers: ResolverFunctions = {
  2. 1: (i) => true,
  3. 2: (i) => true,
  4. 3: (i) => false
  5. };
  1. const bddValue = bdd.resolve(
  2. resolvers,
  3. i // input that is passed to the resolvers
  4. ); // returns a value from the truth table

bddToMinimalString()

Returns a string-representation of the BDD which can be used in the client side to have a small javascript-bundle.
BDDs can be very big so an effective storage format was needed.

  1. const minimalString = bddToMinimalString(bdd)

minimalStringToSimpleBdd()

Parses the minimal string into an SimpleBdd. The SimpleBdd very small and only can resolve stuff.

  1. const simpleBdd = minimalStringToSimpleBdd(str);

resolveWithMinimalBdd()

Resolves a value with the SimpleBdd and the ResolverFunctions.

  1. resolveWithSimpleBdd(
  2. simpleBdd,
  3. resolvers,
  4. key
  5. );

optimizeBruteForce()

Optimizes the sorting of the boolean functions to get an optimal BDD. Returns a promise with the best found BDD.

  1. const optimizedResult = await optimizeBruteForce({
  2. truthTable,
  3. iterations: 10000,
  4. // hook that runs whenever a bdd is created (optional)
  5. afterBddCreation: (bdd: RootNode) => {
  6. bdd.removeIrrelevantLeafNodes(unknownValueActionId);
  7. },
  8. // hook that is triggered whenever a better bdd was found (optional)
  9. onBetterBdd: (res: OptimisationResult) => {
  10. const bddMinimalString = bddToMinimalString(res.bdd);
  11. console.log('new string: ' + bddMinimalString);
  12. console.log('value mapping:');
  13. console.dir(res.mapping);
  14. },
  15. // (optional) start with this BDD to optimize. If not set, will create an own one.
  16. initialBdd: myBdd
  17. });