Skip to content

Commit

Permalink
Merge pull request #1264 from fuglede/highspy-write-basis
Browse files Browse the repository at this point in the history
Add writeBasis interface method to highspy
  • Loading branch information
jajhall committed Apr 19, 2023
2 parents 2e27ba3 + be21711 commit 23cd66f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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)

0 comments on commit 23cd66f

Please sign in to comment.