ein2.js.c.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. function mo() {
  2. this.variabler = {};
  3. this.tabel = {};
  4. this.tabel_navn = "";
  5. this.regler = [];
  6. this._internal_step_counter = 0;
  7. this.funcs = {};
  8. }
  9. mo.prototype.addVariabelSet = function(navn, arr) {
  10. this.variabler[navn] = arr;
  11. return this;
  12. }
  13. mo.prototype.addVariableTable = function(navn, arr) {
  14. this.tabel[navn] = arr;
  15. this.tabel_navn = navn;
  16. return this;
  17. }
  18. mo.prototype.addClause = function() {
  19. var args = Array.prototype.slice.apply(arguments, []);
  20. this.regler.push(args.shift());
  21. return this;
  22. }
  23. mo.prototype.addFunction = function(name,func) {
  24. this.funcs[name] = func;
  25. return this;
  26. }
  27. /*mo.prototype.randoma = function(tabl) {
  28. var ll = this.tabel[tabl].length,
  29. l = 0;
  30. var r = [];
  31. var taken = {}
  32. for (var navn in this.variabler) {
  33. taken[navn] = {}
  34. }
  35. while (l < ll) {
  36. var o = {}
  37. o[tabl] = this.tabel[tabl][l];
  38. for (var navn in this.variabler) {
  39. var m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)];
  40. while (taken[navn][m]) {
  41. m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)];
  42. this._internal_step_counter++;
  43. }
  44. taken[navn][m] = 1;
  45. o[navn] = m
  46. }
  47. r.push(o);
  48. l++
  49. }
  50. return r;
  51. }*/
  52. mo.prototype.buildBase = function() {
  53. var tabl = this.tabel_navn;
  54. var ll = this.tabel[tabl].length,
  55. l = 0;
  56. var r = [];
  57. var taken = {}
  58. for (var navn in this.variabler) {
  59. taken[navn] = {}
  60. }
  61. while (l < ll) {
  62. var o = {}
  63. o[tabl] = this.tabel[tabl][l];
  64. for (var navn in this.variabler) {
  65. var m = this.variabler[navn][l];
  66. /* while (taken[navn][m]) {
  67. m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)];
  68. this._internal_step_counter++;
  69. }*/
  70. // taken[navn][m] = 1;
  71. o[navn] = m
  72. }
  73. r.push(o);
  74. o._sure = [];
  75. l++
  76. }
  77. return r;
  78. }
  79. function rra(w){
  80. return function(a){
  81. return a!==w
  82. }
  83. }
  84. mo.prototype.solve = function() {
  85. var current = this.buildBase()
  86. var tabl = this.tabel_navn;
  87. this.regler.map(function(rule){
  88. if(rule[tabl]){
  89. var ruleTarget = current.filter(function(a){ return a[tabl] === rule[tabl]}).shift();
  90. Object.keys(rule).filter(rra(tabl)).map(function(prop){
  91. var currentHolder = current.filter(function(a){ return a[prop]===rule[prop]}).shift();
  92. currentHolder[prop] = ruleTarget[prop]
  93. ruleTarget[prop] = rule[prop];
  94. ruleTarget._sure.push(prop)
  95. })
  96. }
  97. })
  98. var rep = this.regler.filter(function(rule){ return !rule[tabl] })
  99. //this.randoma("House");
  100. var sat = false;
  101. var ii = 0;
  102. while (!sat && ii < 100000) {
  103. ii++
  104. // console.log(this.regler.length);
  105. /* var ffs = this.regler.map(function(reg) {
  106. var t = false;
  107. try {
  108. t = eval(reg);
  109. } catch (e) {
  110. // console.log("EE",e);
  111. }
  112. return t;
  113. })
  114. var longa = ffs.filter(function(a) {
  115. return a === true
  116. }).length;
  117. if(longa >= longest){
  118. longest = longa;
  119. ffou = current;
  120. }
  121. */
  122. /* if (longa === ffs.length) {
  123. sat = true;
  124. } else {
  125. current = this.randoma("House");
  126. }
  127. */
  128. if (ii % 100000 === 0) {
  129. console.log(ii);
  130. }
  131. }
  132. // console.log(longest);
  133. // console.log("TEST",t);
  134. // console.log(current);
  135. // console.log(this.regler);
  136. if (sat) {
  137. console.log("Found Solution:", current);
  138. }
  139. console.log(current);
  140. }
  141. mo.prototype.stats = function() {
  142. console.log("steps ", this._internal_step_counter)
  143. console.log(this);
  144. }
  145. var t = new mo()
  146. .addVariabelSet("Color", ["Green","Red", "White", "Yellow", "Blue" ])
  147. .addVariabelSet("Nationality", ["Norwegian", "Dane", "Swede", "Englishman", "German"])
  148. .addVariabelSet("Smokes", ["Prince", "Blend", "Dunhill", "BlueMasters", "PallMall"])
  149. .addVariabelSet("Drinks", ["Water", "Bier", "Milk", "Tea", "Coffee"])
  150. .addVariabelSet("Animals", ["Dogs", "Birds", "Cat", "Horses", "Fish"])
  151. .addVariableTable("House", [1, 2, 3, 4, 5])
  152. .addFunction("Neighbor", function() {})
  153. .addFunction("Neighbor_Before", function() {})
  154. .addFunction("Neighbor_After", function() {})
  155. .addClause({
  156. "Nationality": "Norwegian",
  157. "House": 1
  158. })
  159. .addClause({
  160. "Drinks": "Milk",
  161. "House": 3
  162. })
  163. .addClause({
  164. "Nationality": "Norwegian",
  165. "Neighbor": {
  166. "Color": "Blue"
  167. }
  168. })
  169. .addClause({
  170. "Nationality": "Englishman",
  171. "Color": "Red"
  172. })
  173. .addClause({
  174. "Smokes": "Blend",
  175. "Neighbor": {
  176. "Drinks": "Water"
  177. }
  178. })
  179. .addClause({
  180. "Nationality": "Swede",
  181. "Animals": "Dogs"
  182. })
  183. .addClause({
  184. "Nationality": "Dane",
  185. "Drinks": "Tea"
  186. })
  187. .addClause({
  188. "Color": "Green",
  189. "Neighbor_Before": {
  190. "Color": "White"
  191. }
  192. })
  193. .addClause({
  194. "Color": "Green",
  195. "Drinks": "Coffee"
  196. })
  197. .addClause({
  198. "Smokes": "PallMall",
  199. "Animals": "Birds"
  200. })
  201. .addClause({
  202. "Color": "Yellow",
  203. "Smokes": "Dunhill"
  204. })
  205. .addClause({
  206. "Smokes": "Blend",
  207. "Neighbor": {
  208. "Animals": "Cats"
  209. }
  210. })
  211. .addClause({
  212. "Animals": "Horses",
  213. "Neighbor": {
  214. "Smokes": "Dunhill"
  215. }
  216. })
  217. .addClause({
  218. "Smokes": "BlueMasters",
  219. "Drinks": "Bier"
  220. })
  221. .addClause({
  222. "Nationality": "German",
  223. "Smokes": "Prince"
  224. })
  225. t.solve();
  226. /*
  227. The Englishman lives in the red house.<br>
  228. The Swede keeps dogs.<br>
  229. The Dane drinks tea.<br>
  230. The green house is just to the left of the white one.<br>
  231. The owner of the green house drinks coffee.<br>
  232. The Pall Mall smoker keeps birds.<br>
  233. The owner of the yellow house smokes Dunhills.<br>
  234. The man in the center house drinks milk.<br>
  235. The Norwegian lives in the first house.<br>
  236. The Blend smoker has a neighbor who keeps cats.<br>
  237. The man who smokes Blue Masters drinks bier.<br>
  238. The man who keeps horses lives next to the Dunhill smoker.<br>
  239. The German smokes Prince.<br>
  240. The Norwegian lives next to the blue house.<br>
  241. The Blend smoker has a neighbor who drinks water.<br>
  242. */
  243. //console.log(t.randoma("House"));
  244. //t.stats();
  245. /*
  246. var Colors = ["Red", "White", "Yellow", "Blue", "Green"],
  247. Nationality = ["Norwegian", "Dane", "Swede", "Englishman", "German"],
  248. Cigarettes = ["Prince", "Blend", "Dunhill", "Blue Masters", "Pall Mall"],
  249. Drinks = ["Water", "Bier", "Milk", "Tea", "Coffee"],
  250. Animals = ["Dogs", "Birds", "Cat", "Horses", "Fish"];*/