nysol-zdd 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/ext/zdd_so/BDD.cc +495 -0
  3. data/ext/zdd_so/BDD.h +356 -0
  4. data/ext/zdd_so/BDDDG.cc +1818 -0
  5. data/ext/zdd_so/BDDDG.h +107 -0
  6. data/ext/zdd_so/BDDHASH.cc +91 -0
  7. data/ext/zdd_so/BtoI.cc +503 -0
  8. data/ext/zdd_so/BtoI.h +144 -0
  9. data/ext/zdd_so/CtoI.cc +1072 -0
  10. data/ext/zdd_so/CtoI.h +186 -0
  11. data/ext/zdd_so/MLZBDDV.cc +153 -0
  12. data/ext/zdd_so/MLZBDDV.h +42 -0
  13. data/ext/zdd_so/SOP.cc +608 -0
  14. data/ext/zdd_so/SOP.h +199 -0
  15. data/ext/zdd_so/ZBDD.cc +1035 -0
  16. data/ext/zdd_so/ZBDD.h +243 -0
  17. data/ext/zdd_so/ZBDDDG.cc +1834 -0
  18. data/ext/zdd_so/ZBDDDG.h +105 -0
  19. data/ext/zdd_so/ZBDDHASH.cc +91 -0
  20. data/ext/zdd_so/bddc.c +2816 -0
  21. data/ext/zdd_so/bddc.h +132 -0
  22. data/ext/zdd_so/extconf.rb +25 -0
  23. data/ext/zdd_so/include/aheap.c +211 -0
  24. data/ext/zdd_so/include/aheap.h +111 -0
  25. data/ext/zdd_so/include/base.c +93 -0
  26. data/ext/zdd_so/include/base.h +60 -0
  27. data/ext/zdd_so/include/itemset.c +473 -0
  28. data/ext/zdd_so/include/itemset.h +153 -0
  29. data/ext/zdd_so/include/problem.c +371 -0
  30. data/ext/zdd_so/include/problem.h +160 -0
  31. data/ext/zdd_so/include/queue.c +518 -0
  32. data/ext/zdd_so/include/queue.h +177 -0
  33. data/ext/zdd_so/include/sgraph.c +331 -0
  34. data/ext/zdd_so/include/sgraph.h +170 -0
  35. data/ext/zdd_so/include/stdlib2.c +832 -0
  36. data/ext/zdd_so/include/stdlib2.h +746 -0
  37. data/ext/zdd_so/include/trsact.c +723 -0
  38. data/ext/zdd_so/include/trsact.h +167 -0
  39. data/ext/zdd_so/include/vec.c +583 -0
  40. data/ext/zdd_so/include/vec.h +159 -0
  41. data/ext/zdd_so/lcm-vsop.cc +596 -0
  42. data/ext/zdd_so/print.cc +683 -0
  43. data/ext/zdd_so/table.cc +330 -0
  44. data/ext/zdd_so/vsop.h +88 -0
  45. data/ext/zdd_so/zdd_so.cpp +3277 -0
  46. data/lib/nysol/zdd.rb +31 -0
  47. metadata +131 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d53e12aa556fd36af5736b48309d53c3f896fb7e
