esch.c 10.0 KB
Newer Older
S
stevenj 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2008-2013 Carlos Henrique da Silva Santos
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 */

#include <stdlib.h>
24
#include <string.h>
S
stevenj 已提交
25 26 27 28 29 30 31 32 33 34
#include "esch.h"

// ---------------------------------------------------------------------------
// Cauchy random number distribution
static double randcauchy(const double params[7]) {
     /* double min, double max, double mi, double t, double band */
     double na_unif, cauchy_mit, limit_inf, limit_sup;
     double valor;
     double min = params[1], max = params[2], mi = params[3],
	  t = params[4], band = params[5];
S
fix #12  
Steven G. Johnson 已提交
35 36
     limit_inf = mi - (band*0.5);
     limit_sup = mi + (band*0.5);
S
stevenj 已提交
37 38
     do {
	  na_unif = nlopt_urand(0,1); // ran2(0,1);
S
fix #12  
Steven G. Johnson 已提交
39
	  cauchy_mit = t*tan((na_unif-0.5)*3.14159265358979323846) + mi;
S
stevenj 已提交
40 41 42 43 44
     } while ( (cauchy_mit<limit_inf) || (cauchy_mit>limit_sup) );   
     
     if (cauchy_mit < 0)
	  cauchy_mit = -cauchy_mit;
     else
S
fix #12  
Steven G. Johnson 已提交
45 46
	  cauchy_mit = cauchy_mit + (band*0.5);
     valor  = cauchy_mit/band;
S
stevenj 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59
     valor = min+(max-min)*valor;   
     return valor;  
}

// ---------------------------------------------------------------------------

// main Individual representation type
typedef struct IndividualStructure {
     double * parameters;
     double fitness;
} Individual; 

static int CompareIndividuals(void *unused, const void *a_, const void *b_) {
S
fix #12  
Steven G. Johnson 已提交
60
     // (void) unused;
S
stevenj 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
     const Individual *a = (const Individual *) a_;
     const Individual *b = (const Individual *) b_;
     return a->fitness < b->fitness ? -1 : (a->fitness > b->fitness ? +1 : 0);
}

