Skip to content

Commit

Permalink
Add solver API integration and enhance main function for input handling
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardodebenedictis committed Nov 11, 2024
1 parent e0f7aaf commit f7fbb95
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ option(CHECK_INCONSISTENCIES "Check inconsistencies at each step" OFF)
option(ENABLE_API "oRatio API" OFF)

add_library(oRatioLib src/solver.cpp src/graph.cpp src/flaw.cpp src/resolver.cpp src/smart_type.cpp src/flaws/atom_flaw.cpp src/flaws/bool_flaw.cpp src/flaws/disj_flaw.cpp src/flaws/disjunction_flaw.cpp src/flaws/enum_flaw.cpp src/types/agent.cpp src/types/state_variable.cpp src/types/consumable_resource.cpp src/types/reusable_resource.cpp $<$<BOOL:${ENABLE_API}>:${CMAKE_CURRENT_SOURCE_DIR}/src/solver_api.cpp>)
add_dependencies(oRatioLib RiDDLe SeMiTONE)
target_link_libraries(oRatioLib PUBLIC RiDDLe SeMiTONE)
target_include_directories(oRatioLib PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/flaws> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/types> $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)

Expand Down Expand Up @@ -70,9 +71,10 @@ if(ENABLE_API)
target_compile_definitions(oRatioLib PRIVATE ENABLE_API)
endif()

add_executable(oRatio src/main.cpp)
add_executable(oRatio src/main.cpp src/solver_api.cpp)
add_dependencies(oRatio oRatioLib)
target_link_libraries(oRatio PRIVATE oRatioLib)
target_include_directories(oRatio PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_compile_definitions(oRatio PRIVATE ENABLE_API=1 COMPUTE_NAMES=1)

if(BUILD_TESTING)
add_subdirectory(tests)
Expand Down
49 changes: 49 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
#include "solver.hpp"
#include "logging.hpp"
#include "solver_api.hpp"
#include <chrono>
#include <numeric>
#include <fstream>

int main(int argc, char const *argv[])
{
if (argc < 3)
{
LOG_FATAL("usage: oRatio <input-file> [<input-file> ...] <output-file>");
return -1;
}

// the problem files..
std::vector<std::string> prob_names;
for (int i = 1; i < argc - 1; i++)
prob_names.push_back(argv[i]);

// the solution file..
std::string sol_name = argv[argc - 1];

LOG_INFO("starting oRatio");
auto start = std::chrono::high_resolution_clock::now();
auto s = std::make_shared<ratio::solver>();
s->init();
try
{
s->read(prob_names);

if (s->solve())
{
LOG_INFO("hurray!! we have found a solution..");

std::ofstream sol_file;
sol_file.open(sol_name);
sol_file << to_json(*s).dump();
sol_file.close();
}
else
{
LOG_INFO("the problem is unsolvable..");
}
auto dur = std::chrono::high_resolution_clock::now() - start;
LOG_INFO("running time: " + std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(dur).count()) + " ms");
}
catch (const std::exception &ex)
{
LOG_FATAL("exception: " + std::string(ex.what()));
return 1;
}

return 0;
}
4 changes: 2 additions & 2 deletions tests/test_solver.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <chrono>
#include <numeric>
#include "solver.hpp"
#include "logging.hpp"
#include <chrono>
#include <numeric>

int main(int argc, char const *argv[])
{
Expand Down

0 comments on commit f7fbb95

Please sign in to comment.