Skip to content

Commit

Permalink
Use HiGHS interface to check if model is linear or quadratic
Browse files Browse the repository at this point in the history
  • Loading branch information
fuglede committed Oct 9, 2023
1 parent 5efbf64 commit 6493a90
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ Module["solve"] = function (model_str, highs_options) {
() => Module.Highs_writeSolutionPretty(highs, ""),
"write and extract solution"
);
const output = parseResult(highs, stdout_lines, status);
_Highs_destroy(highs);
const output = parseResult(stdout_lines, status);
// Flush the content of stdout and stderr because these streams are not used anymore
stdout_lines.length = 0;
stderr_lines.length = 0;
Expand Down Expand Up @@ -156,10 +156,11 @@ function lineToObj(headers, line) {
/**
* Parse HiGHS output lines
* @param {string[]} lines stdout from highs
* @param {import("../types").Highs} highs status
* @param {import("../types").HighsModelStatus} status status
* @returns {import("../types").HighsSolution} The solution
*/
function parseResult(lines, status) {
function parseResult(highs, lines, status) {
if (lines.length < 3)
throw new Error("Unable to parse solution. Too few lines.");

Expand All @@ -168,8 +169,8 @@ function parseResult(lines, status) {
// We identity whether the problem is a QP by the available headers: For infeasible
// problems, "Status", "Dual", and "Primal" are missing, for integer linear programs,
// "Status" and "Dual" are missing, and for QPs, only "Status" is missing
const isQuadratic = !headers.includes("Status") && headers.includes("Dual");
const isLinear = !headers.includes("Type") && !isQuadratic;
const isQuadratic = highs.getModel().isQp();
const isLinear = !isQuadratic;

var result = {
"Status": /** @type {"Infeasible"} */(status),
Expand Down
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type Highs = {
solve(problem: string, options?: HighsOptions): HighsSolution;
getModel(): HighsModel;
};

type HighsOptions = Readonly<
Expand Down Expand Up @@ -298,6 +299,10 @@ type GenericHighsSolution<IsLinear extends boolean, ColType, RowType, Status ext
Rows: RowType[];
};

type HighsModel = {
isQp(): boolean;
}

type HighsModelStatus =
| "Not Set"
| "Load error"
Expand Down

0 comments on commit 6493a90

Please sign in to comment.