// Minima.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
// Copyright (c) Sunday, September 20, 2026
// by James Pate Williams, Jr., BA, BS, MSwE, PhD
// Evolutionary hill climber (c) 1999 by Pate
#include <iostream>
#include <time.h>
#include "Algorithms.h"
int main()
{
while (true)
{
double minimum = 0.0, x0 = 0.0, y0 = 0.0;
double f1 = 0.0, x = 0.0, y = 0.0;
int evaluations = 0, maxIterations = 10000;
int generation = 0, population = 0, option = 0;
unsigned int seed = 1;
std::cout << "== Menu == \r\n";
std::cout << "1 Rastrigin Function\r\n";
std::cout << "2 Ackley Function\r\n";
std::cout << "3 Sphere Function\r\n";
std::cout << "4 Rosenbrock Function\r\n";
std::cout << "5 Beale Function\r\n";
std::cout << "6 Exit\r\n";
std::cout << "Option (1 - 6): ";
std::cin >> option;
if (option == 6)
break;
std::cout << "population = ";
std::cin >> population;
std::cout << "generation = ";
std::cin >> generation;
std::cout << "seed = ";
std::cin >> seed;
srand(seed);
x0 = y0 = 10.0;
clock_t clock0 = clock();
if (option == 1)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Rastrigin,
x, y, minimum,
evaluations,
seed);
}
else if (option == 2)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Ackley,
x, y, minimum,
evaluations,
seed);
}
else if (option == 3)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Sphere,
x, y, minimum,
evaluations,
seed);
}
else if (option == 4)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Rosenbrock,
x, y, minimum,
evaluations,
seed);
}
else if (option == 5)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Beale,
x, y, minimum,
evaluations,
seed);
}
clock_t clock1 = clock();
double runtime = (double)(clock1 - clock0) /
CLOCKS_PER_SEC;
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
std::cout << "f = " << minimum << std::endl;
std::cout << "t = " << runtime << " seconds";
std::cout << std::endl;
}
return 0;
}
#pragma once
class Algorithms
{
public:
static double Rastrigin(double x, double y);
static double Ackley(double x, double y);
static double Sphere(double x, double y);
static double Rosenbrock(double x, double y);
static double Beale(double x, double y);
static void HillClimber(
int population,
int generation,
double(*f)(double, double),
double& x, double& y,
double& minimum,
int& evaluations,
unsigned int seed);
};
#include "Algorithms.h"
#include <vector>
double Algorithms::Rastrigin(double x, double y)
{
double A = 10.0, pi = 4.0 * atan(1.0);
return
2.0 * A +
x * x - A * cos(2.0 * pi * x) +
y * y - A * cos(2.0 * pi * y);
}
double Algorithms::Ackley(double x, double y)
{
double pi = 4.0 * atan(1.0);
double xy = sqrt(0.5 * (x * x + y * y));
double cx = cos(2.0 * pi * x);
double cy = cos(2.0 * pi * y);
double expf1 = -20.0 * exp(-0.2 * xy);
double expf2 = -exp(0.5 * (cx + cy));
return expf1 + expf2 + exp(1.0) + 20.0;
}
double Algorithms::Sphere(double x, double y)
{
return x * x + y * y;
}
double Algorithms::Rosenbrock(double x, double y)
{
return 100.0 * pow(y - x * x, 2.0) + pow(1.0 - x, 2.0);
}
double Algorithms::Beale(double x, double y)
{
return
pow(1.500 - x + x * y, 2.0) +
pow(2.250 - x + x * y * y, 2.0) +
pow(2.625 - x + x * y * y * y, 2.0);
}
void Algorithms::HillClimber(
int population,
int generation,
double(*f)(double, double),
double& x, double& y,
double& minimum,
int& evaluations,
unsigned int seed)
{
srand(seed);
std::vector<double> fitness(population);
std::vector<std::vector<double>> genes(
population, std::vector<double>(2));
for (int i = 0; i < population; i++)
{
genes[i][0] = (10.0 * rand()) / RAND_MAX;
genes[i][1] = (10.0 * rand()) / RAND_MAX;
fitness[i] = f(genes[i][0], genes[i][1]);
}
for (int i = 0; i < generation; i++)
{
int parent1 = rand() % population;
int parent2 = rand() % population;
int parent0 = parent1 > parent2 ? parent1 : parent2;
int sign = rand() % 2 == 0 ? -1 : +1;
double childGeneX = (10.0 * rand()) / RAND_MAX;
double childGeneY = (10.0 * rand()) / RAND_MAX;
double childFitness = f(childGeneX, childGeneY);
double maxFitness = fitness[0];
for (int i = 1; i < population; i++)
if (fitness[i] > maxFitness)
maxFitness = fitness[i];
std::vector<int> maxIndex;
for (int i = 0; i < population; i++)
if (fitness[i] == maxFitness)
maxIndex.push_back(i);
int replace = maxIndex[rand() % maxIndex.size()];
fitness[replace] = childFitness;
genes[replace][0] = childGeneX;
genes[replace][1] = childGeneY;
}
evaluations = generation + population;
double minFitness = fitness[0];
for (int i = 1; i < population; i++)
if (fitness[i] < minFitness)
minFitness = fitness[i];
std::vector<int> minIndex;
for (int i = 0; i < population; i++)
if (fitness[i] == minFitness)
minIndex.push_back(i);
int replace = minIndex[rand() % minIndex.size()];
minimum = fitness[replace];
x = genes[replace][0];
y = genes[replace][1];
}
Author: jamespatewilliamsjr
My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.
View all posts by jamespatewilliamsjr