4
+ data.tar.gz: bc5f23437b48faf2ff366e682f7c8583e7cab707
5
+ SHA512:
6
+ metadata.gz: f38af417709a0c84bb44ae22baac62fc9a3af3444117fc30cce3bbf161700a28a3d283d17a51ee9d44544ac401e08745940e2eab335752d3e451c72b10c1554a
7
+ data.tar.gz: c811d9afc67cc8be3cca26a0777049956d94fa68af98e642305c25a49b76fb2377e71592d71ef8e0db67e8c1bf5c16caf336c6f918db0510c4189164df9bec29
@@ -0,0 +1,495 @@
1
+ /***************************************
2
+ * BDD+ Manipulator (SAPPORO-1.70) *
3
+ * (Basic methods) *
4
+ * (C) Shin-ichi MINATO (Sep. 16, 2015) *
5
+ ****************************************/
6
+
7
+ #include "BDD.h"
8
+
9
+ using std::cout;
10
+ using std::cerr;
11
+
12
+ static const char BC_Smooth = 60;
13
+ static const char BC_Spread = 61;
14
+
15
+ extern "C"
16
+ {
17
+ int rand();
18
+ };
19
+
20
+
21
+ //--- SBDD class for default initialization ----
22
+
23
+ int BDDV_Active = 0;
24
+
25
+ class SBDD
26
+ {
27
+ public:
28
+ SBDD(bddword init, bddword limit) { bddinit(init, limit); }
29
+ };
30
+ static SBDD BDD_Manager((bddword)256, (bddword)1024);
31
+
32
+
33
+ //----- External constant data for BDD -------
34
+
35
+ const bddword BDD_MaxNode = B_VAL_MASK >> 1U;
36
+ const int BDD_MaxVar = bddvarmax;
37
+
38
+ //-------------- class BDD --------------------
39
+
40
+ bddword BDD::Size() const { return bddsize(_bdd); }
41
+
42
+ void BDD::Export(FILE *strm) const
43
+ {
44
+ bddword p = _bdd;
45
+ bddexport(strm, &p, 1);
46
+ }
47
+
48
+ void BDD::Print() const
49
+ {
50
+ cout << "[ " << GetID();
51
+ cout << " Var:" << Top() << "(" << BDD_LevOfVar(Top()) << ")";
52
+ cout << " Size:" << Size() << " ]\n";
53
+ cout.flush();
54
+ }
55
+
56
+ BDD BDD::Swap(const int& v1, const int& v2) const
57
+ {
58
+ if(v1 == v2) return *this;
59
+ BDD x = BDDvar(v1);
60
+ BDD y = BDDvar(v2);
61
+ BDD fx0 = At0(v1);
62
+ BDD fx1 = At1(v1);
63
+ return x & ( ~y & fx0.At1(v2) | y & fx1.At1(v2) ) |
64
+ ~x & ( ~y & fx0.At0(v2) | y & fx1.At0(v2) );
65
+ }
66
+
67
+ #define BDD_CACHE_CHK_RETURN(op, fx, gx) \
68
+ { BDD h = BDD_CacheBDD(op, fx, gx); \
69
+ if(h != -1) return h; \
70
+ BDD_RECUR_INC; }
71
+
72
+ #define BDD_CACHE_ENT_RETURN(op, fx, gx, h) \
73
+ { BDD_RECUR_DEC; \
74
+ if(h != -1) BDD_CacheEnt(op, fx, gx, h.GetID()); \
75
+ return h; }
76
+
77
+ BDD BDD::Smooth(const int& v) const
78
+ {
79
+ int t = Top();
80
+ if(t == 0) return *this;
81
+ if(BDD_LevOfVar(t) <= BDD_LevOfVar(v)) return 1;
82
+ bddword fx = GetID();
83
+ bddword gx = BDDvar(v).GetID();
84
+ BDD_CACHE_CHK_RETURN(BC_Smooth, fx, gx);
85
+ BDD x = BDDvar(t);
86
+ BDD h = (~x & At0(t).Smooth(v))|(x & At1(t).Smooth(v));
87
+ BDD_CACHE_ENT_RETURN(BC_Smooth, fx, gx, h);
88
+ }
89
+
90
+ BDD BDD::Spread(const int& k) const
91
+ {
92
+ int t = Top();
93
+ if(t == 0) return *this;
94
+ if(k == 0) return *this;
95
+ if(k < 0) BDDerr("BDD::Spread: k < 0.",k);
96
+ bddword fx = GetID();
97
+ bddword gx = BDDvar(k).GetID();
98
+ BDD_CACHE_CHK_RETURN(BC_Spread, fx, gx);
99
+ BDD x = BDDvar(t);
100
+ BDD f0 = At0(t);
101
+ BDD f1 = At1(t);
102
+ BDD h = (~x & f0.Spread(k)) | (x & f1.Spread(k))
103
+ | (~x & f1.Spread(k-1)) | (x & f0.Spread(k-1));
104
+ BDD_CACHE_ENT_RETURN(BC_Spread, fx, gx, h);
105
+ }
106
+
107
+ //----- External functions for BDD -------
108
+
109
+ void BDD_Init(bddword init, bddword limit)
110
+ {
111
+ bddinit(init, limit);
112
+ BDDV_Active = 0;
113
+ }
114
+
115
+ int BDD_NewVarOfLev(int lev)
116
+ {
117
+ if(lev > BDD_TopLev() + 1)
118
+ BDDerr("BDD_NewVarOfLev:Invald lev ", (bddword)lev);
119
+ return bddnewvaroflev(lev);
120
+ }
121
+
122
+ int BDD_VarUsed(void) { return bddvarused(); }
123
+
124
+ bddword BDD_Used(void) { return bddused(); }
125
+
126
+ void BDD_GC() { bddgc(); }
127
+
128
+ BDD BDD_Import(FILE *strm)
129
+ {
130
+ bddword bdd;
131
+ if(bddimport(strm, &bdd, 1)) return -1;
132
+ return BDD_ID(bdd);
133
+ }
134
+
135
+ BDD BDD_Random(int level, int density)
136
+ {
137
+ if(level < 0)
138
+ BDDerr("BDD_Random: level < 0.",level);
139
+ if(level == 0) return ((rand()%100) < density)? 1: 0;
140
+ return (BDDvar(BDD_VarOfLev(level))
141
+ & BDD_Random(level-1, density)) |
142
+ (~BDDvar(BDD_VarOfLev(level))
143
+ & BDD_Random(level-1, density));
144
+ }
145
+
146
+ void BDDerr(const char* msg)
147
+ {
148
+ cerr << "<ERROR> " << msg << " \n";
149
+ exit(1);
150
+ }
151
+
152
+ void BDDerr(const char* msg, bddword key)
153
+ {
154
+ cerr << "<ERROR> " << msg << " (" << key << ")\n";
155
+ exit(1);
156
+ }
157
+
158
+ void BDDerr(const char* msg, const char* name)
159
+ {
160
+ cerr << "<ERROR> " << msg << " (" << name << ")\n";
161
+ exit(1);
162
+ }
163
+
164
+
165
+ //----- External constant data for BDDV -------
166
+
167
+ const int BDDV_SysVarTop = 20;
168
+ const int BDDV_MaxLen = 1 << BDDV_SysVarTop;
169
+ const int BDDV_MaxLenImport = 1000;
170
+
171
+
172
+ //--------------- class BDDV ------------------------
173
+
174
+ BDDV::BDDV(const BDD& f, int len)
175
+ {
176
+ if(len < 0) BDDerr("BDDV::BDDV: len < 0.", len);
177
+ if(len > BDDV_MaxLen) BDDerr("BDDV::BDDV: Too large len.", len);
178
+ int t = f.Top();
179
+ if(t > 0 && BDD_LevOfVar(t) > BDD_TopLev())
180
+ BDDerr("BDDV::BDDV: Invalid Top Var.", t);
181
+ _bdd = (len == 0)? 0: f;
182
+ _len = (f == -1)? 1: len;
183
+ _lev = GetLev(len);
184
+ }
185
+
186
+ BDDV BDDV::operator<<(int shift) const
187
+ {
188
+ if(!Uniform()) return (Former() << shift) || (Latter() << shift);
189
+ BDDV hv;
190
+ if((hv._bdd = _bdd << shift) == -1) return BDDV(-1);
191
+ hv._len = _len;
192
+ hv._lev = _lev;
193
+ return hv;
194
+ }
195
+
196
+ BDDV BDDV::operator>>(int shift) const
197
+ {
198
+ if(!Uniform()) return (Former() >> shift) || (Latter() >> shift);
199
+ BDDV hv;
200
+ if((hv._bdd = _bdd >> shift) == -1) return BDDV(-1);
201
+ hv._len = _len;
202
+ hv._lev = _lev;
203
+ return hv;
204
+ }
205
+
206
+ BDDV BDDV::Cofact(const BDDV& fv) const
207
+ {
208
+ if(_lev > 0)
209
+ return Former().Cofact(fv.Former()) || Latter().Cofact(fv.Latter());
210
+ BDDV hv;
211
+ if((hv._bdd = _bdd.Cofact(fv._bdd)) == -1) return BDDV(-1);
212
+ if(_len != fv._len) BDDerr("BDDV::Cofact: Length mismatch.");
213
+ hv._len = _len;
214
+ // hv._lev = _lev; (always zero)
215
+ return hv;
216
+ }
217
+
218
+ BDDV BDDV::Swap(int v1, int v2) const
219
+ {
220
+ if(BDD_LevOfVar(v1) > BDD_TopLev())
221
+ BDDerr("BDDV::Swap: Invalid VarID.", v1);
222
+ if(BDD_LevOfVar(v2) > BDD_TopLev())
223
+ BDDerr("BDDV::Swap: Invalid VarID.", v2);
224
+ BDDV hv;
225
+ if((hv._bdd = _bdd.Swap(v1, v2)) == -1) return BDDV(-1);
226
+ hv._len = _len;
227
+ hv._lev = _lev;
228
+ return hv;
229
+ }
230
+
231
+ int BDDV::Top() const
232
+ {
233
+ if(Uniform()) return _bdd.Top();
234
+ int t0 = Former().Top();
235
+ int t1 = Latter().Top();
236
+ if(BDD_LevOfVar(t0) > BDD_LevOfVar(t1)) return t0;
237
+ else return t1;
238
+ }
239
+
240
+ bddword BDDV::Size() const
241
+ {
242
+ bddword* bddv = new bddword[_len];
243
+ for(int i=0; i<_len; i++) bddv[i] = GetBDD(i).GetID();
244
+ bddword s = bddvsize(bddv, _len);
245
+ delete[] bddv;
246
+ return s;
247
+ }
248
+
249
+ void BDDV::Export(FILE *strm) const
250
+ {
251
+ bddword* bddv = new bddword[_len];
252
+ for(int i=0; i<_len; i++) bddv[i] = GetBDD(i).GetID();
253
+ bddexport(strm, bddv, _len);
254
+ delete[] bddv;
255
+ }
256
+
257
+ BDDV BDDV::Spread(int k) const
258
+ {
259
+ if(Uniform()) return _bdd.Spread(k);
260
+ return Former().Spread(k) || Latter().Spread(k);
261
+ }
262
+
263
+ BDDV BDDV::Part(int start, int len) const
264
+ {
265
+ if(_bdd == -1) return *this;
266
+ if(len == 0) return BDDV();
267
+
268
+ if(start < 0 || start + len > _len)
269
+ BDDerr("BDDV::Part: Illegal index.");
270
+
271
+ if(start == 0 && len == _len) return *this;
272
+
273
+ int half = 1 << (_lev-1);
274
+
275
+ if(start + len <= half) return Former().Part(start, len);
276
+ if(start >= half) return Latter().Part(start - half, len);
277
+ return Former().Part(start, half - start)
278
+ || Latter().Part(0, start + len - half);
279
+ }
280
+
281
+ BDD BDDV::GetBDD(int index) const
282
+ {
283
+ if(index < 0 || index >= _len)
284
+ BDDerr("BDDV::GetBDD: Illegal index.",index);
285
+ if(_len == 1) return _bdd;
286
+ BDD f = _bdd;
287
+ for(int i=_lev-1; i>=0; i--)
288
+ if((index & (1<<i)) == 0) f = f.At0(i + 1);
289
+ else f = f.At1(i + 1);
290
+ return f;
291
+ }
292
+
293
+ void BDDV::Print() const
294
+ {
295
+ for(int i=0; i<_len; i++)
296
+ {
297
+ cout << "f" << i << ": ";
298
+ GetBDD(i).Print();
299
+ }
300
+ cout << "Size= " << Size() << "\n\n";
301
+ cout.flush();
302
+ }
303
+
304
+ //----- External functions for BDD Vector -------
305
+
306
+ void BDDV_Init(bddword init, bddword limit)
307
+ {
308
+ bddinit(init, limit);
309
+ for(int i=0; i<BDDV_SysVarTop; i++) bddnewvar();
310
+ BDDV_Active = 1;
311
+ }
312
+
313
+ BDDV operator||(const BDDV& fv, const BDDV& gv)
314
+ {
315
+ if(fv._len == 0) return gv;
316
+ if(gv._len == 0) return fv;
317
+ if(fv._len != (1 << fv._lev))
318
+ return BDDV(fv).Former() || (BDDV(fv).Latter() || gv);
319
+ if(fv._len < gv._len)
320
+ return (fv || BDDV(gv).Former()) || BDDV(gv).Latter();
321
+ BDDV hv;
322
+ BDD x = BDDvar(fv._lev + 1);
323
+ if((hv._bdd = (~x & fv._bdd)|(x & gv._bdd)) == -1) return BDDV(-1);
324
+ if((hv._len = fv._len + gv._len) > BDDV_MaxLen)
325
+ BDDerr("BDDV::operatop||: Too large len.", hv._len);
326
+ hv._lev = fv._lev + 1;
327
+ return hv;
328
+ }
329
+
330
+ BDDV BDDV_Mask1(int index, int len)
331
+ {
332
+ if(len < 0) BDDerr("BDDV_Mask1: len < 0.", len);
333
+ if(index < 0 || index >= len)
334
+ BDDerr("BDDV_Mask1: Illegal index.", index);
335
+ return BDDV(0,index)||BDDV(1,1)||BDDV(0,len-index-1);
336
+ }
337
+
338
+ BDDV BDDV_Mask2(int index, int len)
339
+ {
340
+ if(len < 0) BDDerr("BDDV_Mask2: len < 0.", len);
341
+ if(index < 0 || index > len)
342
+ BDDerr("BDDV_Mask2: Illegal index.", index);
343
+ return BDDV(0,index)||BDDV(1,len-index);
344
+ }
345
+
346
+ #define IMPORTHASH(x) ((((x)>>1)^((x)<<8)^((x)<<16)) & (hashsize-1))
347
+
348
+ #ifdef B_64
349
+ # define B_STRTOI strtoll
350
+ #else
351
+ # define B_STRTOI strtol
352
+ #endif
353
+
354
+ BDDV BDDV_Import(FILE *strm)
355
+ {
356
+ int inv, e;
357
+ bddword hashsize;
358
+ BDD f, f0, f1;
359
+ char s[256];
360
+ bddword *hash1;
361
+ BDD *hash2;
362
+
363
+ if(fscanf(strm, "%s", &s) == EOF) return BDDV(-1);
364
+ if(strcmp(s, "_i") != 0) return BDDV(-1);
365
+ if(fscanf(strm, "%s", &s) == EOF) return BDDV(-1);
366
+ int n = strtol(s, NULL, 10);
367
+ while(n > BDD_TopLev()) BDD_NewVar();
368
+
369
+ if(fscanf(strm, "%s", &s) == EOF) return BDDV(-1);
370
+ if(strcmp(s, "_o") != 0) return BDDV(-1);
371
+ if(fscanf(strm, "%s", &s) == EOF) return BDDV(-1);
372
+ int m = strtol(s, NULL, 10);
373
+
374
+ if(fscanf(strm, "%s", &s) == EOF) return BDDV(-1);
375
+ if(strcmp(s, "_n") != 0) return BDDV(-1);
376
+ if(fscanf(strm, "%s", &s) == EOF) return BDDV(-1);
377
+ bddword n_nd = B_STRTOI(s, NULL, 10);
378
+
379
+ for(hashsize = 1; hashsize < (n_nd<<1); hashsize <<= 1)
380
+ ; /* empty */
381
+ hash1 = new bddword[hashsize];
382
+ if(hash1 == 0) return BDDV(-1);
383
+ hash2 = new BDD[hashsize];
384
+ if(hash2 == 0) { delete[] hash1; return BDDV(-1); }
385
+ for(bddword ix=0; ix<hashsize; ix++)
386
+ {
387
+ hash1[ix] = B_VAL_MASK;
388
+ hash2[ix] = 0;
389
+ }
390
+
391
+ e = 0;
392
+ for(bddword ix=0; ix<n_nd; ix++)
393
+ {
394
+ if(fscanf(strm, "%s", &s) == EOF) { e = 1; break; }
395
+ bddword nd = B_STRTOI(s, NULL, 10);
396
+
397
+ if(fscanf(strm, "%s", &s) == EOF) { e = 1; break; }
398
+ int lev = strtol(s, NULL, 10);
399
+ int var = bddvaroflev(lev);
400
+
401
+ if(fscanf(strm, "%s", &s) == EOF) { e = 1; break; }
402
+ if(strcmp(s, "F") == 0) f0 = 0;
403
+ else if(strcmp(s, "T") == 0) f0 = 1;
404
+ else
405
+ {
406
+ bddword nd0 = B_STRTOI(s, NULL, 10);
407
+
408
+ bddword ixx = IMPORTHASH(nd0);
409
+ while(hash1[ixx] != nd0)
410
+ {
411
+ if(hash1[ixx] == B_VAL_MASK)
412
+ BDDerr("BDDV_Import(): internal error", ixx);
413
+ ixx++;
414
+ ixx &= (hashsize-1);
415
+ }
416
+ f0 = hash2[ixx];
417
+ }
418
+
419
+ if(fscanf(strm, "%s", &s) == EOF) { e = 1; break; }
420
+ if(strcmp(s, "F") == 0) f1 = 0;
421
+ else if(strcmp(s, "T") == 0) f1 = 1;
422
+ else
423
+ {
424
+ bddword nd1 = B_STRTOI(s, NULL, 10);
425
+ if(nd1 & 1) { inv = 1; nd1 ^= 1; }
426
+ else inv = 0;
427
+
428
+ bddword ixx = IMPORTHASH(nd1);
429
+ while(hash1[ixx] != nd1)
430
+ {
431
+ if(hash1[ixx] == B_VAL_MASK)
432
+ BDDerr("BDDV_Import(): internal error", ixx);
433
+ ixx++;
434
+ ixx &= (hashsize-1);
435
+ }
436
+ f1 = (inv)? ~hash2[ixx]: hash2[ixx];
437
+ }
438
+
439
+ BDD x = BDDvar(var);
440
+ f = (x & f1) | (~x & f0);
441
+ if(f == -1) { e = 1; break; }
442
+
443
+ bddword ixx = IMPORTHASH(nd);
444
+ while(hash1[ixx] != B_VAL_MASK)
445
+ {
446
+ if(hash1[ixx] == nd)
447
+ BDDerr("BDDV_Import(): internal error", ixx);
448
+ ixx++;
449
+ ixx &= (hashsize-1);
450
+ }
451
+ hash1[ixx] = nd;
452
+ hash2[ixx] = f;
453
+ }
454
+
455
+ if(e)
456
+ {
457
+ delete[] hash2;
458
+ delete[] hash1;
459
+ return BDDV(-1);
460
+ }
461
+
462
+ BDDV v = BDDV();
463
+ for(int i=0; i<m; i++)
464
+ {
465
+ if(fscanf(strm, "%s", &s) == EOF)
466
+ {
467
+ delete[] hash2;
468
+ delete[] hash1;
469
+ return BDDV(-1);
470
+ }
471
+ bddword nd = B_STRTOI(s, NULL, 10);
472
+ if(strcmp(s, "F") == 0) v = v || BDD(0);
473
+ else if(strcmp(s, "T") == 0) v = v || BDD(1);
474
+ else
475
+ {
476
+ if(nd & 1) { inv = 1; nd ^= 1; }
477
+ else inv = 0;
478
+
479
+ bddword ixx = IMPORTHASH(nd);
480
+ while(hash1[ixx] != nd)
481
+ {
482
+ if(hash1[ixx] == B_VAL_MASK)
483
+ BDDerr("BDDV_Import(): internal error", ixx);
484
+ ixx++;
485
+ ixx &= (hashsize-1);
486
+ }
487
+ v = v || (inv? ~hash2[ixx]: hash2[ixx]);
488
+ }
489
+ }
490
+
491
+ delete[] hash2;
492
+ delete[] hash1;
493
+ return v;
494
+ }
495
+