solver.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. function EV() {
  2. this.__e = {};
  3. }
  4. EV.prototype.__e = {};
  5. EV.prototype.emit = function(n) {
  6. var that = this;
  7. var args = [].slice.apply(arguments, [1]);
  8. (this.__e[n] || []).map(function(lis) {
  9. lis.map(function(fn) {
  10. fn.apply(that, args)
  11. })
  12. })
  13. return this;
  14. }
  15. EV.prototype.on = function(n) {
  16. var args = [].slice.apply(arguments, [1]);
  17. this.__e[n] = this.__e[n] || [];
  18. this.__e[n].push(args);
  19. return this;
  20. }
  21. function inherits(ctor, superCtor) {
  22. var Obj = function() {};
  23. Obj.prototype = superCtor.prototype;
  24. ctor.prototype = new Obj
  25. }
  26. function permutates(xs) {
  27. var ret = [];
  28. for (var i = 0; i < xs.length; i = i + 1) {
  29. var rest = permutates(xs.slice(0, i).concat(xs.slice(i + 1)));
  30. if (!rest.length) {
  31. ret.push([xs[i]])
  32. } else {
  33. for (var j = 0; j < rest.length; j = j + 1) {
  34. ret.push([xs[i]].concat(rest[j]))
  35. }
  36. }
  37. }
  38. return ret;
  39. }
  40. function mo() {
  41. this.variabler = {};
  42. this.funcs = {};
  43. this.rulas = [];
  44. }
  45. inherits(mo, EV)
  46. mo.prototype.addVariabelSet = function(navn, arr) {
  47. this.variabler[navn] = arr
  48. return this;
  49. }
  50. mo.prototype.addVariableTable = function(navn, arr) {
  51. return this;
  52. }
  53. mo.prototype.addFunction = function(name, func) {
  54. this.funcs[name] = func;
  55. return this;
  56. }
  57. mo.prototype.addClause = function() {
  58. var args = Array.prototype.slice.apply(arguments, []);
  59. while (args.length) {
  60. var arg = args.shift();
  61. var props = Object.keys(arg);
  62. var propid1, propid2;
  63. if (this.variabler[props[0]]) {
  64. propid1 = Object.keys(this.variabler).indexOf(props[0]);
  65. }
  66. if (this.variabler[props[1]]) {
  67. propid2 = Object.keys(this.variabler).indexOf(props[1]);
  68. } else {}
  69. var arr = []
  70. if (propid1 > -1 && propid2 > -1) {
  71. val1 = this.variabler[props[0]].indexOf(arg[props[0]])
  72. val2 = this.variabler[props[1]].indexOf(arg[props[1]])
  73. arr = ["check", propid1, propid2, val1, val2, [props[0], arg[props[0]], props[1], arg[props[1]]].join(" ")];
  74. } else {
  75. var lookfor = props[props.length - 1];
  76. if (this.funcs[lookfor]) {
  77. arr = this.funcs[lookfor].apply(this, [arg])
  78. }
  79. }
  80. }
  81. this.rulas.push(arr);
  82. return this;
  83. }
  84. mo.prototype.build = function(tabl) {
  85. var r = [];
  86. var arr = [];
  87. var pertable = []
  88. for (var navn in this.variabler) {
  89. pertable.push(permutates((this.variabler[navn]).map(function(a, i) {
  90. return i
  91. })));
  92. }
  93. return pertable.sort(function(a,b){ a.length > b.length ? 1 : a.length < b.length ? -1 : 0 }).shift();
  94. }
  95. mo.prototype.checkPos = function(prop1, prop2, val1, val2) {
  96. return this.current[this.sos[prop1]][val2] == val1
  97. }
  98. mo.prototype.check = function(prop1, prop2, val1, val2) {
  99. var i;
  100. // console.log(prop1,this.sos[prop1], prop2, this.current[this.sos[prop1]],this.current[this.sos[prop2]],val1,val2)
  101. for (i = 0; i < 5; i++) {
  102. if (this.current[this.sos[prop1]][i] == val1) {
  103. // console.log("II,",i,this.current[this.sos[prop1]][i],"=",val1,"&", this.current[this.sos[prop2]][i],"=",val2 );
  104. var p;
  105. p = (this.current[this.sos[prop2]][i] == val2)
  106. return p;
  107. }
  108. }
  109. return false;
  110. }
  111. mo.prototype.checkLeft = function(prop1, prop2, val1, val2) {
  112. var i;
  113. for (i = 0; i < 4; i++) {
  114. if (this.current[this.sos[prop1]][i] == val1) {
  115. var p;
  116. p = (this.current[this.sos[prop2]][i + 1] == val2)
  117. return p;
  118. }
  119. }
  120. return false;
  121. }
  122. mo.prototype.checkRight = function(prop1, prop2, val1, val2) {
  123. var i;
  124. for (i = 1; i < 5; i++) {
  125. if (this.current[this.sos[prop1]][i] == val1) {
  126. var p;
  127. p = (this.current[this.sos[prop2]][i - 1] == val2)
  128. return p;
  129. }
  130. }
  131. return false;
  132. }
  133. mo.prototype.checkBoth = function(prop1, prop2, val1, val2) {
  134. return this.checkLeft(prop1, prop2, val1, val2) || this.checkRight(prop1, prop2, val1, val2)
  135. }
  136. /*
  137. mo.prototype.inner_solve_l_1 = function(cands) {
  138. }
  139. mo.prototype.inner_solve_l_2 = function(cands) {
  140. }
  141. mo.prototype.inner_solve_l_3 = function(cands) {
  142. }
  143. mo.prototype.inner_solve_l_4 = function(cands) {
  144. }
  145. mo.prototype.inner_solve_l_5 = function(cands) {
  146. }
  147. */
  148. mo.prototype.inner_solve = function(cands) {
  149. /*todo tada - slip fri for for for for for */
  150. var self = this;
  151. var cc = cands.length;
  152. var ticks = [0, 0, 0, 0, 0];
  153. var ic = 0;
  154. var iomax = self.rulas.length;
  155. var ioa = 0;
  156. var suma = cands.reduce(function(a, b) {
  157. return b[1].length * a
  158. }, 1)
  159. for (var i0 = 0; i0 < cands[0][1].length; i0++) {
  160. if (cands[1]) {
  161. for (var i1 = 0; i1 < cands[1][1].length; i1++) {
  162. if (cands[2]) {
  163. for (var i2 = 0; i2 < cands[2][1].length; i2++) {
  164. if (cands[3]) {
  165. } else {
  166. total = suma * self.subspace * self.subspace
  167. var st = [0, 1, 2, 3, 4]
  168. self.sos[cands[0][0]] = cands[0][1][i0]
  169. self.sos[cands[1][0]] = cands[1][1][i1]
  170. self.sos[cands[2][0]] = cands[2][1][i2]
  171. st.splice(st.indexOf(cands[0][0]), 1);
  172. st.splice(st.indexOf(cands[1][0]), 1);
  173. st.splice(st.indexOf(cands[2][0]), 1);
  174. for (var i3 = 0; i3 < self.subspace; i3++) {
  175. for (var i4 = 0; i4 < self.subspace; i4++) {
  176. ic++
  177. self.sos[st[0]] = i3
  178. self.sos[st[1]] = i4
  179. var io = 0;
  180. var ff = true;
  181. while (io < iomax && ff) {
  182. ff = false;
  183. var t = self.rulas[io];
  184. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  185. if (ff) {
  186. io++;
  187. }
  188. }
  189. if (io > ioa) {
  190. ioa = io;
  191. var tt = self.rulas.map(function(t) {
  192. return [self[t.slice(0, 1)[0]].apply(self, t.slice(1))].concat(t);
  193. })
  194. self.emit("part", ic, io, ioa, iomax, self.sos, tt.map(function(a) {
  195. return a.join(" ")
  196. }))
  197. }
  198. if (ioa >= iomax) {
  199. return self.sos;
  200. }
  201. if (ic % 1000000 === 0) {
  202. self.emit("status", (ic / (total / 100)).toFixed(2) + "%", total, ic, ioa, iomax)
  203. // console.log("X2", ic, io, ioa, iomax,":>", self.sos[cands[0][0]], self.sos[cands[1][0]], self.sos[cands[2][0]], i3, i4);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. } else {
  210. var st = [0, 1, 2, 3, 4]
  211. self.sos[cands[0][0]] = cands[0][1][i0]
  212. self.sos[cands[1][0]] = cands[1][1][i1]
  213. // self.sos[cands[2][0]] = cands[2][1][i2]
  214. st.splice(st.indexOf(cands[0][0]), 1);
  215. st.splice(st.indexOf(cands[1][0]), 1);
  216. // st.splice(st.indexOf(cands[2][0]), 1);
  217. total = suma * self.subspace * self.subspace * self.subspace;
  218. for (var i2 = 0; i2 < self.subspace; i2++) {
  219. for (var i3 = 0; i3 < self.subspace; i3++) {
  220. for (var i4 = 0; i4 < self.subspace; i4++) {
  221. ic++
  222. self.sos[st[0]] = i2
  223. self.sos[st[1]] = i3
  224. self.sos[st[2]] = i4
  225. var io = 0;
  226. var ff = true;
  227. while (io < iomax && ff) {
  228. ff = false;
  229. var t = self.rulas[io];
  230. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  231. if (ff) {
  232. io++;
  233. }
  234. }
  235. if (io > ioa) {
  236. ioa = io;
  237. var tt = self.rulas.map(function(t) {
  238. return [self[t.slice(0, 1)[0]].apply(self, t.slice(1))].concat(t);
  239. })
  240. self.emit("part", ic, io, ioa, iomax, self.sos, tt.map(function(a) {
  241. return a.join(" ")
  242. }))
  243. }
  244. if (ioa >= iomax) {
  245. return self.sos;
  246. }
  247. if (ic % 1000000 === 0) {
  248. console.log("X3", ic, io, ioa, iomax, ":>", self.sos[cands[0][0]], self.sos[cands[1][0]], i2, i3, i4);
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. return false;
  258. }
  259. mo.prototype.solve = function() {
  260. var self = this;
  261. self.current = this.build("House");
  262. console.log(self.current);
  263. var sa = Object.keys(this.variabler);
  264. self.sos = sa.map(function(a) {
  265. return 0; //self.current.length - 1; //Math.floor(Math.random()*self.current.length-1);
  266. })
  267. self.subspace = self.current.length;
  268. self.varspace = sa.length;
  269. var iteration = 0;
  270. var maxi = 0;
  271. var ioa = -1;
  272. // først find variabler som bruges i samme opslag.
  273. var tas = {}
  274. var firsta = [];
  275. self.rulas.map(function(ru) {
  276. console.log(ru);
  277. if (ru[1] == ru[2]) {
  278. firsta.push(ru);
  279. }
  280. })
  281. self.sos = self.sos.map(function(a) {
  282. return 0
  283. })
  284. var osa = -1
  285. console.log(firsta);
  286. var cands = [];
  287. for (var x = 0; x < firsta.length; x++) {
  288. self.sos = self.sos.map(function(a) {
  289. return 0
  290. })
  291. var t = firsta[x];
  292. var ca = [];
  293. for (var i = 0; i < self.subspace; i++) {
  294. var ff = false;
  295. self.sos[t[1]] = i
  296. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  297. if (ff) {
  298. ca.push(i);
  299. }
  300. }
  301. cands.push([t[1], ca]);
  302. }
  303. self.sos = self.sos.map(function(a) {
  304. return 0
  305. })
  306. var result = this.inner_solve(cands);
  307. this.emit("done", result);
  308. }
  309. mo.prototype.printit = function(vals) {
  310. var self = this;
  311. var vals = vals || self.sos;
  312. var lpad = function(s, l) {
  313. while (s.length < l) {
  314. s = " " + s
  315. };
  316. return s;
  317. }
  318. var rpad = function(s, l) {
  319. while (s.length < l) {
  320. s = s + " "
  321. };
  322. return s;
  323. }
  324. var s = [];
  325. s.push([" ", 1, 2, 3, 4, 5].map(function(a) {
  326. return lpad("" + a, 12)
  327. }).join(" "));
  328. Object.keys(self.variabler).map(function(k, ia) {
  329. s.push([k, 0, 1, 2, 3, 4].map(function(a, io) {
  330. return a == k ? rpad(k + ":" + vals[ia], 20) : lpad("" + self.variabler[k][self.current[vals[ia]][a]], 12)
  331. }).join(" "))
  332. })
  333. console.log(s.join("\n") + "\n")
  334. }
  335. module.exports = mo;