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 writeBasis interface method to highspy #1264

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions highspy/highs_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ PYBIND11_MODULE(highs_bindings, m)
.def("passRowName", &Highs::passRowName)
.def("readModel", &Highs::readModel)
.def("readBasis", &Highs::readBasis)
.def("writeBasis", &Highs::writeBasis)
.def("presolve", &Highs::presolve)
.def("run", &Highs::run)
.def("postsolve", &Highs::postsolve)
Expand Down
35 changes: 35 additions & 0 deletions highspy/tests/test_highspy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tempfile
import unittest
import highspy
import numpy as np
Expand Down Expand Up @@ -404,3 +405,37 @@ def test_ranging(self):
self.assertEqual(ranging.row_bound_up.value_[1], inf);
self.assertEqual(ranging.row_bound_up.objective_[1], inf);

def test_write_basis_before_running(self):
h = self.get_basic_model()
with tempfile.NamedTemporaryFile() as f:
h.writeBasis(f.name)
contents = f.read()
self.assertEqual(contents, b'HiGHS v1\nNone\n')

def test_write_basis_after_running(self):
h = self.get_basic_model()
h.run()
with tempfile.NamedTemporaryFile() as f:
h.writeBasis(f.name)
contents = f.read()
self.assertEqual(
contents, b'HiGHS v1\nValid\n# Columns 2\n1 1 \n# Rows 2\n0 0 \n'
)

def test_read_basis(self):
# Read basis from one run model into an unrun model
expected_status_before = highspy.HighsBasisStatus.kLower
expected_status_after = highspy.HighsBasisStatus.kBasic

h1 = self.get_basic_model()
self.assertEqual(h1.getBasis().col_status[0], expected_status_before)
h1.run()
self.assertEqual(h1.getBasis().col_status[0], expected_status_after)

h2 = self.get_basic_model()
self.assertEqual(h2.getBasis().col_status[0], expected_status_before)

with tempfile.NamedTemporaryFile() as f:
h1.writeBasis(f.name)
h2.readBasis(f.name)
self.assertEqual(h2.getBasis().col_status[0], expected_status_after)