/*
ga2.h  Selah Lynch  July '04

Class GAProb (version2)

Improved(hopefully) Genetic Algorithm Class

Takes a predeclared problem and a population and optimizes the population
to suit the problem.
Can set different variables for percent to kill each generation, and
for percent to mutate.

is dependant on mpi.h
*/

#ifndef GA2_H
#define GA2_H

#include<fstream.h>
#include "population.h"
#include "problem.h"

class GAProb{
 public:
  GAProb(Problem& prob, Population& pop);

  GAProb(Problem& prob, int popsizearg=20);

  GAProb(GAProb& gap);
  ~GAProb();

  void RunTillDone(int checkinterval=100);
  //checks the best solution over time over the cluster at specified
  //intervals, when it hasn't improved it will stop running

  void RunFor(int howmanygens);
  //run for a specified number of generations

  void NextGeneration();
  //run for one generation

  Solution& BestSlnOverTime(); //best solution over time
  //returns a solution that is saved by the ga object

  Solution BestOverCluster(Solution& slnarg); 
  //of the Solution arguments passed, this picks the best
  //this must be called by every computer in the cluster at once
  //slnarg must be defined in all computers

  int GetGeneration(); 

  void SetVariables(int percentkillarg,int percentmutatearg);

  private:

  void WriteSolutionsFile(ostream& o);
  //writes the population of the ROOT computer into a file that can
  //be read by the visualizer

  Population *gapop;
  Problem *gaprob;
  //they are pointers because they are so big and we dont want to copy them

  int popsize, numpoints;

  Solution* bestslnovertime;//of all time on this computer

  int generation;

  int percentkill, percentmutate;

  ofstream slnout;

  static void check(bool b, char* mess);

  int myrank, sizeofcluster;
  static const int ROOT = 0;

  bool deletepop; //this is here because of the two different constructors
};

#endif
