#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "headfile.h"

#include "sharefunc.h"
#include "ESSRSort.h"
#include "ESES.h"


/*********************************************************************
 ** double transform(double x)                                      **
 ** transform x: for coding                                         **
 *********************************************************************/
double transform(double);

int main(int argc, char ** argv)
{
  void fitness(double y[], double *f, double g[]);
  void donlp2_sol(double y[], double xinput[]);
  void sol(double xhat[], FILE*);

  char* filename;
  filename 	= argv[1];

  FILE *pFile;
  pFile = fopen (filename , "wb" );

  double r_value[] = {1.0, 0.3};
  int rvalue_run, run;

  double bstep = 0.3/10.0;

  alpha_value = 0.2;

  for (rvalue_run = 0; rvalue_run < 2; rvalue_run++)
  {
	rvalue = r_value[rvalue_run];

	printf("r_value = %f\n", rvalue);

	fprintf(pFile, "-----------------------------\n");
	fprintf(pFile, "r_value = %f\n", rvalue);
	fprintf(pFile, "-----------------------------\n");

  	for (run = 0; run <= 10; run ++)
  	{
		betaf  = 0.0 + run * bstep;

		fprintf(pFile, "\n---------------------------------\n");
		printf("beta = %f\n", betaf);
  		fprintf(pFile, "beta = %f\n", betaf);
		fprintf(pFile, "-----------------------------------\n");

  		ESParameter *param;
  		ESPopulation *population;
  		ESStatistics *stats;
  		ESfcnTrsfm *trsfm;
  		unsigned int seed;
  		int es, i;
  		int constraint, dim;
  		double *ub, *lb;
  		int miu, lambda, gen;
  		double gamma, alpha, varphi;
  		int retry;
  		double pf;

  		/*********************************************************************
  		 ** change the parameters here as you want                          **
  		 ** random seed, gamma, alpha, varphi, retry number, pf,            **
  		 ** if you dont know how to set, keep default settings              **
  		 **                                                                 **
  		 ** constraint number, x dimension, miu:parent number,              **
  		 ** lambda:offspring number, generation number,                     **
  		 ** up and low bounds on x,                                         **
  		 *********************************************************************/
  		seed = shareDefSeed;
  		gamma = esDefGamma;
  		alpha = esDefAlpha;
  		varphi = esDefVarphi;
  		retry = esDefRetry;

  		pf = essrDefPf;

  		es = esDefESSlash;
  		constraint = 1;
  		dim = 7;
  		miu = 30;
  		lambda = 200;
  		gen = 800;
  		ub = NULL;
  		lb = NULL;
  		ub = ShareMallocM1d(dim);
  		lb = ShareMallocM1d(dim);
  		trsfm = (ESfcnTrsfm *)ShareMallocM1c(dim*sizeof(ESfcnTrsfm));


 		/*********************************************************************
 		 ** set the up and low bounds on x here                             **
 		 ** lb[dim] and ub[dim]                                             **
 		 *********************************************************************/
 		for(i=0; i<dim; i++)
 		{
			 lb[i] = 0.0;
			 ub[i] = 1.0;
 		}
		lb[dim-1] = -1.0;

 		for(i=0;i<dim;i++)
 		   trsfm[i] = transform;

 		/********************************************************************
 			** end of user parameter setting                                  **
			** get started                                                    **
 		********************************************************************/
 		ESInitial(seed, &param,trsfm, fitness, es,constraint, dim, ub, lb,  \
 		           miu, lambda, gen, gamma, alpha, varphi,  \
 		           retry, &population, &stats);

		printf("Initialization finished\n");

 		while(stats->curgen < param->gen)
 		{
			printf("gen = %d\n", stats->curgen);
			ESStep(population, param, stats, pf);

			printf("The %d-th gen finished\n", stats->curgen);
		}

 		ESPrintOp(stats->bestindvdl, param);

 		fprintf(pFile, "\nThe found best solution is ");
 		printf("\nThe found best solution is ");
 		for (i=0; i<dim; i++)
 		{
			fprintf(pFile, "%10.30f ", stats->bestindvdl->op[i]);
			printf("%10.30f ", stats->bestindvdl->op[i]);
 		}

 		double ttf;
 		double *ttg;
 		ttg = ShareMallocM1d(1);

 		fitness(stats->bestindvdl->op, &ttf, ttg);

 		fprintf(pFile, " with objective function value %e\n", stats->bestindvdl->f);
 		printf(" with objective function value %e\n", stats->bestindvdl->f);

		/*void sol(double xhat[], double *f)*/
		sol(stats->bestindvdl->op, pFile);

 		ESDeInitial(param, population, stats);

 		ShareFreeM1d(ub);
 		ub = NULL;
 		ShareFreeM1d(lb);
 		lb = NULL;
 		ShareFreeM1c((char*)trsfm);
 		trsfm = NULL;


 		ShareFreeM1d(ttg);
 		ttg = NULL;
	}
  }

  fclose(pFile);

  return 0;
}



/*********************************************************************
 ** double transform(double x)                                      **
 ** transform x: for coding                                         **
 *********************************************************************/
double transform(double x)
{
  double y;

  y = x;

  return y;
}


