SatSolver3.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import { JavaObject, cast_java_lang_Object } from '../../../java/lang/JavaObject';
  2. import { JavaInteger, cast_java_lang_Integer } from '../../../java/lang/JavaInteger';
  3. import { AVLSet, cast_de_nrw_schule_svws_core_adt_set_AVLSet } from '../../../core/adt/set/AVLSet';
  4. import { Variable, cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable } from '../../../core/kursblockung/satsolver/Variable';
  5. import { SatSolverA, cast_de_nrw_schule_svws_core_kursblockung_satsolver_SatSolverA } from '../../../core/kursblockung/satsolver/SatSolverA';
  6. import { NullPointerException, cast_java_lang_NullPointerException } from '../../../java/lang/NullPointerException';
  7. import { LinkedCollection, cast_de_nrw_schule_svws_core_adt_collection_LinkedCollection } from '../../../core/adt/collection/LinkedCollection';
  8. import { Clause, cast_de_nrw_schule_svws_core_kursblockung_satsolver_Clause } from '../../../core/kursblockung/satsolver/Clause';
  9. import { JavaString, cast_java_lang_String } from '../../../java/lang/JavaString';
  10. import { Arrays, cast_java_util_Arrays } from '../../../java/util/Arrays';
  11. import { Heap, cast_de_nrw_schule_svws_core_kursblockung_satsolver_Heap } from '../../../core/kursblockung/satsolver/Heap';
  12. import { System, cast_java_lang_System } from '../../../java/lang/System';
  13. export class SatSolver3 extends SatSolverA {
  14. private static readonly MAX_LEARNED_CLAUSE_SIZE : number = 3;
  15. private heap : Heap;
  16. private vSize : number = 0;
  17. private vArrayPos : Array<Variable>;
  18. private vArrayNeg : Array<Variable>;
  19. private cSize : number = 0;
  20. private learnClauseMin : number = 0;
  21. /**
  22. * Konstruktor.
  23. */
  24. public constructor() {
  25. super();
  26. this.heap = new 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. public isVarTrue(pVar : number) : boolean {
  33. let v : Variable = this.getVarOf(pVar);
  34. return v.index === -1;
  35. }
  36. public createNewVar() : number {
  37. this.vSize++;
  38. if (this.vSize >= this.vArrayPos.length) {
  39. let newSize : number = this.vSize * 2;
  40. this.vArrayPos = Arrays.copyOf(this.vArrayPos, newSize);
  41. this.vArrayNeg = Arrays.copyOf(this.vArrayNeg, newSize);
  42. }
  43. let vPos : Variable = new Variable(+this.vSize);
  44. let vNeg : Variable = new 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. public addClause(pVars : Array<number>) : void {
  54. if (pVars.length === 0) {
  55. console.log(JSON.stringify("WARNUNG: Leere Klausel bei SatSolver.addClause(int[] pVars)!"));
  56. return;
  57. }
  58. let set : AVLSet<Number> = new AVLSet();
  59. for (let v of pVars) {
  60. if (set.contains(-v)) {
  61. return;
  62. }
  63. set.add(v);
  64. }
  65. let list : LinkedCollection<Number> = new LinkedCollection();
  66. while (!set.isEmpty()) {
  67. list.addLast(set.pollFirst());
  68. }
  69. while (list.size() > 3) {
  70. let x : number = list.removeFirst().valueOf();
  71. let y : number = list.removeFirst().valueOf();
  72. let z : number = 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. private addClause1(x : number) : void {
  94. let varX : Variable = this.getVarOf(x);
  95. if (varX.negation === null)
  96. throw new NullPointerException()
  97. this.heap.remove(varX);
  98. this.heap.remove(varX.negation);
  99. let c : Clause = new 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. private addClause2(x : number, y : number) : void {
  113. let varX : Variable = this.getVarOf(x);
  114. let varY : Variable = this.getVarOf(y);
  115. if ((varX.negation === null) || (varY.negation === null))
  116. throw new 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 : Clause = new 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. private addClause3(x : number, y : number, z : number) : void {
  142. let varX : Variable = this.getVarOf(x);
  143. let varY : Variable = this.getVarOf(y);
  144. let varZ : Variable = this.getVarOf(z);
  145. if ((varX.negation === null) || (varY.negation === null) || (varZ.negation === null))
  146. throw new 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 : Clause = new 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. public getVarCount() : number {
  175. return this.vSize;
  176. }
  177. public getClauseCount() : number {
  178. return this.cSize;
  179. }
  180. public solve(pMaxTimeMillis : number) : number {
  181. let timeStart : number = System.currentTimeMillis();
  182. let backtrackV : Array<Variable | null> = Array(this.vSize).fill(null);
  183. let backtrackLearn : Array<Variable> = Array(SatSolver3.MAX_LEARNED_CLAUSE_SIZE).fill(null);
  184. let backtrackB : Array<boolean> = Array(this.vSize).fill(false);
  185. let index : number = 0;
  186. let max : number = 1;
  187. let countDown : number = 0;
  188. this.learnClauseMin = SatSolver3.MAX_LEARNED_CLAUSE_SIZE + 1;
  189. let maxIndex : number = 0;
  190. while ((index >= 0) && !this.heap.isEmpty()) {
  191. if (index > maxIndex) {
  192. maxIndex = index;
  193. }
  194. if (countDown === 0) {
  195. for (let i : number = index; i >= 0; i--){
  196. if (backtrackV[i] !== null) {
  197. this.unitpropagation_undo(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<number> = Array(this.learnClauseMin).fill(0);
  205. for (let i : number = 0; i < clause.length; i++){
  206. clause[i] = -backtrackLearn[i].nr;
  207. }
  208. this.addClause(clause);
  209. }
  210. let result : number = this.simplify();
  211. if (result !== 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.currentTimeMillis() - timeStart > pMaxTimeMillis) {
  218. return SatSolverA.RESULT_UNKNOWN;
  219. }
  220. continue;
  221. }
  222. countDown--;
  223. let varP : Variable | null = 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. } else {
  241. this.unitpropagation_undo(varP);
  242. }
  243. if (varP.negation === null)
  244. throw new NullPointerException()
  245. varP = varP.negation;
  246. if (varP.isUnsat()) {
  247. backtrackV[index] = null;
  248. this.learnClause(backtrackV, backtrackLearn, index);
  249. index--;
  250. continue;
  251. }
  252. backtrackV[index] = varP;
  253. backtrackB[index] = true;
  254. this.unitpropagation(varP);
  255. index++;
  256. continue;
  257. }
  258. return this.heap.isEmpty() ? SatSolverA.RESULT_SATISFIABLE : SatSolverA.RESULT_UNSATISFIABLE;
  259. }
  260. private learnClause(backtrackV : Array<Variable | null>, backtrackLearn : Array<Variable | null>, size : number) : void {
  261. if (size < 1) {
  262. return;
  263. }
  264. if (size >= this.learnClauseMin) {
  265. return;
  266. }
  267. this.learnClauseMin = size;
  268. for (let i : number = 0; i < size; i++){
  269. backtrackLearn[i] = backtrackV[i];
  270. }
  271. }
  272. private simplify() : number {
  273. let count1 : number = 0;
  274. let changed : boolean = true;
  275. while (changed) {
  276. changed = false;
  277. for (let nr : number = 1; nr <= this.vSize; nr++){
  278. let varP : Variable | null = this.vArrayPos[nr];
  279. if (varP.index < 0) {
  280. continue;
  281. }
  282. if (varP.statSatFree[0][0] > 0) {
  283. return SatSolverA.RESULT_UNSATISFIABLE;
  284. }
  285. if (varP.negation === null)
  286. throw new NullPointerException()
  287. if (varP.negation.statSatFree[0][0] > 0) {
  288. return SatSolverA.RESULT_UNSATISFIABLE;
  289. }
  290. if ((varP.statSatFree[0][1] > 0) && (varP.negation.statSatFree[0][1] > 0)) {
  291. return SatSolverA.RESULT_UNSATISFIABLE;
  292. }
  293. if (varP.statSatFree[0][1] > 0) {
  294. this.unitpropagation(varP);
  295. count1++;
  296. changed = true;
  297. continue;
  298. }
  299. if (varP.negation.statSatFree[0][1] > 0) {
  300. this.unitpropagation(varP.negation);
  301. count1++;
  302. changed = true;
  303. continue;
  304. }
  305. if (varP.getClauseOccurences() === 0) {
  306. this.unitpropagation(varP.negation);
  307. count1++;
  308. changed = true;
  309. continue;
  310. }
  311. if (varP.negation.getClauseOccurences() === 0) {
  312. this.unitpropagation(varP);
  313. count1++;
  314. changed = true;
  315. continue;
  316. }
  317. }
  318. }
  319. return SatSolverA.RESULT_UNKNOWN;
  320. }
  321. private static fill(index : number) : String | null {
  322. let s : String | null = "";
  323. for (let i : number = 0; i < index; i++){
  324. s = s.valueOf() + " ";
  325. }
  326. return s;
  327. }
  328. /**
  329. * Setzt die Variable {@code varP} auf TRUE und aktualisiert die gesamte Datenstruktur entsprechend.
  330. *
  331. * @param varP Die Variable, die auf TRUE gesetzt wird.
  332. */
  333. private unitpropagation(varP : Variable) : void {
  334. let varN : Variable | null = varP.negation;
  335. if (varN === null)
  336. throw new NullPointerException()
  337. this.heap.remove(varP);
  338. this.heap.remove(varN);
  339. varP.index = -1;
  340. varN.index = -2;
  341. this.unitpropagationHelper(varP.clauses, +1, -1);
  342. this.unitpropagationHelper(varN.clauses, +0, -1);
  343. }
  344. /**
  345. * Setzt die Variable {@code varP} von TRUE auf FREI und aktualisiert die gesamte Datenstruktur entsprechend.
  346. *
  347. * @param varP Die Variable, die von TRUE auf FREI gesetzt wird.
  348. */
  349. private unitpropagation_undo(varP : Variable) : void {
  350. let varN : Variable | null = varP.negation;
  351. if (varN === null)
  352. throw new NullPointerException()
  353. this.unitpropagationHelper(varP.clauses, -1, +1);
  354. this.unitpropagationHelper(varN.clauses, +0, +1);
  355. this.heap.insert(varP);
  356. this.heap.insert(varN);
  357. }
  358. /**
  359. * Eine Hilsmethode, die alle Klauseln der Liste aktualisiert.
  360. *
  361. * @param clauses Alle zu informmierenden Klausel.
  362. * @param pDeltaSat Die Veränderung der erfüllten Klauseln, relativ zum 1. Index im 2D-Array
  363. * {@link Variable#statSatFree}.
  364. * @param pDeltaFree Die Veränderung der freien Variablen, relativ zum 2. Index im 2D-Array
  365. * {@link Variable#statSatFree}.
  366. */
  367. private unitpropagationHelper(clauses : LinkedCollection<Clause>, pDeltaSat : number, pDeltaFree : number) : void {
  368. for (let c of clauses) {
  369. let sat1 : number = c.sat;
  370. let free1 : number = c.free;
  371. let sat2 : number = sat1 + pDeltaSat;
  372. let free2 : number = free1 + pDeltaFree;
  373. for (let v of c.variables) {
  374. v.statSatFree[sat1][free1]--;
  375. v.statSatFree[sat2][free2]++;
  376. this.heap.update(v);
  377. }
  378. c.sat = sat2;
  379. c.free = free2;
  380. }
  381. }
  382. /**
  383. * Liefert das zugehörige Variablenobjekt.
  384. *
  385. * @param pNr Der Variablennummer.
  386. *
  387. * @return Das zugehörige Variablenobjekt.
  388. */
  389. private getVarOf(pNr : number) : Variable {
  390. return (pNr > 0) ? this.vArrayPos[pNr] : this.vArrayNeg[-pNr];
  391. }
  392. isTranspiledInstanceOf(name : string): boolean {
  393. return ['de.nrw.schule.svws.core.kursblockung.satsolver.SatSolverA', 'de.nrw.schule.svws.core.kursblockung.satsolver.SatSolver3'].includes(name);
  394. }
  395. }
  396. export function cast_de_nrw_schule_svws_core_kursblockung_satsolver_SatSolver3(obj : unknown) : SatSolver3 {
  397. return obj as SatSolver3;
  398. }