UNPKG

d3-dag

Version:

Layout algorithms for visualizing directed acylic graphs.

2 lines 64.6 kB
// d3-dag Version 0.3.3. Copyright 2019 undefined. !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.d3=t.d3||{})}(this,function(t){"use strict";function e(){let t=i;return function(e,i){e.forEach(t=>t.forEach(t=>t.degree=t.children.length+(t.data?0:-3))),e.forEach(t=>t.forEach(t=>t.children.forEach(t=>++t.degree))),e[0][0].x=0,e[0].slice(1).forEach((t,r)=>{const n=e[0][r];t.x=n.x+i(n,t)}),e.slice(0,e.length-1).forEach((r,n)=>{const a=e[n+1];t(r,a),a.map((t,e)=>[t,e]).sort(([t,e],[i,r])=>t.degree===i.degree?e-r:i.degree-t.degree).forEach(([t,e])=>{a.slice(e+1).reduce((t,e)=>(e.x=Math.max(e.x,t.x+i(t,e)),e),t),a.slice(0,e).reverse().reduce((t,e)=>(e.x=Math.min(e.x,t.x-i(e,t)),e),t)})});const r=Math.min(...e.map(t=>Math.min(...t.map(t=>t.x)))),n=Math.max(...e.map(t=>Math.max(...t.map(t=>t.x))))-r;return e.forEach(t=>t.forEach(t=>t.x=(t.x-r)/n)),e.forEach(t=>t.forEach(t=>delete t.degree)),e}}function i(t,e){e.forEach(t=>{t.x=0,t._count=0}),t.forEach(t=>t.children.forEach(e=>e.x+=(t.x-e.x)/++e._count)),e.forEach(t=>delete t._count)}let r,n,a=1e-60;do{r=1+.1*(a+=a),n=1+.2*a}while(r<=1||n<=1);var o=a;var s=function(t,e,i){let r,n;for(let e=1;e<=i;e+=1){t[e][e]=1/t[e][e],n=-t[e][e];for(let i=1;i<e;i+=1)t[i][e]*=n;if(i<(r=e+1))break;for(let a=r;a<=i;a+=1){n=t[e][a],t[e][a]=0;for(let i=1;i<=e;i+=1)t[i][a]+=n*t[i][e]}}};var h=function(t,e,i,r){let n,a;for(n=1;n<=i;n+=1){a=0;for(let e=1;e<n;e+=1)a+=t[e][n]*r[e];r[n]=(r[n]-a)/t[n][n]}for(let e=1;e<=i;e+=1){r[n=i+1-e]/=t[n][n],a=-r[n];for(let e=1;e<n;e+=1)r[e]+=a*t[e][n]}};var l=function(t,e,i,r){let n,a,o;for(let e=1;e<=i;e+=1){if(r[1]=e,o=0,(n=e-1)<1){if((o=t[e][e]-o)<=0)break;t[e][e]=Math.sqrt(o)}else{for(let i=1;i<=n;i+=1){a=t[i][e];for(let r=1;r<i;r+=1)a-=t[r][e]*t[r][i];a/=t[i][i],t[i][e]=a,o+=a*a}if((o=t[e][e]-o)<=0)break;t[e][e]=Math.sqrt(o)}r[1]=0}};var c=function(t,e,i,r,n,a,c,u,d,f,p,v,m,y=0,x,g,b){let w,I,E,C,B,_,V,M,k,O,z,j,R,S;const A=Math.min(r,p);let $=2*r+A*(A+5)/2+2*p+1;for(let t=1;t<=r;t+=1)g[t]=e[t];for(let t=r+1;t<=$;t+=1)g[t]=0;for(let t=1;t<=p;t+=1)m[t]=0,a[t]=0;const T=[];if(0===b[1]){if(l(t,i,r,T),0!==T[1])return void(b[1]=2);h(t,i,r,e),s(t,i,r)}else{for(let i=1;i<=r;i+=1){n[i]=0;for(let r=1;r<=i;r+=1)n[i]+=t[r][i]*e[r]}for(let i=1;i<=r;i+=1){e[i]=0;for(let a=i;a<=r;a+=1)e[i]+=t[i][a]*n[a]}}c[1]=0;for(let i=1;i<=r;i+=1){n[i]=e[i],c[1]+=g[i]*n[i],g[i]=0;for(let e=i+1;e<=r;e+=1)t[e][i]=0}c[1]=-c[1]/2,b[1]=0;const P=r,N=P+r,L=N+A,q=L+A+1,D=q+A*(A+1)/2,F=D+p;for(let t=1;t<=p;t+=1){_=0;for(let e=1;e<=r;e+=1)_+=u[e][t]*u[e][t];g[F+t]=Math.sqrt(_)}function H(){x[1]+=1,$=D;for(let t=1;t<=p;t+=1){$+=1,_=-d[t];for(let e=1;e<=r;e+=1)_+=u[e][t]*n[e];if(Math.abs(_)<o&&(_=0),t>v)g[$]=_;else if(g[$]=-Math.abs(_),_>0){for(let e=1;e<=r;e+=1)u[e][t]=-u[e][t];d[t]=-d[t]}}for(let t=1;t<=C;t+=1)g[D+m[t]]=0;E=0,B=0;for(let t=1;t<=p;t+=1)g[D+t]<B*g[F+t]&&(E=t,B=g[D+t]/g[F+t]);if(0===E){for(let t=1;t<=C;t+=1)a[m[t]]=g[L+t];return 999}return 0}function U(){for(let e=1;e<=r;e+=1){_=0;for(let i=1;i<=r;i+=1)_+=t[i][e]*u[i][E];g[e]=_}w=P;for(let t=1;t<=r;t+=1)g[w+t]=0;for(let e=C+1;e<=r;e+=1)for(let i=1;i<=r;i+=1)g[w+i]=g[w+i]+t[i][e]*g[e];j=!0;for(let t=C;t>=1;t-=1){_=g[t],w=($=q+t*(t+3)/2)-t;for(let e=t+1;e<=C;e+=1)_-=g[$]*g[N+e],$+=e;_/=g[w],g[N+t]=_,m[t]<=v||_<=0||(j=!1,I=t)}if(!j){V=g[L+I]/g[N+I];for(let t=1;t<=C;t+=1)m[t]<=v||g[N+t]<=0||(B=g[L+t]/g[N+t])<V&&(V=B,I=t)}_=0;for(let t=P+1;t<=P+r;t+=1)_+=g[t]*g[t];if(Math.abs(_)<=o){if(j)return b[1]=1,999;for(let t=1;t<=C;t+=1)g[L+t]=g[L+t]-V*g[N+t];return g[L+C+1]=g[L+C+1]+V,700}_=0;for(let t=1;t<=r;t+=1)_+=g[P+t]*u[t][E];M=-g[D+E]/_,R=!0,j||V<M&&(M=V,R=!1);for(let t=1;t<=r;t+=1)n[t]+=M*g[P+t],Math.abs(n[t])<o&&(n[t]=0);c[1]+=M*_*(M/2+g[L+C+1]);for(let t=1;t<=C;t+=1)g[L+t]=g[L+t]-M*g[N+t];if(g[L+C+1]=g[L+C+1]+M,!R){_=-d[E];for(let t=1;t<=r;t+=1)_+=n[t]*u[t][E];if(E>v)g[D+E]=_;else if(g[D+E]=-Math.abs(_),_>0){for(let t=1;t<=r;t+=1)u[t][E]=-u[t][E];d[E]=-d[E]}return 700}m[C+=1]=E,$=q+(C-1)*C/2+1;for(let t=1;t<=C-1;t+=1)g[$]=g[t],$+=1;if(C===r)g[$]=g[r];else{for(let e=r;e>=C+1;e-=1)if(0!==g[e]&&(k=Math.max(Math.abs(g[e-1]),Math.abs(g[e])),O=Math.min(Math.abs(g[e-1]),Math.abs(g[e])),B=g[e-1]>=0?Math.abs(k*Math.sqrt(1+O*O/(k*k))):-Math.abs(k*Math.sqrt(1+O*O/(k*k))),k=g[e-1]/B,O=g[e]/B,1!==k))if(0===k){g[e-1]=O*B;for(let i=1;i<=r;i+=1)B=t[i][e-1],t[i][e-1]=t[i][e],t[i][e]=B}else{g[e-1]=B,z=O/(1+k);for(let i=1;i<=r;i+=1)B=k*t[i][e-1]+O*t[i][e],t[i][e]=z*(t[i][e-1]+B)-t[i][e],t[i][e-1]=B}g[$]=g[C]}return 0}function W(){if(0===g[w=($=q+I*(I+1)/2+1)+I])return 798;if(k=Math.max(Math.abs(g[w-1]),Math.abs(g[w])),O=Math.min(Math.abs(g[w-1]),Math.abs(g[w])),B=g[w-1]>=0?Math.abs(k*Math.sqrt(1+O*O/(k*k))):-Math.abs(k*Math.sqrt(1+O*O/(k*k))),k=g[w-1]/B,O=g[w]/B,1===k)return 798;if(0===k){for(let t=I+1;t<=C;t+=1)B=g[w-1],g[w-1]=g[w],g[w]=B,w+=t;for(let e=1;e<=r;e+=1)B=t[e][I],t[e][I]=t[e][I+1],t[e][I+1]=B}else{z=O/(1+k);for(let t=I+1;t<=C;t+=1)B=k*g[w-1]+O*g[w],g[w]=z*(g[w-1]+B)-g[w],g[w-1]=B,w+=t;for(let e=1;e<=r;e+=1)B=k*t[e][I]+O*t[e][I+1],t[e][I+1]=z*(t[e][I]+B)-t[e][I+1],t[e][I]=B}return 0}function J(){w=$-I;for(let t=1;t<=I;t+=1)g[w]=g[$],$+=1,w+=1;return g[L+I]=g[L+I+1],m[I]=m[I+1],(I+=1)<C?797:0}function Z(){return g[L+C]=g[L+C+1],g[L+C+1]=0,m[C]=0,C-=1,x[2]+=1,0}for(C=y,x[1]=0,x[2]=0,S=0;;){if(999===(S=H()))return;for(;0!==(S=U());){if(999===S)return;if(700===S)if(I===C)Z();else{for(;W(),797===(S=J()););Z()}}}};var u={solveQP:function(t,e,i,r=[],n=0,a=[0,0]){const o=[],s=[],h=[],l=[],u=[],d=[];let f="";const p=t.length-1,v=i[1].length-1;if(!r)for(let t=1;t<=v;t+=1)r[t]=0;if(p!==t[1].length-1&&(f="Dmat is not symmetric!"),p!==e.length-1&&(f="Dmat and dvec are incompatible!"),p!==i.length-1&&(f="Amat and dvec are incompatible!"),v!==r.length-1&&(f="Amat and bvec are incompatible!"),(n>v||n<0)&&(f="Value of meq is invalid!"),""!==f)return{message:f};for(let t=1;t<=v;t+=1)s[t]=0,l[t]=0;const m=Math.min(p,v);for(let t=1;t<=p;t+=1)h[t]=0;o[1]=0;for(let t=1;t<=2*p+m*(m+5)/2+2*v+1;t+=1)u[t]=0;for(let t=1;t<=2;t+=1)d[t]=0;return c(t,e,p,p,h,l,o,i,r,p,v,n,s,0,d,u,a),1===a[1]&&(f="constraints are inconsistent, no solution!"),2===a[1]&&(f="matrix D in quadratic function is not positive definite!"),{solution:h,Lagrangian:l,value:o,unconstrained_solution:e,iterations:d,iact:s,message:f}}};const{solveQP:d}=u;var f=function(t,e,i,r,n=0,a=!1){const o=[null].concat(t.map(t=>[null].concat(t))),s=[null].concat(e.map(t=>-t)),h=[null].concat(0===i.length?new Array(t.length).fill([null]):i[0].map((t,e)=>[null].concat(i.map(t=>-t[e])))),l=[null].concat(r.map(t=>-t)),{solution:c,Lagrangian:u,value:f,unconstrained_solution:p,iterations:v,iact:m,message:y}=d(o,s,h,l,n,[,+a]);if(y.length>0)throw new Error(y);{c.shift(),u.shift(),p.shift(),m.push(0);const t=m.slice(1,m.indexOf(0)).map(t=>t-1),[,e]=f,[,i,r]=v;return{solution:c,lagrangian:u,unconstrained:p,iterations:i,inactive:r,active:t,value:e}}};function p(t){const e={};let i=0;return t.forEach(t=>t.forEach(t=>e[t.id]=i++)),e}function v(t,e,i){const r=1+Math.max(...Object.values(e)),n=[],a=[];return t.forEach(t=>t.slice(0,t.length-1).forEach((o,s)=>{const h=t[s+1],l=e[o.id],c=e[h.id],u=new Array(r).fill(0);u[l]=1,u[c]=-1,n.push(u),a.push(-i(o,h))})),[n,a]}function m(t,e,i,r){t[i][i]+=r,t[i][e]-=r,t[e][i]-=r,t[e][e]+=r}function y(t,e,i,r,n){t[r][r]+=n,t[r][i]-=2*n,t[r][e]+=n,t[i][r]-=2*n,t[i][i]+=4*n,t[i][e]-=2*n,t[e][r]+=n,t[e][i]-=2*n,t[e][e]+=n}function x(t,e,i,r,n=0){e.pop(),t.pop(),t.forEach(t=>t.pop()),i.forEach(t=>t.pop());const{solution:a}=f(t,e,i,r,n);return a.push(0),a}function g(t,e,i){const r=Math.min(...i),n=Math.max(...i)-r;t.forEach(t=>t.forEach(t=>t.x=(i[e[t.id]]-r)/n))}function b(){return function(t,e,i){const r=Math.max(...t.map(t=>Math.max(...t.map(t=>t.columnIndex+1))));let n=Array.from(Array(r).keys()).map((t,e)=>e).map(e),a=Array.from(Array(r).keys()).map((t,e)=>e).map(i);const o=Math.max(...t.map(t=>(t.forEach(t=>{t.x0=function(t,e,i){let r=t.filter((t,e)=>e<i),n=e.filter((t,e)=>e<i);return r.concat(n).reduce((t,e)=>t+e,0)}(n,a,t.columnIndex),t.x1=t.x0+n[t.columnIndex]}),Math.max(...t.map(t=>t.x1)))));return t.forEach(t=>{t.forEach(t=>{t.x0=t.x0/o,t.x1=t.x1/o})}),t}}function w(){let t=!1;function e(e){if(0==e.length)return;let i=[];e.forEach(t=>t.forEach(t=>{null!=t.parents&&0!==t.parents.length||i.push(t)}));let r=0;function n(t){return 0===t.children.length?1:t.children.reduce((t,e)=>t+n(e),0)}i.forEach(e=>{const i=n(e);e.columnIndex=r+(t?Math.floor((i-1)/2):0),function e(i,r){const a=i.children.map(n);let o=r;i.children.forEach((i,r)=>{void 0===i.columnIndex&&(i.columnIndex=o+(t?Math.floor((a[r]-1)/2):0),e(i,o),o+=a[r])})}(e,r),r+=i})}return e.center=function(i){return arguments.length?(t=i,e):t},e}function I(t){const e={};return t.each(t=>e[t.id]=[t.data,function(t){const e={};return t.forEach(t=>e[t]=!0),e}(t.children.map(t=>t.id))]),e}const E={};const C={};function B(t,e){this.id=t,this.data=e,this.children=[],this._childLinkData=[]}function _(t){if(void 0!==t.id)throw new Error("invalid format for verification");if(!t.children.length)throw new Error("no roots");const e={},i={};let r=void 0;function n(t){if(e[t.id])return!1;if(i[t.id])return r=t.id,[t.id];{i[t.id]=!0;let a=t.children.reduce((t,e)=>t||n(e),!1);return delete i[t.id],e[t.id]=!0,a&&r&&a.push(t.id),r===t.id&&(r=void 0),a}}const a=void 0===t.id?t.children.reduce((t,e)=>t||n(e),!1):n(t);if(a)throw new Error("dag contained a cycle: "+a.reverse().join(" -> "));if(t.each(t=>{if(t.id.indexOf("\0")>=0)throw new Error("node id contained null character");if(!t.data)throw new Error("node contained falsy data")}),t.links().some(({data:t})=>!t))throw new Error("dag had falsy link data")}function V(){if(arguments.length)throw Error(`got arguments to dagStratify(${arguments}), but constructor takes no aruguments. `+"These were probably meant as data which should be called as dagStratify()(...)");let t=M,e=k,i=O;function r(r){if(!r.length)throw new Error("can't stratify empty data");const n=r.map((e,i)=>{const r=t(e,i);try{return new B(r.toString(),e)}catch(t){throw Error(`node ids must have toString but got ${r} from ${e}`)}}),a={};n.forEach(t=>{if(a[t.id])throw new Error("found a duplicate id: "+t.id);a[t.id]=t});const o=new B(void 0,void 0);return n.forEach(t=>{const r=e(t.data)||[];r.forEach(e=>{const r=a[e];if(!r)throw new Error("missing id: "+e);return r.children.push(t),r._childLinkData.push(i(r.data,t.data)),r}),r.length||o.children.push(t)}),_(o),o.children.length>1?o:o.children[0]}return r.id=function(e){return arguments.length?(t=e,r):t},r.parentIds=function(t){return arguments.length?(e=t,r):e},r.linkData=function(t){return arguments.length?(i=t,r):i},r}function M(t){return t.id}function k(t){return t.parentIds}function O(){return{}}function z(t){return t[0]}function j(t){return t[1]}function R(t){return t}function S(t){return t.id}function A(t){return t.children}function $(){return{}}function T(t,e,i,r){this.feasible=i,this.evaluation=e,this.bounded=r,this._tableau=t}B.prototype={constructor:B,childLinks:function(){const t=[];return this.eachChildLinks(e=>t.push(e)),t},copy:function(){const t=[],e=[],i={};if(this.each(r=>{t.push(r);const n=new B(r.id,r.data);e.push(n),i[n.id]=n}),e.forEach((e,r)=>{const n=t[r];e.children=n.children.map(t=>i[t.id])}),void 0!==this.id)return i[this.id];new B(void 0,void 0).children=this.children.map(t=>i[t.id])},count:function(){return this.eachAfter(t=>{t.children.length?(t._leaves=Object.assign({},...t.children.map(t=>t._leaves)),t.value=Object.keys(t._leaves).length):(t._leaves={[t.id]:!0},t.value=1)}),this.each(t=>delete t._leaves),this},connected:function(){if(void 0!==this.id)return!0;const t=this.roots().map(t=>t.descendants().map(t=>t.id)),e=t.map(()=>!1),i=[e.length-1];for(;i.length;){const r=i.pop();if(e[r])continue;const n={};e[r]=!0,t[r].forEach(t=>n[t]=!0),t.forEach((t,e)=>{t.some(t=>n[t])&&i.push(e)})}return e.every(t=>t)},depth:function(){return this.each(t=>{t.children.forEach(e=>(e._parents||(e._parents=[])).push(t))}),this.eachBefore(t=>{t.value=Math.max(0,...(t._parents||[]).map(t=>1+t.value))}),this.each(t=>delete t._parents),this},descendants:function(){const t=[];return this.each(e=>t.push(e)),t},each:function(t){const e=this.roots(),i={};let r,n=0;for(;r=e.pop();)i[r.id]||(i[r.id]=!0,t(r,n++),e.push(...r.children));return this},eachAfter:function(t){const e=[];return this.eachBefore(t=>e.push(t)),e.reverse().forEach(t),this},eachBefore:function(t){this.each(t=>t._num_before=0),this.each(t=>t.children.forEach(t=>++t._num_before));const e=this.roots();let i,r=0;for(;i=e.pop();)t(i,r++),i.children.forEach(t=>--t._num_before||e.push(t));return this.each(t=>delete t._num_before),this},eachBreadth:function(t){const e={};let i=[],r=this.roots(),n=0;do{let a;for(i=r.reverse(),r=[];a=i.pop();)e[a.id]||(e[a.id]=!0,t(a,n++),r.push(...a.children))}while(r.length)},eachChildLinks:function(t){if(void 0!==this.id){let e=0;this.children.forEach((i,r)=>t({source:this,target:i,data:this._childLinkData[r]},e++))}return this},eachLinks:function(t){let e=0;return this.each(i=>i.eachChildLinks(i=>t(i,e++))),this},equals:function(t){const e=I(this),i=I(t);return Object.keys(e).length===Object.keys(i).length&&Object.entries(e).every(([t,[e,a]])=>{const o=i[t];if(!o)return!1;const[s,h]=o;return e===s&&(r=a,n=h,Object.keys(r).length===Object.keys(n).length&&Object.keys(r).every(t=>n[t]))});var r,n},every:function(t){try{this.each((e,i)=>{if(!t(e,i))throw E})}catch(t){if(t===E)return!1;throw t}return!0},height:function(){return this.eachAfter(t=>t.value=Math.max(0,...t.children.map(t=>1+t.value)))},links:function(){const t=[];return this.eachLinks(e=>t.push(e)),t},reduce:function(t,e){let i=e;return this.each((e,r)=>{i=t(i,e,r)}),i},reverse:function(){const t=[],e=[],i={},r=new B(void 0,void 0);return this.each(n=>{t.push(n);const a=new B(n.id,n.data);e.push(a),i[a.id]=a,n.children.length||r.children.push(a)}),e.forEach((e,r)=>{const n=t[r];n.children.map((t,r)=>{const a=i[t.id];a.children.push(e),a._childLinkData.push(n._childLinkData[r])})}),r.children.length>1?r:r.children[0]},roots:function(){return void 0===this.id?this.children.slice():[this]},size:function(){return this.reduce(t=>t+1,0)},some:function(t){try{this.each(e=>{if(t(e))throw C})}catch(t){if(t===C)return!0;throw t}return!1},sum:function(t){return this.eachAfter((e,i)=>{const r=+t(e.data,i);e._descendants=Object.assign({[e.id]:r},...e.children.map(t=>t._descendants)),e.value=Object.values(e._descendants).reduce((t,e)=>t+e)}),this.each(t=>delete t._descendants),this}};var P=T;function N(t,e,i,r,n){P.call(this,t,e,i,r),this.iter=n}T.prototype.generateSolutionSet=function(){for(var t={},e=this._tableau,i=e.varIndexByRow,r=e.variablesPerIndex,n=e.matrix,a=e.rhsColumn,o=e.height-1,s=Math.round(1/e.precision),h=1;h<=o;h+=1){var l=r[i[h]];if(void 0!==l&&!0!==l.isSlack){var c=n[h][a];t[l.id]=Math.round((Number.EPSILON+c)*s)/s}}return t};var L=N;function q(t){this.model=null,this.matrix=null,this.width=0,this.height=0,this.costRowIndex=0,this.rhsColumn=0,this.variablesPerIndex=[],this.unrestrictedVars=null,this.feasible=!0,this.evaluation=0,this.simplexIters=0,this.varIndexByRow=null,this.varIndexByCol=null,this.rowByVarIndex=null,this.colByVarIndex=null,this.precision=t||1e-8,this.optionalObjectives=[],this.objectivesByPriority={},this.savedState=null,this.availableIndexes=[],this.lastElementIndex=0,this.variables=null,this.nVars=0,this.bounded=!0,this.unboundedVarIndex=null,this.branchAndCutIterations=0}N.prototype=Object.create(P.prototype),N.constructor=N;var D=q;function F(t,e){this.priority=t,this.reducedCosts=new Array(e);for(var i=0;i<e;i+=1)this.reducedCosts[i]=0}q.prototype.solve=function(){return this.model.getNumberOfIntegerVariables()>0?this.branchAndCut():this.simplex(),this.updateVariableValues(),this.getSolution()},F.prototype.copy=function(){var t=new F(this.priority,this.reducedCosts.length);return t.reducedCosts=this.reducedCosts.slice(),t},q.prototype.setOptionalObjective=function(t,e,i){var r=this.objectivesByPriority[t];void 0===r&&(r=new F(t,Math.max(this.width,e+1)),this.objectivesByPriority[t]=r,this.optionalObjectives.push(r),this.optionalObjectives.sort(function(t,e){return t.priority-e.priority}));r.reducedCosts[e]=i},q.prototype.initialize=function(t,e,i,r){this.variables=i,this.unrestrictedVars=r,this.width=t,this.height=e;for(var n=new Array(t),a=0;a<t;a++)n[a]=0;this.matrix=new Array(e);for(var o=0;o<e;o++)this.matrix[o]=n.slice();this.varIndexByRow=new Array(this.height),this.varIndexByCol=new Array(this.width),this.varIndexByRow[0]=-1,this.varIndexByCol[0]=-1,this.nVars=t+e-2,this.rowByVarIndex=new Array(this.nVars),this.colByVarIndex=new Array(this.nVars),this.lastElementIndex=this.nVars},q.prototype._resetMatrix=function(){var t,e,i=this.model.variables,r=this.model.constraints,n=i.length,a=r.length,o=this.matrix[0],s=!0===this.model.isMinimization?-1:1;for(t=0;t<n;t+=1){var h=i[t],l=h.priority,c=s*h.cost;0===l?o[t+1]=c:this.setOptionalObjective(l,t+1,c),e=i[t].index,this.rowByVarIndex[e]=-1,this.colByVarIndex[e]=t+1,this.varIndexByCol[t+1]=e}for(var u=1,d=0;d<a;d+=1){var f,p,v=r[d],m=v.index;this.rowByVarIndex[m]=u,this.colByVarIndex[m]=-1,this.varIndexByRow[u]=m;var y=v.terms,x=y.length,g=this.matrix[u++];if(v.isUpperBound){for(f=0;f<x;f+=1)p=y[f],g[this.colByVarIndex[p.variable.index]]=p.coefficient;g[0]=v.rhs}else{for(f=0;f<x;f+=1)p=y[f],g[this.colByVarIndex[p.variable.index]]=-p.coefficient;g[0]=-v.rhs}}},q.prototype.setModel=function(t){this.model=t;var e=t.nVariables+1,i=t.nConstraints+1;return this.initialize(e,i,t.variables,t.unrestrictedVariables),this._resetMatrix(),this},q.prototype.getNewElementIndex=function(){if(this.availableIndexes.length>0)return this.availableIndexes.pop();var t=this.lastElementIndex;return this.lastElementIndex+=1,t},q.prototype.density=function(){for(var t=0,e=this.matrix,i=0;i<this.height;i++)for(var r=e[i],n=0;n<this.width;n++)0!==r[n]&&(t+=1);return t/(this.height*this.width)},q.prototype.setEvaluation=function(){var t=Math.round(1/this.precision),e=this.matrix[this.costRowIndex][this.rhsColumn],i=Math.round((Number.EPSILON+e)*t)/t;this.evaluation=i,0===this.simplexIters&&(this.bestPossibleEval=i)},q.prototype.getSolution=function(){var t=!0===this.model.isMinimization?this.evaluation:-this.evaluation;return this.model.getNumberOfIntegerVariables()>0?new L(this,t,this.feasible,this.bounded,this.branchAndCutIterations):new P(this,t,this.feasible,this.bounded)},D.prototype.simplex=function(){return this.bounded=!0,this.phase1(),!0===this.feasible&&this.phase2(),this},D.prototype.phase1=function(){for(var t=this.model.checkForCycles,e=[],i=this.matrix,r=this.rhsColumn,n=this.width-1,a=this.height-1,o=0;;){for(var s=0,h=-this.precision,l=1;l<=a;l++){!0===this.unrestrictedVars[this.varIndexByRow[l]];var c=i[l][r];c<h&&(h=c,s=l)}if(0===s)return this.feasible=!0,o;for(var u=0,d=-1/0,f=i[0],p=i[s],v=1;v<=n;v++){var m=p[v];if(!0===this.unrestrictedVars[this.varIndexByCol[v]]||m<-this.precision){var y=-f[v]/m;d<y&&(d=y,u=v)}}if(0===u)return this.feasible=!1,o;if(t){e.push([this.varIndexByRow[s],this.varIndexByCol[u]]);var x=this.checkForCycles(e);if(x.length>0)return this.model.messages.push("Cycle in phase 1"),this.model.messages.push("Start :"+x[0]),this.model.messages.push("Length :"+x[1]),this.feasible=!1,o}this.pivot(s,u),o+=1}},D.prototype.phase2=function(){for(var t,e,i=this.model.checkForCycles,r=[],n=this.matrix,a=this.rhsColumn,o=this.width-1,s=this.height-1,h=this.precision,l=this.optionalObjectives.length,c=null,u=0;;){var d=n[this.costRowIndex];l>0&&(c=[]);for(var f=0,p=h,v=!1,m=1;m<=o;m++)t=d[m],e=!0===this.unrestrictedVars[this.varIndexByCol[m]],l>0&&-h<t&&t<h?c.push(m):e&&t<0?-t>p&&(p=-t,f=m,v=!0):t>p&&(p=t,f=m,v=!1);if(l>0)for(var y=0;0===f&&c.length>0&&y<l;){var x=[],g=this.optionalObjectives[y].reducedCosts;p=h;for(var b=0;b<c.length;b++)t=g[m=c[b]],e=!0===this.unrestrictedVars[this.varIndexByCol[m]],-h<t&&t<h?x.push(m):e&&t<0?-t>p&&(p=-t,f=m,v=!0):t>p&&(p=t,f=m,v=!1);c=x,y+=1}if(0===f)return this.setEvaluation(),this.simplexIters+=1,u;for(var w=0,I=1/0,E=(this.varIndexByRow,1);E<=s;E++){var C=n[E],B=C[a],_=C[f];if(!(-h<_&&_<h)){if(_>0&&h>B&&B>-h){I=0,w=E;break}var V=v?-B/_:B/_;V>h&&I>V&&(I=V,w=E)}}if(I===1/0)return this.evaluation=-1/0,this.bounded=!1,this.unboundedVarIndex=this.varIndexByCol[f],u;if(i){r.push([this.varIndexByRow[w],this.varIndexByCol[f]]);var M=this.checkForCycles(r);if(M.length>0)return this.model.messages.push("Cycle in phase 2"),this.model.messages.push("Start :"+M[0]),this.model.messages.push("Length :"+M[1]),this.feasible=!1,u}this.pivot(w,f,!0),u+=1}};var H=[];function U(t,e,i,r){this.id=t,this.cost=e,this.index=i,this.value=0,this.priority=r}function W(t,e,i,r){U.call(this,t,e,i,r)}function J(t,e){U.call(this,t,0,e,0)}function Z(t,e){this.variable=t,this.coefficient=e}function G(t,e,i){return 0===i||"required"===i?null:(e=e||1,i=i||1,!1===t.isMinimization&&(e=-e),t.addVariable(e,"r"+t.relaxationIndex++,!1,!1,i))}function Q(t,e,i,r){this.slack=new J("s"+i,i),this.index=i,this.model=r,this.rhs=t,this.isUpperBound=e,this.terms=[],this.termsByVarIndex={},this.relaxation=null}function K(t,e){this.upperBound=t,this.lowerBound=e,this.model=t.model,this.rhs=t.rhs,this.relaxation=null}D.prototype.pivot=function(t,e){var i=this.matrix,r=i[t][e],n=this.height-1,a=this.width-1,o=this.varIndexByRow[t],s=this.varIndexByCol[e];this.varIndexByRow[t]=s,this.varIndexByCol[e]=o,this.rowByVarIndex[s]=t,this.rowByVarIndex[o]=-1,this.colByVarIndex[s]=-1,this.colByVarIndex[o]=e;for(var h,l,c,u=i[t],d=0,f=0;f<=a;f++)u[f]>=-1e-16&&u[f]<=1e-16?u[f]=0:(u[f]/=r,H[d]=f,d+=1);u[e]=1/r;this.precision;for(var p=0;p<=n;p++)if(p!==t){var v=i[p];if((h=v[e])>=-1e-16&&h<=1e-16)0!==h&&(v[e]=0);else{for(l=0;l<d;l++)(c=u[f=H[l]])>=-1e-16&&c<=1e-16?0!==c&&(u[f]=0):v[f]=v[f]-h*c;v[e]=-h/r}}var m=this.optionalObjectives.length;if(m>0)for(var y=0;y<m;y+=1){var x=this.optionalObjectives[y].reducedCosts;if(0!==(h=x[e])){for(l=0;l<d;l++)0!==(c=u[f=H[l]])&&(x[f]=x[f]-h*c);x[e]=-h/r}}},D.prototype.checkForCycles=function(t){for(var e=0;e<t.length-1;e++)for(var i=e+1;i<t.length;i++){var r=t[e],n=t[i];if(r[0]===n[0]&&r[1]===n[1]){if(i-e>t.length-i)break;for(var a=!0,o=1;o<i-e;o++){var s=t[e+o],h=t[i+o];if(s[0]!==h[0]||s[1]!==h[1]){a=!1;break}}if(a)return[e,i-e]}}return[]},W.prototype.isInteger=!0,J.prototype.isSlack=!0,Q.prototype.addTerm=function(t,e){var i=e.index,r=this.termsByVarIndex[i];if(void 0===r)r=new Z(e,t),this.termsByVarIndex[i]=r,this.terms.push(r),!0===this.isUpperBound&&(t=-t),this.model.updateConstraintCoefficient(this,e,t);else{var n=r.coefficient+t;this.setVariableCoefficient(n,e)}return this},Q.prototype.removeTerm=function(t){return this},Q.prototype.setRightHandSide=function(t){if(t!==this.rhs){var e=t-this.rhs;!0===this.isUpperBound&&(e=-e),this.rhs=t,this.model.updateRightHandSide(this,e)}return this},Q.prototype.setVariableCoefficient=function(t,e){var i=e.index;if(-1!==i){var r=this.termsByVarIndex[i];if(void 0===r)this.addTerm(t,e);else if(t!==r.coefficient){var n=t-r.coefficient;!0===this.isUpperBound&&(n=-n),r.coefficient=t,this.model.updateConstraintCoefficient(this,e,n)}return this}console.warn("[Constraint.setVariableCoefficient] Trying to change coefficient of inexistant variable.")},Q.prototype.relax=function(t,e){this.relaxation=G(this.model,t,e),this._relax(this.relaxation)},Q.prototype._relax=function(t){null!==t&&(this.isUpperBound?this.setVariableCoefficient(-1,t):this.setVariableCoefficient(1,t))},K.prototype.isEquality=!0,K.prototype.addTerm=function(t,e){return this.upperBound.addTerm(t,e),this.lowerBound.addTerm(t,e),this},K.prototype.removeTerm=function(t){return this.upperBound.removeTerm(t),this.lowerBound.removeTerm(t),this},K.prototype.setRightHandSide=function(t){this.upperBound.setRightHandSide(t),this.lowerBound.setRightHandSide(t),this.rhs=t},K.prototype.relax=function(t,e){this.relaxation=G(this.model,t,e),this.upperBound.relaxation=this.relaxation,this.upperBound._relax(this.relaxation),this.lowerBound.relaxation=this.relaxation,this.lowerBound._relax(this.relaxation)};var X={Constraint:Q,Variable:U,IntegerVariable:W,SlackVariable:J,Equality:K,Term:Z},Y=X.SlackVariable;function tt(t,e){this.index=t,this.value=e}D.prototype.addCutConstraints=function(t){for(var e,i=t.length,r=this.height,n=r+i,a=r;a<n;a+=1)void 0===this.matrix[a]&&(this.matrix[a]=this.matrix[a-1].slice());this.height=n,this.nVars=this.width+this.height-2;for(var o=this.width-1,s=0;s<i;s+=1){var h=t[s],l=r+s,c="min"===h.type?-1:1,u=h.varIndex,d=this.rowByVarIndex[u],f=this.matrix[l];if(-1===d){for(f[this.rhsColumn]=c*h.value,e=1;e<=o;e+=1)f[e]=0;f[this.colByVarIndex[u]]=c}else{var p=this.matrix[d],v=p[this.rhsColumn];for(f[this.rhsColumn]=c*(h.value-v),e=1;e<=o;e+=1)f[e]=-c*p[e]}var m=this.getNewElementIndex();this.varIndexByRow[l]=m,this.rowByVarIndex[m]=l,this.colByVarIndex[m]=-1,this.variablesPerIndex[m]=new Y("s"+m,m),this.nVars+=1}},D.prototype._addLowerBoundMIRCut=function(t){if(t===this.costRowIndex)return!1;this.model;var e=this.matrix;if(!this.variablesPerIndex[this.varIndexByRow[t]].isInteger)return!1;var i=e[t][this.rhsColumn],r=i-Math.floor(i);if(r<this.precision||1-this.precision<r)return!1;var n=this.height;e[n]=e[n-1].slice(),this.height+=1,this.nVars+=1;var a=this.getNewElementIndex();this.varIndexByRow[n]=a,this.rowByVarIndex[a]=n,this.colByVarIndex[a]=-1,this.variablesPerIndex[a]=new Y("s"+a,a),e[n][this.rhsColumn]=Math.floor(i);for(var o=1;o<this.varIndexByCol.length;o+=1){if(this.variablesPerIndex[this.varIndexByCol[o]].isInteger){var s=e[t][o],h=Math.floor(s)+Math.max(0,s-Math.floor(s)-r)/(1-r);e[n][o]=h}else e[n][o]=Math.min(0,e[t][o]/(1-r))}for(var l=0;l<this.width;l+=1)e[n][l]-=e[t][l];return!0},D.prototype._addUpperBoundMIRCut=function(t){if(t===this.costRowIndex)return!1;this.model;var e=this.matrix;if(!this.variablesPerIndex[this.varIndexByRow[t]].isInteger)return!1;var i=e[t][this.rhsColumn],r=i-Math.floor(i);if(r<this.precision||1-this.precision<r)return!1;var n=this.height;e[n]=e[n-1].slice(),this.height+=1,this.nVars+=1;var a=this.getNewElementIndex();this.varIndexByRow[n]=a,this.rowByVarIndex[a]=n,this.colByVarIndex[a]=-1,this.variablesPerIndex[a]=new Y("s"+a,a),e[n][this.rhsColumn]=-r;for(var o=1;o<this.varIndexByCol.length;o+=1){var s=this.variablesPerIndex[this.varIndexByCol[o]],h=e[t][o],l=h-Math.floor(h);s.isInteger?e[n][o]=l<=r?-l:-(1-l)*r/l:e[n][o]=h>=0?-h:h*r/(1-r)}return!0},D.prototype.applyMIRCuts=function(){},D.prototype._putInBase=function(t){var e=this.rowByVarIndex[t];if(-1===e){for(var i=this.colByVarIndex[t],r=1;r<this.height;r+=1){var n=this.matrix[r][i];if(n<-this.precision||this.precision<n){e=r;break}}this.pivot(e,i)}return e},D.prototype._takeOutOfBase=function(t){var e=this.colByVarIndex[t];if(-1===e){for(var i=this.rowByVarIndex[t],r=this.matrix[i],n=1;n<this.height;n+=1){var a=r[n];if(a<-this.precision||this.precision<a){e=n;break}}this.pivot(i,e)}return e},D.prototype.updateVariableValues=function(){for(var t=this.variables.length,e=Math.round(1/this.precision),i=0;i<t;i+=1){var r=this.variables[i],n=r.index,a=this.rowByVarIndex[n];if(-1===a)r.value=0;else{var o=this.matrix[a][this.rhsColumn];r.value=Math.round((o+Number.EPSILON)*e)/e}}},D.prototype.updateRightHandSide=function(t,e){var i=this.height-1,r=this.rowByVarIndex[t.index];if(-1===r){for(var n=this.colByVarIndex[t.index],a=0;a<=i;a+=1){var o=this.matrix[a];o[this.rhsColumn]-=e*o[n]}var s=this.optionalObjectives.length;if(s>0)for(var h=0;h<s;h+=1){var l=this.optionalObjectives[h].reducedCosts;l[this.rhsColumn]-=e*l[n]}}else this.matrix[r][this.rhsColumn]-=e},D.prototype.updateConstraintCoefficient=function(t,e,i){if(t.index===e.index)throw new Error("[Tableau.updateConstraintCoefficient] constraint index should not be equal to variable index !");var r=this._putInBase(t.index),n=this.colByVarIndex[e.index];if(-1===n)for(var a=this.rowByVarIndex[e.index],o=0;o<this.width;o+=1)this.matrix[r][o]+=i*this.matrix[a][o];else this.matrix[r][n]-=i},D.prototype.updateCost=function(t,e){var i=t.index,r=this.width-1,n=this.colByVarIndex[i];if(-1===n){var a,o=this.matrix[this.rowByVarIndex[i]];if(0===t.priority){var s=this.matrix[0];for(a=0;a<=r;a+=1)s[a]+=e*o[a]}else{var h=this.objectivesByPriority[t.priority].reducedCosts;for(a=0;a<=r;a+=1)h[a]+=e*o[a]}}else this.matrix[0][n]-=e},D.prototype.addConstraint=function(t){var e=t.isUpperBound?1:-1,i=this.height,r=this.matrix[i];void 0===r&&(r=this.matrix[0].slice(),this.matrix[i]=r);for(var n=this.width-1,a=0;a<=n;a+=1)r[a]=0;r[this.rhsColumn]=e*t.rhs;for(var o=t.terms,s=o.length,h=0;h<s;h+=1){var l=o[h],c=l.coefficient,u=l.variable.index,d=this.rowByVarIndex[u];if(-1===d)r[this.colByVarIndex[u]]+=e*c;else{var f=this.matrix[d];f[this.rhsColumn];for(a=0;a<=n;a+=1)r[a]-=e*c*f[a]}}var p=t.index;this.varIndexByRow[i]=p,this.rowByVarIndex[p]=i,this.colByVarIndex[p]=-1,this.height+=1},D.prototype.removeConstraint=function(t){var e=t.index,i=this.height-1,r=this._putInBase(e),n=this.matrix[i];this.matrix[i]=this.matrix[r],this.matrix[r]=n,this.varIndexByRow[r]=this.varIndexByRow[i],this.varIndexByRow[i]=-1,this.rowByVarIndex[e]=-1,this.availableIndexes[this.availableIndexes.length]=e,t.slack.index=-1,this.height-=1},D.prototype.addVariable=function(t){var e=this.height-1,i=this.width,r=!0===this.model.isMinimization?-t.cost:t.cost,n=t.priority,a=this.optionalObjectives.length;if(a>0)for(var o=0;o<a;o+=1)this.optionalObjectives[o].reducedCosts[i]=0;0===n?this.matrix[0][i]=r:(this.setOptionalObjective(n,i,r),this.matrix[0][i]=0);for(var s=1;s<=e;s+=1)this.matrix[s][i]=0;var h=t.index;this.varIndexByCol[i]=h,this.rowByVarIndex[h]=-1,this.colByVarIndex[h]=i,this.width+=1},D.prototype.removeVariable=function(t){var e=t.index,i=this._takeOutOfBase(e),r=this.width-1;if(i!==r){for(var n=this.height-1,a=0;a<=n;a+=1){var o=this.matrix[a];o[i]=o[r]}var s=this.optionalObjectives.length;if(s>0)for(var h=0;h<s;h+=1){var l=this.optionalObjectives[h].reducedCosts;l[i]=l[r]}var c=this.varIndexByCol[r];this.varIndexByCol[i]=c,this.colByVarIndex[c]=i}this.varIndexByCol[r]=-1,this.colByVarIndex[e]=-1,this.availableIndexes[this.availableIndexes.length]=e,t.index=-1,this.width-=1},D.prototype.log=function(t,e){console.log("****",t,"****"),console.log("Nb Variables",this.width-1),console.log("Nb Constraints",this.height-1),console.log("Basic Indexes",this.varIndexByRow),console.log("Non Basic Indexes",this.varIndexByCol),console.log("Rows",this.rowByVarIndex),console.log("Cols",this.colByVarIndex);var i,r,n,a,o,s,h,l,c,u,d="",f=[" "];for(r=1;r<this.width;r+=1)o=this.varIndexByCol[r],h=" ",l="\t",(s=void 0===(a=this.variablesPerIndex[o])?"c"+o:a.id).length>5?h+=" ":l+="\t",f[r]=h,d+=l+s;console.log(d);var p=this.matrix[this.costRowIndex],v="\t";for(i=1;i<this.width;i+=1)v+="\t",v+=f[i],v+=p[i].toFixed(5);for(v+="\t"+f[0]+p[0].toFixed(5),console.log(v+"\tZ"),n=1;n<this.height;n+=1){for(c=this.matrix[n],u="\t",r=1;r<this.width;r+=1)u+="\t"+f[r]+c[r].toFixed(5);u+="\t"+f[0]+c[0].toFixed(5),o=this.varIndexByRow[n],s=void 0===(a=this.variablesPerIndex[o])?"c"+o:a.id,console.log(u+"\t"+s)}console.log("");var m=this.optionalObjectives.length;if(m>0){console.log(" Optional objectives:");for(var y=0;y<m;y+=1){var x=this.optionalObjectives[y].reducedCosts,g="";for(i=1;i<this.width;i+=1)g+=x[i]<0?"":" ",g+=f[i],g+=x[i].toFixed(5);g+=(x[0]<0?"":" ")+f[0]+x[0].toFixed(5),console.log(g+" z"+y)}}return console.log("Feasible?",this.feasible),console.log("evaluation",this.evaluation),this},D.prototype.copy=function(){var t=new D(this.precision);t.width=this.width,t.height=this.height,t.nVars=this.nVars,t.model=this.model,t.variables=this.variables,t.variablesPerIndex=this.variablesPerIndex,t.unrestrictedVars=this.unrestrictedVars,t.lastElementIndex=this.lastElementIndex,t.varIndexByRow=this.varIndexByRow.slice(),t.varIndexByCol=this.varIndexByCol.slice(),t.rowByVarIndex=this.rowByVarIndex.slice(),t.colByVarIndex=this.colByVarIndex.slice(),t.availableIndexes=this.availableIndexes.slice();for(var e=[],i=0;i<this.optionalObjectives.length;i++)e[i]=this.optionalObjectives[i].copy();t.optionalObjectives=e;for(var r=this.matrix,n=new Array(this.height),a=0;a<this.height;a++)n[a]=r[a].slice();return t.matrix=n,t},D.prototype.save=function(){this.savedState=this.copy()},D.prototype.restore=function(){if(null!==this.savedState){var t,e,i=this.savedState,r=i.matrix;for(this.nVars=i.nVars,this.model=i.model,this.variables=i.variables,this.variablesPerIndex=i.variablesPerIndex,this.unrestrictedVars=i.unrestrictedVars,this.lastElementIndex=i.lastElementIndex,this.width=i.width,this.height=i.height,t=0;t<this.height;t+=1){var n=r[t],a=this.matrix[t];for(e=0;e<this.width;e+=1)a[e]=n[e]}var o=i.varIndexByRow;for(e=0;e<this.height;e+=1)this.varIndexByRow[e]=o[e];for(;this.varIndexByRow.length>this.height;)this.varIndexByRow.pop();var s=i.varIndexByCol;for(t=0;t<this.width;t+=1)this.varIndexByCol[t]=s[t];for(;this.varIndexByCol.length>this.width;)this.varIndexByCol.pop();for(var h=i.rowByVarIndex,l=i.colByVarIndex,c=0;c<this.nVars;c+=1)this.rowByVarIndex[c]=h[c],this.colByVarIndex[c]=l[c];if(i.optionalObjectives.length>0&&this.optionalObjectives.length>0){this.optionalObjectives=[],this.optionalObjectivePerPriority={};for(var u=0;u<i.optionalObjectives.length;u++){var d=i.optionalObjectives[u].copy();this.optionalObjectives[u]=d,this.optionalObjectivePerPriority[d.priority]=d}}}},D.prototype.getMostFractionalVar=function(){for(var t=0,e=null,i=null,r=this.model.integerVariables,n=r.length,a=0;a<n;a++){var o=r[a].index,s=this.rowByVarIndex[o];if(-1!==s){var h=this.matrix[s][this.rhsColumn],l=Math.abs(h-Math.round(h));t<l&&(t=l,e=o,i=h)}}return new tt(e,i)},D.prototype.getFractionalVarWithLowestCost=function(){for(var t=1/0,e=null,i=null,r=this.model.integerVariables,n=r.length,a=0;a<n;a++){var o=r[a],s=o.index,h=this.rowByVarIndex[s];if(-1!==h){var l=this.matrix[h][this.rhsColumn];if(Math.abs(l-Math.round(l))>this.precision){var c=o.cost;t>c&&(t=c,e=s,i=l)}}}return new tt(e,i)},D.prototype.countIntegerValues=function(){for(var t=0,e=1;e<this.height;e+=1)if(this.variablesPerIndex[this.varIndexByRow[e]].isInteger){var i=this.matrix[e][this.rhsColumn];(i-=Math.floor(i))<this.precision&&-i<this.precision&&(t+=1)}return t},D.prototype.isIntegral=function(){for(var t=this.model.integerVariables,e=t.length,i=0;i<e;i++){var r=this.rowByVarIndex[t[i].index];if(-1!==r){var n=this.matrix[r][this.rhsColumn];if(Math.abs(n-Math.round(n))>this.precision)return!1}}return!0},D.prototype.computeFractionalVolume=function(t){for(var e=-1,i=1;i<this.height;i+=1)if(this.variablesPerIndex[this.varIndexByRow[i]].isInteger){var r=this.matrix[i][this.rhsColumn];if(r=Math.abs(r),Math.min(r-Math.floor(r),Math.floor(r+1))<this.precision){if(!t)return 0}else-1===e?e=r:e*=r}return-1===e?0:e};var et=D;function it(t,e,i){this.type=t,this.varIndex=e,this.value=i}function rt(t,e){this.relaxedEvaluation=t,this.cuts=e}function nt(t,e){return e.relaxedEvaluation-t.relaxedEvaluation}D.prototype.applyCuts=function(t){if(this.restore(),this.addCutConstraints(t),this.simplex(),this.model.useMIRCuts)for(var e=!0;e;){var i=this.computeFractionalVolume(!0);this.applyMIRCuts(),this.simplex(),this.computeFractionalVolume(!0)>=.9*i&&(e=!1)}},D.prototype.branchAndCut=function(){var t=[],e=0,i=this.model.tolerance,r=!0,n=1e99;this.model.timeout&&(n=Date.now()+this.model.timeout);for(var a=1/0,o=null,s=[],h=0;h<this.optionalObjectives.length;h+=1)s.push(1/0);var l,c=new rt(-1/0,[]);for(t.push(c);t.length>0&&!0===r&&Date.now()<n;)if(l=this.model.isMinimization?this.bestPossibleEval*(1+i):this.bestPossibleEval*(1-i),i>0&&a<l&&(r=!1),!((c=t.pop()).relaxedEvaluation>a)){var u=c.cuts;if(this.applyCuts(u),e++,!1!==this.feasible){var d=this.evaluation;if(!(d>a)){if(d===a){for(var f=!0,p=0;p<this.optionalObjectives.length&&!(this.optionalObjectives[p].reducedCosts[0]>s[p]);p+=1)if(this.optionalObjectives[p].reducedCosts[0]<s[p]){f=!1;break}if(f)continue}if(!0===this.isIntegral()){if(this.__isIntegral=!0,1===e)return void(this.branchAndCutIterations=e);o=c,a=d;for(var v=0;v<this.optionalObjectives.length;v+=1)s[v]=this.optionalObjectives[v].reducedCosts[0]}else{1===e&&this.save();for(var m=this.getMostFractionalVar(),y=m.index,x=[],g=[],b=u.length,w=0;w<b;w+=1){var I=u[w];I.varIndex===y?"min"===I.type?g.push(I):x.push(I):(x.push(I),g.push(I))}var E=Math.ceil(m.value),C=Math.floor(m.value),B=new it("min",y,E);x.push(B);var _=new it("max",y,C);g.push(_),t.push(new rt(d,x)),t.push(new rt(d,g)),t.sort(nt)}}}}null!==o&&this.applyCuts(o.cuts),this.branchAndCutIterations=e};var at={},ot=X.Constraint,st=X.Equality,ht=X.Variable,lt=X.IntegerVariable;function ct(t,e){this.tableau=new D(t),this.name=e,this.variables=[],this.integerVariables=[],this.unrestrictedVariables={},this.constraints=[],this.nConstraints=0,this.nVariables=0,this.isMinimization=!0,this.tableauInitialized=!1,this.relaxationIndex=1,this.useMIRCuts=!1,this.checkForCycles=!0,this.messages=[]}var ut=ct;ct.prototype.minimize=function(){return this.isMinimization=!0,this},ct.prototype.maximize=function(){return this.isMinimization=!1,this},ct.prototype._getNewElementIndex=function(){if(this.availableIndexes.length>0)return this.availableIndexes.pop();var t=this.lastElementIndex;return this.lastElementIndex+=1,t},ct.prototype._addConstraint=function(t){var e=t.slack;this.tableau.variablesPerIndex[e.index]=e,this.constraints.push(t),this.nConstraints+=1,!0===this.tableauInitialized&&this.tableau.addConstraint(t)},ct.prototype.smallerThan=function(t){var e=new ot(t,!0,this.tableau.getNewElementIndex(),this);return this._addConstraint(e),e},ct.prototype.greaterThan=function(t){var e=new ot(t,!1,this.tableau.getNewElementIndex(),this);return this._addConstraint(e),e},ct.prototype.equal=function(t){var e=new ot(t,!0,this.tableau.getNewElementIndex(),this);this._addConstraint(e);var i=new ot(t,!1,this.tableau.getNewElementIndex(),this);return this._addConstraint(i),new st(e,i)},ct.prototype.addVariable=function(t,e,i,r,n){if("string"==typeof n)switch(n){case"required":n=0;break;case"strong":n=1;break;case"medium":n=2;break;case"weak":n=3;break;default:n=0}var a,o=this.tableau.getNewElementIndex();return null!==e&&void 0!==e||(e="v"+o),null!==t&&void 0!==t||(t=0),null!==n&&void 0!==n||(n=0),i?(a=new lt(e,t,o,n),this.integerVariables.push(a)):a=new ht(e,t,o,n),this.variables.push(a),this.tableau.variablesPerIndex[o]=a,r&&(this.unrestrictedVariables[o]=!0),this.nVariables+=1,!0===this.tableauInitialized&&this.tableau.addVariable(a),a},ct.prototype._removeConstraint=function(t){var e=this.constraints.indexOf(t);-1!==e?(this.constraints.splice(e,1),this.nConstraints-=1,!0===this.tableauInitialized&&this.tableau.removeConstraint(t),t.relaxation&&this.removeVariable(t.relaxation)):console.warn("[Model.removeConstraint] Constraint not present in model")},ct.prototype.removeConstraint=function(t){return t.isEquality?(this._removeConstraint(t.upperBound),this._removeConstraint(t.lowerBound)):this._removeConstraint(t),this},ct.prototype.removeVariable=function(t){var e=this.variables.indexOf(t);if(-1!==e)return this.variables.splice(e,1),!0===this.tableauInitialized&&this.tableau.removeVariable(t),this;console.warn("[Model.removeVariable] Variable not present in model")},ct.prototype.updateRightHandSide=function(t,e){return!0===this.tableauInitialized&&this.tableau.updateRightHandSide(t,e),this},ct.prototype.updateConstraintCoefficient=function(t,e,i){return!0===this.tableauInitialized&&this.tableau.updateConstraintCoefficient(t,e,i),this},ct.prototype.setCost=function(t,e){var i=t-e.cost;return!1===this.isMinimization&&(i=-i),e.cost=t,this.tableau.updateCost(e,i),this},ct.prototype.loadJson=function(t){this.isMinimization="max"!==t.opType;for(var e=t.variables,i=t.constraints,r={},n={},a=Object.keys(i),o=a.length,s=0;s<o;s+=1){var h,l,c=a[s],u=i[c],d=u.equal,f=u.weight,p=u.priority,v=void 0!==f||void 0!==p;if(void 0===d){var m=u.min;void 0!==m&&(h=this.greaterThan(m),r[c]=h,v&&h.relax(f,p));var y=u.max;void 0!==y&&(l=this.smallerThan(y),n[c]=l,v&&l.relax(f,p))}else{h=this.greaterThan(d),r[c]=h,l=this.smallerThan(d),n[c]=l;var x=new st(h,l);v&&x.relax(f,p)}}var g=Object.keys(e),b=g.length;this.tolerance=t.tolerance||0,t.timeout&&(this.timeout=t.timeout),t.options&&(t.options.timeout&&(this.timeout=t.options.timeout),0===this.tolerance&&(this.tolerance=t.options.tolerance||0),t.options.useMIRCuts&&(this.useMIRCuts=t.options.useMIRCuts),void 0===t.options.exitOnCycles?this.checkForCycles=!0:this.checkForCycles=t.options.exitOnCycles);for(var w=t.ints||{},I=t.binaries||{},E=t.unrestricted||{},C=t.optimize,B=0;B<b;B+=1){var _=g[B],V=e[_],M=V[C]||0,k=!!I[_],O=!!w[_]||k,z=!!E[_],j=this.addVariable(M,_,O,z);k&&this.smallerThan(1).addTerm(1,j);var R=Object.keys(V);for(s=0;s<R.length;s+=1){var S=R[s];if(S!==C){var A=V[S],$=r[S];void 0!==$&&$.addTerm(A,j);var T=n[S];void 0!==T&&T.addTerm(A,j)}}}return this},ct.prototype.getNumberOfIntegerVariables=function(){return this.integerVariables.length},ct.prototype.solve=function(){return!1===this.tableauInitialized&&(this.tableau.setModel(this),this.tableauInitialized=!0),this.tableau.solve()},ct.prototype.isFeasible=function(){return this.tableau.feasible},ct.prototype.save=function(){return this.tableau.save()},ct.prototype.restore=function(){return this.tableau.restore()},ct.prototype.activateMIRCuts=function(t){this.useMIRCuts=t},ct.prototype.debug=function(t){this.checkForCycles=t},ct.prototype.log=function(t){return this.tableau.log(t)};var dt={CleanObjectiveAttributes:function(t){var e,i,r;if("string"==typeof t.optimize){if(t.constraints[t.optimize]){for(i in e=Math.random(),t.variables)t.variables[i][t.optimize]&&(t.variables[i][e]=t.variables[i][t.optimize]);return t.constraints[e]=t.constraints[t.optimize],delete t.constraints[t.optimize],t}return t}for(r in t.optimize)if(t.constraints[r])if("equal"===t.constraints[r])delete t.optimize[r];else{for(i in e=Math.random(),t.variables)t.variables[i][r]&&(t.variables[i][e]=t.variables[i][r]);t.constraints[e]=t.constraints[r],delete t.constraints[r]}return t}};var ft=function(t){return t.length?function(t){var e={is_blank:/^\W{0,}$/,is_objective:/(max|min)(imize){0,}\:/i,is_int:/^(?!\/\*)\W{0,}int/i,is_bin:/^(?!\/\*)\W{0,}bin/i,is_constraint:/(\>|\<){0,}\=/i,is_unrestricted:/^\S{0,}unrestricted/i,parse_lhs:/(\-|\+){0,1}\s{0,1}\d{0,}\.{0,}\d{0,}\s{0,}[A-Za-z]\S{0,}/gi,parse_rhs:/(\-|\+){0,1}\d{1,}\.{0,}\d{0,}\W{0,}\;{0,1}$/i,parse_dir:/(\>|\<){0,}\=/gi,parse_int:/[^\s|^\,]+/gi,parse_bin:/[^\s|^\,]+/gi,get_num:/(\-|\+){0,1}(\W|^)\d+\.{0,1}\d{0,}/g,get_word:/[A-Za-z].*/},i={opType:"",optimize:"_obj",constraints:{},variables:{}},r={">=":"min","<=":"max","=":"equal"},n="",a=null,o="",s="",h="",l=0;"string"==typeof t&&(t=t.split("\n"));for(var c=0;c<t.length;c++)if(h="__"+c,n=t[c],a=null,e.is_objective.test(n))i.opType=n.match(/(max|min)/gi)[0],(a=n.match(e.parse_lhs).map(function(t){return t.replace(/\s+/,"")}).slice(1)).forEach(function(t){o=null===(o=t.match(e.get_num))?"-"===t.substr(0,1)?-1:1:o[0],o=parseFloat(o),s=t.match(e.get_word)[0].replace(/\;$/,""),i.variables[s]=i.variables[s]||{},i.variables[s]._obj=o});else if(e.is_int.test(n))a=n.match(e.parse_int).slice(1),i.ints=i.ints||{},a.forEach(function(t){t=t.replace(";",""),i.ints[t]=1});else if(e.is_bin.test(n))a=n.match(e.parse_bin).slice(1),i.binaries=i.binaries||{},a.forEach(function(t){t=t.replace(";",""),i.binaries[t]=1});else if(e.is_constraint.test(n)){var u=n.indexOf(":");(a=(-1===u?n:n.slice(u+1)).match(e.parse_lhs).map(function(t){return t.replace(/\s+/,"")})).forEach(function(t){o=null===(o=t.match(e.get_num))?"-"===t.substr(0,1)?-1:1:o[0],o=parseFloat(o),s=t.match(e.get_word)[0],i.variables[s]=i.variables[s]||{},i.variables[s][h]=o}),l=parseFloat(n.match(e.parse_rhs)[0]),n=r[n.match(e.parse_dir)[0]],i.constraints[h]=i.constraints[h]||{},i.constraints[h][n]=l}else e.is_unrestricted.test(n)&&(a=n.match(e.parse_int).slice(1),i.unrestricted=i.unrestricted||{},a.forEach(function(t){t=t.replace(";",""),i.unrestricted[t]=1}));return i}(t):function(t){if(!t)throw new Error("Solver requires a model to operate on");var e="",i={max:"<=",min:">=",equal:"="},r=new RegExp("[^A-Za-z0-9]+","gi");for(var n in e+=t.opType+":",t.variables)t.variables[n][n]=t.variables[n][n]?t.variables[n][n]:1,t.variables[n][t.optimize]&&(e+=" "+t.variables[n][t.optimize]+" "+n.replace(r,"_"));for(n in e+=";\n",t.constraints)for(var a in t.constraints[n]){for(var o in t.variables)t.variables[o][n]&&(e+=" "+t.variables[o][n]+" "+o.replace(r,"_"));e+=" "+i[a]+" "+t.constraints[n][a],e+=";\n"}if(t.ints)for(n in e+="\n\n",t.ints)e+="int "+n.replace(r,"_")+";\n";if(t.unrestricted)for(n in e+="\n\n",t.unrestricted)e+="unrestricted "+n.replace(r,"_")+";\n";return e}(t)},pt=X.Constraint,vt=X.Variable,mt=X.Numeral,yt=X.Term,xt=function(){this.Model=ut,this.branchAndCut=at,this.Constraint=pt,this.Variable=vt,this.Numeral=mt,this.Term=yt,this.Tableau=et,this.lastSolvedModel=null,this.Solve=function(t,e,i,r){if(r)for(var n in dt)t=dt[n](t);if(!t)throw new Error("Solver requires a model to operate on");t instanceof ut==!1&&(t=new ut(e).loadJson(t));var a=t.solve();if(this.lastSolvedModel=t,a.solutionSet=a.generateSolutionSet(),i)return a;var o={};return o.feasible=a.feasible,o.result=a.evaluation,o.bounded=a.bounded,a._tableau.__isIntegral&&(o.isIntegral=!0),Object.keys(a.solutionSet).forEach(function(t){0!==a.solutionSet[t]&&(o[t]=a.solutionSet[t])}),o},this.ReformatLP=ft,this.MultiObjective=function(t){return function(t,e){var i,r,n,a,o,s=e.optimize,h=JSON.parse(JSON.stringify(e.optimize)),l=Object.keys(e.optimize),c=0,u={},d="",f={},p=[];for(delete e.optimize,r=0;r<l.length;r++)h[l[r]]=0;for(r=0;r<l.length;r++){for(o in e.optimize=l[r],e.opType=s[l[r]],i=t.Solve(e,void 0,void 0,!0),l)if(!e.variables[l[o]])for(a in i[l[o]]=i[l[o]]?i[l[o]]:0,e.variables)e.variables[a][l[o]]&&i[a]&&(i[l[o]]+=i[a]*e.variables[a][l[o]]);for(d="base",n=0;n<l.length;n++)i[l[n]]?d+="-"+(1e3*i[l[n]]|0)/1e3:d+="-0";if(!u[d]){for(u[d]=1,c++,n=0;n<l.length;n++)i[l[n]]&&(h[l[n]]+=i[l[n]]);delete i.feasible,delete i.result,p.push(i)}}for(r=0;r<l.length;r++)e.constraints[l[r]]={equal:h[l[r]]/c};for(r in e.optimize="cheater-"+Math.random(),e.opType="max",e.variables)e.variables[r].cheater=1;for(r in p)for(a in p[r])f[a]=f[a]||{min:1e99,max:-1e99};for(r in f)for(a in p)p[a][r]?(p[a][r]>f[r].max&&(f[r].max=p[a][r]),p[a][r]<f[r].min&&(f[r].min=p[a][r])):(p[a][r]=0,f[r].min=0);return{midpoint:i=t.Solve(e,void 0,void 0,!0),vertices:p,ranges:f}}(this,t)}};"object"==typeof window&&(window.solver=new xt);var gt=new xt;const bt="crossings";function wt(t,e){return t<e?-1:t>e?1:t>=e?0:NaN}var It,Et;1===(It=wt).length&&(Et=It,It=function(t,e){return wt(Et(t),e)});function Ct(t){return null===t?NaN:+t}function Bt(t,e){var i,r=t.length,n=-1,a=[];if(null==e)for(;++n<r;)isNaN(i=Ct(t[n]))||a.push(i);else for(;++n<r;)isNaN(i=Ct(e(t[n],n,t)))||a.push(i);return function(t,e,i){if(null==i&&(i=Ct),r=t.length){if((e=+e)<=0||r<2)return+i(t[0],0,t);if(e>=1)return+i(t[r-1],r-1,t);var r,n=(r-1)*e,a=Math.floor(n),o=+i(t[a],a,t);return o+(+i(t[a+1],a+1,t)-o)*(n-a)}}(a.sort(wt),.5)}function _t(){return function(t,e){e.forEach(t=>t._median=[]),t.forEach((t,e)=>t.children.forEach(t=>t._median.push(e))),e.forEach(t=>t._median=Bt(t._median)||0),e.sort((t,e)=>t._median-e._median),e.forEach(t=>delete t._median)}}function Vt(){let t=_t();function e(e){return e.slice(0,e.length-1).forEach((i,r)=>t(i,e[r+1])),e}return e.order=function(i){return arguments.length?(t=i,e):t},e}var Mt,kt=(function(t){var e=function(t,e){return t<e};function i(t){if(!(this instanceof i))return new i(t);this.array=[],this.size=0,this.compare=t||e}i.prototype.clone=function(){var t=new i(this.compare);t.size=this.size;for(var e=0;e<this.size;e++)t.array.push(this.array[e]);return t},i.prototype.add=function(t){var e,i,r=this.size;for(this.array[this.size]=t,this.size+=1;r>0&&(e=r-1>>1,i=this.array[e],this.compare(t,i));)this.array[r]=i,r=e;this.array[r]=t},i.prototype.heapify=function(t){var e;for(this.array=t,this.size=t.length,e=this.size>>1;e>=0;e--)this._percolateDown(e)},i.prototype._percolateUp=function(t,e){for(var i,r,n=this.array[t];t>0&&(i=t-1>>1,r=this.array[i],e||this.compare(n,r));)this.array[t]=r,t=i;this.array[t]=n},i.prototype._percolateDown=function(t){for(var e,i,r,n=this.size,a=this.size>>>1,o=this.array[t];t<a&&(i=(e=1+(t<<1))+1,r=this.array[e],i<n&&this.compare(this.array[i],r)&&(e=i,r=this.array[i]),this.compare(r,o));)this.array[t]=r,t=e;this.array[t]=o},i.prototype._removeAt=function(t){return!(this.isEmpty()||t>this.size-1||t<0)&&(this._percolateUp(t,!0),this.poll(),!0)},i.prototype.remove=function(t,e){if(e||(e=this.compare),this.isEmpty())return!1;for(var i=0;i<this.size;i++)if(!e(this.array[i],t)&&!e(t,this.array[i]))return this._removeAt(i);return!1},i.prototype.peek=function(){if(0!=this.size)return this.array[0]},i.prototype.poll=function(){if(0!=this.size){var t=this.array[0];return this.size>1?(this.array[0]=this.array[--this.size],this._percolateDown(0)):this.size-=1,t}},i.prototype.replaceTop=function(t){if(0!=this.size){var e=this.array[0];return this.array[0]=t,this._percolateDown(0),e}},i.prototype.trim=function(){this.array=this.array.slice(0,this.size)},i.prototype.isEmpty=function(){return 0===this.size},i.prototype.forEach=function(t){if(!this.isEmpty()&&"function"==typeof t)for(var e=0,i=this.clone();!i.isEmpty();)t(i.poll(),e++)},i.prototype.kSmallest=function(t){if(0==this.size)return[];var e=this.compare,r=this.array,n=new i(function(t,i){return e(r[t],r[i])});t=Math.min(this.size,t);var a=new Array(t),o=0;for(n.add(0);o<t;){var s=n.poll();a[o++]=this.array[s];var h=1+(s<<1),l=h+1;h<this.size&&n.add(h),l<this.size&&n.add(l)}return a};(function(){throw new Error("Dyna