Epsilon Moea (Can some one teach me out with the code here, there are a lot of errors after compiling)

#include <iostream.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define INFINITY 1e7
#define DELTA 1e-6
#define PI 4.0*atan(1.0)


#define N_of_x 30


#define MAX 2


#define MAX_CONSTRAINT 0

#define MAX_ARCHIVE_SIZE 1000

// Define the EPSILON matrix
double EPSILON[MAX] = {0.0075, 0.0075};
// any additional constants etc. are defined here

/* declaring the global variables*/

// Define the initial Population Size (to be kept constant throughout the algo.)
const int pop_size = 100;
int max_no_gen;
double pxover;
double eeta, n_distribution_m, p_mutation_real;
int archive_size = 0; //starting archive size = 0
double seed;
double infimumx[N_of_x];
double supremumx[N_of_x];

// Class definitions for an individual
class individual
{
public:
double xreal[N_of_x];
double gxm; // value of the g(x) function for the DTLZ-1 problem
double f[MAX]; // the objective functions
double cv; // cv - parameter for constraint violation
double gxv;
double box[MAX]; // box vectors for the individual
double constraint[MAX_CONSTRAINT]; // defining the constraint values
double gx(){
double gx_val;
double sum=0;
for (int i=1;i<N_of_x ;i++){
sum+= xreal[i];
}
//if ( sum ==0)
//cout<<" 0000000";
gx_val= 1+ ( (9 * sum ) / ( N_of_x -1 ) ) ;
//if (gx_val ==1 )
//cout<<" ajkfljk";
return (gx_val);
}

double function1() {
return (xreal[0]);
}

double function2(){
double val_func=0.0,gxval=0.0;
gxval=gxv;
val_func=gxval*( 1 - sqrt(xreal[0]/gxval) );
//if (val_ind == 0)
// cout<<gxval<<"\t"<<val_func<<"\n";
return val_func;
}
void init(){
gxv=gx();
f[0]=function1();
f[1]=function2();
}
};

individual population[pop_size];
individual archive[MAX_ARCHIVE_SIZE];
individual newchild, newchild1, newchild2;

#include "random.h"
#include "realmut.h"
#include "realcross.h"
#include "input.h"

void box_func (individual * ind1); // prototype of the box function

// Create the random pop
void create_random_pop ()
{
double ran;
for (int i = 0; i < pop_size; i++)
{
for (int j = 0; j < N_of_x; j++)
{
ran = randomperc ();
population[i].xreal[j] = ran * supremumx[j] + (1.0 - ran) * infimumx[j];
}
population[i].init ();
}
}

// Strict Domination check function for population members indexed by m and n
// returns 1 ( 0 ) meaning m dominates (does not dominate) n in minimization sense
// only for population members _to_be_called_in_ create_archive()
int
strict_dom_check (int m, int n)
{
int flag = 1;
for (int p = 0; p < MAX; p++)
{
if ((flag == 1) && (population[m].f[p] < population[n].f[p]))
flag = 1;
else
{
flag = 0;
break;
}
}
return (flag);
}


// Create the initial archive
// Consists of the non-dominated (strict domination) members
// of the population
void
create_archive ()
{
int flag = 0;
for (int i = 0; i < pop_size; i++)
{
flag = 0;
for (int j = 0; j < pop_size; j++)
{
if (strict_dom_check (j, i) == 1)
flag = -1;
}
if (flag == 0)
{
archive[archive_size] = population[i];
archive_size++;
}
}
}



// Prints the population members, whenever called
void
print_pop ()
{
cout << "\n Printing real values of the function values\n";
for (int i = 0; i < pop_size; i++)
{
for (int j = 0; j < MAX; j++)
cout << population[i].f[j] << "\t";
cout << "\n";
}
}


// Prints the function values of the archive members, when called
void
print_archive ()
{
cout << "\n Printing the function values of the archive members\n";
for (int i = 0; i < archive_size; i++)
{
for (int j = 0; j < MAX; j++)
cout << archive[i].f[j] << "\t";
cout << "\n";
}
}


// Prints into files the population and archive members
// Also prints the Box vectors and Decision Variables of the archive members
void
print_func_values ()
{
FILE *fpop, *farch, *fvar, *fbox;
fpop = fopen ("popltn.out", "w");
farch = fopen ("a", "w");
fvar = fopen ("ar_variables.out", "w");
fbox = fopen ("box_vectors.out", "w");
cout << "\n\n The size of the archive is " << archive_size << "\n";
cout << " The final archive is in file archive.out\n\n";
for (int i = 0; i < pop_size; i++)
{
for (int j = 0; j < MAX; j++)
fprintf (fpop, "%f\t", population[i].f[j]);
fprintf (fpop, "\n");
}
for (int i = 0; i < archive_size; i++)
{
for (int j = 0; j < MAX; j++)
{
fprintf (farch, "%f\t", archive[i].f[j]);
fprintf (fbox, "%f\t", archive[i].box[j] * EPSILON[j]);
}

for (int j = 0; j < N_of_x; j++)
fprintf (fvar, "%f\t", archive[i].xreal[j]);

fprintf (farch, "\n");
fprintf (fbox, "\n");
fprintf (fvar, "\n");
}
}


