* 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.
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include "esch.h"
-// ---------------------------------------------------------------------------
-// Cauchy random number distribution
+/****************************************************************************/
+/* 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;
limit_inf = mi - (band*0.5);
limit_sup = mi + (band*0.5);
do {
- na_unif = nlopt_urand(0,1); // ran2(0,1);
+ na_unif = nlopt_urand(0,1); /* ran2(0,1); */
cauchy_mit = t*tan((na_unif-0.5)*3.14159265358979323846) + mi;
- } while ( (cauchy_mit<limit_inf) || (cauchy_mit>limit_sup) );
-
+ } while ( (cauchy_mit<limit_inf) || (cauchy_mit>limit_sup) );
+
if (cauchy_mit < 0)
cauchy_mit = -cauchy_mit;
else
cauchy_mit = cauchy_mit + (band*0.5);
valor = cauchy_mit/band;
- valor = min+(max-min)*valor;
- return valor;
+ valor = min+(max-min)*valor;
+ return valor;
}
-// ---------------------------------------------------------------------------
+/****************************************************************************/
-// main Individual representation type
+/* main Individual representation type */
typedef struct IndividualStructure {
double * parameters;
double fitness;
-} Individual;
+} Individual;
static int CompareIndividuals(void *unused, const void *a_, const void *b_) {
- // (void) unused;
+ /* (void) unused; */
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(
+nlopt_result chevolutionarystrategy(
unsigned nparameters, /* Number of input parameters */
nlopt_func f, /* Recursive Objective Funtion Call */
void * data_f, /* Data to Objective Function */
double* x, /*in: initial guess, out: minimizer */
double* minf,
nlopt_stopping* stop, /* nlopt stop condition */
- unsigned np, /* Number of Parents */
+ unsigned np, /* Number of Parents */
unsigned no) { /* Number of Offsprings */
- // variables from nlopt
+ /* 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
+ 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
- // -------------------------------
+ /*********************************
+ * controling the population size
+ *********************************/
if (!np) np = 40;
- if (!no) no = 60;
+ if (!no) no = 60;
if ((np < 1)||(no<1)) {
nlopt_stop_msg(stop, "populations %d, %d are too small", np, no);
return NLOPT_INVALID_ARGS;
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);
+ 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
+ /* 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)
+ 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
+ /* 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
- // ------------------------------------
+ vetor[6] = 1;
+ vetor[7] = 0; /* ignored */
+ /**************************************
+ * Initializing parents population
+ **************************************/
for (id=0; id < np; id++) {
- esparents[id].parameters =
+ 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[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
- // ------------------------------------
+ /**************************************
+ * Initializing offsprings population
+ **************************************/
for (id=0; id < no; id++) {
- esoffsprings[id].parameters =
+ 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[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 =
+ /**************************************
+ * 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,
+ memcpy(x, esparents[id].parameters,
nparameters * sizeof(double));
}
if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
if (ret != NLOPT_SUCCESS) goto done;
}
- // ------------------------------------
- // Main Loop - Generations
- // ------------------------------------
+ /**************************************
+ * Main Loop - Generations
+ **************************************/
while (1) {
- // ------------------------------------
- // Crossover
- // ------------------------------------
+ /**************************************
+ * 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];
+ esoffsprings[id].parameters[item]
+ = esparents[parent1].parameters[item];
for (item=crosspoint; item < nparameters; item++)
- esoffsprings[id].parameters[item]
- = esparents[parent2].parameters[item];
+ esoffsprings[id].parameters[item]
+ = esparents[parent2].parameters[item];
}
- // ------------------------------------
- // Gaussian Mutation
- // ------------------------------------
+ /**************************************
+ * 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[1] = lb[paramoffmutation];
vetor[2] = ub[paramoffmutation];
vetor[7] = vetor[7]+contmutation;
- esoffsprings[idoffmutation].parameters[paramoffmutation]
+ 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);
+ /**************************************
+ * 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,
+ memcpy(x, esoffsprings[id].parameters,
nparameters * sizeof(double));
}
if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
- else if (*minf < stop->minf_max)
+ 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
+ /**************************************
+ * 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
+ /* Sorting */
nlopt_qsort_r(estotal, no+np, sizeof(Individual), NULL,
CompareIndividuals);
- // copy after sorting:
+ /* 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
-
+ } /* 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);
-/*
+/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.
- Before using, initialize the state by using init_genrand(seed)
- or init_by_array(init_key, key_length).
+ Before using, initialize the state by using nlopt_init_genrand(seed)
+ or nlopt_init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
- All rights reserved.
+ All rights reserved.
Modified 2007 by Steven G. Johnson for use with NLopt (to avoid
namespace pollution, use uint32_t instead of unsigned long,
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- 3. The names of its contributors may not be used to endorse or promote
- products derived from this software without specific prior written
+ 3. The names of its contributors may not be used to endorse or promote
+ products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# endif
#endif
-/* Period parameters */
+/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
{
mt[0]= s & 0xffffffffUL;
for (mti=1; mti<N; mti++) {
- mt[mti] =
- (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
+ mt[mti] =
+ (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */
mti = 0;
}
-
+
y = mt[mti++];
/* Tempering */
if (i>=N) { mt[0] = mt[N-1]; i=1; }
}
- mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
+ mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
}
/* generates a random number on [0,0x7fffffff]-interval */
/* generates a random number on [0,1]-real-interval */
static double nlopt_genrand_real1(void)
{
- return nlopt_genrand_int32()*(1.0/4294967295.0);
- /* divided by 2^32-1 */
+ return nlopt_genrand_int32()*(1.0/4294967295.0);
+ /* divided by 2^32-1 */
}
/* generates a random number on [0,1)-real-interval */
static double nlopt_genrand_real2(void)
{
- return nlopt_genrand_int32()*(1.0/4294967296.0);
+ return nlopt_genrand_int32()*(1.0/4294967296.0);
/* divided by 2^32 */
}
/* generates a random number on (0,1)-real-interval */
static double nlopt_genrand_real3(void)
{
- return (((double)nlopt_genrand_int32()) + 0.5)*(1.0/4294967296.0);
+ return (((double)nlopt_genrand_int32()) + 0.5)*(1.0/4294967296.0);
/* divided by 2^32 */
}
#endif
/* generates a random number on [0,1) with 53-bit resolution*/
-static double nlopt_genrand_res53(void)
-{
- uint32_t a=nlopt_genrand_int32()>>5, b=nlopt_genrand_int32()>>6;
- return(a*67108864.0+b)*(1.0/9007199254740992.0);
-}
+static double nlopt_genrand_res53(void)
+{
+ uint32_t a=nlopt_genrand_int32()>>5, b=nlopt_genrand_int32()>>6;
+ return(a*67108864.0+b)*(1.0/9007199254740992.0);
+}
/* These real versions are due to Isaku Wada, 2002/01/09 added */
/* generate uniform random number in [a,b) with 53-bit resolution,
added by SGJ */
double nlopt_nrand(double mean, double stddev)
{
- // Box-Muller algorithm to generate Gaussian from uniform
- // see Knuth vol II algorithm P, sec. 3.4.1
+ /* Box-Muller algorithm to generate Gaussian from uniform
+ see Knuth vol II algorithm P, sec. 3.4.1 */
double v1, v2, s;
do {
v1 = nlopt_urand(-1, 1);