Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

// Harris_Hawk.cpp : This file contains the 'main' function.

Program execution
begins and ends there.
//

//Including header files


#include <bits/stdc++.h>
//#include "helper.h"
//#include "hawk_ops.h"

using std::vector;
using std::cin;
using std::cout;
using std::endl;

//INPUT PARAMETERS TO BE ADJUSTED AT WILL


int population = 6;
int maxIter = 100; //The maximum number of iterations...
int currentIter = 0;

const int random_max = 2;

double objectiveFunction(vector<double>& factor)


{
/*

*/
// this should be edited by user
//-----------------------------------------------------------------------------
-
double F = factor.at(0);
double DC = factor.at(1);
double PO = factor.at(2);
double N = factor.at(3);
double GS = factor.at(4);
double output;

output = (-1.53 * F) + (2.76 * DC) + (0.169 * PO) - (1.92 * N) - (0.565 * GS) +


(0.00363 * F * F) + (0.222 * DC * DC) + (0.001081 * PO * PO) + (0.179 * N * N) +
(0.01285 * GS * GS) + (0.086 * F * DC) + (0.00592 * F * PO) + (0.0630 * F * N) +
(0.0080 * F * GS) - (0.0683 * DC * PO);

//-----------------------------------------------------------------------------
-

return output;
}

vector<vector<double>> randomInitializer(vector<vector<double>>& a, int population)


{
vector<vector<double>> randommatrix;

double initializedelement{};
srand((unsigned)time(NULL)); // seeding the random numbers
cout << "The Randomly initialized matrix is given as follows: \n\n";
cout << "The procedure followed in obtaining the first variable in the first
hawk of the population is shown below\n";

for (int p{ 0 }; p < population; p++)


{
vector<double> initialsolution;
double randomnumber;

for (int i{ 0 }; i < a.size(); i++)


{
randomnumber = (float)rand() / RAND_MAX;

//For the first variable in the first Hawk


if (p == 0 && i == 0) {

initializedelement = a.at(i).at(0) + randomnumber * (a.at(i).at(1)


- a.at(i).at(0));
initialsolution.push_back(initializedelement);

// Just to depict how the values were obtained...


cout << "Lower Boundary: " << a.at(i).at(0) << endl;
cout << "Upper Boundary: " << a.at(i).at(1) << endl;
cout << "Random Number: " << randomnumber << endl;
cout << "Formular for Randomly initialised value: L + R(U - L)" <<
endl;
cout << a.at(i).at(0) << " + " << randomnumber << " * " <<
a.at(i).at(1) << " - " << a.at(i).at(0) << " = " << initializedelement << endl <<
endl;
}
else {
initializedelement = a.at(i).at(0) + randomnumber * (a.at(i).at(1)
- a.at(i).at(0));
initialsolution.push_back(initializedelement);
}
}
double outputvalue = objectiveFunction(initialsolution);
// the output value is then attached to be part of the elements for that
solution
initialsolution.push_back(outputvalue);
// the solution just finalised is pushed into the matrix of the randomly
intialised solution...
randommatrix.push_back(initialsolution);
}

return randommatrix;
}

// This objective function is the function used for the particular output parameter
that is being optimized and should be edited to suit

double energy_first(double initial_energy, int maxIter) {


double E = 2 * initial_energy;

cout << "Obtaining the energy E of the prey...The required values are as
follows" << endl;
cout << "Eo = " << initial_energy << " - t = 0 (first iteration) - T (Maximum
Iteration) = " << maxIter << endl;
cout << "E = " << E << endl;

return E;
}

float getRandomFloat(int x) {
// Seed the random number generator
std::srand(static_cast<unsigned int>(x));

// Generate a random float between 0 and 1


float randomFloat = static_cast<float>(std::rand()) /
static_cast<float>(RAND_MAX);

return randomFloat;
}

int main()
{
cout << "\t\t\t\tHARRIS HAWK OPTIMIZATION ALGORITHM\n\n";
vector<vector<double>> factorBoundaries;
factorBoundaries = { {5.0, 20.0}, {4.5, 6.0}, {60.0, 90.0}, {3.0, 6.0}, {15.0,
30.0} };
vector<vector<double>> Population = randomInitializer(factorBoundaries,
population);

//Harris Hawk Specific parameters


double Eo{};
double J{};

//Printing the Randomly initialized matrix


cout << "The randomly initialized matrix is shown below (together with their
fitness values in the last column)\n";
for (auto m : Population)
{
for (auto n : m)
{
cout << n << "--";
}
cout << std::endl;
}

while (currentIter < maxIter) {

for (int i{ 0 }; i < 5; i++) {


if (i == 0 && currentIter == 0) {

double randomNumberE = getRandomFloat(currentIter); // random


number between 0 & 1 to find Eo...
double randomNumberJ = getRandomFloat(currentIter + 1); // random
number between 0 & 1 to find J...
Eo = 2 * randomNumberE - 1;
J = 2 * (1 - randomNumberJ);

//Displaying the values and the steps for the first hawk
cout << endl << endl;
cout << "Initializing the values of Eo & J for the first wolf\n";
cout << "Eo = 2rand() - 1\n";
cout << "random number (between 0 & 1) = " << randomNumberE <<
endl;
cout << "Eo = 2*" << randomNumberE << " -1 = " << Eo << endl;

cout << "J = 2(1 - rand())\n";


cout<< "random number (between 0 & 1) = " << randomNumberJ << endl;
cout << "J = 2(1 - " << randomNumberJ << " ) = " << J <<
endl<<endl;

double E = energy_first(Eo, maxIter); //obtaining initial energy of


the prey
cout << endl;
}
else {
//Excecuting the steps for the other hawks asides the first hawk
}
}
//cout << endl;

currentIter++;
}
}

You might also like