I'm trying to run my program but i can't find out why its giving me a segmentation fault. Please Help.
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
using std :: ifstream;
using std :: ofstream;
using std :: endl;
template<class T>
class myVector
{
public:
myVector();
int get_size() const;
int get_capacity() const;
void push_back(T);
void pop();
void resize();
void print();
T& operator[](int index);
~myVector();
private:
T *basket;
int capacity, size;
};
template<class T>
myVector<T>::myVector() :capacity(2), size(0)
{
basket = new T [capacity];
}
template<class T>
void myVector<T>::push_back(T element)
{
if(size < capacity)
{
basket[size] = element;
size++;
}
else
{
resize();
basket[size] = element;
size++;
}
}
template<class T>
T& myVector<T>::operator[](int index)
{
if(index >= size)
{
cout << "Illegal index." << endl;
exit(0);
}
return basket[index];
}
template<class T>
void myVector<T>::pop()
{
if(size != 0)
{
size--;
capacity = size + 1;
T *newBasket = new T[capacity];
for(int i = 0; i < size; i++)
{
newBasket[i] = basket[i];
}
delete [] basket;
basket = newBasket;
}
else
{
cout << "Vector is Empty." << endl;
}
}
template<class T>
myVector<T>::~myVector()
{
delete [] basket;
}
template<class T>
void myVector<T>::resize()
{
T *newBasket = new T[capacity + 2];
for(int i = 0; i < size; i++)
{
newBasket[i] = basket[i];
}
delete [] basket;
basket = newBasket;
capacity += 2;
}
template<class T>
void myVector<T>::print()
{
for(int i = 0; i < size; i++)
{
cout << basket[i] << " ";
}
}
template<class T>
int myVector<T>::get_size() const
{
return size;
}
template<class T>
int myVector<T>::get_capacity() const
{
return capacity;
}
class UserInterface
/*
The user interface class is used primarily for the menu of the program. The user
is presented with a menu and throughout the use of the program, loops through the
menu until the user decides to quit.
*/
{
public:
friend class Individual;
UserInterface();
void MainMenu();
bool get_number ( int& number );
private:
bool menuBool;
int inputNumber;
int problemChoice;
double mutation_chance;
double mutation_amount;
double mutation_rate;
int populationSize;
int choice1;
int algorithmChoice;
int choice2;
};
class Individual
{
friend class HillClimberAlgorithm;
friend class GeneticAlgorithm;
public:
Individual();
Individual (int problem);
Individual(int& Algorithm,int& Problem, double& mutationChance, double& mutationAmount);
void SetProblem(int Problem);
void SetAlgorithm(int Algorithm);
void SetParameters(double mutationChance, double mutationAmonut);
void SetParameters (double populationSize, double mutationRate, double mutationAmount);
int GetProblem() const;
int GetAlgorithm() const;
void MathProblemsFitness();
double GetFitness() const;
double GetData(int i) const;
void SetData(int i, double result);
myVector<Individual> mainVector;
private:
int problem;
int algorithm;
double mutation_chance;
double mutation_amount;
double individualFitness;
double data[30];
};
class HillClimberAlgorithm
{
public:
HillClimberAlgorithm ();
HillClimberAlgorithm(int problem, double mutation_chance, double mutation_amount);
myVector <Individual> hillIndividual;
void replace (Individual);
private:
double fitness;
vector <double> moreFit;
vector <double> leastFit;
Individual newIndividualA;
Individual newIndividualB;
};
class GeneticAlgorithm
{
public:
GeneticAlgorithm();
int BinarySelection(int populationSize);
void ShowResults();
void Optimize(int problem, int popSize, double mutRate, double mutAmount);
myVector<Individual> myvector;
private:
int firstRandom;
int secondRandom;
int parentA;
int parentB;
};
const double PI=3.14159265;
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
UserInterface newUserInterface;
newUserInterface.MainMenu();
return 0;
}
//------------------------------------------------------------------------------
Individual::Individual()
{
}
void Individual::SetProblem(int Problem)
{
problem = Problem;
}
void Individual::SetAlgorithm(int Algorithm)
{
algorithm = Algorithm;
}
void Individual::SetParameters(double mutationChance, double mutationAmount)
{
mutation_chance = mutationChance;
mutation_amount = mutationAmount;
}
int Individual::GetProblem() const
{
return problem;
}
double Individual::GetData(int i) const
{
return data[i];
}
int Individual::GetAlgorithm() const
{
return algorithm;
}
void Individual::SetData(int i, double result)
{
data[i]=result;
}
Individual::Individual(int& Algorithm, int& Problem, double& mutationChance, double& mutationAmount)
{
SetProblem(Problem);
SetAlgorithm(Algorithm);
SetParameters(mutationChance,mutationAmount);
if (problem==1)
{
for(int i = 0; i < 30; i++)
{
data[i] = static_cast<double>(rand())/ static_cast<double>( RAND_MAX) *(65.536-(-65.536) + 1) + (-65.536);
}
}
if (problem==2)
{
for(int i = 0; i < 30; i++)
{
data[i] = static_cast<double>(rand())/ static_cast<double>( RAND_MAX) *(2.048-(-2.048) + 1) + (-2.048);
}
}
if (problem==3)
{
for(int i = 0; i < 30; i++)
{
data[i] = static_cast<double>(rand())/ static_cast<double>( RAND_MAX) *(600-(-600) + 1) + (-600);
}
}
MathProblemsFitness();
}
Individual::Individual(int Problem)
{
SetProblem(Problem);
if (problem==1)
{
for(int i = 0; i < 30; i++)
{
data[i] = static_cast<double>(rand())/ static_cast<double>( RAND_MAX) *(65.536-(-65.536) + 1) + (-65.536);
}
}
if (problem==2)
{
for(int i = 0; i < 30; i++)
{
data[i] = static_cast<double>(rand())/ static_cast<double>( RAND_MAX) *(2.048-(-2.048) + 1) + (-2.048);
}
}
if (problem==3)
{
for(int i = 0; i < 30; i++)
{
data[i] = static_cast<double>(rand())/ static_cast<double>( RAND_MAX) *(600-(-600) + 1) + (-600);
}
}
MathProblemsFitness();
}
void Individual::MathProblemsFitness()
{
int upperLimit=1;
double holder=0;
double holder1 = 0;
individualFitness=0;
switch(problem)
{
case 1:
{
for(int i = 0; i < 30; i++)
{
holder = 0;
for(int k = 0; k < upperLimit; k++)
{
holder = holder+ data[k];
}
upperLimit++;
individualFitness = individualFitness+ (holder*holder);
}
}
break;
case 2:
{
for(int i = 0; i < 30; i++)
{
holder = 0;
holder =(100*(data[i+1]-(data[i]*data[i]))*(data[i+1]-(data[i]*data[i])) + (data[i] - 1)*(data[i] - 1));
individualFitness= individualFitness + holder;
}
}
case 3:
{
for(int i = 0; i < 30; i++)
{
holder = 0 ;
holder = (data[i]*data[i])/4000;
individualFitness = individualFitness + holder;
}
individualFitness= individualFitness+1;
for(int i = 0; i < 30; i++)
{
holder = 0;
holder = cos(data[i]/sqrt(i+1));
holder1= holder * holder1;
}
individualFitness = individualFitness - holder1;
}
}
}
double Individual::GetFitness() const
{
return individualFitness;
}
//------------------------------------------------------------------------------
HillClimberAlgorithm::HillClimberAlgorithm()
{
}
void HillClimberAlgorithm::replace(Individual newIndividual)
{
double holder =hillIndividual[0].GetFitness();
int index=0;
for(int i = 1; i < hillIndividual.get_size(); i++)
{
if(holder < hillIndividual[i].GetFitness())
{
holder = hillIndividual[i].GetFitness();
index = i;
}
}
for(int i = 0; i < 30; i++)
{
hillIndividual[index].SetData(i,newIndividual.GetData(i));
hillIndividual[index].MathProblemsFitness();
}
}
HillClimberAlgorithm::HillClimberAlgorithm(int problem, double mutation_chance, double mutation_amount)
{
Individual newIndividualA (problem);
fitness= newIndividualA.GetFitness();
while( fitness < .1)
{
int iterations=0;
for (int i=0; i < 30; i++)
{
Individual newIndividualB (problem);
newIndividualB.SetData(i,newIndividualB.GetData(i)+newIndividualB.GetData(i)*((static_cast<double>(rand())/ static_cast<double>( RAND_MAX) *(2-0 + 1) + 0))*mutation_amount);
double newFitness=newIndividualB.GetFitness();
if (newFitness < fitness)
{
replace(newIndividualB);
}
iterations++;
}
cout << iterations << endl;
}
}
//------------------------------------------------------------------------------
GeneticAlgorithm::GeneticAlgorithm()
{
}
int GeneticAlgorithm::BinarySelection(int populationSize)
{
firstRandom = static_cast<int>(( static_cast<double>(rand()))/ static_cast<double>( RAND_MAX) *(populationSize-0 + 1) + 0);
secondRandom = static_cast<int>(( static_cast<double>(rand()))/ static_cast<double>( RAND_MAX) *(populationSize-0 + 1) + 0);
}
void GeneticAlgorithm::Optimize(int problem, int population, double mutation_rate, double mutation_amount)
{
parentA=BinarySelection(population);
parentB=BinarySelection(population);
Individual child (problem);
myVector <Individual>mainVector;
double upperBound=0;
if(problem == 1) //set range according to problem
{
upperBound = 131.072;
}
else if(problem == 2)
{
upperBound = 4.096;
}
else
{
upperBound = 1200;
}
int i=0;
while(i < 30)//select data from each pare
{
int randomPick = 0;
int selection = rand() % 2 + 1;
if(selection == 1)
{
selection = parentA;
}
else
{
selection = parentB;
}
int mutationChance;
double number=rand() / (double)RAND_MAX;
mutationChance=number < mutation_rate;
if(mutationChance== 1)
{
double newData = mainVector[selection].GetData(i);
newData = newData + upperBound*((rand()/ ( RAND_MAX) *(2-0 + 1) + 0)-1)* mutation_amount;
child.SetData(i,newData);
if(newData > (upperBound/2))
{
child.SetData(i,(upperBound/2));
}
else if(newData < ((upperBound/2)*-1))
{
child.SetData(i,((upperBound/2)*-1));
}
}
else
{
child.SetData(i,mainVector[selection].GetData(i));
}
i++;
}
child.SetProblem(mainVector[parentA].GetProblem());
child.MathProblemsFitness();
// replace(child);
}
//------------------------------------------------------------------------------
UserInterface::UserInterface()
{
}
bool UserInterface::get_number ( int& number )
{
while ( !( cin >> number ) ) {
if ( cin.eof() )
return false;
else {
char ch;
cin.clear();
cout<<"Invalid input, please try again: ";
while (cin.get(ch) && ch != '\n' );
}
}
return true;
}
void UserInterface::MainMenu()
{
Individual newIndividual;
menuBool=true;
cout <<"\n"
<<"//////////////////////////////////////////////////////////////////////////////" <<endl
<<"/ /" <<endl
<<" Function Optimization /" << endl
<<"/ /" << endl
<<"/ /" << endl
<<"/////////////////////////////////////////////////////////////////////////////\n"
<< "\n";
while (menuBool=true)
{
cout << "1) Choose Algorithm (Hill-Climber or Genetic) \n"
<< "2) Choose Algorithm Parameters \n"
<< "3) Choose Problem (Schwefel, Rosenbrock, Griewangk) \n"
<< "4) Perform Opimization \n"
<< "5) Show All Optimization Results \n"
<< "6 Exit \n"
<< "\n";
problemChoice=0;
algorithmChoice=0;
int inputNumber=0;
populationSize=0;
mutation_amount=0;
mutation_chance=0;
mutation_rate=0;
if ( get_number ( inputNumber ) )
switch (inputNumber)
{
case 1:
cout << "Choose Algorithm \n"
<< "1 Hill Climber Algorithm \n"
<< "2 Genetic Algorithm \n";
cin >> algorithmChoice;
if (algorithmChoice==1)
cout << "Hill Climber Algorithm Chosen \n";
else if (algorithmChoice==2)
cout << "Genetic Algorithm Chose \n";
newIndividual.SetAlgorithm(algorithmChoice);
break;
case 2:
algorithmChoice=newIndividual.GetAlgorithm();
if (!algorithmChoice)
cout <<"algorithm not chose\n";
else if (algorithmChoice==1)
{
cout << "enter mutation chance: \n";
cin >> mutation_chance;
cout << "mutation chance: " << mutation_chance <<endl;
cout << "enter mutation amount [0.3-1.0] (enter 3-100): \n";
cin >> mutation_amount;
mutation_amount=mutation_amount/100;
cout <<" mutation amount: " << mutation_amount << endl;
}
else if (algorithmChoice==2)
{
cout << "enter population size [2-50] \n";
cin >> populationSize;
cout <<"population size: " <<populationSize <<endl;
cout <<"enter mutation amonut [.03-1.0](enter 3-100): \n";
cin >> mutation_amount;
mutation_amount=mutation_amount/100;
cout <<"mutation amount: " << mutation_amount << endl;
cout <<"enter mutation rate [.03-.25] (enter 3-25): \n";
cin >> mutation_rate;
mutation_rate=mutation_rate/100;
cout <<"mutation rate: " <<mutation_rate <<endl;
}
break;
case 3:
cout << "Choose Problem \n"
<< "1 Schwefel \n"
<< "2 Rosenbrock \n"
<< "3 Griewangk" << endl;
cin >>problemChoice;
if (problemChoice==1)
cout << "Schwefel Problem Chosen" << endl;
else if (problemChoice==2)
cout << "Rosenbrock Problem Chosen" << endl;
else if (problemChoice==3)
cout << "Griewangk Problem Chosen" <<endl;
newIndividual.SetProblem(problemChoice);
break;
case 4:
{
Individual individual2(algorithmChoice, problemChoice, mutation_chance,mutation_amount);
}
break;
case 5:
cout <<" Hill Climber Genetic \n"
<<"Schwefel getIterations getIterations\n"
<<"Rosebrock getIterations getIterations\n"
<<"Griegwanzk getIterations getIterations\n";
break;
case 6:
cout << "Exit";
exit(1);
break;
case 7:
{
cin >>problemChoice;
cin >>mutation_amount;
cin >> mutation_chance;
HillClimberAlgorithm newHillClimber(problemChoice,mutation_amount,mutation_chance);
}
break;
default:
cout <<"Unknow Input\n";
break;
}
menuBool=true;
}
}