logo NEO

 

xxGA:New Problems

Example New Problems Self-Adaptiveness
Compiling Configuration Distributed Version
Functs.hpp Functs.cpp Download Both Files



Including New Problems

All the functions and information of the problem must be implemented in the files 'functs.hpp' and 'functs.cpp'. Once these files are modified, we can compile them using 'make functs'.

For example, we explain in the two following paragraphs how ONEMAX and MAXSAT functions can be added to xxGA library.

File 'functs.hpp'

/******************************
.... function declarations ....
******************************/

// ONEMAX problem. Finds a string with maximum number of ones
// Number of variables = 'n'. Strlen is 'n'
// Maximun fitness ===> 'n'

// CLASS DEFINITION

class ONEMAX : public Problem
{
public:
double Evaluate (double [], unsigned long);
};

// Satisfiability problem. Finds an input satisfying as maximum number of clauses as possible
// Number of variables = 'n'. Strlen is 'n'
// Maximun fitness ===> number of clauses

// CLASS DEFINITION

class MAXSAT : public Problem
{
public:
double Evaluate (double [], unsigned long);
};

// End of file 'functs.hpp'

File 'functs.cpp'

/*********************************
.... function implementations ....
*********************************/

// ONEMAX Problem
double ONEMAX::Evaluate (double individual[], unsigned long len)
{
register int index;
double fitness = 0.0;
fitness_counter++;

for (index=0; index<len; index++) {
     fitness += individual[index]; }

return fitness;
}

// MAXSAT Problem
#define MAXSAT_DATA_FILENAME "./maxsat1.dat" // "./maxsat2.dat"

double MAXSAT::Evaluate(double x[], unsigned long Length)
{
static int Flg = 1, VblesNum,ClausNum,ClausDim;
static int **Clauses;
FILE *DatFil;
int i, j,c1,c2,c3;
double fitness=0.0;
fitness_counter++;

// Read the data from a configuration file - first time only
if (Flg){
     if ((DatFil = fopen(MAXSAT_DATA_FILENAME, "r")) == NULL) {
          printf("%s/MAXSAT: Couldn't open datafile (%s)\n", _GA, MAXSAT_DATA_FILENAME);
          exit(-1);
     }

     fscanf(DatFil, "%d", &VblesNum); // READ THE NUMBER OF VARIABLES
     if (VblesNum != Length) {
          printf("%s/MAXSAT: Wrong data dimension (%d, %d)\n", _GA, VblesNum, Length);
          exit(-1);
     }
     fscanf(DatFil, "%d", &ClausNum); // READ NUMBER OF CLAUSES
     fscanf(DatFil, "%d", &ClausDim); // READ THE LENGTH OF THE CLAUSES
     if ((Clauses = (int **) calloc((unsigned) ClausNum, sizeof(int *))) == NULL) {
          printf("%s/MAXSAT: Calloc failed\n", _GA);
          exit(-1);
     }
     for (i = 0; i < ClausNum; i++) {
          if ((Clauses[i] = (int *) calloc((unsigned) VblesNum, sizeof(int))) == NULL) {
               printf("%s/MAXSAT: Calloc failed\n", _GA);
               exit(-1);
          }
     }
     for (i = 0; i < ClausNum; i++) {
          fscanf(DatFil, "%d", &j); // First variable of the clause
          Clauses[i][0] = j;
          fscanf(DatFil, "%d", &j); // Second variable of the clause
          Clauses[i][1] = j;
     fscanf(DatFil, "%d", &j); // Third variable of the clause
          Clauses[i][2] = j;
          fscanf(DatFil, "%d", &j); // The file has a zero after each clause
     }

     fclose(DatFil);
     Flg = 0;
}


for( i = 0; i < ClausNum ; i++ ) {
     if (Clauses[i][0] > 0) c1 = x[abs(Clauses[i][0])-1];
     else c1 = 1 - x[abs(Clauses[i][0])-1];
     if (Clauses[i][1] > 0) c2 = x[abs(Clauses[i][1])-1];
     else c2 = 1 - x[abs(Clauses[i][1])-1];
     if (Clauses[i][2] > 0) c3 = x[abs(Clauses[i][2])-1];
     else c3 = 1 - x[abs(Clauses[i][2])-1];
     if ((c1+c2+c3)>0) fitness += 1.0;
     }
return fitness;
}

// End of file 'functs.cpp'

Our Software

orangebox Mallba

orangebox DLOPT

orangebox epiGA

orangebox epiGApy

orangebox ssGA

orangebox JGDS

orangebox xxGA

orangebox JCell

orangebox MHTB

orangebox DEME

orangebox JMetal

orangebox More...

orangebox Go Back



J. Cabello Galisteo 2008