From 9845ff557f1be7abb27e39dbbf85c4f07487372a Mon Sep 17 00:00:00 2001 From: Amro Date: Tue, 13 Sep 2016 20:42:16 +0300 Subject: [PATCH] minor edits - use C-style comments /* .. */ in .c files - trim trailing spaces and other minor style edits --- api/options.c | 2 +- esch/esch.c | 166 +++++++++++++++++++++---------------------- slsqp/slsqp.c | 2 +- stogo/stogo_config.h | 17 +++-- test/box.c | 8 +-- test/testfuncs.c | 6 +- util/mt19937ar.c | 44 ++++++------ 7 files changed, 122 insertions(+), 123 deletions(-) diff --git a/api/options.c b/api/options.c index 626a38b..ba2a1b8 100644 --- a/api/options.c +++ b/api/options.c @@ -211,7 +211,7 @@ nlopt_opt NLOPT_STDCALL nlopt_copy(const nlopt_opt opt) return nopt; oom: - nopt->munge_on_destroy = NULL; // better to leak mem than crash + nopt->munge_on_destroy = NULL; /* better to leak mem than crash */ nlopt_destroy(nopt); return NULL; } diff --git a/esch/esch.c b/esch/esch.c index 8a21cb9..6fd5ecb 100644 --- a/esch/esch.c +++ b/esch/esch.c @@ -7,25 +7,25 @@ * 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 #include #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; @@ -35,35 +35,35 @@ static double randcauchy(const double params[7]) { 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_mitlimit_sup) ); - + } while ( (cauchy_mitlimit_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 */ @@ -72,29 +72,29 @@ nlopt_result chevolutionarystrategy( 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; @@ -102,38 +102,38 @@ nlopt_result chevolutionarystrategy( 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; itemnevals++; 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; @@ -177,84 +177,84 @@ nlopt_result chevolutionarystrategy( 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 #include - -#include +#include "nlopt.h" /****************************************************************************/ /* test function from M. J. Box, "A new method of constrained optimization @@ -130,11 +129,12 @@ static const double box_lb[5] = {0,1.2,20,9,6.5}; static const double box_ub[5] = {HUGE_VAL, 2.4, 60, 9.3, 7.0}; static const double box_xmin[5] = {4.53743, 2.4, 60, 9.3, 7.0}; -int main(void) { +int main(void) +{ int cdata[6] = {-1,1,-2,2,-3,3}; double x[5] = {2.52, 2, 37.5, 9.25, 6.8}; double minf; - + nlopt_minimize_constrained(NLOPT_LN_COBYLA, 5, box, NULL, 6, box_constraint, cdata, sizeof(int), box_lb, box_ub, x, &minf, diff --git a/test/testfuncs.c b/test/testfuncs.c index 6c89ee1..74237a2 100644 --- a/test/testfuncs.c +++ b/test/testfuncs.c @@ -147,7 +147,7 @@ static double goldsteinprice_f(unsigned n, const double *x, double *grad, void * b1 = 2*x0-3*x1; b12 = sqr(b1); b2 = 18 - 32*x0 + 12*x0*x0 + 48*x1 - 36*x0*x1 + 27*x1*x1; if (grad) { - grad[0] = (1 + a12 * a2) * (2 * b1 * 2 * b2 + grad[0] = (1 + a12 * a2) * (2 * b1 * 2 * b2 + b12 * (-32 + 24*x0 - 36*x1)) + (2 * a1 * a2 + a12 * (-14 + 6*x0 + 6*x1)) * (30 + b12 * b2); grad[1] = (1 + a12 * a2) * (2 * b1 * (-3) * b2 @@ -180,7 +180,7 @@ static double shekel_f(unsigned n, const double *x, double *grad, void *data) if (grad) for (i = 0; i < n; ++i) grad[i] = 0; unsigned m = *((unsigned *) data); for (i = 0; i < m; ++i) { - double fi = 1.0 / (c[i] + double fi = 1.0 / (c[i] + sqr(x[0]-A[i][0]) + sqr(x[1]-A[i][1]) + sqr(x[2]-A[i][2]) @@ -264,7 +264,7 @@ static double sixhumpcamel_f(unsigned n, const double *x, double *grad, void *da grad[0] = 8*x[0] - 2.1*4*pow(x[0],3.) + 2*pow(x[0],5.) + x[1]; grad[1] = x[0] - 8*x[1] + 16*pow(x[1],3.); } - RETURN(4*sqr(x[0]) - 2.1 * pow(x[0],4.) + pow(x[0],6.)/3. + RETURN(4*sqr(x[0]) - 2.1 * pow(x[0],4.) + pow(x[0],6.)/3. + x[0]*x[1] - 4*sqr(x[1]) + 4*pow(x[1],4.)); } diff --git a/util/mt19937ar.c b/util/mt19937ar.c index f6d7f60..d0b90ac 100644 --- a/util/mt19937ar.c +++ b/util/mt19937ar.c @@ -1,12 +1,12 @@ -/* +/* 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, @@ -24,8 +24,8 @@ 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 @@ -63,7 +63,7 @@ # endif #endif -/* Period parameters */ +/* Period parameters */ #define N 624 #define M 397 #define MATRIX_A 0x9908b0dfUL /* constant vector a */ @@ -81,8 +81,8 @@ void nlopt_init_genrand(unsigned long s) { mt[0]= s & 0xffffffffUL; for (mti=1; mti> 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[]. */ @@ -118,7 +118,7 @@ static uint32_t nlopt_genrand_int32(void) mti = 0; } - + y = mt[mti++]; /* Tempering */ @@ -158,7 +158,7 @@ static void nlopt_init_by_array(uint32_t init_key[], int key_length) 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 */ @@ -170,32 +170,32 @@ static long nlopt_genrand_int31(void) /* 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, @@ -215,8 +215,8 @@ int nlopt_iurand(int n) 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); -- 2.30.2