Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quadratic objective support and 'direct' mode for MOSEK #226

Closed
wants to merge 38 commits into from
Closed

Add quadratic objective support and 'direct' mode for MOSEK #226

wants to merge 38 commits into from

Conversation

ulfworsoe
Copy link
Contributor

This PR adds a to_mosekpy (similar to to_gurobipy) for passing a model to MOSEK without files.

This should add support for quadratic objective terms.

I have attempted to fix support for reading solution (warmstart) files and disabled reading basis files since these are not directly supported by MOSEK.

Copy link

codecov bot commented Feb 5, 2024

Codecov Report

Attention: Patch coverage is 4.58716% with 104 lines in your changes are missing coverage. Please review.

Project coverage is 85.86%. Comparing base (5582174) to head (9656735).
Report is 7 commits behind head on master.

Files Patch % Lines
linopy/solvers.py 1.33% 73 Missing and 1 partial ⚠️
linopy/io.py 6.25% 30 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #226      +/-   ##
==========================================
- Coverage   88.29%   85.86%   -2.44%     
==========================================
  Files          15       15              
  Lines        3470     3572     +102     
  Branches      803      836      +33     
==========================================
+ Hits         3064     3067       +3     
- Misses        296      393      +97     
- Partials      110      112       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ulfworsoe ulfworsoe changed the title Add quadratic objective support and 'direct' mode for MOSEK Work in progress: Add quadratic objective support and 'direct' mode for MOSEK Feb 5, 2024
@ulfworsoe ulfworsoe changed the title Work in progress: Add quadratic objective support and 'direct' mode for MOSEK Add quadratic objective support and 'direct' mode for MOSEK Feb 5, 2024
Copy link
Collaborator

@FabianHofmann FabianHofmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great @ulfworsoe, thanks for the PR! could you add the implementation to the test routine? Just add a

if "mosek" in available_solvers:
    params.append(("mosek", "direct"))

in the beginning of the test_optimization script

linopy/io.py Outdated
@@ -307,6 +307,80 @@ def to_file(m, fn, integer_label="general"):
return fn


def to_mosekpy(model, task):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to optionally set task. With the default None, a new mosek model would be created which would be returned.

Comment on lines 893 to 896
# Gurobi solution format is not supported by MOSEK.
# What is the warmstart file? A Gurobi .sol file? The gurobi
# docs are a bit sparse on the format. Does it include dual
# information?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably a left-over, can you take that out?
A warmstart file is a simplex basis which can be used by some solvers as a starting point for the optimization. It is stored as a .bas file. However I am not sure whether xpress can handle these. Did you try out the original call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original call used task.readdata. I'll check, but I am reasonably sure that it will only read problem files (LP, MPS, etc). MOSEK does have a .bas file but that is an entirely different format.
But I implemented a .sol reader here so I'll, have to fix that too.

@FabianHofmann
Copy link
Collaborator

And a hint: if you install pre-commit locally, the pre-commit bot does not interfere with your PR, as all stylistic changes are already applied before committing.

@ulfworsoe
Copy link
Contributor Author

Ah, that's what was happening. I should probably do that.

@FabianHofmann
Copy link
Collaborator

Hey @ulfworsoe, this looks good. I will merge #223 before, and hope this does not lead to merge conflicts.

@ulfworsoe
Copy link
Contributor Author

@FabianHofmann I can see that the coverage tests fail. My guess is that the tests don't run for MOSEK at all because there is no license file. We have a similar issue in other places (the MOSEK Julia API, for example), and what we do in these cases is to use http://solve.mosek.com/web/ instead of solving locally. This doesn't require a license for small-ish problems.
I am not sure how to do this, though, as it means calling task.putoptserverhost() somewhere.

@FabianHofmann
Copy link
Collaborator

@ulfworsoe you let me know as soon as this is ready? Then I can take a final look and merge it.

@ulfworsoe
Copy link
Contributor Author

@FabianHofmann i think it is ready. The tests actually run and pass now.

Copy link
Collaborator

@FabianHofmann FabianHofmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ulfworsoe thank you for the huge work! looks almost ready, just some minor points. Sorry that this takes so long. But ones it's there it will be quite stable.

linopy/solvers.py Outdated Show resolved Hide resolved
linopy/solvers.py Outdated Show resolved Hide resolved
linopy/solvers.py Outdated Show resolved Hide resolved
test/test_optimization.py Outdated Show resolved Hide resolved
linopy/io.py Outdated Show resolved Hide resolved
linopy/io.py Show resolved Hide resolved
@ulfworsoe
Copy link
Contributor Author

@FabianHofmann I have removed the mosek_remote, but this means that MOSEK will not run in CI. What do you do for Gurobi? Does the CI image contain a license file?

@FabianHofmann
Copy link
Collaborator

@ulfworsoe for some solvers I am keeping a github secret for the CI. Is something similar possible for mosek?

@ulfworsoe
Copy link
Contributor Author

@ulfworsoe for some solvers I am keeping a github secret for the CI. Is something similar possible for mosek?

Yes. Either a secret string that is passed to MOSEK or as a license file placed in $HOME/mosek/mosek.lic. The latter will probably be easier to manage.
I'll have to ask my colleague how we can do this.

@FabianHofmann
Copy link
Collaborator

@ulfworsoe it seems I have mosek license to the github! So if you don't mind I would pull everything into another branch to properly run the CI and then we can merge itl

@ulfworsoe
Copy link
Contributor Author

@ulfworsoe it seems I have mosek license to the github! So if you don't mind I would pull everything into another branch to properly run the CI and then we can merge itl

Please do. I'm working on a more permanent solution for using licenses in github CI so they won't have to be updated by hand regularly.

FabianHofmann added a commit that referenced this pull request Mar 5, 2024
Add quadratic objective support and 'direct' mode for MOSEK (superset of #226)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants