@@ -6,7 +6,11 @@ import {
66 fillTruthTable ,
77 optimizeBruteForce ,
88 OptimisationResult ,
9- RootNode
9+ RootNode ,
10+ resolveWithSimpleBdd ,
11+ ResolverFunctions ,
12+ booleanStringToBoolean ,
13+ minimalStringToSimpleBdd
1014} from 'binary-decision-diagram' ;
1115
1216import type { StateActionIdMap } from './types.js' ;
@@ -33,6 +37,7 @@ import {
3337 getBetterBdd ,
3438 getQualityOfBdd
3539} from './calculate-bdd-quality.js' ;
40+ import { orderedStateList } from '../states/index.js' ;
3641
3742/**
3843 * sort object attributes
@@ -48,6 +53,14 @@ export function sortObject<T>(obj: T): T {
4853 } ) , { } ) as T ;
4954}
5055
56+ function loadTruthTable ( ) {
57+ const truthTable : TruthTable = objectToMap (
58+ readJsonFile ( OUTPUT_TRUTH_TABLE_PATH )
59+ ) ;
60+ return truthTable ;
61+ }
62+
63+
5164const unknownValueActionId : number = 42 ;
5265
5366async function run ( ) {
@@ -109,11 +122,6 @@ async function run() {
109122 let totalAmountOfHandled = 0 ;
110123 let totalAmountOfOptimized = 0 ;
111124
112- function loadTruthTable ( ) : StateActionIdMap {
113- return objectToMap (
114- readJsonFile ( OUTPUT_TRUTH_TABLE_PATH )
115- ) ;
116- }
117125 let truthTable : StateActionIdMap = loadTruthTable ( ) ;
118126 const startTruthTableEntries = truthTable . size ;
119127
@@ -213,9 +221,7 @@ async function run() {
213221 case 'create-bdd' :
214222 ( async function createBdd ( ) {
215223 console . log ( 'read table..' ) ;
216- const truthTable : TruthTable = objectToMap (
217- readJsonFile ( OUTPUT_TRUTH_TABLE_PATH )
218- ) ;
224+ const truthTable = loadTruthTable ( ) ;
219225 console . log ( 'table size: ' + truthTable . size ) ;
220226
221227 // fill missing rows with unknown
@@ -226,6 +232,7 @@ async function run() {
226232 ) ;
227233
228234 console . log ( 'create bdd..' ) ;
235+
229236 const bdd = createBddFromTruthTable ( truthTable ) ;
230237 console . log ( 'mimizing..' ) ;
231238 console . log ( 'remove unkown states..' ) ;
@@ -248,9 +255,7 @@ async function run() {
248255 ( async function optimizeBdd ( ) {
249256 console . log ( 'read table..' ) ;
250257 let lastBetterFoundTime = new Date ( ) . getTime ( ) ;
251- const truthTable : TruthTable = objectToMap (
252- readJsonFile ( OUTPUT_TRUTH_TABLE_PATH )
253- ) ;
258+ const truthTable = loadTruthTable ( ) ;
254259 console . log ( 'table size: ' + truthTable . size ) ;
255260
256261 // fill missing rows with unknown
@@ -267,6 +272,12 @@ async function run() {
267272 console . dir ( perfMeasurement ) ;
268273
269274
275+ const resolvers : ResolverFunctions = { } ;
276+ new Array ( orderedStateList . length ) . fill ( 0 ) . forEach ( ( _x , index ) => {
277+ const fn = ( state : string ) => booleanStringToBoolean ( ( state as any ) [ index ] ) ;
278+ resolvers [ index ] = fn ;
279+ } ) ;
280+
270281 function getQuality ( bdd : RootNode ) {
271282 return getQualityOfBdd (
272283 bdd ,
@@ -280,12 +291,29 @@ async function run() {
280291 truthTable,
281292 iterations : 10000000 ,
282293 afterBddCreation : ( bdd : RootNode ) => {
283-
284294 const lastBetterAgo = new Date ( ) . getTime ( ) - lastBetterFoundTime ;
285295 const lastBetterHours = lastBetterAgo / 1000 / 60 / 60 ;
286296 console . log ( 'Last better bdd found ' + roundToTwoDecimals ( lastBetterHours ) + 'hours ago' ) ;
287-
288297 bdd . removeIrrelevantLeafNodes ( unknownValueActionId ) ;
298+
299+ // ensure correctness to have a double-check that the bdd works correctly
300+ const bddMinimalString = bddToMinimalString ( bdd ) ;
301+ const simpleBdd = minimalStringToSimpleBdd ( bddMinimalString ) ;
302+ for ( const [ key , value ] of loadTruthTable ( ) . entries ( ) ) {
303+ const bddValue = resolveWithSimpleBdd (
304+ simpleBdd ,
305+ resolvers ,
306+ key
307+ ) ;
308+
309+ if ( value !== bddValue ) {
310+ console . error ( '# Error: minimalBdd has different value compared to truth table ' + key ) ;
311+ console . dir ( { value, bddValue } ) ;
312+ process . exit ( - 1 ) ;
313+ }
314+ }
315+
316+
289317 if ( currentBest ) {
290318 console . log (
291319 'current best bdd has ' + currentBest . countNodes ( ) + ' nodes ' +
0 commit comments