yalps
Version:
Yet another linear programming solver. (A rewrite of javascript-lp-solver.) Aims to be decently fast.
2 lines • 9.91 kB
JavaScript
;var YALPS=(()=>{var he=Object.create;var _=Object.defineProperty;var de=Object.getOwnPropertyDescriptor;var me=Object.getOwnPropertyNames;var be=Object.getPrototypeOf,ye=Object.prototype.hasOwnProperty;var j=(e,n)=>()=>(n||e((n={exports:{}}).exports,n),n.exports),ge=(e,n)=>{for(var l in n)_(e,l,{get:n[l],enumerable:!0})},U=(e,n,l,u)=>{if(n&&typeof n=="object"||typeof n=="function")for(let c of me(n))!ye.call(e,c)&&c!==l&&_(e,c,{get:()=>n[c],enumerable:!(u=de(n,c))||u.enumerable});return e};var ve=(e,n,l)=>(l=e!=null?he(be(e)):{},U(n||!e||!e.__esModule?_(l,"default",{value:e,enumerable:!0}):l,e)),we=e=>U(_({},"__esModule",{value:!0}),e);var Y=j((z,X)=>{"use strict";(function(){var e,n,l,u,c,h,g,s,f,b,V,p,v,x,C;l=Math.floor,b=Math.min,n=function(t,o){return t<o?-1:t>o?1:0},f=function(t,o,i,a,y){var r;if(i==null&&(i=0),y==null&&(y=n),i<0)throw new Error("lo must be non-negative");for(a==null&&(a=t.length);i<a;)r=l((i+a)/2),y(o,t[r])<0?a=r:i=r+1;return[].splice.apply(t,[i,i-i].concat(o)),o},h=function(t,o,i){return i==null&&(i=n),t.push(o),x(t,0,t.length-1,i)},c=function(t,o){var i,a;return o==null&&(o=n),i=t.pop(),t.length?(a=t[0],t[0]=i,C(t,0,o)):a=i,a},s=function(t,o,i){var a;return i==null&&(i=n),a=t[0],t[0]=o,C(t,0,i),a},g=function(t,o,i){var a;return i==null&&(i=n),t.length&&i(t[0],o)<0&&(a=[t[0],o],o=a[0],t[0]=a[1],C(t,0,i)),o},u=function(t,o){var i,a,y,r,d,m,w,A;for(o==null&&(o=n),m=(function(){A=[];for(var M=0,I=l(t.length/2);0<=I?M<I:M>I;0<=I?M++:M--)A.push(M);return A}).apply(this).reverse(),w=[],a=0,r=m.length;a<r;a++)i=m[a],w.push(C(t,i,o));return w},v=function(t,o,i){var a;if(i==null&&(i=n),a=t.indexOf(o),a!==-1)return x(t,0,a,i),C(t,a,i)},V=function(t,o,i){var a,y,r,d,m;if(i==null&&(i=n),y=t.slice(0,o),!y.length)return y;for(u(y,i),m=t.slice(o),r=0,d=m.length;r<d;r++)a=m[r],g(y,a,i);return y.sort(i).reverse()},p=function(t,o,i){var a,y,r,d,m,w,A,M,I,O;if(i==null&&(i=n),o*10<=t.length){if(d=t.slice(0,o).sort(i),!d.length)return d;for(r=d[d.length-1],M=t.slice(o),m=0,A=M.length;m<A;m++)a=M[m],i(a,r)<0&&(f(d,a,0,null,i),d.pop(),r=d[d.length-1]);return d}for(u(t,i),O=[],y=w=0,I=b(o,t.length);0<=I?w<I:w>I;y=0<=I?++w:--w)O.push(c(t,i));return O},x=function(t,o,i,a){var y,r,d;for(a==null&&(a=n),y=t[i];i>o;){if(d=i-1>>1,r=t[d],a(y,r)<0){t[i]=r,i=d;continue}break}return t[i]=y},C=function(t,o,i){var a,y,r,d,m;for(i==null&&(i=n),y=t.length,m=o,r=t[o],a=2*o+1;a<y;)d=a+1,d<y&&!(i(t[a],t[d])<0)&&(a=d),t[o]=t[a],o=a,a=2*o+1;return t[o]=r,x(t,m,o,i)},e=(function(){t.push=h,t.pop=c,t.replace=s,t.pushpop=g,t.heapify=u,t.updateItem=v,t.nlargest=V,t.nsmallest=p;function t(o){this.cmp=o??n,this.nodes=[]}return t.prototype.push=function(o){return h(this.nodes,o,this.cmp)},t.prototype.pop=function(){return c(this.nodes,this.cmp)},t.prototype.peek=function(){return this.nodes[0]},t.prototype.contains=function(o){return this.nodes.indexOf(o)!==-1},t.prototype.replace=function(o){return s(this.nodes,o,this.cmp)},t.prototype.pushpop=function(o){return g(this.nodes,o,this.cmp)},t.prototype.heapify=function(){return u(this.nodes,this.cmp)},t.prototype.updateItem=function(o){return v(this.nodes,o,this.cmp)},t.prototype.clear=function(){return this.nodes=[]},t.prototype.empty=function(){return this.nodes.length===0},t.prototype.size=function(){return this.nodes.length},t.prototype.clone=function(){var o;return o=new t,o.nodes=this.nodes.slice(0),o},t.prototype.toArray=function(){return this.nodes.slice(0)},t.prototype.insert=t.prototype.push,t.prototype.top=t.prototype.peek,t.prototype.front=t.prototype.peek,t.prototype.has=t.prototype.contains,t.prototype.copy=t.prototype.clone,t})(),(function(t,o){return typeof define=="function"&&define.amd?define([],o):typeof z=="object"?X.exports=o():t.Heap=o()})(this,function(){return e})}).call(z)});var ee=j((qe,$)=>{"use strict";$.exports=Y()});var Ie={};ge(Ie,{defaultOptions:()=>le,equalTo:()=>Ce,greaterEq:()=>xe,inRange:()=>Ke,lessEq:()=>Ve,solve:()=>ue});var Ve=e=>({max:e}),xe=e=>({min:e}),Ce=e=>({equal:e}),Ke=(e,n)=>({min:e,max:n});var K=(e,n,l)=>e.matrix[Math.imul(n,e.width)+l],T=(e,n,l,u)=>{e.matrix[Math.imul(n,e.width)+l]=u},L=e=>Symbol.iterator in e&&typeof e[Symbol.iterator]=="function"?e:Object.entries(e),G=e=>e===!0?!0:e===!1?new Set:e instanceof Set?e:new Set(e),J=e=>{let{direction:n,objective:l,integers:u,binaries:c}=e,h=n==="minimize"?-1:1,g=L(e.constraints),s=L(e.variables),f=Array.isArray(s)?s:Array.from(s),b=[],V=[];if(u!=null||c!=null){let r=G(c),d=r===!0?!0:G(u);for(let m=1;m<=f.length;m++){let[w]=f[m-1];r===!0||r.has(w)?(b.push(m),V.push(m)):(d===!0||d.has(w))&&V.push(m)}}let p=new Map;for(let[r,d]of g){let m=p.get(r)??{row:NaN,lower:-1/0,upper:1/0};m.lower=Math.max(m.lower,d.equal??d.min??-1/0),m.upper=Math.min(m.upper,d.equal??d.max??1/0),p.has(r)||p.set(r,m)}let v=1;for(let r of p.values())r.row=v,v+=(Number.isFinite(r.lower)?1:0)+(Number.isFinite(r.upper)?1:0);let x=f.length+1,C=v+b.length,t=x+C,o=new Float64Array(x*C),i=new Int32Array(t),a=new Int32Array(t),y={matrix:o,width:x,height:C,positionOfVariable:i,variableAtPosition:a};for(let r=0;r<t;r++)i[r]=r,a[r]=r;for(let r=1;r<x;r++)for(let[d,m]of L(f[r-1][1])){d===l&&T(y,0,r,h*m);let w=p.get(d);w!=null&&(Number.isFinite(w.upper)?(T(y,w.row,r,m),Number.isFinite(w.lower)&&T(y,w.row+1,r,-m)):Number.isFinite(w.lower)&&T(y,w.row,r,-m))}for(let r of p.values())Number.isFinite(r.upper)?(T(y,r.row,0,r.upper),Number.isFinite(r.lower)&&T(y,r.row+1,0,-r.lower)):Number.isFinite(r.lower)&&T(y,r.row,0,-r.lower);for(let r=0;r<b.length;r++){let d=v+r;T(y,d,0,1),T(y,d,b[r],1)}return{tableau:y,sign:h,variables:f,integers:V}};var q=(e,n)=>{let l=Math.round(1/n);return Math.round((e+Number.EPSILON)*l)/l};var Q=(e,n,l)=>{let u=K(e,n,l),c=e.variableAtPosition[e.width+n],h=e.variableAtPosition[l];e.variableAtPosition[e.width+n]=h,e.variableAtPosition[l]=c,e.positionOfVariable[c]=l,e.positionOfVariable[h]=e.width+n;let g=[];for(let s=0;s<e.width;s++){let f=K(e,n,s);Math.abs(f)>1e-16?(T(e,n,s,f/u),g.push(s)):T(e,n,s,0)}T(e,n,l,1/u);for(let s=0;s<e.height;s++){if(s===n)continue;let f=K(e,s,l);if(Math.abs(f)>1e-16){for(let b=0;b<g.length;b++){let V=g[b];T(e,s,V,K(e,s,V)-f*K(e,n,V))}T(e,s,l,-f/u)}}},W=(e,n,l,u)=>{e.push([n.variableAtPosition[n.width+l],n.variableAtPosition[u]]);for(let c=6;c<=Math.trunc(e.length/2);c++){let h=!0;for(let g=0;g<c;g++){let s=e.length-1-g,[f,b]=e[s],[V,p]=e[s-c];if(f!==V||b!==p){h=!1;break}}if(h)return!0}return!1},Te=(e,n)=>{let l=[],{precision:u,maxPivots:c,checkCycles:h}=n;for(let g=0;g<c;g++){let s=0,f=u;for(let p=1;p<e.width;p++){let v=K(e,0,p);v>f&&(f=v,s=p)}if(s===0)return["optimal",q(K(e,0,0),u)];let b=0,V=1/0;for(let p=1;p<e.height;p++){let v=K(e,p,s);if(v<=u)continue;let C=K(e,p,0)/v;if(C<V&&(b=p,V=C,C<=u))break}if(b===0)return["unbounded",s];if(h&&W(l,e,b,s))return["cycled",NaN];Q(e,b,s)}return["cycled",NaN]},H=(e,n)=>{let l=[],{precision:u,maxPivots:c,checkCycles:h}=n;for(let g=0;g<c;g++){let s=0,f=-u;for(let p=1;p<e.height;p++){let v=K(e,p,0);v<f&&(f=v,s=p)}if(s===0)return Te(e,n);let b=0,V=-1/0;for(let p=1;p<e.width;p++){let v=K(e,s,p);if(v<-u){let x=-K(e,0,p)/v;x>V&&(V=x,b=p)}}if(b===0)return["infeasible",NaN];if(h&&W(l,e,s,b))return["cycled",NaN];Q(e,s,b)}return["cycled",NaN]};var oe=ve(ee(),1),te=(e,n)=>({matrix:new Float64Array(e),positionOfVariable:new Int32Array(n),variableAtPosition:new Int32Array(n)}),Me=(e,{matrix:n,positionOfVariable:l,variableAtPosition:u},c)=>{let{width:h,height:g}=e;n.set(e.matrix);for(let f=0;f<c.length;f++){let[b,V,p]=c[f],v=(g+f)*h,x=e.positionOfVariable[V];if(x<h)n[v]=b*p,n.fill(0,v+1,v+h),n[v+x]=b;else{let C=(x-h)*h;n[v]=b*(p-n[C]);for(let t=1;t<h;t++)n[v+t]=-b*n[C+t]}}l.set(e.positionOfVariable),u.set(e.variableAtPosition);let s=h+g+c.length;for(let f=h+g;f<s;f++)l[f]=f,u[f]=f;return{matrix:n.subarray(0,e.matrix.length+h*c.length),width:h,height:g+c.length,positionOfVariable:l.subarray(0,s),variableAtPosition:u.subarray(0,s)}},ne=(e,n)=>{let l=0,u=0,c=0;for(let h=0;h<n.length;h++){let g=n[h],s=e.positionOfVariable[g]-e.width;if(s<0)continue;let f=K(e,s,0),b=Math.abs(f-Math.round(f));b>l&&(l=b,u=g,c=f)}return[u,c,l]},re=(e,n,l)=>{let{tableau:u,sign:c,integers:h}=e,{precision:g,maxIterations:s,tolerance:f,timeout:b}=l,[V,p,v]=ne(u,h);if(v<=g)return[e,"optimal",n];let x=new oe.default((R,S)=>R[0]-S[0]);x.push([n,[[-1,V,Math.ceil(p)]]]),x.push([n,[[1,V,Math.floor(p)]]]);let C=h.length*2,t=u.matrix.length+C*u.width,o=u.positionOfVariable.length+C,i=te(t,o),a=te(t,o),y=n*(1-c*f),r=b+Date.now(),d=Date.now()>=r,m=!1,w=1/0,A=u,M=0;for(;M<s&&!x.empty()&&w>=y&&!d;){let[R,S]=x.pop();if(R>w)break;let B=Me(u,i,S),[ae,F]=H(B,l);if(ae==="optimal"&&F<w){let[E,Z,fe]=ne(B,h);if(fe<=g){m=!0,w=F,A=B;let N=a;a=i,i=N}else{let N=[],k=[];for(let D=0;D<S.length;D++){let P=S[D],[ce,pe]=P;pe===E?ce<0?k.push(P):N.push(P):(N.push(P),k.push(P))}k.push([1,E,Math.floor(Z)]),N.push([-1,E,Math.ceil(Z)]),x.push([F,N]),x.push([F,k])}}d=Date.now()>=r,M++}let O=(d||M>=s)&&!x.empty()&&w>=y?"timedout":m?"optimal":"infeasible";return[{...e,tableau:A},O,m?w:NaN]};var ie=({tableau:e,sign:n,variables:l},u,c,{precision:h,includeZeroVariables:g})=>{if(u==="optimal"||u==="timedout"&&!Number.isNaN(c)){let s=[];for(let f=0;f<l.length;f++){let[b]=l[f],V=e.positionOfVariable[f+1]-e.width,p=V>=0?K(e,V,0):0;p>h?s.push([b,q(p,h)]):g&&s.push([b,0])}return{status:u,result:-n*c,variables:s}}else if(u==="unbounded"){let s=e.variableAtPosition[c]-1;return{status:"unbounded",result:n*(1/0),variables:0<=s&&s<l.length?[[l[s][0],1/0]]:[]}}else return{status:u,result:NaN,variables:[]}},se={precision:1e-8,checkCycles:!1,maxPivots:8192,tolerance:0,timeout:1/0,maxIterations:32768,includeZeroVariables:!1},le={...se},ue=(e,n)=>{let l=J(e),u={...se,...n},[c,h]=H(l.tableau,u);if(l.integers.length===0||c!=="optimal")return ie(l,c,h,u);{let[g,s,f]=re(l,h,u);return ie(g,s,f,u)}};return we(Ie);})();
//# sourceMappingURL=index.min.js.map