Skip to content

Commit bd1a432

Browse files
committed
f
1 parent 7495cb4 commit bd1a432

File tree

9 files changed

+96
-44
lines changed

9 files changed

+96
-44
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ file(
4141
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
4242
add_library(cecxx SHARED ${SOURCES} ${HEADERS})
4343
generate_export_header(cecxx)
44-
target_compile_options(cecxx PRIVATE -Wall -Wpedantic -Wextra -O3)
44+
target_compile_options(cecxx PRIVATE -Wall -Wpedantic -Wextra -Ofast)
4545
target_include_directories(cecxx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
4646
set_target_properties(cecxx PROPERTIES LINKER_LANGUAGE CXX CXX_STANDARD 20)
4747

@@ -81,4 +81,4 @@ install(DIRECTORY ${PROJECT_SOURCE_DIR}/data DESTINATION share/cecxx)
8181
add_executable(main main.cpp)
8282
target_link_libraries(main cecxx fmt::fmt)
8383
set_target_properties(main PROPERTIES CXX_STANDARD 20)
84-
target_compile_options(main PRIVATE -Wall -Wextra -Wpedantic -O3)
84+
target_compile_options(main PRIVATE -Wall -Wextra -Wpedantic -Ofast)

include/cecxx/detail/benchmark/cec2017/cec2017.hpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55
#include "cecxx/detail/benchmark/cec2017/consts.hpp"
66
#include "cecxx/detail/benchmark/invoke.hpp"
77
#include "cecxx/detail/context.hpp"
8+
#include "cecxx/problem/multimodal/ackley.hpp"
89
#include "cecxx/problem/multimodal/bi_rastrigin.hpp"
10+
#include "cecxx/problem/multimodal/discus.hpp"
11+
#include "cecxx/problem/multimodal/escaffer.hpp"
12+
#include "cecxx/problem/multimodal/grie_rosen.hpp"
13+
#include "cecxx/problem/multimodal/hgbat.hpp"
14+
#include "cecxx/problem/multimodal/katsuura.hpp"
915
#include "cecxx/problem/multimodal/levy.hpp"
1016
#include "cecxx/problem/multimodal/rastrigin.hpp"
1117
#include "cecxx/problem/multimodal/rosenbrock.hpp"
1218
#include "cecxx/problem/multimodal/schaffer.hpp"
1319
#include "cecxx/problem/multimodal/schwefel.hpp"
1420
#include "cecxx/problem/multimodal/step_rastrigin.hpp"
21+
#include "cecxx/problem/multimodal/weierstrass.hpp"
1522
#include "cecxx/problem/multimodal/zakharov.hpp"
1623
#include "cecxx/problem/unimodal/bent_cigar.hpp"
1724
#include "cecxx/problem/unimodal/ellips.hpp"
@@ -62,36 +69,54 @@ auto dispatch_problem(problem_context_view ctx, const int fn, const auto& input)
6269
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
6370
case 13:
6471
return invoke_hybrid_problem(
65-
input, {problem::unimodal::bent_cigar, problem::multimodal::rosenbrock, problem::multimodal::legacy::bi_rastrigin},
72+
input,
73+
{problem::unimodal::bent_cigar, problem::multimodal::rosenbrock, problem::multimodal::legacy::bi_rastrigin},
6674
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 2.048 / 100.0, 1.0});
6775
case 14:
68-
return invoke_hybrid_problem(
69-
input, {problem::unimodal::ellips, problem::multimodal::schwefel, problem::unimodal::bent_cigar},
70-
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
76+
return invoke_hybrid_problem(input,
77+
{problem::unimodal::ellips, problem::multimodal::ackley,
78+
problem::multimodal::legacy::schaffer, problem::multimodal::rastrigin},
79+
{0.2, 0.2, 0.2, 0.4}, ctx,
80+
{.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
7181
case 15:
72-
return invoke_hybrid_problem(
73-
input, {problem::unimodal::ellips, problem::multimodal::schwefel, problem::unimodal::bent_cigar},
74-
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
82+
return invoke_hybrid_problem(input,
83+
{problem::unimodal::bent_cigar, problem::multimodal::hgbat,
84+
problem::multimodal::rastrigin, problem::multimodal::rosenbrock},
85+
{0.2, 0.2, 0.3, 0.3}, ctx,
86+
{.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
7587
case 16:
76-
return invoke_hybrid_problem(
77-
input, {problem::unimodal::ellips, problem::multimodal::schwefel, problem::unimodal::bent_cigar},
78-
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
88+
return invoke_hybrid_problem(input,
89+
{problem::multimodal::escaffer, problem::multimodal::hgbat,
90+
problem::multimodal::rosenbrock, problem::multimodal::schwefel},
91+
{0.2, 0.2, 0.3, 0.3}, ctx,
92+
{.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
7993
case 17:
8094
return invoke_hybrid_problem(
81-
input, {problem::unimodal::ellips, problem::multimodal::schwefel, problem::unimodal::bent_cigar},
82-
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
95+
input,
96+
{problem::multimodal::katsuura, problem::multimodal::ackley, problem::multimodal::grie_rosen,
97+
problem::multimodal::schwefel, problem::multimodal::rastrigin},
98+
{0.1, 0.2, 0.2, 0.2, 0.3}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
8399
case 18:
84100
return invoke_hybrid_problem(
85-
input, {problem::unimodal::ellips, problem::multimodal::schwefel, problem::unimodal::bent_cigar},
86-
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
101+
input,
102+
{problem::unimodal::ellips, problem::multimodal::ackley, problem::multimodal::rastrigin,
103+
problem::multimodal::hgbat, problem::multimodal::discus},
104+
{0.2, 0.2, 0.2, 0.2, 0.2}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
87105
case 19:
88106
return invoke_hybrid_problem(
89-
input, {problem::unimodal::ellips, problem::multimodal::schwefel, problem::unimodal::bent_cigar},
90-
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
107+
input,
108+
{problem::unimodal::bent_cigar, problem::multimodal::rastrigin, problem::multimodal::grie_rosen,
109+
problem::multimodal::weierstrass, problem::multimodal::escaffer},
110+
{0.2, 0.2, 0.2, 0.2, 0.2}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
91111
case 20:
92112
return invoke_hybrid_problem(
93-
input, {problem::unimodal::ellips, problem::multimodal::schwefel, problem::unimodal::bent_cigar},
94-
{0.3, 0.3, 0.4}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes}, {1.0, 10.0, 1.0});
113+
input,
114+
{problem::multimodal::hgbat, problem::multimodal::katsuura, problem::multimodal::ackley,
115+
problem::multimodal::rastrigin, problem::multimodal::rastrigin, problem::multimodal::schwefel,
116+
problem::multimodal::rastrigin},
117+
// problem::multimodal::legacy::schaffer},
118+
{0.1, 0.1, 0.2, 0.2, 0.2, 0.2}, ctx, {.rot = do_affine_trans::yes, .shift = do_affine_trans::yes},
119+
{1.0, 10.0, 1.0});
95120
// case 21:
96121
// cec2017_cf01(input.data(), output.data(), input.size(), ctx.shift(fn), ctx.rotate(fn),
97122
// to_underlying(do_shift::yes));

include/cecxx/detail/benchmark/invoke.hpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <span>
77

88
#include "cecxx/detail/context.hpp"
9+
#include "cecxx/detail/eval_func.hpp"
910
#include "cecxx/detail/legacy/affine_trans.h"
1011
#include "cecxx/detail/types.hpp"
1112

@@ -16,11 +17,10 @@ auto invoke_basic_problem(auto&& fn, std::span<const double> input, problem_cont
1617
auto y = std::vector<double>(nrow);
1718
auto z = std::vector<double>(nrow);
1819
sr_func(input, z, *ctx.shift, *ctx.rotate, mask.rate, mask.shift, mask.rot, y);
19-
2020
return fn(z);
2121
}
2222

23-
auto calc_hybrid_chunks(std::ranges::range auto&& mix_ratios, const int dim) {
23+
auto calc_hybrid_chunks(std::ranges::range auto&& mix_ratios, const std::integral auto dim) {
2424
const auto fn_num = mix_ratios.size();
2525
auto chunk_size = std::vector<double>(fn_num);
2626
double acc{};
@@ -48,13 +48,25 @@ auto invoke_hybrid_problem(std::span<const double> input, std::vector<evaluation
4848

4949
const auto [sizes, offsets] = calc_hybrid_chunks(std::move(ratios), input.size());
5050

51+
auto fn_idxs = std::vector<int>(compounds.size());
52+
std::iota(fn_idxs.begin(), fn_idxs.end(), 0);
53+
5154
auto partial_eval = std::vector<double>(compounds.size());
52-
for (auto fn_idx{0u}; fn_idx < compounds.size(); ++fn_idx) {
55+
std::for_each(fn_idxs.begin(), fn_idxs.end(), [&](const auto fn_idx) {
5356
const auto& fn = compounds[fn_idx];
5457
auto xs = std::span{y}.subspan(offsets[fn_idx], sizes[fn_idx]);
5558
std::for_each(xs.begin(), xs.end(), [s = scales[fn_idx]](auto& elem) { elem *= s; });
56-
partial_eval[fn_idx] = fn(xs);
57-
}
59+
partial_eval[fn_idx] = std::visit(
60+
overloaded{
61+
[&](std::function<double(std::span<const double>)> fn) { return fn(xs); },
62+
[&](std::function<double(std::span<const double>, problem_context_view, affine_mask_t)> fn) {
63+
return fn(xs, ctx, {.rot = do_affine_trans::no, .shift = do_affine_trans::no});
64+
},
65+
[&](std::function<double(std::span<const double>, problem_context_view, affine_mask_t, std::vector<double>)>
66+
fn) { return fn(xs, ctx, mask, y); },
67+
},
68+
std::move(fn));
69+
});
5870

5971
return std::accumulate(partial_eval.begin(), partial_eval.end(), 0.0, std::plus{});
6072
}

include/cecxx/detail/eval_func.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <span>
5+
#include <variant>
6+
#include <vector>
7+
8+
#include "cecxx/detail/context.hpp"
9+
#include "cecxx/detail/types.hpp"
10+
11+
namespace cecxx::detail {
12+
13+
template<class... Ts>
14+
struct overloaded : Ts... { using Ts::operator()...; };
15+
16+
using evaluation_function_t = std::variant<
17+
std::function<double(std::span<const double>)>,
18+
std::function<double(std::span<const double>, problem_context_view, affine_mask_t)>,
19+
std::function<double(std::span<const double>, problem_context_view, affine_mask_t, std::vector<double>)>>;
20+
21+
} // namespace cecxx::detail

include/cecxx/detail/types.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#pragma once
22

33
#include <vector>
4-
#include <span>
5-
#include <functional>
64

75
namespace cecxx::detail {
86
using fn_num = int;
97
template <typename T> using table_data = std::vector<T>;
108

11-
using evaluation_function_t = std::function<double(std::span<const double>)>;
12-
139
enum class table_type_t { rotate, shift, shuffle };
1410

1511
enum class do_affine_trans : int { yes = 1, no = 0 };
@@ -21,5 +17,4 @@ struct affine_mask_t {
2117
double rate{1.0};
2218
};
2319

24-
2520
} // namespace cecxx::detail

include/cecxx/problem/multimodal/discus.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <cmath>
44
#include <span>
55

6-
namespace cecxx::problem::multimodalinput {
6+
namespace cecxx::problem::multimodal {
77
auto discus(std::span<const double> input) -> double {
88
auto output = std::pow(10.0, 6.0) * input[0] * input[0];
99
for (auto i = 1u; i < input.size(); i++) {

include/cecxx/problem/unimodal/sphere.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace cecxx::problem::unimodal {
77

88
auto sphere(std::span<const double> input) -> double {
99
auto output{0.0};
10-
for (double x : input) {
10+
for (const auto& x : input) {
1111
output += x * x;
1212
}
1313

main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "cecxx/cecxx.hpp"
1111

1212
const auto CEC_STORAGE = std::filesystem::path{"/home/ewarchul/open-source/optim/cec/data"};
13-
constexpr auto DIMENSION{10};
13+
constexpr auto DIMENSION{100};
1414

1515
namespace rv = ranges::views;
1616

@@ -24,7 +24,7 @@ auto main() -> int {
2424

2525
// Evaluate given input on each optimization problem from CEC2017
2626
const auto start = std::chrono::system_clock::now();
27-
for (const auto& fn : rv::closed_iota(1, 20)) {
27+
for (const auto& fn : rv::closed_iota(1, 19)) {
2828
auto output = cec2017_eval(fn, input);
2929
fmt::println("fn = {}, output = {}", fn, output);
3030
}

src/legacy/affine_trans.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ void shufflefunc(std::span<const double> input, std::span<double> output, std::s
1414
}
1515
}
1616

17-
1817
void shiftfunc(std::span<const double> input, std::span<double> output, std::span<const double> shift_vec) {
19-
for (auto i = 0; i < output.size(); i++) {
18+
for (auto i = 0u; i < output.size(); i++) {
2019
output[i] = input[i] - shift_vec[i];
2120
}
2221
}
2322

2423
void rotatefunc(std::span<const double> input, std::span<double> output, std::span<const double> rotate_mat) {
2524
const auto nrow = output.size();
26-
for (auto i = 0; i < nrow; i++) {
25+
for (auto i = 0u; i < nrow; i++) {
2726
output[i] = 0;
28-
for (auto j = 0; j < nrow; j++) {
27+
for (auto j = 0u; j < nrow; j++) {
2928
output[i] = output[i] + input[j] * rotate_mat[i * nrow + j];
3029
}
3130
}
@@ -38,24 +37,24 @@ void sr_func(std::span<const double> input, std::span<double> sr_x, std::span<co
3837
if (to_underlying(shift) == 1) {
3938
if (to_underlying(rotate) == 1) {
4039
shiftfunc(input, output, shit_vec);
41-
for (auto i = 0; i < nrow; i++) {
40+
for (auto i = 0u; i < nrow; i++) {
4241
output[i] = output[i] * sh_rate;
4342
}
4443
rotatefunc(output, sr_x, rot_mat);
4544
} else {
4645
shiftfunc(input, sr_x, shit_vec);
47-
for (auto i = 0; i < nrow; i++) {
46+
for (auto i = 0u; i < nrow; i++) {
4847
sr_x[i] = sr_x[i] * sh_rate;
4948
}
5049
}
5150
} else {
5251
if (to_underlying(rotate) == 1) {
53-
for (auto i = 0; i < nrow; i++) {
52+
for (auto i = 0u; i < nrow; i++) {
5453
output[i] = input[i] * sh_rate;
5554
}
5655
rotatefunc(output, sr_x, rot_mat);
5756
} else
58-
for (auto i = 0; i < nrow; i++) {
57+
for (auto i = 0u; i < nrow; i++) {
5958
sr_x[i] = input[i] * sh_rate;
6059
}
6160
}
@@ -69,11 +68,11 @@ void cf_cal(std::span<const double> input, std::span<double> output, std::span<c
6968
for (auto i = 0; i < cf_num; i++) {
7069
fit[i] += bias[i];
7170
w[i] = 0;
72-
for (auto j = 0; j < nrow; j++) {
71+
for (auto j = 0u; j < nrow; j++) {
7372
w[i] += pow(input[j] - shift_vec[i * nrow + j], 2.0);
7473
}
7574
if (w[i] != 0) {
76-
w[i] = pow(1.0 / w[i], 0.5) * exp(-w[i] / 2.0 / nrow / pow(delta[i], 2.0));
75+
w[i] = pow(1.0 / w[i], 0.5) * exp(-w[i] / 2.0 / static_cast<double>(nrow) / pow(delta[i], 2.0));
7776
} else {
7877
w[i] = INF;
7978
}

0 commit comments

Comments
 (0)