Skip to content

Commit e8b7df9

Browse files
committed
Enable user to write solution file in HiGHS
1 parent fd502c8 commit e8b7df9

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ debug/
1616
docs/jupyter_execute
1717
docs/build
1818

19-
*.lp
19+
*.lp
20+
*.sol

include/pyoptinterface/highs_model.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
B(Highs_create); \
1515
B(Highs_destroy); \
1616
B(Highs_writeModel); \
17+
B(Highs_writeSolution); \
18+
B(Highs_writeSolutionPretty); \
1719
B(Highs_addCol); \
1820
B(Highs_getNumCol); \
1921
B(Highs_changeColIntegrality); \
@@ -112,7 +114,7 @@ class POIHighsModel
112114
void init();
113115
void close();
114116

115-
void write(const std::string &filename);
117+
void write(const std::string &filename, bool pretty);
116118

117119
VariableIndex add_variable(VariableDomain domain = VariableDomain::Continuous,
118120
double lb = -kHighsInf, double ub = kHighsInf,

lib/highs_model.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,25 @@ void POIHighsModel::close()
119119
m_model.reset();
120120
}
121121

122-
void POIHighsModel::write(const std::string &filename)
122+
void POIHighsModel::write(const std::string &filename, bool pretty)
123123
{
124-
auto error = highs::Highs_writeModel(m_model.get(), filename.c_str());
124+
bool is_solution = false;
125+
if (filename.ends_with(".sol"))
126+
{
127+
is_solution = true;
128+
}
129+
HighsInt error;
130+
if (is_solution)
131+
{
132+
if (pretty)
133+
error = highs::Highs_writeSolutionPretty(m_model.get(), filename.c_str());
134+
else
135+
error = highs::Highs_writeSolution(m_model.get(), filename.c_str());
136+
}
137+
else
138+
{
139+
error = highs::Highs_writeModel(m_model.get(), filename.c_str());
140+
}
125141
check_error(error);
126142
}
127143

lib/highs_model_ext.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ NB_MODULE(highs_model_ext, m)
3939
.def_ro("m_n_constraints", &HighsModelMixin::m_n_constraints)
4040
// clang-format off
4141
BIND_F(init)
42-
BIND_F(write)
4342
BIND_F(close)
4443
// clang-format on
44+
.def("write", &HighsModelMixin::write, nb::arg("filename"), nb::arg("pretty") = false)
4545

4646
.def_ro("solution", &HighsModelMixin::m_solution)
4747

lib/mosek_model.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void MOSEKModel::close()
184184
void MOSEKModel::write(const std::string &filename)
185185
{
186186
bool is_solution = false;
187-
if (filename.ends_with(".sol") || filename.ends_with(".bas") || filename.ends_with("int"))
187+
if (filename.ends_with(".sol") || filename.ends_with(".bas") || filename.ends_with(".int"))
188188
{
189189
is_solution = true;
190190
}

tests/highs_writefile.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pyoptinterface as poi
2+
from pyoptinterface import highs
3+
4+
model = highs.Model()
5+
6+
x = model.add_m_variables(10, lb=0.0, ub=10.0)
7+
model.add_linear_constraint(poi.quicksum(x) >= 20.0)
8+
9+
model.set_objective(poi.quicksum(v * v for v in x))
10+
model.optimize()
11+
12+
model.write("highs_test.sol")
13+
model.write("highs_test_pretty.sol", pretty=True)

0 commit comments

Comments
 (0)