Skip to content

Commit fc18eb6

Browse files
fix gcc compiler error
1 parent c91e0e3 commit fc18eb6

File tree

10 files changed

+400
-363
lines changed

10 files changed

+400
-363
lines changed
+22-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
cmake_minimum_required(VERSION 3.15)
2-
set(Data_Dir ${CMAKE_CURRENT_LIST_DIR}/../include/SEvoBench/problem/cec/)
2+
get_filename_component(Data_Dir
3+
"${CMAKE_CURRENT_LIST_DIR}/../../../include/SEvoBench/problem/cec/"
4+
ABSOLUTE
5+
)
6+
7+
if(NOT EXISTS ${Data_Dir})
8+
message(FATAL_ERROR "Data directory not found: ${Data_Dir}")
9+
endif()
310
include("../../function.cmake")
4-
find_package(ioh 0.3.18 REQUIRED ioh)
5-
add_executable(ioh_benchmark ioh_benchmark.cpp)
6-
target_link_libraries(ioh_benchmark PRIVATE ioh::ioh)
11+
12+
find_package(ioh 0.3.18 QUIET)
13+
714
add_executable(sevobench_benchmark_parallel sevobench_benchmark.cpp)
8-
target_compile_options(sevobench_benchmark_parallel PRIVATE DATA_DIR="${Data_Dir}",PARALLEL=1)
15+
target_compile_definitions(sevobench_benchmark_parallel DATA_DIR="${Data_Dir}" PARALLEL=1)
16+
917
add_executable(sevobench_benchmark sevobench_benchmark.cpp)
10-
target_compile_options(sevobench_benchmark PRIVATE DATA_DIR="${Data_Dir}",PARALLEL=0)
11-
common(ioh_benchmark)
18+
target_compile_definitions(sevobench_benchmark DATA_DIR="${Data_Dir}" PARALLEL=0)
19+
1220
common(sevobench_benchmark_parallel)
13-
common(sevobench_benchmark)
21+
common(sevobench_benchmark)
22+
23+
if(ioh_FOUND)
24+
add_executable(ioh_benchmark ioh_benchmark.cpp)
25+
target_link_libraries(ioh_benchmark PRIVATE ioh::ioh)
26+
common(ioh_benchmark)
27+
endif()

example/experiment_example/main.cpp

