
//  INDIVIDUAL.CC

#ifndef INDIVIDUAL_CC
#define INDIVIDUAL_CC

#include "individual.h"
// include "printChrom.cc"


Individual::Individual() {} ;


void Individual::RandomizeChrom()
{ 
   int temp;

   for (int loop = 0; loop < CHROMLENGTH; loop++)
     {
      temp = RandomGenerator.flip_coin(BIAS);
      if (temp == 1)
        MyChrom[loop] = true;
        else MyChrom[loop] = false;
     }     
};


void Individual::evaluate()
{
  int NumberOfOnes;
  FILE *inputfile, *outputfile;
  
  inputfile = fopen("chromosome","w");           // try to open the input file
  if(!inputfile) {perror("Error opening chromosome input file"); exit(1);}
  PrintChromToFile(inputfile);
  fclose(inputfile);

  int res=system("./knapsack");
                if(res==127||res==-1) {perror("Error running blackbox evaluation code"); exit(1);}  

  outputfile = fopen("fitness","r");            //  try to open output file
  if(!outputfile) {perror("Error opening fitness output file"); exit(1);}
  fscanf(outputfile,"%d\n",&NumberOfOnes);     //   read number of ones from file
  fclose(outputfile);
  fitness = NumberOfOnes;
  objective_value = fitness;
};


void Individual::decode_XYcoordinates()
         {
          x_coordinate = BinaryCodedInteger(0,CHROMLENGTH/2);
          y_coordinate = BinaryCodedInteger(CHROMLENGTH/2, CHROMLENGTH);
         }

void Individual::printfitness()
{
	cout << "\nCost    = " << objective_value
	     << "\nFitness = " << fitness << endl;
}

boolean Individual::operator == (Individual other)
                 {
                   return equal(&other);
                  };

boolean Individual::equal(Individual *other)
                 {
                   int i;
                   boolean eq = true;
                   for(i=0;i<=CHROMLENGTH;i++)
                       if(MyChrom[i] != other->MyChrom[i])
                          eq = false;
                   return eq;
                  };

boolean Individual::notequal(Individual *other)
                 {
                   if(equal(other))
                      return(false);
                      else return(true);
                  };


#endif
