|
From: Lewis <os...@el...> - 2008-02-21 20:32:28
|
Yeah I think I agree. When changing those in particular, it didn't seem
to make particular sense as it didn't "look clearer", thought it best to
follow the rules though. Noted though, I will see about putting those
ones back.
Lewis.
Tim Watson wrote:
> Lewis,
>
> It might conform but one rule in any good codingstyle is not to conform
> if it's best not to! I prefer no space around some operators, e.g.
>
> fred = harry*joe + 2*fred;
>
> is easier to read than
>
> fred = harry * joe + 2 * fred;
>
> In the code you changed, I prefer
>
> if (((gen-1)%displfreq) == 0) {
>
> to
>
> if (((gen-1) % displfreq) == 0) {
>
> as it makes clear that the meat of the expression is the ==.
>
> I applaud any attempt to clean up code, though!
>
> Tim.
>
> fif...@us... wrote:
>
>> Revision: 47
>> http://sga.svn.sourceforge.net/sga/?rev=47&view=rev
>> Author: fifthdim
>> Date: 2008-02-21 12:11:00 -0800 (Thu, 21 Feb 2008)
>>
>> Log Message:
>> -----------
>> Begin modifications to code layout to hopefully make it easier to read, encourage participation and conform with CODINGSTYLE docs.
>>
>> Modified Paths:
>> --------------
>> trunk/code/sga.c
>>
>> Modified: trunk/code/sga.c
>> ===================================================================
>> --- trunk/code/sga.c 2008-02-21 12:05:11 UTC (rev 46)
>> +++ trunk/code/sga.c 2008-02-21 20:11:00 UTC (rev 47)
>> @@ -132,30 +132,30 @@
>> #endif /* NOGSL */
>>
>> /* initialise the population */
>> - for (i=0; i<popsize; i++)
>> - for (j=0; j<chromsize; j++)
>> + for (i = 0; i < popsize; i++)
>> + for (j = 0; j < chromsize; j++)
>> curr[i][j] = '0' + (RNG(2)); /* RNG(N+1) returns rand int from [0..N] */
>> -
>> +
>> /* main loop of genetic algorithm */
>> - for (gen=1; gen!=gens; gen++) {
>> + for (gen = 1; gen != gens; gen++) {
>>
>> /* calculate fitness of current population */
>> - for (i=0; i<popsize; i++) {
>> + for (i = 0; i < popsize; i++) {
>> switch (fitfunc) {
>> case 2: fit[i] = RNG(chromsize+1) + 1; /* random */
>> break;
>> case 3: fit[i] = 1; /* onemax */
>> - for (j=0; j<chromsize; j++)
>> + for (j = 0; j < chromsize; j++)
>> fit[i] += curr[i][j] - '0';
>> break;
>> case 4: fit[i] = 1; /* diffmax */
>> - for (j=0; j<chromsize; j++)
>> + for (j = 0; j < chromsize; j++)
>> fit[i] += curr[i][j] - '0';
>> if (fit[i] < ((chromsize+1)/2)+1)
>> fit[i] = chromsize + 2 - fit[i];
>> break;
>> case 5: fit[i] = curr[i][0] - '0'; /* integer */
>> - for (j=1; j<chromsize; j++)
>> + for (j = 1; j < chromsize; j++)
>> fit[i] = (fit[i]<<1) + curr[i][j] - '0';
>> fit[i]++;
>> break;
>> @@ -166,9 +166,9 @@
>> }
>>
>> /* calculate stats, and display stats and individuals, if required */
>> - if (((gen-1)%displfreq) == 0) {
>> + if (((gen-1) % displfreq) == 0) {
>> mean = best = worst = fit[0];
>> - for (i=1; i<popsize; i++) {
>> + for (i = 1; i < popsize; i++) {
>> mean += fit[i];
>> if (fit[i] > best)
>> best = fit[i];
>> @@ -177,31 +177,31 @@
>> }
>> mean /= popsize;
>> std_dev = 0.0;
>> - for (i=0; i<popsize; i++)
>> + for (i = 0; i < popsize; i++)
>> std_dev += (fit[i]-mean)*(fit[i]-mean) / popsize;
>> std_dev = sqrt(std_dev);
>> - if ((!fconv) && (std_dev==0)) { /* if fit converged */
>> + if ((!fconv) && (std_dev == 0)) { /* if fit converged */
>> fconv = 1;
>> fconv_gen = gen;
>> }
>> setGenInfo(gen);
>> - for (i=0; i<chromsize; i++) /* init column counts */
>> + for (i = 0; i < chromsize; i++) /* init column counts */
>> count[i] = 0;
>> - for (i=0; i<sizedispl; i++) {
>> + for (i = 0; i < sizedispl; i++) {
>> setSequenceOpen();
>> - for (j=0; j<chromsize; j++) { /* show individuals */
>> + for (j = 0; j < chromsize; j++) { /* show individuals */
>> setSequenceChar(curr[i][j]);
>> count[j] += curr[i][j] - '0'; /* sum columns in pop */
>> }
>> setSequenceClose();
>> }
>> - for (; i<popsize; i++)
>> - for (j=0; j<chromsize; j++)
>> + for (; i < popsize; i++)
>> + for (j = 0; j < chromsize; j++)
>> count[j] += curr[i][j] - '0';
>> pconv_count = 0;
>> setSequenceOpen();
>> - for (i=0; i<chromsize; i++) {
>> - if ((count[i]!=0) && (count[i]!=popsize))
>> + for (i = 0; i < chromsize; i++) {
>> + if ((count[i] != 0) && (count[i] != popsize))
>> count[i] = count[i]*10/popsize + '0';
>> else {
>> count[i] = '-';
>> @@ -210,7 +210,7 @@
>> setSequenceChar(count[i]);
>> }
>> setSequenceClose();
>> - if ((!pconv) && (pconv_count==chromsize)) { /* if pop converged */
>> + if ((!pconv) && (pconv_count == chromsize)) { /* if pop converged */
>> pconv = 1;
>> pconv_gen = gen;
>> }
>>
>>
>> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by: Microsoft
>> Defy all challenges. Microsoft(R) Visual Studio 2008.
>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>> _______________________________________________
>> sga-commit mailing list
>> sga...@li...
>> https://lists.sourceforge.net/lists/listinfo/sga-commit
>>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> sga-devel mailing list
> sga...@li...
> https://lists.sourceforge.net/lists/listinfo/sga-devel
>
|