Blog Entry © Monday, September 8, 2025, by James Pate Williams, Jr., Comparison of Two Applications to Find the Minima of Classical Objective Functions

#pragma once
#include "pch.h"

class Functions
{

public:

    // Ackley's function

    static double lbx1[3];
    static double ubx1[3];
    static double fBest1;
    static double xBest1[3];

    static double f1(
        int n, std::vector<double> z);

    // Beale's function

    static double lbx2[3];
    static double ubx2[3];
    static double fBest2;
    static double xBest2[3];

    static double f2(
        int n, std::vector<double> z);
    
    // Booth's function

    static double lbx3[3];
    static double ubx3[3];
    static double fBest3;
    static double xBest3[3];

    static double f3(
        int n, std::vector<double> z);

    // Rosenbrock function

    static double lbx4[3];
    static double ubx4[3];
    static double fBest4;
    static double xBest4[3];

    static double f4(
        int n, std::vector<double> z);

    // Holder table function

    static double lbx5[3];
    static double ubx5[3];
    static double fBest5;
    static double xBest5[3];

    static double f5(
        int n, std::vector<double> z);

    // McCormick function

    static double lbx6[3];
    static double ubx6[3];
    static double fBest6;
    static double xBest6[3];

    static double f6(
        int n, std::vector<double> z);

    static double rosenbrock(
        int n, std::vector<double> x,
        std::vector<double>& g);

    static double mccormick(
        int n, std::vector<double> z,
        std::vector<double>& g);
};

#include "pch.h"
#include "Functions.h"

// Ackley's function

double Functions::lbx1[3] = { 0, -5, -5 };
double Functions::ubx1[3] = { 0, +5, +5 };
double Functions::fBest1 = 0.0;
double Functions::xBest1[3] = { 0, 0.0, 0.0 };

double Functions::f1(int n, std::vector<double> z)
{
	double e = exp(1.0), pi = 4.0 * atan(1.0);
	double x = z[1], y = z[2], pi2 = 2.0 * pi;

	return -20 * exp(-0.2 * sqrt(0.5 * (x * x + y * y))) -
		exp(0.5 * (cos(pi2 * x) + cos(pi2 * y))) + e + 20;
}

// Beale's function

double Functions::lbx2[3] = { 0, -4.5, -4.5 };
double Functions::ubx2[3] = { 0, +4.5, +4.5 };
double Functions::fBest2 = 0.0;
double Functions::xBest2[3] = { 0, 3.0, 2.5 };

double Functions::f2(int n, std::vector<double> z)
{
	double x = z[1], y = z[2];

	return pow(1.5 - x + x * y, 2) + pow(2.25 - x + x * y * y, 2) +
		pow(2.625 - x + x * y * y * y, 2);
}

// Booth's function

double Functions::lbx3[3] = {0, -10, -10};
double Functions::ubx3[3] = {0, +10, +10};
double Functions::fBest3 = 0.0;
double Functions::xBest3[3] = {0, 1.0, 3.0};

double Functions::f3(int n, std::vector<double> z)
{
	double x = z[1], y = z[2];

	return pow(x + 2 * y - 7, 2) + pow(2 * x + y - 5, 2);
}

// Rosenbrock function

double Functions::lbx4[3] = {0, -100.0, -100.0};
double Functions::ubx4[3] = {0, +100.0, +100.0};
double Functions::fBest4 = 0.0;
double Functions::xBest4[3] = {0, 1.0, 1.0};

double Functions::f4(int n, std::vector<double> x)
{
	double temp = x[2] - x[1] * x[1];

	return temp * temp * 100.0 + (1.0 - x[1]) * (1.0 - x[1]);
}

// Holder table function

double Functions::lbx5[3] = {0, -10.0, -10.0};
double Functions::ubx5[3] = {0, +10.0, +10.0};
double Functions::fBest5 = -19.2085;
double Functions::xBest5[3] = {0, 8.05502, 9.66459};

double Functions::f5(int n, std::vector<double> z)
{
	double pi = 4.0 * atan(1.0);
	double x = z[1], y = z[2];

	return -fabs(sin(x) * cos(y) *
		exp(fabs(1.0 - sqrt(x * x + y * y) / pi)));
}

// McCormick function

double Functions::lbx6[3] = {0, -1.5, -3};
double Functions::ubx6[3] = {0, +4, +4};
double Functions::fBest6 = -1.9133;
double Functions::xBest6[3] = {0, -0.54719, -1.54719};

double Functions::f6(int n, std::vector<double> z)
{
	double x = z[1], y = z[2];

	return sin(x + y) + pow(x - y, 2) - 1.5 * x + 2.5 * y + 1;
}

double Functions::rosenbrock(
	int n, std::vector<double> x,
	std::vector<double>& g)
{
	double temp;

	temp = x[2] - x[1] * x[1];
	g[1] = (-temp * 400.0 + 2.0) * x[1] - 2.0;
	g[2] = temp * 200.0;
	return temp * temp * 100.0 + (1.0 - x[1]) * (1.0 - x[1]);
}

double Functions::mccormick(
	int n, std::vector<double> z,
	std::vector<double>& g)
{
	double x = z[1], y = z[2];
	double c = cos(x + y);
	double p = 2.0 * (x - y);

	g[1] = c + p - 1.5;
	g[2] = c - p + 2.5;

	return sin(x + y) + pow(x - y, 2) - 1.5 * x + 2.5 * y + 1;
}

The PRAXIS and FLEMIN C source code can be found in the handbook “A Numerical Library in C for Scientists and Engineers” (c) 1996 by H. T. Lau, Ph.D. I translated the C code to C++ using Standard Template Library vectors.