// Main function starts here
int
main (int argc, char **argv)
{

void generate_replace ();
void compete ();
int update ();

// returns 0 if not accepted and 1 if the child is accepted
int con_update ();

time_t start, end;
time (&start);
// Take the start time reading

int child_flag1 = 0, flg = 0, child_flag2 = 0;

input_param ();
// Input the necessary parameters

// cout << "\n Enter the seed : ";
// cin >> seed;
seed = (double)atof(argv[1]);
warmup_random (seed);
// set up the random no. generator

create_random_pop ();
create_archive ();

// cout << "\n Enter the No of Generations Needed : ";
// No of function evaluations = max_no_gen * 2
// cin >> max_no_gen;
max_no_gen = (int)atoi(argv[2]);

cout << "\n Starting loop........";
for (int no_gen = 0; no_gen < max_no_gen; no_gen++)
{
generate_replace ();
cout << "\n GEN = " << no_gen + 1;
cout << "\n NO of members in Archive at present :" << archive_size <<
"\n";
newchild = newchild1;
child_flag1 = con_update ();
compete ();
newchild = newchild2;
child_flag2 = con_update ();
compete ();
}
/* printing the values of the functions */
print_func_values ();
time (&end);
cout << "\n\n Total Time taken == " << difftime (end,
start) << " seconds.\n\n";


return (0);
}

// Box funtion definitons, here done for minimization case
void
box_func (individual * ind1)
{
for (int i = 0; i < MAX; i++)
ind1->box[i] = floor ((ind1->f[i] / EPSILON[i]));
}


// function to check for Box domination
int
box_dom (individual ind1, individual ind2)
{
int flag = 1;
int elag = 0;
for (int j = 0; j < MAX; j++)
{
if ((flag == 1) && (ind1.box[j] <= ind2.box[j]))
flag = 1;
else
flag = 0;

if ((ind1.box[j] < ind2.box[j]) || (elag == 1))
elag = 1;
}
if ((flag == 1) && (elag == 1))
return 1;
else
return 0;
}


//to check whether 2 individuals are in the same box or not
int
same_box_check (individual ind1, individual ind2)
{
int flag = 1;
for (int i = 0; i < MAX; i++)
{
if ((flag == 1) && (ind1.box[i] == ind2.box[i]))
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
return (flag);
}
Normally when you have a big project, you should write small portions of the code, test and fix all the errors before you continue. That way if you get an error, it's normally in the last code you wrote. Makes it much easier to fix.

If you could add code tags and format your code it would make it much easier to read.

Thx
#include <iostream.h>
Seems you use a very old compiler - Turbo C++ ?

To fix the errors we need to see the error messages in the original.
just make pi the constant. #define is a *macro* so every time you use pi you execute a pointless atan call.
@SamuelAdams thanks for your suggestion. Here is the first segment of the code and following are the errors I get.


#include <iostream.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define INFINITY 1e7
#define DELTA 1e-6
#define PI 4.0*atan(1.0)

// Number of decision variables
#define N_of_x 30

// MAX is the number of objective functions
#define MAX 2

//MAX_CONSTRAINT is the maximum number of constraints
#define MAX_CONSTRAINT 0

//MAX_ARCHIVE_SIZE is the maximum size possible (the upper bound) of the archive
#define MAX_ARCHIVE_SIZE 1000

// Define the EPSILON matrix
double EPSILON[MAX] = {0.0075, 0.0075};
// any additional constants etc. are defined here

/* declaring the global variables*/

// Define the initial Population Size (to be kept constant throughout the algo.)
const int pop_size = 100;
int max_no_gen;
double pxover;
double eeta, n_distribution_m, p_mutation_real;
int archive_size = 0; //starting archive size = 0
double seed;
double infimumx[N_of_x];
double supremumx[N_of_x];

// Class definitions for an individual
class individual
{
public:
double xreal[N_of_x];
double gxm; // value of the g(x) function for the DTLZ-1 problem
double f[MAX]; // the objective functions
double cv; // cv - parameter for constraint violation
double gxv;
double box[MAX]; // box vectors for the individual
double constraint[MAX_CONSTRAINT]; // defining the constraint values
double gx(){
double gx_val;
double sum=0;
for (int i=1;i<N_of_x ;i++){
sum+= xreal[i];
}
//if ( sum ==0)
//cout<<" 0000000";
gx_val= 1+ ( (9 * sum ) / ( N_of_x -1 ) ) ;
//if (gx_val ==1 )
//cout<<" ajkfljk";
return (gx_val);
}

double function1() {
return (xreal[0]);
}

double function2(){
double val_func=0.0,gxval=0.0;
gxval=gxv;
val_func=gxval*( 1 - sqrt(xreal[0]/gxval) );
//if (val_ind == 0)
// cout<<gxval<<"\t"<<val_func<<"\n";
return val_func;
}
void init(){
gxv=gx();
f[0]=function1();
f[1]=function2();
}
};

42[Error] syntax error before "individual"
43[Error] syntax error before '{' token
55[Error] 'for' loop initial declaration used outside C99 mode
56[Error] `xreal' undeclared (first use in this function)
56[Error] (Each undeclared identifier is reported only once
83[Error] syntax error before '}' token
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

You've been asked to do this once already.

2) From your error messages, it looks though you're trying to use a C compiler to compile C++ code, hence the compiler is calling your class definition a syntax error. Check that you're using the right tools to compile your code.

3) Why are you making all your data members public?

4) function1 and function2 are not exactly helpful names. It helps to give your methods names that sensibly indicate what they do; it will make your code more comprehensible to you and anyone else.
Last edited on
Registered users can post here. Sign in or register to post.