Skip to content

Commit

Permalink
ASL-style 'writeprob=(file).sol' #218
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Jul 28, 2023
1 parent deac242 commit d3bde99
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
50 changes: 36 additions & 14 deletions include/mp/backend-std.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ class StdBackend :
ALLOW_STD_FEATURE(WRITE_PROBLEM, false)
FEATURE_API_TO_IMPLEMENT(WRITE_PROBLEM,
void DoWriteProblem(const std::string& ))
/// Redefine this if you want some extensions
/// to be written after solving instead.
/// This is for compatibility wiht ASL drivers
/// where 'writeprob' was used for native-format model
/// and solution output #218.
virtual std::set<std::string> NativeResultExtensions() const
{ return {".sol", ".ilp", ".mst", ".hnt", ".bas", ".json"}; }

/**
* Solution export.
Expand Down Expand Up @@ -949,21 +956,36 @@ class StdBackend :
/// Write model
virtual void ExportModel(
const std::vector<std::string>& filenames) {
auto resext = NativeResultExtensions();
for (const auto& fln: filenames) {
try {
DoWriteProblem(fln);
} catch (const std::exception& exc) {
auto msg
= std::string("Model export to file '")
+ fln
+ "' failed:\n"
+ exc.what();
if (IMPL_HAS_STD_FEATURE(WRITE_SOLUTION))
msg +=
"\n Note: to export solutions and results\n"
" in the solver's native formats,\n"
" use option 'tech:writesolution'";
MP_RAISE(msg);
bool fPostpone = false;
if (IMPL_HAS_STD_FEATURE(WRITE_SOLUTION)) {
auto dot = fln.rfind('.'); // Postpone this file?
if (std::string::npos != dot) {
auto ext = fln.substr(dot, fln.size()-dot);
if (resext.end() != resext.find(ext)) {
fPostpone = true;
}
}
}
if (fPostpone) { // ASL-style usage of 'writeprob' #218
storedOptions_.export_sol_files_.push_back(fln);
} else {
try {
DoWriteProblem(fln);
} catch (const std::exception& exc) {
auto msg
= std::string("Model export to file '")
+ fln
+ "' failed:\n"
+ exc.what();
if (IMPL_HAS_STD_FEATURE(WRITE_SOLUTION))
msg +=
"\n Note: to export solutions and results\n"
" in the solver's native formats,\n"
" use option 'tech:writesolution'";
MP_RAISE(msg);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion test/end2end/cases/categorized/fast/suf_std/modellist.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,17 @@
"files" : ["diet.mod", "diet.dat"],
"options": {
"ANYSOLVER_options": "writesol=diet.sol"
}
},
{
"name" : "diet writeprob=diet.sol",
"objective" : 88.2,
"tags" : ["linear", "continuous", "writelp", "writesol"],
"files" : ["diet.mod", "diet.dat"],
"options": {
"ANYSOLVER_options": "writeprob=diet.sol"
},
"comment": "Testing quoted string options"
"comment": "Test that writing of .sol is delayed #218"
},
{
"name" : "diet duals; var/con sstatus",
Expand Down

0 comments on commit d3bde99

Please sign in to comment.