solver.js.c.js 4.7 KB

1
  1. function t(){this.__e={}}function s(){this.variabler={},this.funcs={},this.rulas=[]}t.prototype.__e={},t.prototype.emit=function(t){var s=this,e=[].slice.call(arguments,1);return(this.__e[t]||[]).map(function(t){t.map(function(t){t.apply(s,e)})}),this},t.prototype.on=function(t){var s=[].slice.call(arguments,1);return this.__e[t]=this.__e[t]||[],this.__e[t].push(s),this},function t(s,e){function r(){}r.prototype=e.prototype,s.prototype=new r}(s,t),s.prototype.addVariabelSet=function(t,s){return this.variabler[t]=s,this},s.prototype.addVariableTable=function(t,s){return this},s.prototype.addFunction=function(t,s){return this.funcs[t]=s,this},s.prototype.addClause=function(){for(var t,s,e,r,n,o,i=[].slice.call(arguments);i.length;)t=i.shift(),s=Object.keys(t),this.variabler[s[0]]&&(e=Object.keys(this.variabler).indexOf(s[0])),this.variabler[s[1]]&&(r=Object.keys(this.variabler).indexOf(s[1])),n=[],-1<e&&-1<r?(val1=this.variabler[s[0]].indexOf(t[s[0]]),val2=this.variabler[s[1]].indexOf(t[s[1]]),n=["check",e,r,val1,val2,[s[0],t[s[0]],s[1],t[s[1]]].join(" ")]):(o=s[s.length-1],this.funcs[o]&&(n=this.funcs[o].call(this,t)));return this.rulas.push(n),this},s.prototype.build=function(t){var s,e=[];for(s in this.variabler)e.push(function t(s){for(var e,r,n=[],o=0;o<s.length;o+=1)if((e=t(s.slice(0,o).concat(s.slice(o+1)))).length)for(r=0;r<e.length;r+=1)n.push([s[o]].concat(e[r]));else n.push([s[o]]);return n}(this.variabler[s].map(function(t,s){return s})));return e.sort(function(t,s){t.length>s.length||(t.length,s.length)}).shift()},s.prototype.checkPos=function(t,s,e,r){return this.current[this.sos[t]][r]==e},s.prototype.check=function(t,s,e,r){for(var n=0;n<5;n++)if(this.current[this.sos[t]][n]==e)return this.current[this.sos[s]][n]==r;return!1},s.prototype.checkLeft=function(t,s,e,r){for(var n=0;n<4;n++)if(this.current[this.sos[t]][n]==e)return this.current[this.sos[s]][n+1]==r;return!1},s.prototype.checkRight=function(t,s,e,r){for(var n=1;n<5;n++)if(this.current[this.sos[t]][n]==e)return this.current[this.sos[s]][n-1]==r;return!1},s.prototype.checkBoth=function(t,s,e,r){return this.checkLeft(t,s,e,r)||this.checkRight(t,s,e,r)},s.prototype.inner_solve=function(t){for(var s,e,r,n,o,i,c,u,a,l=this,p=(t.length,0),h=l.rulas.length,f=0,b=t.reduce(function(t,s){return s[1].length*t},1),v=0;v<t[0][1].length;v++)if(t[1])for(s=0;s<t[1][1].length;s++)if(t[2]){for(e=0;e<t[2][1].length;e++)if(!t[3])for(total=b*l.subspace*l.subspace,r=[0,1,2,3,4],l.sos[t[0][0]]=t[0][1][v],l.sos[t[1][0]]=t[1][1][s],l.sos[t[2][0]]=t[2][1][e],r.splice(r.indexOf(t[0][0]),1),r.splice(r.indexOf(t[1][0]),1),r.splice(r.indexOf(t[2][0]),1),n=0;n<l.subspace;n++)for(o=0;o<l.subspace;o++){for(p++,l.sos[r[0]]=n,l.sos[r[1]]=o,c=!(i=0);i<h&&c;)c=!1,u=l.rulas[i],(c=l[u.slice(0,1)[0]].apply(l,u.slice(1)))&&i++;if(f<i&&(f=i,a=l.rulas.map(function(t){return[l[t.slice(0,1)[0]].apply(l,t.slice(1))].concat(t)}),l.emit("part",p,i,f,h,l.sos,a.map(function(t){return t.join(" ")}))),h<=f)return l.sos;p%1e6==0&&l.emit("status",(p/total*100).toFixed(2)+"%",total,p,f,h)}}else for(r=[0,1,2,3,4],l.sos[t[0][0]]=t[0][1][v],l.sos[t[1][0]]=t[1][1][s],r.splice(r.indexOf(t[0][0]),1),r.splice(r.indexOf(t[1][0]),1),total=b*l.subspace*l.subspace*l.subspace,e=0;e<l.subspace;e++)for(n=0;n<l.subspace;n++)for(o=0;o<l.subspace;o++){for(p++,l.sos[r[0]]=e,l.sos[r[1]]=n,l.sos[r[2]]=o,c=!(i=0);i<h&&c;)c=!1,u=l.rulas[i],(c=l[u.slice(0,1)[0]].apply(l,u.slice(1)))&&i++;if(f<i&&(f=i,a=l.rulas.map(function(t){return[l[t.slice(0,1)[0]].apply(l,t.slice(1))].concat(t)}),l.emit("part",p,i,f,h,l.sos,a.map(function(t){return t.join(" ")}))),h<=f)return l.sos;p%1e6==0&&console.log("X3",p,i,f,h,":>",l.sos[t[0][0]],l.sos[t[1][0]],e,n,o)}return!1},s.prototype.solve=function(){var t,s,e,r,n,o,i,c,u=this;for(u.current=this.build("House"),console.log(u.current),t=Object.keys(this.variabler),u.sos=t.map(function(t){return 0}),u.subspace=u.current.length,u.varspace=t.length,s=[],u.rulas.map(function(t){console.log(t),t[1]==t[2]&&s.push(t)}),u.sos=u.sos.map(function(t){return 0}),console.log(s),e=[],r=0;r<s.length;r++){for(u.sos=u.sos.map(function(t){return 0}),n=s[r],o=[],i=0;i<u.subspace;i++)u.sos[n[1]]=i,u[n.slice(0,1)[0]].apply(u,n.slice(1))&&o.push(i);e.push([n[1],o])}u.sos=u.sos.map(function(t){return 0}),c=this.inner_solve(e),this.emit("done",c)},s.prototype.printit=function(n){var o,i,t,c=this;n=n||c.sos,o=function(t,s){for(;t.length<s;)t=" "+t;return t},i=function(t,s){for(;t.length<s;)t+=" ";return t},(t=[]).push([" ",1,2,3,4,5].map(function(t){return o(""+t,12)}).join(" ")),Object.keys(c.variabler).map(function(e,r){t.push([e,0,1,2,3,4].map(function(t,s){return t==e?i(e+":"+n[r],20):o(""+c.variabler[e][c.current[n[r]][t]],12)}).join(" "))}),console.log(t.join("\n")+"\n")},module.exports=s;