/********************************************************************************
 * Project: FUSS                                                                *
 * Module : fuss.h                                                              *
 * Author : Marcus Hutter                                                       *
 * Content: Fittness uniform selection on D-dimensional functions               *
 * Source : http://www.hutter1.de/ai/fuss.htm                                   *
 * (c) 2000 by Marcus Hutter                                                    *
 ********************************************************************************/

/******************** See fussTSP.cpp for further description *******************/

#include <float.h>   // DBL_MAX
#include <math.h>
#include <stdlib.h>  // rand()
#include <stdio.h>   // printf()

/********************************************************************************/
/*                V a r i a b l e s   &   C o n s t a n t s                     */
/********************************************************************************/

#define rrand() ((rand()+(float)rand()/(RAND_MAX+1))/(RAND_MAX+1)) /* random number in [0..1[          */
#define irand(n) ((int)((n)*rrand()))             /* random number in 0..n-1          */
#define NULL 0
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

/********************************************************************************/
/*                V a r i a b l e s   &   C o n s t a n t s                     */
/********************************************************************************/

//const int NROUNDS = 10000000;           // number of selection/mutation rounds
#define SELECTMODE  1                  // 1=Fuss, 2=Tournament, ...
const int    TOURNSIZE = 2;            // Tournament size
const double T2SSTRENGTH = 1.0;         // Tournament selection strength (0.5..1)
const double CROSSPROB = 0.5;           // crossing probability

/********************************************************************************/
/*                           S t a t i s t i c s                                */
/********************************************************************************/

/*------------------------------------------------------------------------------*
  Description: Individual class
  Remark:       
 *-MH---------------------------------------------------------------------------*/
class CStat
/*------------------------------------------------------------------------------*/
{
  public:
    double nselect;                     // total number of selections
    double nmutate;                     // total number of mutations
    double nadd;                        // total number of adds
    int    nruns;                       // number of optimization runs
  public:
    CStat();
};

/********************************************************************************/
/*                   I n d i v i d u a l   T S P   P a t h s                    */
/********************************************************************************/

const int INVMEM  = 10000000;          // maximal memory for individuals
const int DINV = 2;                     // dimension of individual

/*------------------------------------------------------------------------------*
  Description: Individual class
 *-MH---------------------------------------------------------------------------*/
class CInv
/*------------------------------------------------------------------------------*/
{
  public:
    double v[DINV];                     // array of next city pointer
    double fitness;                     // fitness(pathlength)
  public:
    CInv(const CInv &source);
    CInv(int mode=0);                   // 0=some, 1=random

    double Mutate();
    double Fitness();                   // fitness in range 0..1
};

/********************************************************************************/
/*              P o p u l a t i o n   o f   I n d i v i d u a l s               */
/********************************************************************************/

const int NINVS         = 10000; //INVMEM/sizeof(CInv);  // ma
int tninvs              = 10000; //INVMEM/sizeof(CInv);  // ma
#if (SELECTMODE==1) // Fuss
const int NFITLVL       = 5;
const int NIPF          = 1;           // number of invs per fitness
#else               // StdGATourn
const int NFITLVL       = 1;
const int NIPF          = 1;            // number of invs per fitness
#endif
/*------------------------------------------------------------------------------*
  Description: Population class
 *-MH---------------------------------------------------------------------------*/
class CPop
/*------------------------------------------------------------------------------*/
{
  public:
    CInv  invs[NINVS];                  // array of individuals
    int   ninvs;                        // actual number of individuals
    CInv  *fits[NFITLVL][NIPF];
    int   fmin,fmax;                    // minimal and maximal nonempty fit-level
    int   nipf[NFITLVL];                // number of invs per fitness level
    int   maxnipf;                      // current maximal value <=NIPF
    CInv  *bestinv;                     // best individual in population (including deleted ones!)
  public:
    CPop();
    AddInv(CInv *inv);
    CInv *FussSelect();
    CInv *TournSelect(int ts, double p);
    CInv *SelectInv(int mode);
};

CPop *pop;
CStat stat;

/*----------------------------End-of-File-fussDD.h------------------------------*/