nlopt_result chevolutionarystrategy( 
     unsigned nparameters, /* Number of input parameters */
     nlopt_func f,	/* Recursive Objective Funtion Call */
     void * data_f,	/* Data to Objective Function */
     const double* lb,			/* Lower bound values */
     const double* ub,			/* Upper bound values */
     double* x,				/*in: initial guess, out: minimizer */
     double* minf,
     nlopt_stopping* stop, 		/* nlopt stop condition */
     unsigned np, 			/* Number of Parents */ 
     unsigned no) { 			/* Number of Offsprings */

     // variables from nlopt
     nlopt_result ret = NLOPT_SUCCESS;
     double vetor[8];
     unsigned  i, id, item;
     int  parent1, parent2;
     unsigned crosspoint;  // crossover parameteres
     int  contmutation, totalmutation;	// mutation parameters
     int  idoffmutation, paramoffmutation;	// mutation parameters
     Individual * esparents;			// Parents population
     Individual * esoffsprings;		// Offsprings population
     Individual * estotal;// copy containing Parents and Offsprings pops
     /* It is interesting to maintain the parents and offsprings
      * populations stablished and sorted; when the final iterations
      * is achieved, they are ranked and updated. */

     // -------------------------------
     // controling the population size
     // -------------------------------
     if (!np) np = 40;
     if (!no) no = 60;     
98 99 100 101
     if ((np < 1)||(no<1)) {
         nlopt_stop_msg(stop, "populations %d, %d are too small", np, no);
         return NLOPT_INVALID_ARGS;
     }
S
stevenj 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
     esparents    = (Individual*) malloc(sizeof(Individual) * np);
     esoffsprings = (Individual*) malloc(sizeof(Individual) * no);
     estotal 	 = (Individual*) malloc(sizeof(Individual) * (np+no));
     if ((!esparents)||(!esoffsprings)||(!estotal)) { 
	  free(esparents); free(esoffsprings); free(estotal); 
	  return NLOPT_OUT_OF_MEMORY;
     }
     for (id=0; id < np; id++) esparents[id].parameters = NULL;
     for (id=0; id < no; id++) esoffsprings[id].parameters = NULL;
     // From here the population is initialized 
     /* we don't handle unbounded search regions;
	this check is unnecessary since it is performed in nlopt_optimize.
	for (j = 0; j < nparameters; ++j) 
   	  if (nlopt_isinf(low[j]) || nlopt_isinf(up[j]))
	    return NLOPT_INVALID_ARGS;
     */
     // main vector of parameters to randcauchy
     vetor[0] = 4; // ignored
     vetor[3] = 0;
     vetor[4] = 1;
     vetor[5] = 10;
     vetor[6] = 1; 	
     vetor[7] = 0; // ignored
     // ------------------------------------
     // Initializing parents population
     // ------------------------------------
     for (id=0; id < np; id++) {
	  esparents[id].parameters = 
	       (double*) malloc(sizeof(double) * nparameters);
	  if (!esparents[id].parameters) {
	       ret = NLOPT_OUT_OF_MEMORY;
	       goto done;
	  }
	  for (item=0; item<nparameters; item++) {
	       vetor[1] = lb[item];	
	       vetor[2] = ub[item];
	       vetor[7]=vetor[7]+1;
	       esparents[id].parameters[item] = randcauchy(vetor);
	  }
     }
     memcpy(esparents[0].parameters, x, nparameters * sizeof(double));

     // ------------------------------------
     // Initializing offsprings population
     // ------------------------------------
     for (id=0; id < no; id++) {
	  esoffsprings[id].parameters = 
	       (double*) malloc(sizeof(double) * nparameters);
	  if (!esoffsprings[id].parameters) {
	       ret = NLOPT_OUT_OF_MEMORY;
	       goto done;
	  }
	  for (item=0; item<nparameters; item++) {
	       vetor[1] = lb[item];	
	       vetor[2] = ub[item];
	       vetor[7]=vetor[7]+1;
	       esoffsprings[id].parameters[item] = randcauchy(vetor);
	  }
     }
     // ------------------------------------
     // Parents fitness evaluation  
     // ------------------------------------
     for (id=0; id < np; id++) { 
	  esparents[id].fitness = 
	       f(nparameters, esparents[id].parameters, NULL, data_f);
	  estotal[id].fitness = esparents[id].fitness;
	  stop->nevals++;
	  if (*minf > esparents[id].fitness) {
	       *minf = esparents[id].fitness;
	       memcpy(x, esparents[id].parameters, 
		      nparameters * sizeof(double));
	  }
	  if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
	  else if (*minf < stop->minf_max) ret = NLOPT_MINF_MAX_REACHED;
	  else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
	  else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
	  if (ret != NLOPT_SUCCESS) goto done;
     }
     // ------------------------------------
     // Main Loop - Generations
     // ------------------------------------
     while (1) {
	  // ------------------------------------
	  // Crossover 
	  // ------------------------------------
	  for (id=0; id < no; id++)
	  {
	       parent1  = nlopt_iurand((int) np);
	       parent2  = nlopt_iurand((int) np);
	       crosspoint = (unsigned) nlopt_iurand((int) nparameters);
	       for (item=0; item < crosspoint; item++)
		    esoffsprings[id].parameters[item] 
			 = esparents[parent1].parameters[item]; 
	       for (item=crosspoint; item < nparameters; item++)
		    esoffsprings[id].parameters[item] 
			 = esparents[parent2].parameters[item]; 
	  }
	  // ------------------------------------
	  // Gaussian Mutation 
	  // ------------------------------------
	  totalmutation = (int) ((no * nparameters) / 10);
	  if (totalmutation < 1) totalmutation = 1;
	  for (contmutation=0; contmutation < totalmutation;
	       contmutation++) {
	       idoffmutation = nlopt_iurand((int) no);
	       paramoffmutation = nlopt_iurand((int) nparameters);
	       vetor[1] = lb[paramoffmutation];	
	       vetor[2] = ub[paramoffmutation];
	       vetor[7] = vetor[7]+contmutation;
	       esoffsprings[idoffmutation].parameters[paramoffmutation] 
		    = randcauchy(vetor);
	  }
	  // ------------------------------------
	  // Offsprings fitness evaluation 
	  // ------------------------------------
	  for (id=0; id < no; id++){ 
	       //esoffsprings[id].fitness = (double)fitness(esoffsprings[id].parameters, nparameters,fittype);
	       esoffsprings[id].fitness = f(nparameters, esoffsprings[id].parameters, NULL, data_f);
	       estotal[id+np].fitness = esoffsprings[id].fitness;
	       stop->nevals++;
	       if (*minf > esoffsprings[id].fitness) {
		    *minf = esoffsprings[id].fitness;
		    memcpy(x, esoffsprings[id].parameters, 
			   nparameters * sizeof(double));
	       }
	       if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
	       else if (*minf < stop->minf_max) 
		    ret = NLOPT_MINF_MAX_REACHED;
	       else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
	       else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
	       if (ret != NLOPT_SUCCESS) goto done;
	  }
	  // ------------------------------------
	  // Individual selection
	  // ------------------------------------
	  // all the individuals are copied to one vector to easily identify best solutions		
	  for (i=0; i < np; i++)
	       estotal[i] = esparents[i];
	  for (i=0; i < no; i++)
	       estotal[np+i] = esoffsprings[i];
	  // Sorting
	  nlopt_qsort_r(estotal, no+np, sizeof(Individual), NULL,
			CompareIndividuals);
	  // copy after sorting:
	  for (i=0; i < no+np; i++) {
	       if (i<np)
		    esparents[i] = estotal[i];
	       else
		    esoffsprings[i-np] = estotal[i];
	  }
     } // generations loop
     
done:
     for (id=0; id < np; id++) free(esparents[id].parameters);
     for (id=0; id < no; id++) free(esoffsprings[id].parameters);
     
     if (esparents) 	free(esparents);
     if (esoffsprings) 	free(esoffsprings);
     if (estotal) 		free(estotal);
     return ret;
}