123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- function mo() {
- this.variabler = {};
- this.tabel = {};
- this.tabel_navn = "";
- this.regler = [];
- this._internal_step_counter = 0;
- this.funcs = {};
- }
- mo.prototype.addVariabelSet = function(navn, arr) {
- this.variabler[navn] = arr;
- return this;
- }
- mo.prototype.addVariableTable = function(navn, arr) {
- this.tabel[navn] = arr;
- this.tabel_navn = navn;
- return this;
- }
- mo.prototype.addClause = function() {
- var args = Array.prototype.slice.apply(arguments, []);
- this.regler.push(args.shift());
- return this;
- }
- mo.prototype.addFunction = function(name,func) {
- this.funcs[name] = func;
- return this;
- }
- /*mo.prototype.randoma = function(tabl) {
- var ll = this.tabel[tabl].length,
- l = 0;
- var r = [];
- var taken = {}
- for (var navn in this.variabler) {
- taken[navn] = {}
- }
- while (l < ll) {
- var o = {}
- o[tabl] = this.tabel[tabl][l];
- for (var navn in this.variabler) {
- var m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)];
-
- while (taken[navn][m]) {
- m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)];
- this._internal_step_counter++;
- }
- taken[navn][m] = 1;
- o[navn] = m
- }
- r.push(o);
- l++
- }
- return r;
- }*/
- mo.prototype.buildBase = function() {
- var tabl = this.tabel_navn;
- var ll = this.tabel[tabl].length,
- l = 0;
- var r = [];
- var taken = {}
- for (var navn in this.variabler) {
- taken[navn] = {}
- }
- while (l < ll) {
- var o = {}
- o[tabl] = this.tabel[tabl][l];
-
- for (var navn in this.variabler) {
-
- var m = this.variabler[navn][l];
-
- /* while (taken[navn][m]) {
- m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)];
- this._internal_step_counter++;
- }*/
- // taken[navn][m] = 1;
- o[navn] = m
-
- }
- r.push(o);
- o._sure = [];
- l++
- }
- return r;
- }
- function rra(w){
- return function(a){
- return a!==w
- }
- }
- mo.prototype.solve = function() {
- var current = this.buildBase()
- var tabl = this.tabel_navn;
- this.regler.map(function(rule){
- if(rule[tabl]){
- var ruleTarget = current.filter(function(a){ return a[tabl] === rule[tabl]}).shift();
- Object.keys(rule).filter(rra(tabl)).map(function(prop){
- var currentHolder = current.filter(function(a){ return a[prop]===rule[prop]}).shift();
- currentHolder[prop] = ruleTarget[prop]
- ruleTarget[prop] = rule[prop];
- ruleTarget._sure.push(prop)
- })
- }
- })
- var rep = this.regler.filter(function(rule){ return !rule[tabl] })
- //this.randoma("House");
- var sat = false;
- var ii = 0;
-
- while (!sat && ii < 100000) {
- ii++
-
- // console.log(this.regler.length);
- /* var ffs = this.regler.map(function(reg) {
- var t = false;
- try {
- t = eval(reg);
- } catch (e) {
- // console.log("EE",e);
- }
- return t;
- })
- var longa = ffs.filter(function(a) {
- return a === true
- }).length;
- if(longa >= longest){
- longest = longa;
- ffou = current;
- }
- */
- /* if (longa === ffs.length) {
- sat = true;
- } else {
- current = this.randoma("House");
- }
- */
- if (ii % 100000 === 0) {
- console.log(ii);
- }
- }
- // console.log(longest);
- // console.log("TEST",t);
- // console.log(current);
- // console.log(this.regler);
- if (sat) {
- console.log("Found Solution:", current);
- }
- console.log(current);
- }
- mo.prototype.stats = function() {
- console.log("steps ", this._internal_step_counter)
- console.log(this);
- }
- var t = new mo()
- .addVariabelSet("Color", ["Green","Red", "White", "Yellow", "Blue" ])
- .addVariabelSet("Nationality", ["Norwegian", "Dane", "Swede", "Englishman", "German"])
- .addVariabelSet("Smokes", ["Prince", "Blend", "Dunhill", "BlueMasters", "PallMall"])
- .addVariabelSet("Drinks", ["Water", "Bier", "Milk", "Tea", "Coffee"])
- .addVariabelSet("Animals", ["Dogs", "Birds", "Cat", "Horses", "Fish"])
- .addVariableTable("House", [1, 2, 3, 4, 5])
- .addFunction("Neighbor", function() {})
- .addFunction("Neighbor_Before", function() {})
- .addFunction("Neighbor_After", function() {})
- .addClause({
- "Nationality": "Norwegian",
- "House": 1
- })
- .addClause({
- "Drinks": "Milk",
- "House": 3
- })
- .addClause({
- "Nationality": "Norwegian",
- "Neighbor": {
- "Color": "Blue"
- }
- })
- .addClause({
- "Nationality": "Englishman",
- "Color": "Red"
- })
- .addClause({
- "Smokes": "Blend",
- "Neighbor": {
- "Drinks": "Water"
- }
- })
- .addClause({
- "Nationality": "Swede",
- "Animals": "Dogs"
- })
- .addClause({
- "Nationality": "Dane",
- "Drinks": "Tea"
- })
- .addClause({
- "Color": "Green",
- "Neighbor_Before": {
- "Color": "White"
- }
- })
- .addClause({
- "Color": "Green",
- "Drinks": "Coffee"
- })
- .addClause({
- "Smokes": "PallMall",
- "Animals": "Birds"
- })
- .addClause({
- "Color": "Yellow",
- "Smokes": "Dunhill"
- })
- .addClause({
- "Smokes": "Blend",
- "Neighbor": {
- "Animals": "Cats"
- }
- })
- .addClause({
- "Animals": "Horses",
- "Neighbor": {
- "Smokes": "Dunhill"
- }
- })
- .addClause({
- "Smokes": "BlueMasters",
- "Drinks": "Bier"
- })
- .addClause({
- "Nationality": "German",
- "Smokes": "Prince"
- })
-
-
- t.solve();
- /*
- The Englishman lives in the red house.<br>
- The Swede keeps dogs.<br>
- The Dane drinks tea.<br>
- The green house is just to the left of the white one.<br>
- The owner of the green house drinks coffee.<br>
- The Pall Mall smoker keeps birds.<br>
- The owner of the yellow house smokes Dunhills.<br>
- The man in the center house drinks milk.<br>
- The Norwegian lives in the first house.<br>
- The Blend smoker has a neighbor who keeps cats.<br>
- The man who smokes Blue Masters drinks bier.<br>
- The man who keeps horses lives next to the Dunhill smoker.<br>
- The German smokes Prince.<br>
- The Norwegian lives next to the blue house.<br>
- The Blend smoker has a neighbor who drinks water.<br>
- */
- //console.log(t.randoma("House"));
- //t.stats();
- /*
- var Colors = ["Red", "White", "Yellow", "Blue", "Green"],
- Nationality = ["Norwegian", "Dane", "Swede", "Englishman", "German"],
- Cigarettes = ["Prince", "Blend", "Dunhill", "Blue Masters", "Pall Mall"],
- Drinks = ["Water", "Bier", "Milk", "Tea", "Coffee"],
- Animals = ["Dogs", "Birds", "Cat", "Horses", "Fish"];*/
|