SatSolver3.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.cast_de_nrw_schule_svws_core_kursblockung_satsolver_SatSolver3 = exports.SatSolver3 = void 0;
  4. const AVLSet_1 = require("../../../core/adt/set/AVLSet");
  5. const Variable_1 = require("../../../core/kursblockung/satsolver/Variable");
  6. const SatSolverA_1 = require("../../../core/kursblockung/satsolver/SatSolverA");
  7. const NullPointerException_1 = require("../../../java/lang/NullPointerException");
  8. const LinkedCollection_1 = require("../../../core/adt/collection/LinkedCollection");
  9. const Clause_1 = require("../../../core/kursblockung/satsolver/Clause");
  10. const Arrays_1 = require("../../../java/util/Arrays");
  11. const Heap_1 = require("../../../core/kursblockung/satsolver/Heap");
  12. const System_1 = require("../../../java/lang/System");
  13. class SatSolver3 extends SatSolverA_1.SatSolverA {
  14. static MAX_LEARNED_CLAUSE_SIZE = 3;
  15. heap;
  16. vSize = 0;
  17. vArrayPos;
  18. vArrayNeg;
  19. cSize = 0;
  20. learnClauseMin = 0;
  21. /**
  22. * Konstruktor.
  23. */
  24. constructor() {
  25. super();
  26. this.heap = new Heap_1.Heap();
  27. this.vSize = 0;
  28. this.cSize = 0;
  29. this.vArrayPos = Array(1).fill(null);
  30. this.vArrayNeg = Array(1).fill(null);
  31. }
  32. isVarTrue(pVar) {
  33. let v = this.getVarOf(pVar);
  34. return v.index === -1;
  35. }
  36. createNewVar() {
  37. this.vSize++;
  38. if (this.vSize >= this.vArrayPos.length) {
  39. let newSize = this.vSize * 2;
  40. this.vArrayPos = Arrays_1.Arrays.copyOf(this.vArrayPos, newSize);
  41. this.vArrayNeg = Arrays_1.Arrays.copyOf(this.vArrayNeg, newSize);
  42. }
  43. let vPos = new Variable_1.Variable(+this.vSize);
  44. let vNeg = new Variable_1.Variable(-this.vSize);
  45. vPos.negation = vNeg;
  46. vNeg.negation = vPos;
  47. this.vArrayPos[this.vSize] = vPos;
  48. this.vArrayNeg[this.vSize] = vNeg;
  49. this.heap.insert(vPos);
  50. this.heap.insert(vNeg);
  51. return this.vSize;
  52. }
  53. addClause(pVars) {
  54. if (pVars.length === 0) {
  55. console.log(JSON.stringify("WARNUNG: Leere Klausel bei SatSolver.addClause(int[] pVars)!"));
  56. return;
  57. }
  58. let set = new AVLSet_1.AVLSet();
  59. for (let v of pVars) {
  60. if (set.contains(-v)) {
  61. return;
  62. }
  63. set.add(v);
  64. }
  65. let list = new LinkedCollection_1.LinkedCollection();
  66. while (!set.isEmpty()) {
  67. list.addLast(set.pollFirst());
  68. }
  69. while (list.size() > 3) {
  70. let x = list.removeFirst().valueOf();
  71. let y = list.removeFirst().valueOf();
  72. let z = this.createNewVar();
  73. list.addLast(z);
  74. this.addClause2(-x, z);
  75. this.addClause2(-y, z);
  76. this.addClause3(x, y, -z);
  77. }
  78. if (list.size() === 3) {
  79. this.addClause3(list.removeFirst().valueOf(), list.removeFirst().valueOf(), list.removeFirst().valueOf());
  80. }
  81. if (list.size() === 2) {
  82. this.addClause2(list.removeFirst().valueOf(), list.removeFirst().valueOf());
  83. }
  84. if (list.size() === 1) {
  85. this.addClause1(list.removeFirst().valueOf());
  86. }
  87. }
  88. /**
  89. * Fügt der Datenstruktur eine 1-CNF-Klausel hinzu.
  90. *
  91. * @param x Das 1. Literal (Variablennummer) der Klausel.
  92. */
  93. addClause1(x) {
  94. let varX = this.getVarOf(x);
  95. if (varX.negation === null)
  96. throw new NullPointerException_1.NullPointerException();
  97. this.heap.remove(varX);
  98. this.heap.remove(varX.negation);
  99. let c = new Clause_1.Clause(varX);
  100. this.cSize++;
  101. varX.clauses.addLast(c);
  102. varX.statSatFree[c.sat][c.free]++;
  103. this.heap.insert(varX);
  104. this.heap.insert(varX.negation);
  105. }
  106. /**
  107. * Fügt der Datenstruktur eine 2-CNF-Klausel hinzu.
  108. *
  109. * @param x Das 1. Literal (Variablennummer) der Klausel.
  110. * @param y Das 2. Literal (Variablennummer) der Klausel.
  111. */
  112. addClause2(x, y) {
  113. let varX = this.getVarOf(x);
  114. let varY = this.getVarOf(y);
  115. if ((varX.negation === null) || (varY.negation === null))
  116. throw new NullPointerException_1.NullPointerException();
  117. this.heap.remove(varX);
  118. this.heap.remove(varY);
  119. this.heap.remove(varX.negation);
  120. this.heap.remove(varY.negation);
  121. let c = new Clause_1.Clause(varX, varY);
  122. this.cSize++;
  123. varX.clauses.addLast(c);
  124. varX.statSatFree[c.sat][c.free]++;
  125. varX.neighbours.addLast(varY);
  126. varY.clauses.addLast(c);
  127. varY.statSatFree[c.sat][c.free]++;
  128. varY.neighbours.addLast(varX);
  129. this.heap.insert(varX);
  130. this.heap.insert(varY);
  131. this.heap.insert(varX.negation);
  132. this.heap.insert(varY.negation);
  133. }
  134. /**
  135. * Fügt der Datenstruktur eine 3-CNF-Klausel hinzu.
  136. *
  137. * @param x Das 1. Literal (Variablennummer) der Klausel.
  138. * @param y Das 2. Literal (Variablennummer) der Klausel.
  139. * @param z Das 3. Literal (Variablennummer) der Klausel.
  140. */
  141. addClause3(x, y, z) {
  142. let varX = this.getVarOf(x);
  143. let varY = this.getVarOf(y);
  144. let varZ = this.getVarOf(z);
  145. if ((varX.negation === null) || (varY.negation === null) || (varZ.negation === null))
  146. throw new NullPointerException_1.NullPointerException();
  147. this.heap.remove(varX);
  148. this.heap.remove(varY);
  149. this.heap.remove(varZ);
  150. this.heap.remove(varX.negation);
  151. this.heap.remove(varY.negation);
  152. this.heap.remove(varZ.negation);
  153. let c = new Clause_1.Clause(varX, varY, varZ);
  154. this.cSize++;
  155. varX.clauses.addLast(c);
  156. varX.statSatFree[c.sat][c.free]++;
  157. varX.neighbours.addLast(varY);
  158. varX.neighbours.addLast(varZ);
  159. varY.clauses.addLast(c);
  160. varY.statSatFree[c.sat][c.free]++;
  161. varY.neighbours.addLast(varX);
  162. varY.neighbours.addLast(varZ);
  163. varZ.clauses.addLast(c);
  164. varZ.statSatFree[c.sat][c.free]++;
  165. varZ.neighbours.addLast(varX);
  166. varZ.neighbours.addLast(varY);
  167. this.heap.insert(varX);
  168. this.heap.insert(varY);
  169. this.heap.insert(varZ);
  170. this.heap.insert(varX.negation);
  171. this.heap.insert(varY.negation);
  172. this.heap.insert(varZ.negation);
  173. }
  174. getVarCount() {
  175. return this.vSize;
  176. }
  177. getClauseCount() {
  178. return this.cSize;
  179. }
  180. solve(pMaxTimeMillis) {
  181. let timeStart = System_1.System.currentTimeMillis();
  182. let backtrackV = Array(this.vSize).fill(null);
  183. let backtrackLearn = Array(SatSolver3.MAX_LEARNED_CLAUSE_SIZE).fill(null);
  184. let backtrackB = Array(this.vSize).fill(false);
  185. let index = 0;
  186. let max = 1;
  187. let countDown = 0;
  188. this.learnClauseMin = SatSolver3.MAX_LEARNED_CLAUSE_SIZE + 1;
  189. let maxIndex = 0;
  190. while ((index >= 0) && !this.heap.isEmpty()) {
  191. if (index > maxIndex) {
  192. maxIndex = index;
  193. }
  194. if (countDown === 0) {
  195. for (let i = index; i >= 0; i--) {
  196. if (backtrackV[i] !== null) {
  197. this.unitpropagation_undo((0, Variable_1.cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable)(backtrackV[i]));
  198. }
  199. backtrackV[i] = null;
  200. backtrackB[i] = false;
  201. }
  202. index = 0;
  203. if ((this.learnClauseMin >= 1) && (this.learnClauseMin <= SatSolver3.MAX_LEARNED_CLAUSE_SIZE)) {
  204. let clause = Array(this.learnClauseMin).fill(0);
  205. for (let i = 0; i < clause.length; i++) {
  206. clause[i] = -backtrackLearn[i].nr;
  207. }
  208. this.addClause(clause);
  209. }
  210. let result = this.simplify();
  211. if (result !== SatSolverA_1.SatSolverA.RESULT_UNKNOWN) {
  212. return result;
  213. }
  214. max = max + 1 + Math.trunc(max / 2);
  215. countDown = max;
  216. this.learnClauseMin = SatSolver3.MAX_LEARNED_CLAUSE_SIZE + 1;
  217. if (System_1.System.currentTimeMillis() - timeStart > pMaxTimeMillis) {
  218. return SatSolverA_1.SatSolverA.RESULT_UNKNOWN;
  219. }
  220. continue;
  221. }
  222. countDown--;
  223. let varP = backtrackV[index];
  224. if ((varP !== null) && (backtrackB[index])) {
  225. this.unitpropagation_undo(varP);
  226. backtrackV[index] = null;
  227. backtrackB[index] = false;
  228. this.learnClause(backtrackV, backtrackLearn, index);
  229. index--;
  230. continue;
  231. }
  232. if (varP === null) {
  233. varP = this.heap.top();
  234. if (!varP.isUnsat()) {
  235. backtrackV[index] = varP;
  236. this.unitpropagation(varP);
  237. index++;
  238. continue;
  239. }
  240. }
  241. else {
  242. this.unitpropagation_undo(varP);
  243. }
  244. if (varP.negation === null)
  245. throw new NullPointerException_1.NullPointerException();
  246. varP = varP.negation;
  247. if (varP.isUnsat()) {
  248. backtrackV[index] = null;
  249. this.learnClause(backtrackV, backtrackLearn, index);
  250. index--;
  251. continue;
  252. }
  253. backtrackV[index] = varP;
  254. backtrackB[index] = true;
  255. this.unitpropagation(varP);
  256. index++;
  257. continue;
  258. }
  259. return this.heap.isEmpty() ? SatSolverA_1.SatSolverA.RESULT_SATISFIABLE : SatSolverA_1.SatSolverA.RESULT_UNSATISFIABLE;
  260. }
  261. learnClause(backtrackV, backtrackLearn, size) {
  262. if (size < 1) {
  263. return;
  264. }
  265. if (size >= this.learnClauseMin) {
  266. return;
  267. }
  268. this.learnClauseMin = size;
  269. for (let i = 0; i < size; i++) {
  270. backtrackLearn[i] = backtrackV[i];
  271. }
  272. }
  273. simplify() {
  274. let count1 = 0;
  275. let changed = true;
  276. while (changed) {
  277. changed = false;
  278. for (let nr = 1; nr <= this.vSize; nr++) {
  279. let varP = this.vArrayPos[nr];
  280. if (varP.index < 0) {
  281. continue;
  282. }
  283. if (varP.statSatFree[0][0] > 0) {
  284. return SatSolverA_1.SatSolverA.RESULT_UNSATISFIABLE;
  285. }
  286. if (varP.negation === null)
  287. throw new NullPointerException_1.NullPointerException();
  288. if (varP.negation.statSatFree[0][0] > 0) {
  289. return SatSolverA_1.SatSolverA.RESULT_UNSATISFIABLE;
  290. }
  291. if ((varP.statSatFree[0][1] > 0) && (varP.negation.statSatFree[0][1] > 0)) {
  292. return SatSolverA_1.SatSolverA.RESULT_UNSATISFIABLE;
  293. }
  294. if (varP.statSatFree[0][1] > 0) {
  295. this.unitpropagation(varP);
  296. count1++;
  297. changed = true;
  298. continue;
  299. }
  300. if (varP.negation.statSatFree[0][1] > 0) {
  301. this.unitpropagation(varP.negation);
  302. count1++;
  303. changed = true;
  304. continue;
  305. }
  306. if (varP.getClauseOccurences() === 0) {
  307. this.unitpropagation(varP.negation);
  308. count1++;
  309. changed = true;
  310. continue;
  311. }
  312. if (varP.negation.getClauseOccurences() === 0) {
  313. this.unitpropagation(varP);
  314. count1++;
  315. changed = true;
  316. continue;
  317. }
  318. }
  319. }
  320. return SatSolverA_1.SatSolverA.RESULT_UNKNOWN;
  321. }
  322. static fill(index) {
  323. let s = "";
  324. for (let i = 0; i < index; i++) {
  325. s = s.valueOf() + " ";
  326. }
  327. return s;
  328. }
  329. /**
  330. * Setzt die Variable {@code varP} auf TRUE und aktualisiert die gesamte Datenstruktur entsprechend.
  331. *
  332. * @param varP Die Variable, die auf TRUE gesetzt wird.
  333. */
  334. unitpropagation(varP) {
  335. let varN = varP.negation;
  336. if (varN === null)
  337. throw new NullPointerException_1.NullPointerException();
  338. this.heap.remove(varP);
  339. this.heap.remove(varN);
  340. varP.index = -1;
  341. varN.index = -2;
  342. this.unitpropagationHelper(varP.clauses, +1, -1);
  343. this.unitpropagationHelper(varN.clauses, +0, -1);
  344. }
  345. /**
  346. * Setzt die Variable {@code varP} von TRUE auf FREI und aktualisiert die gesamte Datenstruktur entsprechend.
  347. *
  348. * @param varP Die Variable, die von TRUE auf FREI gesetzt wird.
  349. */
  350. unitpropagation_undo(varP) {
  351. let varN = varP.negation;
  352. if (varN === null)
  353. throw new NullPointerException_1.NullPointerException();
  354. this.unitpropagationHelper(varP.clauses, -1, +1);
  355. this.unitpropagationHelper(varN.clauses, +0, +1);
  356. this.heap.insert(varP);
  357. this.heap.insert(varN);
  358. }
  359. /**
  360. * Eine Hilsmethode, die alle Klauseln der Liste aktualisiert.
  361. *
  362. * @param clauses Alle zu informmierenden Klausel.
  363. * @param pDeltaSat Die Veränderung der erfüllten Klauseln, relativ zum 1. Index im 2D-Array
  364. * {@link Variable#statSatFree}.
  365. * @param pDeltaFree Die Veränderung der freien Variablen, relativ zum 2. Index im 2D-Array
  366. * {@link Variable#statSatFree}.
  367. */
  368. unitpropagationHelper(clauses, pDeltaSat, pDeltaFree) {
  369. for (let c of clauses) {
  370. let sat1 = c.sat;
  371. let free1 = c.free;
  372. let sat2 = sat1 + pDeltaSat;
  373. let free2 = free1 + pDeltaFree;
  374. for (let v of c.variables) {
  375. v.statSatFree[sat1][free1]--;
  376. v.statSatFree[sat2][free2]++;
  377. this.heap.update(v);
  378. }
  379. c.sat = sat2;
  380. c.free = free2;
  381. }
  382. }
  383. /**
  384. * Liefert das zugehörige Variablenobjekt.
  385. *
  386. * @param pNr Der Variablennummer.
  387. *
  388. * @return Das zugehörige Variablenobjekt.
  389. */
  390. getVarOf(pNr) {
  391. return (pNr > 0) ? this.vArrayPos[pNr] : this.vArrayNeg[-pNr];
  392. }
  393. isTranspiledInstanceOf(name) {
  394. return ['de.nrw.schule.svws.core.kursblockung.satsolver.SatSolverA', 'de.nrw.schule.svws.core.kursblockung.satsolver.SatSolver3'].includes(name);
  395. }
  396. }
  397. exports.SatSolver3 = SatSolver3;
  398. function cast_de_nrw_schule_svws_core_kursblockung_satsolver_SatSolver3(obj) {
  399. return obj;
  400. }
  401. exports.cast_de_nrw_schule_svws_core_kursblockung_satsolver_SatSolver3 = cast_de_nrw_schule_svws_core_kursblockung_satsolver_SatSolver3;
  402. //# sourceMappingURL=SatSolver3.js.map