From d3bde99d425778ceb6cf4c10557a2aa4221afff3 Mon Sep 17 00:00:00 2001 From: Gleb Belov Date: Fri, 28 Jul 2023 13:16:59 +1000 Subject: [PATCH] ASL-style 'writeprob=(file).sol' #218 --- include/mp/backend-std.h | 50 +++++++++++++------ .../categorized/fast/suf_std/modellist.json | 11 +++- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/include/mp/backend-std.h b/include/mp/backend-std.h index 2ee5dd6dd..1d030f03b 100644 --- a/include/mp/backend-std.h +++ b/include/mp/backend-std.h @@ -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 NativeResultExtensions() const + { return {".sol", ".ilp", ".mst", ".hnt", ".bas", ".json"}; } /** * Solution export. @@ -949,21 +956,36 @@ class StdBackend : /// Write model virtual void ExportModel( const std::vector& 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); + } } } } diff --git a/test/end2end/cases/categorized/fast/suf_std/modellist.json b/test/end2end/cases/categorized/fast/suf_std/modellist.json index 12ae135eb..ee13ebdec 100644 --- a/test/end2end/cases/categorized/fast/suf_std/modellist.json +++ b/test/end2end/cases/categorized/fast/suf_std/modellist.json @@ -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",