UNPKG

gams2js

Version:
2 lines (1 loc) 3.64 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).NEOS=t()}(this,(function(){"use strict";var e={splitNewline:e=>e.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/),removeEmptyLines:e=>e.replace(/^\s*[\r\n]/gm,""),splitByWhitespace:e=>e.split(/[ ,]+/),getSymbolName:e=>e.split(/\s/)[0],splitSolves:e=>e.split(/Solution Report\s*?SOLVE/),fixLinesPW:e=>e=e.reduce((e,t)=>(t.match(/^\./)?e[e.length-1]+=t:e.push(t),e),[]),getColumnWidths(e){const t=this.splitNewline(e).find(e=>e.includes("LOWER")),r=t.split("LOWER")[1].split("LEVEL")[0].length,i=t.split("LEVEL")[1].split("UPPER")[0].length,l=t.split("UPPER")[1].split("MARGINAL")[0].length,s=-14-Math.floor(l/2),n=s-Math.ceil(l/2)-5-Math.floor(i/2),o=n-Math.ceil(i/2)-5-Math.floor(r/2);return{marginal:{start:-6,end:s},upper:{start:s,end:n},level:{start:n,end:o},lower:{start:o,end:o-Math.ceil(r/2)-5-Math.floor(r/2)}}},getValues(e,t){const r=e.slice(t.marginal.end).trim(),i=e.slice(t.upper.end,t.upper.start).trim(),l=e.slice(t.level.end,t.level.start).trim(),s=e.slice(t.lower.end,t.lower.start).trim(),n=e.slice(-e.length,t.lower.end).trim().split(".").map(e=>e.trim());return{marginal:this.parseNumerical(r),level:this.parseNumerical(l),upper:this.parseNumerical(i),lower:this.parseNumerical(s),domain:n}},parseNumerical:e=>"+INF"===e?1/0:"-INF"===e?-1/0:"EPS"===e?Number.MIN_VALUE:"."===e?0:Number(e),readObjective(e){const t=e.search("OBJECTIVE VALUE")+15,r=t+e.slice(t).search("\n"),i=e.slice(t,r);return parseFloat(i)},getSolveLine(e){let t=this.splitByWhitespace(this.splitNewline(e)[0]);return Number(t[t.length-1])},getModelStatus(e){const t=e.search("MODEL STATUS")+18,r=t+2,i=e.slice(t,r);return parseFloat(i)}};function t(t,r,i){let l=r.split(`---- ${t} `);l=l.slice(1,l.length);const s="EQU"===t?"---- ":"****";l[l.length-1]=l[l.length-1].split(s)[0];let n=[];return l.forEach(t=>{t=e.removeEmptyLines(t);let r=e.splitNewline(t);r=e.fixLinesPW(r);const l=e.getSymbolName(t);let s=[],o="";r.forEach(t=>{if(t.includes("LOWER"))return;if(!t.includes("."))return void(t.includes(l)&&(o=t.trim().substring(l.length).trim()));const r=e.getValues(t,i);r.domain[0]&&(1===r.domain.length&&(r.domain=r.domain.filter(e=>e!==l)),s.push(r))}),s=s.map(e=>({...e,name:l,description:o})),n=n.concat(s)}),n}return function(r){let i=e.splitSolves(r);if(1===i.length)throw new Error("Error parsing the listing file. Check the raw listing file for compilation errors.");try{const l=e.getColumnWidths(r);return i=i.slice(1,i.length),{solves:i.map(r=>({objective:e.readObjective(r),line:e.getSolveLine(r),modelStatus:e.getModelStatus(r),equations:t("EQU",r,l),variables:t("VAR",r,l)})),get(e,t,r){t&&"string"==typeof t&&(t=[t]);let i=[];if(r&&"number"==typeof r)try{i=i.concat(this.solves[r].equations,this.solves[r].variables)}catch(e){throw new Error(`No solution found for solve no. ${r}. Please check the solves array manually.`)}else try{this.solves.forEach(e=>{i=i.concat(e.equations,e.variables)})}catch(e){throw new Error("Error processing solver data. Please file an issue on the gams2js Github repo.")}return function(e,t,r=[]){if(!t)throw new Error("Symbol name required for query.");if("string"!=typeof t)throw new TypeError("Symbol name must be of type string.");return e.filter(e=>{if(e.name.toUpperCase()===t.toUpperCase()){if(r&&r.length)try{for(let t=0;t<r.length;t++)if(r[t]&&r[t].toUpperCase()!==e.domain[t].toUpperCase())return}catch(e){return}return e}})}(i,e,t)}}}catch(e){throw console.log(e),new Error("Error parsing the listing file: "+e)}}}));