Commit 60c975e5 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

test: swithes test to boost::ut

parent 87fa11e2
Pipeline #7250 passed with stage
in 3 minutes and 12 seconds
......@@ -21,6 +21,7 @@ endif ()
add_definitions(-DFMT_HEADER_ONLY)
include_directories(${CMAKE_SOURCE_DIR}/external/fmt/include)
include_directories(${CMAKE_SOURCE_DIR}/external/ut/include)
option(WITH_CLANG_TIDY "enable clang tidy checks. [default: OFF]" OFF)
if (WITH_CLANG_TIDY)
......
This diff is collapsed.
This diff is collapsed.
......@@ -20,165 +20,148 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "unit-test.hpp"
#include <baryonyx/core-compare>
#include <baryonyx/core-test>
#include <baryonyx/core>
#include <boost/ut.hpp>
#include <fstream>
#include <map>
#include <numeric>
#include <random>
#include <sstream>
void
test_bibd1n()
int
main()
{
auto ctx = baryonyx::make_context(6);
auto pb = baryonyx::make_problem(ctx, EXAMPLES_DIR "/bibd1n.lp");
baryonyx::solver_parameters params;
params.delta = 1e-2;
params.time_limit = 5.0;
params.mode = baryonyx::solver_parameters::mode_type::branch;
baryonyx::context_set_solver_parameters(ctx, params);
auto result = baryonyx::optimize(ctx, pb);
using namespace boost::ut;
Ensures(result);
}
"test_bibd1n"_test = [] {
auto ctx = baryonyx::make_context(4);
auto pb = baryonyx::make_problem(ctx, EXAMPLES_DIR "/bibd1n.lp");
void
test_qap()
{
auto ctx = baryonyx::make_context(6);
auto pb = baryonyx::make_problem(ctx, EXAMPLES_DIR "/small4.lp");
baryonyx::solver_parameters params;
params.limit = -1;
params.theta = 0.5;
params.delta = 0.2;
params.kappa_step = 10e-4;
params.kappa_max = 10.0;
params.alpha = 0.0;
params.w = 20;
params.time_limit = 40.0;
params.pushing_k_factor = 0.9;
params.pushes_limit = 50;
params.pushing_objective_amplifier = 10;
params.pushing_iteration_limit = 50;
params.thread = 2;
baryonyx::context_set_solver_parameters(ctx, params);
auto result = baryonyx::optimize(ctx, pb);
Ensures(result.status == baryonyx::result_status::success);
if (result.status == baryonyx::result_status::success)
Ensures(result.solutions.back().value == 790.0);
if (result.status == baryonyx::result_status::success) {
pb = baryonyx::make_problem(ctx, EXAMPLES_DIR "/small4.lp");
Ensures(baryonyx::is_valid_solution(pb, result) == true);
Ensures(baryonyx::compute_solution(pb, result) == 790.0);
}
}
baryonyx::solver_parameters params;
params.delta = 1e-2;
params.time_limit = 5.0;
params.mode = baryonyx::solver_parameters::mode_type::branch;
baryonyx::context_set_solver_parameters(ctx, params);
void
test_n_queens_problem()
{
auto ctx = baryonyx::make_context(6);
std::vector<bool> valid_solutions(30, false);
std::vector<double> solutions(30, 0.0);
std::vector<double> cplex_solutions(30, 0.0);
{
// Tries to read the cplex solution files produced by CPLEX 12.7.0.0
// and the `script.sh' `n-queens-problem.commands' files. If an error
// occured, the test fails and returns.*/
std::ifstream ifs{ EXAMPLES_DIR "/n-queens/solutions.txt" };
Ensures(ifs.is_open());
if (!ifs.is_open())
return;
for (auto& elem : cplex_solutions)
ifs >> elem;
Ensures(ifs.good());
if (!ifs.good())
return;
}
baryonyx::solver_parameters params;
params.limit = 100000;
params.theta = 0.5;
params.delta = 1.0;
params.kappa_min = 0.30;
params.kappa_step = 1e-2;
params.kappa_max = 100.0;
params.alpha = 1.0;
params.w = 60;
params.time_limit = 20.0;
params.pushing_k_factor = 0.9;
params.pushes_limit = 50;
params.pushing_objective_amplifier = 10;
params.pushing_iteration_limit = 10;
params.order =
baryonyx::solver_parameters::constraint_order::random_sorting;
baryonyx::context_set_solver_parameters(ctx, params);
for (std::size_t i{ 0 }; i != valid_solutions.size(); ++i) {
std::string filepath{ EXAMPLES_DIR "/n-queens/n-queens-problem-" };
filepath += std::to_string(i);
filepath += ".lp";
auto pb = baryonyx::make_problem(ctx, filepath);
auto result = baryonyx::optimize(ctx, pb);
valid_solutions[i] = (result.remaining_constraints == 0);
if (valid_solutions[i])
solutions[i] = result.solutions.back().value;
}
auto all_found =
std::accumulate(valid_solutions.cbegin(),
valid_solutions.cend(),
static_cast<std::size_t>(0),
[](std::size_t cumul, const auto& elem) -> std::size_t {
return elem ? cumul + 1 : cumul;
});
expect(result);
};
"test_qap"_test = [] {
auto ctx = baryonyx::make_context(4);
auto pb = baryonyx::make_problem(ctx, EXAMPLES_DIR "/small4.lp");
baryonyx::solver_parameters params;
params.limit = -1;
params.theta = 0.5;
params.delta = 0.2;
params.kappa_step = 10e-4;
params.kappa_max = 10.0;
params.alpha = 0.0;
params.w = 20;
params.time_limit = 40.0;
params.pushing_k_factor = 0.9;
params.pushes_limit = 50;
params.pushing_objective_amplifier = 10;
params.pushing_iteration_limit = 50;
params.thread = 2;
baryonyx::context_set_solver_parameters(ctx, params);
double mean_distance{ 0 };
for (std::size_t i{ 0 }, e{ solutions.size() }; i != e; ++i) {
double distance =
((cplex_solutions[i] - solutions[i]) / cplex_solutions[i]) * 100.0;
fmt::print("{}: {} {} {} {}\n",
i,
valid_solutions[i],
solutions[i],
cplex_solutions[i],
distance);
mean_distance += distance;
}
fmt::print("Optimum means: {}\n",
mean_distance / static_cast<double>(solutions.size()));
Ensures(all_found == valid_solutions.size());
}
int
main(int /* argc */, char* /* argv */[])
{
unit_test::checks("bibd1n", test_bibd1n);
unit_test::checks("qap", test_qap);
unit_test::checks("n_queens_problem", test_n_queens_problem);
auto result = baryonyx::optimize(ctx, pb);
return unit_test::report_errors();
expect(result.status == baryonyx::result_status::success);
if (result.status == baryonyx::result_status::success)
expect(result.solutions.back().value == 790.0);
if (result.status == baryonyx::result_status::success) {
pb = baryonyx::make_problem(ctx, EXAMPLES_DIR "/small4.lp");
expect(baryonyx::is_valid_solution(pb, result) == true);
expect(baryonyx::compute_solution(pb, result) == 790.0);
}
};
"test_n_queens_problem"_test = [] {
auto ctx = baryonyx::make_context(4);
std::vector<bool> valid_solutions(30, false);
std::vector<double> solutions(30, 0.0);
std::vector<double> cplex_solutions(30, 0.0);
{
// Tries to read the cplex solution files produced by
// CPLEX 12.7.0.0 and the `script.sh' `n-queens-problem.commands'
// files. If an error occured, the test fails and returns.*/
std::ifstream ifs{ EXAMPLES_DIR "/n-queens/solutions.txt" };
expect(ifs.is_open());
if (!ifs.is_open())
return;
for (auto& elem : cplex_solutions)
ifs >> elem;
expect(ifs.good());
if (!ifs.good())
return;
}
baryonyx::solver_parameters params;
params.limit = 100000;
params.theta = 0.5;
params.delta = 1.0;
params.kappa_min = 0.30;
params.kappa_step = 1e-2;
params.kappa_max = 100.0;
params.alpha = 1.0;
params.w = 60;
params.time_limit = 20.0;
params.pushing_k_factor = 0.9;
params.pushes_limit = 50;
params.pushing_objective_amplifier = 10;
params.pushing_iteration_limit = 10;
params.order =
baryonyx::solver_parameters::constraint_order::random_sorting;
baryonyx::context_set_solver_parameters(ctx, params);
for (std::size_t i{ 0 }; i != valid_solutions.size(); ++i) {
std::string filepath{ EXAMPLES_DIR "/n-queens/n-queens-problem-" };
filepath += std::to_string(i);
filepath += ".lp";
auto pb = baryonyx::make_problem(ctx, filepath);
auto result = baryonyx::optimize(ctx, pb);
valid_solutions[i] = (result.remaining_constraints == 0);
if (valid_solutions[i])
solutions[i] = result.solutions.back().value;
}
auto all_found = std::accumulate(
valid_solutions.cbegin(),
valid_solutions.cend(),
static_cast<std::size_t>(0),
[](std::size_t cumul, const auto& elem) -> std::size_t {
return elem ? cumul + 1 : cumul;
});
double mean_distance{ 0 };
for (std::size_t i{ 0 }, e{ solutions.size() }; i != e; ++i) {
double distance =
((cplex_solutions[i] - solutions[i]) / cplex_solutions[i]) *
100.0;
mean_distance += distance;
}
expect(mean_distance >= 0.0_d);
expect(all_found == valid_solutions.size());
};
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment