Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ewarchul committed Jan 12, 2025
1 parent e02c956 commit dd7f95f
Show file tree
Hide file tree
Showing 49 changed files with 314 additions and 294 deletions.
48 changes: 25 additions & 23 deletions include/cecxx/benchmark/cec_2017/basic_problems.hpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
#pragma once

#include "cecxx/benchmark/detail/legacy/functions/multimodal/bi_rastrigin.hpp"
#include "cecxx/benchmark/detail/legacy/functions/multimodal/schaffer.hpp"
#include "cecxx/benchmark/detail/problem_invokers/basic_problem_invoker.hpp"
#include "cecxx/functions/multimodal/ackley.hpp"
#include "cecxx/functions/multimodal/discus.hpp"
#include "cecxx/functions/multimodal/escaffer.hpp"
#include "cecxx/functions/multimodal/grie_rosen.hpp"
#include "cecxx/functions/multimodal/griewank.hpp"
#include "cecxx/functions/multimodal/happycat.hpp"
#include "cecxx/functions/multimodal/hgbat.hpp"
#include "cecxx/functions/multimodal/katsuura.hpp"
#include "cecxx/functions/multimodal/levy.hpp"
#include "cecxx/functions/multimodal/rastrigin.hpp"
#include "cecxx/functions/multimodal/rosenbrock.hpp"
#include "cecxx/functions/multimodal/schwefel.hpp"
#include "cecxx/functions/multimodal/step_rastrigin.hpp"
#include "cecxx/functions/multimodal/weierstrass.hpp"
#include "cecxx/functions/multimodal/zakharov.hpp"
#include "cecxx/functions/unimodal/bent_cigar.hpp"
#include "cecxx/functions/unimodal/ellips.hpp"
#include "cecxx/functions/unimodal/sum_diff_pow.hpp"
#include <cecxx/benchmark/detail/legacy/functions/multimodal/bi_rastrigin.hpp>
#include <cecxx/benchmark/detail/legacy/functions/multimodal/schaffer.hpp>
#include <cecxx/benchmark/detail/problem_invokers/basic_problem_invoker.hpp>
#include <cecxx/functions/multimodal/ackley.hpp>
#include <cecxx/functions/multimodal/discus.hpp>
#include <cecxx/functions/multimodal/escaffer.hpp>
#include <cecxx/functions/multimodal/grie_rosen.hpp>
#include <cecxx/functions/multimodal/griewank.hpp>
#include <cecxx/functions/multimodal/happycat.hpp>
#include <cecxx/functions/multimodal/hgbat.hpp>
#include <cecxx/functions/multimodal/katsuura.hpp>
#include <cecxx/functions/multimodal/levy.hpp>
#include <cecxx/functions/multimodal/rastrigin.hpp>
#include <cecxx/functions/multimodal/rosenbrock.hpp>
#include <cecxx/functions/multimodal/schwefel.hpp>
#include <cecxx/functions/multimodal/step_rastrigin.hpp>
#include <cecxx/functions/multimodal/weierstrass.hpp>
#include <cecxx/functions/multimodal/zakharov.hpp>
#include <cecxx/functions/unimodal/bent_cigar.hpp>
#include <cecxx/functions/unimodal/ellips.hpp>
#include <cecxx/functions/unimodal/sum_diff_pow.hpp>

namespace cecxx::benchmark::cec_2017 {
static constexpr auto sum_diff_pow = detail::basic_problem_invoker{cecxx::functions::unimodal::sum_diff_pow};
static constexpr auto bent_cigar = detail::basic_problem_invoker{cecxx::functions::unimodal::bent_cigar};
static constexpr auto ellips = detail::basic_problem_invoker{cecxx::functions::unimodal::ellips};
static constexpr auto discus = detail::basic_problem_invoker{cecxx::functions::multimodal::discus};
static constexpr auto zakharov = detail::basic_problem_invoker{cecxx::functions::multimodal::zakharov};
static constexpr auto rosenbrock = detail::basic_problem_invoker{cecxx::functions::multimodal::rosenbrock, 2.048 / 100.0};
static constexpr auto rosenbrock
= detail::basic_problem_invoker{cecxx::functions::multimodal::rosenbrock, 2.048 / 100.0};
static constexpr auto rastrigin = detail::basic_problem_invoker{cecxx::functions::multimodal::rastrigin, 5.12 / 100.0};
static constexpr auto schaffer = detail::basic_problem_invoker{cecxx::functions::multimodal::legacy::schaffer};
static constexpr auto bi_rastrigin = detail::basic_problem_invoker{cecxx::functions::multimodal::legacy::bi_rastrigin};
Expand All @@ -42,6 +43,7 @@ static constexpr auto happycat = detail::basic_problem_invoker{cecxx::functions:
static constexpr auto escaffer = detail::basic_problem_invoker{cecxx::functions::multimodal::escaffer};
static constexpr auto katsuura = detail::basic_problem_invoker{cecxx::functions::multimodal::katsuura, 5.0 / 100.0};
static constexpr auto grie_rosen = detail::basic_problem_invoker{cecxx::functions::multimodal::grie_rosen, 5.0 / 100.0};
static constexpr auto weierstrass = detail::basic_problem_invoker{cecxx::functions::multimodal::weierstrass, 0.5 / 100.0};
static constexpr auto weierstrass
= detail::basic_problem_invoker{cecxx::functions::multimodal::weierstrass, 0.5 / 100.0};
static constexpr auto griewank = detail::basic_problem_invoker{cecxx::functions::multimodal::griewank, 6.0};
} // namespace cecxx::benchmark::cec_2017
6 changes: 3 additions & 3 deletions include/cecxx/benchmark/cec_2017/complex_problems.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "cecxx/benchmark/cec_2017/basic_problems.hpp"
#include "cecxx/benchmark/cec_2017/hybrid_problems.hpp"
#include "cecxx/benchmark/detail/problem_invokers/complex_problem_invoker.hpp"
#include <cecxx/benchmark/cec_2017/basic_problems.hpp>
#include <cecxx/benchmark/cec_2017/hybrid_problems.hpp>
#include <cecxx/benchmark/detail/problem_invokers/complex_problem_invoker.hpp>

namespace cecxx::benchmark::cec_2017 {

Expand Down
2 changes: 1 addition & 1 deletion include/cecxx/benchmark/cec_2017/consts.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "cecxx/benchmark/types.hpp"
#include <cecxx/benchmark/types.hpp>
#include <set>
#include <unordered_map>

Expand Down
4 changes: 2 additions & 2 deletions include/cecxx/benchmark/cec_2017/hybrid_problems.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "cecxx/benchmark/cec_2017/basic_problems.hpp"
#include "cecxx/benchmark/detail/problem_invokers/hybrid_problem_invoker.hpp"
#include <cecxx/benchmark/cec_2017/basic_problems.hpp>
#include <cecxx/benchmark/detail/problem_invokers/hybrid_problem_invoker.hpp>

namespace cecxx::benchmark::cec_2017 {
const auto hybrid_1 = detail::hybrid_problem_invoker{std::tuple{zakharov, rosenbrock, rastrigin}, {0.2, 0.4, 0.4}};
Expand Down
74 changes: 3 additions & 71 deletions include/cecxx/benchmark/cec_2017/problem_evaluation_mappings.hpp
Original file line number Diff line number Diff line change
@@ -1,78 +1,10 @@
#pragma once

#include "cecxx/benchmark/cec_2017/basic_problems.hpp"
#include "cecxx/benchmark/cec_2017/complex_problems.hpp"
#include "cecxx/benchmark/cec_2017/hybrid_problems.hpp"
#include "cecxx/benchmark/detail/context.hpp"
#include "cecxx/benchmark/types.hpp"
#include <cecxx/benchmark/detail/context.hpp>
#include <cecxx/benchmark/types.hpp>

namespace cecxx::benchmark::cec_2017 {

inline auto evaluate(detail::problem_context_view_t ctx, const problem_number_t fn, std::span<const double> input) {
switch (fn) {
case 1:
return bent_cigar(input, ctx);
case 2:
return sum_diff_pow(input, ctx);
case 3:
return zakharov(input, ctx);
case 4:
return rosenbrock(input, ctx);
case 5:
return rastrigin(input, ctx);
case 6:
return schaffer(input, ctx);
case 7:
return bi_rastrigin(input, ctx);
case 8:
return step_rastrigin(input, ctx);
case 9:
return levy(input, ctx);
case 10:
return schwefel(input, ctx);
case 11:
return hybrid_1(input, ctx);
case 12:
return hybrid_2(input, ctx);
case 13:
return hybrid_3(input, ctx);
case 14:
return hybrid_4(input, ctx);
case 15:
return hybrid_5(input, ctx);
case 16:
return hybrid_6(input, ctx);
case 17:
return hybrid_7(input, ctx);
case 18:
return hybrid_8(input, ctx);
case 19:
return hybrid_9(input, ctx);
case 20:
return hybrid_10(input, ctx);
case 21:
return complex_1(input, ctx);
case 22:
return complex_2(input, ctx);
case 23:
return complex_3(input, ctx);
case 24:
return complex_4(input, ctx);
case 25:
return complex_5(input, ctx);
case 26:
return complex_6(input, ctx);
case 27:
return complex_7(input, ctx);
case 28:
return complex_8(input, ctx);
case 29:
return complex_9(input, ctx);
case 30:
return complex_10(input, ctx);
}

throw std::runtime_error{"Unknown CEC problem"};
}
auto evaluate(detail::problem_context_view_t ctx, const problem_number_t fn, std::span<const double> input) -> double;

} // namespace cecxx::benchmark::cec_2017
2 changes: 1 addition & 1 deletion include/cecxx/benchmark/detail/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <span>
#include <unordered_map>

#include "cecxx/benchmark/types.hpp"
#include <cecxx/benchmark/types.hpp>

namespace cecxx::benchmark::detail {
struct problem_context_t {
Expand Down
28 changes: 9 additions & 19 deletions include/cecxx/benchmark/detail/evaluate.hpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
#pragma once

#include "cecxx/benchmark/concepts.hpp"
#include "cecxx/benchmark/detail/context.hpp"
#include "cecxx/benchmark/helpers.hpp"
#include "cecxx/benchmark/types.hpp"

#include "cecxx/benchmark/cec_2017/problem_evaluation_mappings.hpp"
#include "cecxx/detail/mdspan/__p0009_bits/full_extent_t.hpp"
#include "cecxx/detail/mdspan/mdspan"
#include <cecxx/benchmark/concepts.hpp>
#include <cecxx/benchmark/detail/context.hpp>
#include <cecxx/benchmark/helpers.hpp>
#include <cecxx/benchmark/types.hpp>

#include <cecxx/detail/mdspan/__p0009_bits/full_extent_t.hpp>
#include <cecxx/detail/mdspan/mdspan>
#include <concepts>
#include <print>
#include <type_traits>
#include <utility>

namespace cecxx::benchmark::detail {

inline auto evaluate_selected_problem(cec_edition_t cec, problem_context_view_t ctx, const problem_number_t fn,
std::span<const double> input) -> double {
using enum cec_edition_t;
switch (cec) {
case cec2017:
return cec_2017::evaluate(ctx, fn, input);
}

std::unreachable();
}
auto evaluate_selected_problem(cec_edition_t cec, problem_context_view_t ctx, const problem_number_t fn,
std::span<const double> input) -> double;

auto evaluate(cec_edition_t cec, problem_context auto &&ctx, const problem_number_t fn, matrix_t input) {
const auto nrow = input.extent(0);
Expand Down
2 changes: 1 addition & 1 deletion include/cecxx/benchmark/detail/func_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <span>
#include <vector>

#include "cecxx/benchmark/detail/context.hpp"
#include <cecxx/benchmark/detail/context.hpp>

namespace cecxx::benchmark::detail {

Expand Down
4 changes: 2 additions & 2 deletions include/cecxx/benchmark/detail/geom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <span>
#include <vector>

#include "cecxx/benchmark/detail/context.hpp"
#include "cecxx/benchmark/detail/legacy/affine_transformation.hpp"
#include <cecxx/benchmark/detail/context.hpp>
#include <cecxx/benchmark/detail/legacy/affine_transformation.hpp>

namespace cecxx::benchmark::detail {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "cecxx/benchmark/types.hpp"
#include <cecxx/benchmark/types.hpp>
#include <span>

namespace cecxx::benchmark::detail {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,89 +1,12 @@
#pragma once

#include <cmath>
#include <vector>

#include "cecxx/benchmark/detail/context.hpp"
#include "cecxx/benchmark/detail/legacy/affine_transformation.hpp"
#include "cecxx/enum.hpp"

namespace cecxx::functions::multimodal::legacy {

inline auto bi_rastrigin(std::span<const double> input, benchmark::detail::problem_context_view_t ctx,
benchmark::affine_mask_t mask) -> double {
using namespace cecxx::benchmark;
const auto nrow = input.size();
auto y = std::vector<double>(nrow);
auto z = std::vector<double>(nrow);
auto tmpx = std::vector<double>(nrow);

if (to_underlying(mask.shift) == 1) {
detail::shiftfunc(input, y, ctx.shift);
} else {
// shrink to the orginal search range
for (auto i = 0u; i < nrow; i++) {
y[i] = input[i];
}
}

// shrink to the orginal search range
for (auto i = 0u; i < nrow; i++) {
y[i] *= 10.0 / 100.0;
}

constexpr auto mu0 = 2.5;

for (auto i = 0u; i < nrow; i++) {
tmpx[i] = 2 * y[i];
if (ctx.shift[i] < 0.0)
tmpx[i] *= -1.;
}
for (auto i = 0u; i < nrow; i++) {
z[i] = tmpx[i];
tmpx[i] += mu0;
}

double tmp{};
double tmp1{};
double tmp2{};
constexpr auto d = 1.0;
auto s = 1.0 - 1.0 / (2.0 * std::pow(static_cast<double>(nrow) + 20.0, 0.5) - 8.2);
auto mu1 = -std::pow((mu0 * mu0 - d) / s, 0.5);
for (auto i = 0u; i < nrow; i++) {
tmp = tmpx[i] - mu0;
tmp1 += tmp * tmp;
tmp = tmpx[i] - mu1;
tmp2 += tmp * tmp;
}
tmp2 *= s;
tmp2 += d * static_cast<double>(nrow);
tmp = 0.0;
auto output{0.0};

if (to_underlying(mask.rot) == 1) {
detail::rotatefunc(z, y, ctx.rotate);
for (auto i = 0u; i < nrow; i++) {
tmp += std::cos(2.0 * M_PI * y[i]);
}
if (tmp1 < tmp2) {
output = tmp1;
} else {
output = tmp2;
}
output += 10.0 * (static_cast<double>(nrow) - tmp);
} else {
for (auto i = 0u; i < nrow; i++) {
tmp += std::cos(2.0 * M_PI * z[i]);
}
if (tmp1 < tmp2) {
output = tmp1;
} else {
output = tmp2;
}
output += 10.0 * (static_cast<double>(nrow) - tmp);
}

return output;
}
auto bi_rastrigin(std::span<const double> input, benchmark::detail::problem_context_view_t ctx,
benchmark::affine_mask_t mask) -> double;

} // namespace cecxx::functions::multimodal::legacy
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,10 @@
#include <cmath>
#include <vector>

#include "cecxx/benchmark/detail/context.hpp"
#include "cecxx/benchmark/detail/legacy/affine_transformation.hpp"
#include <cecxx/benchmark/detail/context.hpp>

namespace cecxx::functions::multimodal::legacy {
inline auto schaffer(std::span<const double> input, benchmark::detail::problem_context_view_t ctx,
benchmark::affine_mask_t mask, std::vector<double> acc = {}) -> double {
const auto nrow = input.size();
std::vector<double> ys{};
if (acc.empty()) {
ys.resize(nrow);
} else {
ys = acc;
}
auto z = std::vector<double>(nrow);

auto output{0.0};
benchmark::detail::sr_func(input, z, ctx.shift, ctx.rotate, 1.0, mask.shift, mask.rot, ys);
double tmp{};
for (auto i = 0u; i < nrow - 1; ++i) {
z[i] = std::pow(ys[i] * ys[i] + ys[i + 1] * ys[i + 1], 0.5);
tmp = std::sin(50.0 * std::pow(z[i], 0.2));
output += std::pow(z[i], 0.5) + std::pow(z[i], 0.5) * tmp * tmp;
}

return output * output / (static_cast<double>(nrow) - 1) / (static_cast<double>(nrow) - 1);
}
auto schaffer(std::span<const double> input, benchmark::detail::problem_context_view_t ctx,
benchmark::affine_mask_t mask, std::vector<double> acc = {}) -> double;

} // namespace cecxx::functions::multimodal::legacy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "cecxx/benchmark/detail/func_types.hpp"
#include "cecxx/benchmark/detail/geom.hpp"
#include <cecxx/benchmark/detail/func_types.hpp>
#include <cecxx/benchmark/detail/geom.hpp>

namespace cecxx::benchmark::detail {

Expand Down
Loading

0 comments on commit dd7f95f

Please sign in to comment.