-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from scipopt/writemodel
Add Model::writeOrigProblem
- Loading branch information
Showing
4 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
find_package(Boost CONFIG REQUIRED COMPONENTS unit_test_framework) | ||
find_package(Boost CONFIG REQUIRED COMPONENTS unit_test_framework filesystem) | ||
if (NOT TARGET Boost::unit_test_framework) | ||
add_library(Boost::unit_test_framework IMPORTED INTERFACE) | ||
set_property(TARGET Boost::unit_test_framework PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR}) | ||
set_property(TARGET Boost::unit_test_framework PROPERTY INTERFACE_LINK_LIBRARIES ${Boost_LIBRARIES}) | ||
endif () | ||
if (NOT TARGET Boost::filesystem) | ||
add_library(Boost::filesystem IMPORTED INTERFACE) | ||
set_property(TARGET Boost::filesystem PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR}) | ||
set_property(TARGET Boost::filesystem PROPERTY INTERFACE_LINK_LIBRARIES ${Boost_LIBRARIES}) | ||
endif () | ||
|
||
file(GLOB TEST_SOURCES *.cpp) | ||
add_executable(tests ${TEST_SOURCES}) | ||
target_include_directories(tests SYSTEM PRIVATE ${Boost_INCLUDE_DIRS}) | ||
target_link_libraries(tests PRIVATE ScipPP Boost::unit_test_framework) | ||
target_link_libraries(tests PRIVATE ScipPP Boost::unit_test_framework Boost::filesystem) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include <boost/algorithm/string.hpp> | ||
#include <boost/filesystem.hpp> | ||
#include <boost/test/unit_test.hpp> | ||
#include <fstream> | ||
#include <sstream> | ||
|
||
#include "scippp/model.hpp" | ||
|
||
using namespace boost::algorithm; | ||
using namespace scippp; | ||
using namespace std; | ||
|
||
class TempFile { | ||
filesystem::path m_path { filesystem::temp_directory_path() }; | ||
|
||
public: | ||
explicit TempFile(const string& extension) | ||
{ | ||
m_path += boost::filesystem::unique_path("/%%%%-%%%%-%%%%-%%%%." + extension).string(); | ||
} | ||
~TempFile() | ||
{ | ||
filesystem::remove(m_path); | ||
} | ||
[[nodiscard]] filesystem::directory_entry path() const | ||
{ | ||
return filesystem::directory_entry(m_path); | ||
} | ||
[[nodiscard]] string content() const | ||
{ | ||
ifstream t(m_path); | ||
ostringstream sstr; | ||
sstr << t.rdbuf(); | ||
return sstr.str(); | ||
} | ||
}; | ||
|
||
auto createModel() | ||
{ | ||
Model model("Simple"); | ||
auto x1 = model.addVar("x_1", 1); | ||
auto x2 = model.addVar("x_2", 1); | ||
model.addConstr(x1 + x2 >= 1, "capacity"); | ||
model.addConstr(x1 == x2, "equal"); | ||
model.setObjsense(Sense::MINIMIZE); | ||
return model; | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE(IO) | ||
|
||
BOOST_AUTO_TEST_CASE(FileLP) | ||
{ | ||
auto model { createModel() }; | ||
|
||
TempFile tf("lp"); | ||
model.writeOrigProblem(tf.path()); | ||
BOOST_TEST(model.getLastReturnCode() == SCIP_OKAY); | ||
auto content { tf.content() }; | ||
BOOST_TEST(contains(content, "Obj: +1 x_1 +1 x_2")); | ||
BOOST_TEST(contains(content, "capacity:")); | ||
BOOST_TEST(contains(content, "equal:")); | ||
BOOST_TEST(contains(content, "Minimize")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(FileLPGenericNames) | ||
{ | ||
auto model { createModel() }; | ||
|
||
TempFile tf("lp"); | ||
model.writeOrigProblem(tf.path(), true); | ||
BOOST_TEST(model.getLastReturnCode() == SCIP_OKAY); | ||
auto content { tf.content() }; | ||
BOOST_TEST(contains(content, "Obj: +1 x0 +1 x1")); | ||
BOOST_TEST(contains(content, "c0:")); | ||
BOOST_TEST(contains(content, "c1:")); | ||
BOOST_TEST(contains(content, "Minimize")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(FileMPS) | ||
{ | ||
auto model { createModel() }; | ||
|
||
TempFile tf("mps"); | ||
model.writeOrigProblem(tf.path()); | ||
BOOST_TEST(model.getLastReturnCode() == SCIP_OKAY); | ||
auto content { tf.content() }; | ||
BOOST_TEST(contains(content, "G capacity")); | ||
BOOST_TEST(contains(content, "E equal")); | ||
BOOST_TEST(contains(content, "\n MIN")); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(StdoutLP) | ||
{ | ||
auto model { createModel() }; | ||
model.writeOrigProblem("lp"); | ||
BOOST_TEST(model.getLastReturnCode() == SCIP_OKAY); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(StdoutWithInvalidExtension) | ||
{ | ||
auto model { createModel() }; | ||
model.writeOrigProblem("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | ||
BOOST_TEST(model.getLastReturnCode() == SCIP_PLUGINNOTFOUND); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |