|
| 1 | +#include "hybrid_pso_de.h" |
1 | 2 | #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; |
10 | 9 | }
|
11 | 10 | 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; |
43 | 47 | }
|
0 commit comments