+15-21
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,32 @@ int main() {
88
auto suite = problem::suite_builder<sevobench::problem::cec2017>()
99
.template dim<30>()
1010
.type<double>()
11-
.problem_index(problem::problem_range<1,15>())
11+
.problem_index(problem::problem_range<1, 15>())
1212
.instance_count(5)
1313
.build();
1414
// 2. Initialize performance tracker
1515
best_so_far_record<double> recorder(suite, 1e5, 25, 1000);
1616

17-
1817
// 4. Execute benchmark
1918
evo_bench(
20-
[](auto& p) {
19+
[](auto &p) {
2120
evolutionary_algorithm tracker(1e5, 100, 30);
2221
// 3. Configure JADE algorithm
23-
auto config = de_config<true,double>{
22+
auto config = de_config<true, double>{
2423
std::make_unique<jade_parameter<double>>(0.1, 0.5),
2524
std::make_unique<ttpb1_mutation<double>>(0.11),
2625
std::make_unique<projection_repair<double>>(),
2726
std::make_unique<binomial_crossover<double>>(),
28-
std::make_unique<linear_reduction<double>>(tracker,50,100),
29-
std::make_unique<fifo_archive<double>>(2.0)
30-
};
31-
population<double> pop(100, tracker.dim(),
32-
double(-100),
33-
double(100));
34-
auto de=de_algorithm(std::move(config));
35-
return de
36-
.run(pop, [&](std::span<const double> x){ return p(x); },double(-100),
37-
double(100),
38-
tracker);
27+
std::make_unique<linear_reduction<double>>(tracker, 50, 100),
28+
std::make_unique<fifo_archive<double>>(2.0)};
29+
population<double> pop(100, tracker.dim(), double(-100), double(100));
30+
auto de = de_algorithm(std::move(config));
31+
return de.run(
32+
pop, [&](std::span<const double> x) { return p(x); }, double(-100),
33+
double(100), tracker);
3934
},
40-
suite,
41-
recorder,
42-
25 // 25 independent runs
35+
suite, recorder,
36+
25 // 25 independent runs
4337
);
4438

4539
// 5. Export results
@@ -49,9 +43,9 @@ int main() {
4943
for (int inst = 1; inst <= suite.instance_count(); inst++) {
5044
auto data = recorder.at(pid, inst);
5145
for (size_t run = 0; run < data.size(); run++) {
52-
for (const auto& [fes, fit] : data[run]) {
53-
csv << pid << "," << inst << "," << run+1 << ","
54-
<< fes << "," << fit << "\n";
46+
for (const auto &[fes, fit] : data[run]) {
47+
csv << pid << "," << inst << "," << run + 1 << "," << fes << ","
48+
<< fit << "\n";
5549
}
5650
}
5751
}

example/hybrid_pso_de/hybrid_pso_de.h

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
#pragma once
2-
#include"SEvoBench/sevobench.hpp"
3-
template<std::floating_point T>
4-
void hybrid_pso_de(auto &&pso_alg,auto &&de_alg,auto &pop,auto &&f,T lb,T ub,auto &&alg) {
5-
using namespace sevobench;
6-
auto ps=pop.pop_size();
7-
auto dim=pop.dim();
8-
population<T> pop1(ps,dim),pop2(ps,dim),pop3(ps,dim);
9-
pso_module::pso_velocity<T> vec(ps,std::vector<T>(dim));
10-
for (auto &_ : pop)
11-
_.evaluate(f);
12-
alg.add_fes(pop.pop_size());
13-
pso_alg.topology()->prepare(pop);
14-
do {
15-
pop1=pop;
16-
pop3=pop;
17-
pso_alg.iterator(pop1,vec,f,lb,ub,alg);
18-
de_alg.iterator(pop3,pop2,f,lb,ub,alg);
19-
for(int i=0;i<ps;i++)
20-
for(int j=0;j<dim;j++)
21-
vec[i][j]=pop3[i][j]-pop[i][j];
22-
for(int i=0;i<ps;i++)
23-
std::swap(pop[i],pop1[i].fitness()<pop3[i].fitness()?pop1[i]:pop3[i]);
24-
}while(alg.current_fes()<alg.max_fes());
25-
}
2+
#include "SEvoBench/sevobench.hpp"
3+
template <std::floating_point T>
4+
void hybrid_pso_de(auto &&pso_alg, auto &&de_alg, auto &&pop, auto &&f, T lb,
5+
T ub, auto &&alg) {
6+
using namespace sevobench;
7+
auto ps = pop.pop_size();
8+
auto dim = pop.dim();
9+
population<T> pop1(ps, dim), pop2(ps, dim), pop3(ps, dim);
10+
pso_module::pso_velocity<T> vec(ps, std::vector<T>(dim));
11+
for (auto &_ : pop)
12+
_.evaluate(f);
13+
alg.add_fes(pop.pop_size());
14+
pso_alg.topology()->prepare(pop);
15+
do {
16+
pop1 = pop;
17+
pop3 = pop;
18+
pso_alg.iterator(pop1, vec, f, lb, ub, alg);
19+
de_alg.iterator(pop3, pop2, f, lb, ub, alg);
20+
for (int i = 0; i < ps; i++)
21+
for (int j = 0; j < dim; j++)
22+
vec[i][j] = pop3[i][j] - pop[i][j];
23+
for (int i = 0; i < ps; i++)
24+
std::swap(pop[i],
25+
pop1[i].fitness() < pop3[i].fitness() ? pop1[i] : pop3[i]);
26+
} while (alg.current_fes() < alg.max_fes());
27+
}

example/hybrid_pso_de/main.cpp

+43-39
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
1+
#include "hybrid_pso_de.h"
12
#include <iostream>
2-
#include"hybrid_pso_de.h"
3-
template <typename T>
4-
inline auto rosenbrock(std::span<const T> t) noexcept {
5-
T sum = 0;
6-
for (int i = 0; i < t.size() - 1; i++)
7-
sum += sevobench::tool::Pow<2>(t[i] - 1) +
8-
100 * sevobench::tool::Pow<2>((t[i + 1] - t[i] * t[i]));
9-
return sum;
3+
template <typename T> inline auto rosenbrock(std::span<const T> t) noexcept {
4+
T sum = 0;
5+
for (int i = 0; i < t.size() - 1; i++)
6+
sum += sevobench::tool::Pow<2>(t[i] - 1) +
7+
100 * sevobench::tool::Pow<2>((t[i + 1] - t[i] * t[i]));
8+
return sum;
109
}
1110
int main() {
12-
using namespace sevobench;
13-
using namespace sevobench::pso_module;
14-
using namespace sevobench::de_module;
15-
auto topo=std::make_unique<lbest_topology<float>>();
16-
auto upda= std::make_unique<spherical_update<float>>();
17-
auto pso=pso_algorithm_builder().
18-
topology(std::move(topo)).
19-
update(std::move(upda)).
20-
build();
21-
auto p= std::make_unique<shade_parameter<float>>();
22-
auto m=std::make_unique<ttpb1_mutation<float>>();
23-
auto c= std::make_unique<binomial_crossover<float>>();
24-
auto h=std::make_unique<midpoint_target_repair<float>>();
25-
auto s= std::make_unique<de_population<float>>();
26-
auto shade=de_algorithm_builder().
27-
mutation(std::move(m)).
28-
parameter(std::move(p)).
29-
population_strategy(std::move(s)).
30-
crossover(std::move(c)).
31-
constraint_handler(std::move(h)).
32-
build();
33-
constexpr int dim=30;
34-
constexpr int pop_size=100;
35-
constexpr int max_fes=1000*dim;
36-
evolutionary_algorithm alg(max_fes,pop_size,dim);
37-
population<float> pop(pop_size,dim,float(-100),float(100));
38-
hybrid_pso_de(pso,shade,pop,[](std::span<const float> x){return rosenbrock(x);},float(-100),float(100),alg);
39-
std::cout << "best value:"<<std::min_element(pop.begin(),pop.end(),[](auto &l,auto &r){
40-
return l.fitness()<r.fitness();
41-
})->fitness() << std::endl;
42-
return 0;
11+
using namespace sevobench;
12+
using namespace sevobench::pso_module;
13+
using namespace sevobench::de_module;
14+
auto topo = std::make_unique<lbest_topology<float>>();
15+
auto upda = std::make_unique<spherical_update<float>>();
16+
auto pso = pso_algorithm_builder()
17+
.topology(std::move(topo))
18+
.update(std::move(upda))
19+
.build();
20+
auto p = std::make_unique<shade_parameter<float>>();
21+
auto m = std::make_unique<ttpb1_mutation<float>>();
22+
auto c = std::make_unique<binomial_crossover<float>>();
23+
auto h = std::make_unique<midpoint_target_repair<float>>();
24+
auto s = std::make_unique<de_population<float>>();
25+
auto shade = de_algorithm_builder()
26+
.mutation(std::move(m))
27+
.parameter(std::move(p))
28+
.population_strategy(std::move(s))
29+
.crossover(std::move(c))
30+
.constraint_handler(std::move(h))
31+
.build();
32+
constexpr int dim = 30;
33+
constexpr int pop_size = 100;
34+
constexpr int max_fes = 1000 * dim;
35+
evolutionary_algorithm alg(max_fes, pop_size, dim);
36+
population<float> pop(pop_size, dim, float(-100), float(100));
37+
hybrid_pso_de(
38+
pso, shade, pop, [](std::span<const float> x) { return rosenbrock(x); },
39+
float(-100), float(100), alg);
40+
std::cout << "best value:"
41+
<< std::min_element(
42+
pop.begin(), pop.end(),
43+
[](auto &l, auto &r) { return l.fitness() < r.fitness(); })
44+
->fitness()
45+
<< std::endl;
46+
return 0;
4347
}

0 commit comments

Comments
 (0)