SpyOpt
is a C++ implementation of the metaheuristic spy_algorithm [1]. It can search for global optimal solutions for arbitrary non-convex functions.
SpyOpt
はメタヒューリスティクスである spy_algorithm
[1] の C++ 実装です。
任意の非凸関数に対し、大域最適解を探索することができます。
Ackley Function
Eggholder Function
- A compiler that supports C++17
yaml-cpp
library
-
Install yaml-cpp:
On Ubuntu:
sudo apt update sudo apt install libyaml-cpp-dev
-
Clone the repository:
git clone [email protected]:KentaKato/SpyOpt.git cd SpyOpt
-
Create a build directory:
mkdir build cd build
-
Run CMake and build:
cmake .. make
This will produce the executable file spyopt
in the build
directory.
-
Execute Optimization
After building, you can run the spyopt executable:
./spyopt
The results will be saved in
results/history.yaml
. -
Animation of Otimization History
cd SpyOpt python3 scripts/create_history_animation.py
The gif file will be saved in current directory.
How to Modify Search Parameters
Settings can be adjusted via resources/config.yaml
.
num_agents: 50 # Number of agents. Should be greater than (num_high_rank + num_mid_rank)
num_high_rank: 10 # Number of high rank agents
num_mid_rank: 25 # Number of mid rank agents
num_iterations: 50 # Number of iterations
swing_factor: 1
objective_function: Ackley # Booth, Eggholder, Ackley
How to Customize the Objective Function
-
Implement it as following:
double your_custom_function(const std::vector<double> &pos) { ... return ...; }
-
Set the search space in
resources/config.yaml
.lower_bounds: [-5., -5.] upper_bounds: [5., 5.]
-
Pass the custom function as an argument to the
SpyOpt`` constructor and execute the
optimize()` method.Minimum Code Example:
#include <iostream> #include "SpyOpt/spy_opt.h" #include "SpyOpt/config_parser.h" #include "SpyOpt/objective_functions.h" // Assuming you've added your custom function in this header int main() { // Define a simple custom objective function auto custom_function = [](const std::vector<double> &pos) -> double { return pos[0] * pos[0] + pos[1] * pos[1]; }; Config config; if (!parseConfig("resources/config.yaml", config)) { std::cerr << "[Error] Failed to parse config!" << std::endl; return -1; } SpyOpt spy_alg(config, custom_function); spy_alg.optimize(); const auto [fitness, pos] = spy_alg.getBestFitness(); std::cout << "Best solution:" << std::endl; spy_alg.printBestAgent(); spy_alg.dumpHistory("results/history.csv"); return 0; }
[1] Pambudi, Dhidhi, and Masaki Kawamura. "Novel metaheuristic: spy algorithm." IEICE TRANSACTIONS on Information and Systems 105.2 (2022): 309-319.