Skip to content

feat: move set_int_index to solution assignment in model.py#406

Merged
FabianHofmann merged 10 commits intomasterfrom
move_set_int_index
Feb 3, 2025
Merged

feat: move set_int_index to solution assignment in model.py#406
FabianHofmann merged 10 commits intomasterfrom
move_set_int_index

Conversation

@FabianHofmann
Copy link
Collaborator

@FabianHofmann FabianHofmann commented Jan 29, 2025

Follow up on #396

The internal handling of Solution objects was improved for more consistency. Solution objects created from solver calls now preserve the exact index names from the input file.

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Copy link
Collaborator

@siddharth-krishna siddharth-krishna left a comment

Choose a reason for hiding this comment

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

Thanks Fabian, this works for me! If I understand the idea correctly, I think it's also a more elegant approach, and ensures we only call set_int_index on solutions returned from linopy.Model, which is guaranteed to have the form x1, x2, ....

Btw, here is a test file I have been using to test this issue. If it makes sense, I can add it to the tests in this PR, if you can point me to where/how to integrate them. Thanks!

#!/usr/bin/env python3
"""
Created on Tue Jan 28 09:03:35 2025.

@author: sid
"""

import sys
from pathlib import Path
from linopy import solvers

mps_problem = """
NAME        sample_mip
ROWS
 N  obj     
 G  c1      
 L  c2      
 E  c3      
COLUMNS
    col1        obj       5
    col1        c1        2
    col1        c2        4
    col1        c3        1
    MARK0000  'MARKER'                 'INTORG'
    colu2        obj       3
    colu2        c1        3
    colu2        c2        2
    colu2        c3        1
    col3        obj       7
    col3        c1        4
    col3        c2        3
    col3        c3        1
    MARK0001  'MARKER'                 'INTEND'
RHS
    RHS_V     c1        12
    RHS_V     c2        15
    RHS_V     c3        6
BOUNDS
 UP BOUND     col1        4
 UI BOUND     colu2        3
 UI BOUND     col3        5
ENDATA
"""
input_file = "/tmp/sid-linopy.mps"
with open(input_file, "w") as f:
    f.write(mps_problem)

s = solvers.Highs()
result = s.solve_problem(problem_fn=Path(input_file), solution_fn=Path("/tmp/sid-linopy-out.sol"))
print(result)
print(result.solution.objective)

@FabianHofmann
Copy link
Collaborator Author

great, since this a new class of test (which only tackles the solver implementations) I would suggest we create a new module test/test_solvers.py where we test these low-level solver routines. what do you think?

@FabianHofmann
Copy link
Collaborator Author

you can push it directly to this PR (permissions should be in place)

@siddharth-krishna
Copy link
Collaborator

Thanks, Fabian! I added a new test file. I also added some common virtualenv paths to the .gitignore, if you don't mind?

Looks like the test is failing on some solvers: glpk, cbc

Do you think it's worth investigating, or should I reduce the scope of the test to HiGHS?

@FabianHofmann FabianHofmann merged commit 7ef3bf7 into master Feb 3, 2025
20 checks passed
@FabianHofmann FabianHofmann deleted the move_set_int_index branch February 3, 2025 16:56
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.

2 participants