lovasoa / highs-js

Javascript linear programming library
https://lovasoa.github.io/highs-js/
MIT License
51 stars 14 forks source link

Version to solve MIPs? #7

Closed jajhall closed 2 years ago

jajhall commented 2 years ago

I've been contacted by a user who has tried to solve a MIP with HiGHS JS version 0.3.0, but only the LP solution has been returned. Not a problem, since https://www.npmjs.com/package/highs states, "This is a javascript linear programming library".

However, now that the latest version of HiGHS can read MIPs as .lp files (and there's also no longer an finite restriction on line length) is it just a question of updating the version of HiGHS used by HiGHS JS to enable it to solve MIPs?

Work, I know, but a valuable extension that's already got one potential user!

lovasoa commented 2 years ago

Yes, it should be as easy as updating the git submodule. Interested in making a PR ?

jajhall commented 2 years ago

I don't know how to formulate a PR to update HiGHS on in highs-js, although I see that you have some sort of link to a commit of HiGHS. You can replace this with the current master (b66030b). There have been vast changes to HiGHS since May, some in the API (sorry), so debugging may be needed. In particular, the dual values for constraints are now returned with opposite sign.

The compensation is not just a MIP solver, but the fastest open source MIP solver in the world

lovasoa commented 2 years ago

Ok. Is there a CHANGELOG with a documentation of the API changes ?

lovasoa commented 2 years ago

The list of functions used in highs-js is here, I need at least a documentation of all the changes affecting these functions to make the migration: https://github.com/lovasoa/highs-js/blob/main/build/exported_functions.json

lovasoa commented 2 years ago

For instance, it looks like Highs_modelStatusToChar, which this library uses, was deleted

jajhall commented 2 years ago

Ok. Is there a CHANGELOG with a documentation of the API changes ?

Sorry, no. I started to prepare a document, but then work to develop the MIP solver took over.

jajhall commented 2 years ago

The list of functions used in highs-js is here, I need at least a documentation of all the changes affecting these functions to make the migration: https://github.com/lovasoa/highs-js/blob/main/build/exported_functions.json

Thanks, I'll review these and give you the information that you need

jajhall commented 2 years ago

For instance, it looks like Highs_modelStatusToChar, which this library uses, was deleted

Indeed, it didn't work on at least one platform

lovasoa commented 2 years ago

Also, maybe someone could work on https://github.com/ERGO-Code/HiGHS/issues/585 ? Without that, it's hard to make sure any use of the library is correct...

jajhall commented 2 years ago

Also, maybe someone could work on ERGO-Code/HiGHS#585 ? Without that, it's hard to make sure any use of the library is correct...

Sure: Gurobi writes to console and a file by default, which seemed to be a reasonable model.

lovasoa commented 2 years ago

@jajhall : So, can you update https://www.maths.ed.ac.uk/hall/HiGHS/HighsOptions.html ?

lovasoa commented 2 years ago

I published an updated highs-js to npm, with my guesses of what should be updated

jajhall commented 2 years ago

Thanks. I've tested 0.4.0 with a MIP, and it's being communicated to HiGHS OK, but the results are not being parsed correctly. This is because you're parsing the "pretty" solution file that, for MIPs, has no status or dual values - since a MIP has only a primal solution: no optimal basis or dual solution. Hence you're quoting the "lower" value as the "status", the "upper" value as "lower", "primal" as "upper" (which is how I know that the problem is being solved OK) and "primal" and "dual" are NaN since they are parsing the type name "Integer" and column name (eg) "x1"

We've got a "raw" solution file format that should be much easier to parse. I'll send you details as well as updating https://www.maths.ed.ac.uk/hall/HiGHS/HighsOptions.html

I've tested with an LP and note that the row duals are being identified correctly

If you're short of time to do the modifications and @jajetloh (who prompted the update) can write the necessary Javascript, maybe they can modify the parsing code. I'm happy to coordinate with them. I can't write Javascript!

jajhall commented 2 years ago

I've updated https://www.maths.ed.ac.uk/hall/HiGHS/HighsOptions.html and refreshed the rest of the documentation available there. I've updated https://github.com/lovasoa/highs-js/blob/main/README.md accordingly.

jajhall commented 2 years ago

For the MIP problem file DistillationMIP.lp.txt here is the HiGHS solution file Distillation.sol.txt if it's solved as an LP by deleting the following lines General x1 x2 If it's solved as a MIP, the solution file is DistillationMIP.sol.txt Hopefully it's straightforward in JavaScript to identify the primal solution, dual solution and basis (where 0 is basic, 1 is "at lower (LB)" and 2 is "at upper (UB)") and, the absence of the dual solution and basis in the case of MIP problems.

lovasoa commented 2 years ago

@jajhall : It looks like the "raw" format contains less information than the "pretty" one. I can't find the variable bounds in it, for instance. And I don't see myself re-parsing the input to extract that information.

jajhall commented 2 years ago

The variable bounds and integrally are part of the problem data, not the solution.

The alternative is to parse the pretty format, but conditional on whether the problem is an LP or MIP

lovasoa commented 2 years ago

The variable bounds and integrally are part of the problem data, not the solution.

Yes, but I don't feel like re-parsing the problem in js just to extract that information.

The alternative is to parse the pretty format, but conditional on whether the problem is an LP or MIP

I implemented that.

https://github.com/lovasoa/highs-js/blob/main/src/post.js#L126-L129

It's a little bit dirty, but it works :smiley:

jajhall commented 2 years ago

Great! That's good for us, too. The demo needs updating to 4.0.1

lovasoa commented 2 years ago

the demo is always automatically updated to the latest version :)

https://lovasoa.github.io/highs-js/

jajhall commented 2 years ago

OK. Must be a Firefox cache issue, then. Thanks

jajhall commented 2 years ago

Are you sure? I've just opened https://lovasoa.github.io/highs-js/ on my Windows laptop - rather than the Linux machine that I do my coding on - and I get v0.4.0 in both Chrome and Edge (that I otherwise never use).

lovasoa commented 2 years ago

Nope, it's 0.4.1. You can try putting a MIP in the demo, it works :)

image

jajhall commented 2 years ago

Indeed, I'm now getting 0.4.1. However, for

Minimize obj: 8 x1 + 10 x2 Subject To c1: 2 x1 + 2 x2 >= 7 c2: 3 x1 + 4 x2 >= 12 c3: 2 x1 + x2 >= 6 General x1 x2 End

the row report is not correct image The status should not be given, and the values that are quoted as "status" are the "primal" values.

lovasoa commented 2 years ago

It's highs itself that reports the row values as "status". This lib just converts it to json.

jajhall commented 2 years ago

No, HiGHS does not report the row values as "status". Here is the "pretty" solution file for the MIP I've been using for testing

image

The format of the Rows section is identical to that of the Columns section (other than the name and variable type that you ignore).

lovasoa commented 2 years ago

You are right, sorry ! This needs fixing.

lovasoa commented 2 years ago

It's fixed. Now there is a fast and easy to use MIP solver in js :)