Implement ExhaustiveSearch / HillClimbing / SimulatedAnnealing / GeneticAlgo / TabuSearch for Solving OneMax Problem
- Programming language: C++
- Metaheuristic algorithms: Exhaustive Search (ES), Hill Climbing (HC), Simulated Annealing (SA), Genetic Algorithm (GA), Tabu Search (TB)
- Benchmark function: OneMax Problem
- Execution time: approximately 30 minutes total for all algorithms
void RunALG(int bit)
*Runs exhaustive search over all binary strings of length *``void Evaluation(const vector<int>& sol, int& value)
Calculates the fitness value of a solutionvoid Reset()
Resets the counter for exhaustive enumeration
void RunALG(int bit, int run, int iter)
Runs HC for specified number of runs and iterationsvector<int> Init()
Generates a random initial solutionvector<int> Neighbor(const vector<int>& best_sol)
Generates a neighbor by flipping one random bitvoid Evaluation(const vector<int>& sol, int& value)
Calculates fitness value of a solutionvoid Reset()
Resets `` counter per run
void RunALG(int bit, int run, int iter)
Runs SA algorithmvoid Init(vector<int>& sol, int& value)
Generates a random initial solutionvector<int> Neighbor(const vector<int>& sol)
Generates a neighbor by multi-bit mutationvoid Evaluation(const vector<int>& sol, int& value)
Calculates fitness valuevoid T_cooldown(double& temp)/void T_reheat(double& temp)
Temperature schedule functionsvoid Create_Vrecord(const string& filename, const vector<double>& content)
Creates record file for fitness values
void RunALG(int bit, int run, int iter, int pop_size)
Runs GAvoid Init()
*Initializes population of size *``void Select_Roulette()/void Select_Tournament()
Selection strategiesvoid Mutation(vector<int>& child)
Applies adaptive mutationvoid Crossover_Unify(...)/void Crossover_Mask(...)
Two crossover methods:** for diversity, **for stabilityvoid Evaluation(...)
Evaluates fitness of childvoid Create_FitRecord(...)
Creates record file for fitness
void RunALG(int bit, int run, int iter, int tabu_size, int tweak_num)
Runs Tabu Searchvector<int> Init()
Initializes starting solution and best trackingvector<int> Tweak(const vector<int>& origin_sol)
Generates tweaked neighborsint Evaluation(const vector<int>& sol)
Computes OneMax fitnessvoid Create_Record(const string& filename, const vector<T>& content)
Creates record file (templated for flexibility)
- Binary Length:
bit = (64 for ES / 100 for HC, SA, GA / 4 or 10 for TB) - Number of Runs:
run = (e.g., 30) - Iterations Per Run:
iter = (1000 for HC, SA, GA / 5000 for TB) - Population Size:
pop_size = (only for GA) - Type of Algorithm
algo_type = ES / HC / SA / GA / TB - For Tabu Search only:
- ize of Tabu List:
tabu_size = 5 - Times of Tweak:
tweak_num = 20
- ize of Tabu List:
values_of_run_1~30_HC.txtvalues_average_HC.txtplot_HC.pltresult_OneMax_HillClimbing.png
values_of_run_1~30_SA.txtvalues_average_SA.txtplot_SA.pltresult_OneMax_SimulatedAnnealing.png
fitness_of_run_1~30.txtfitness_average.txtplot_GA.pltresult_OneMax_GeneticAlgo.png
fitness_of_run_1~30_TB_bit_size_tweak.txtfitness_average_TB_bit_size_tweak.txtplot_TB.pltresult_OneMax_TB_bit_size_tweak.png
- Open Visual Studio project
OneMax.sln - Press
Ctrl + F5to build
- Open PowerShell or Windows CMD
- Navigate to the correct directory
- Compile with:
g++ main.cpp OneMax.cpp ExhaustiveSearch.cpp HillClimbing.cpp SimulatedAnnealing.cpp GeneticAlgo.cpp Tabu.cpp -o onemax.exe- Open PowerShell or CMD
- Navigate to the folder
- Run the program with:
.\onemax.exe bit run iter pop_size algo_typeExamples:
.\onemax.exe 100 30 1000 1 HC
.\onemax.exe 100 30 1000 1 SA
.\onemax.exe 100 30 1000 20 GA
.\onemax.exe 64 1 1 1 ES
.\onemax.exe 10 30 5000 0 TBYou will be prompted:
Please type tabu_size = (your input)
Please type tweak_num = (your input)
- Install gnuplot
- Open CMD or PowerShell
- Run with:
gnuplot plot_HC.plt
gnuplot plot_SA.plt
gnuplot plot_GA.plt
gnuplot plot_TB.pltOutput PNGs will appear in the working directory
onemax/
|
├── main.cpp
├── OneMax.cpp / OneMax.h
├── ExhaustiveSearch.cpp / ExhaustiveSearch.h
├── HillClimbing.cpp / HillClimbing.h
├── SimulatedAnnealing.cpp / SimulatedAnnealing.h
├── GeneticAlgo.cpp / GeneticAlgo.h
├── Tabu.cpp / Tabu.h
│
├── results/ ← output files (.txt, .png)
└── README.md ← this file
- Exhaustive Search guarantees the global optimum but is only feasible for very small problem sizes.
- Hill Climbing is fast but easily stuck in local optima.
- Simulated Annealing introduces randomness and temperature decay to escape local optima.
- Genetic Algorithm balances exploration and exploitation well, especially with larger populations.
- Tabu Search effectively avoids cycling and revisiting previous solutions using a memory structure.
- Object-oriented C++ implementation
- Unified interface for all metaheuristic algorithms
- Support for command-line parameter configuration
- Exportable performance records and convergence plots
- Compatible with gnuplot for automatic visualization
- Metaheuristic algorithm design and implementation
- Modular software architecture in C++
- Fitness evaluation and neighbor generation
- Algorithm benchmarking and convergence comparison
- Parameter sensitivity analysis
- Shell-based automation and visualization scripting




