int main() { struct particle ps[LIMITL]; /* 입자 군집 */ int i; /* 반복 횟수 제어 */ /* 난수 초기화 */ srand(SEED); /* 입자 군집 초기화 */ initps(ps); printps(ps); /* 최적화의 본체 */ for (i = 0; i < ILIMIT; ++i) { optimize(ps); printf("%d회째\n",i); printps(ps); } return 0; }
/** * scl: If NULL, return the index of the last split. Else cache the splits into scl. */ static int split_and_cache(int word_length, int nparts, split_cache *scl) { int n; int maxindex; p_list ps = alloca(sizeof(p_start)*nparts); /* partition start */ if (0 == word_length) return 0; /* The first partitioning is the whole word. * (Using a first dummy partition would make this code unneeded.) * But in any case the whole word here is unneeded, and I'm * too lazy to change that. */ ps[0] = word_length; maxindex = 0; if (scl) cache_partitions(&scl->sp[0], ps, nparts); /* Generate all possible partitions up to nparts partitions */ for (n = 1; n < nparts; n++) { /* increase the number of partitions */ int m = 0; int t; ps[0] = 1; ps[n] = word_length; /* set last partition end (dummy partition start) */ //printf("New number of partitions: n=%d\n", n); do { /* set next initial partitions lengths to 1 */ //printf("Initialize: m=%d\n", m); for (t = m; t < n; t++) { ps[t] = ps[m] + (t-m); //printf("ps[%d]=%d ", t, ps[t]); } //printf("\n"); /* move last partition */ //printf("Moving m=%d ps[m]=%d ps[m+1]=%d\n", n-1, ps[n-1], ps[n]); for (m = n-1; ps[m] < ps[m+1]; ps[m]++) { maxindex++; if (scl) cache_partitions(&scl->sp[maxindex*nparts], ps, nparts); #if DEBUG_ANYSPLIT printsplit(ps, n); printps(ps, n); #endif } /* last partition got to size 1, backtrack */ do { //printf("Backtrack m %d->%d\n", m, m-1); m--; /* continue as long as there is a place to move for partition m */ } while (m >= 0 && ps[m] + 1 == ps[m+1]); if (m >= 0) ps[m]++; } while (m >= 0); /* we have still positions to move */ //printf("End (n=%d)\n", n); } return maxindex+1; }