36static int randint(
int n_max) {
40 div_t y = div(-RAND_MAX-1, -n_max-1);
43 while (x > RAND_MAX + y.rem);
47#include <gsl/gsl_rng.h>
50static int randint(
int n_max) {
51 return gsl_rng_uniform_int(rng, n_max+1);
63static int *randperm(
int *p,
int n) {
68 for (i = 1; i < n; i++) {
81static void makespan(
struct solution *s) {
87 int *pd = s->prob->data;
91 k = (m+1)*(s->eval+1) + 1;
92 for(j = s->eval; j < n; j++, k++)
93 for(i = 0; i < m; i++, k++)
94 t[k] = pd[m*sd[j]+i] + ((t[k-1] >= t[k-m-1]) ? t[k-1] : t[k-m-1]);
106static int lis(
int *P,
int *M,
int *X,
int n) {
109 for (i = 0; i < n; i++) {
113 mid = (lo + hi + 1) / 2;
114 if (X[M[mid-1]] < X[i])
120 P[i] = (newL > 1) ? M[newL-2] : -1;
134static int binary_search(
int *A,
int *I,
int x,
int low,
int high) {
137 mid = (low + high) / 2;
157struct problem *newProblem(
const char *filename) {
160 int j, i, n=-1, m=-1;
161 infile = fopen(filename,
"r");
163 fscanf(infile,
"%d", &n);
164 fscanf(infile,
"%d", &m);
165 if (n > 0 && m > 0) {
167 p->data = (
int *) malloc(n * m *
sizeof (
int));
168 for (j = 0; j < n; j++)
169 for (i = 0; i < m; i++) {
170 fscanf(infile,
"%*d %d", p->data+m*j+i);
175 fprintf(stderr,
"Invalid knapsack instance %s\n", filename);
178 fprintf(stderr,
"Cannot open file %s\n", filename);
196 s->data = (
int*) malloc(p->n * sizeof (
int));
199 s->times = (
int*) malloc((p->n+1) * (p->m+1) *
sizeof (
int));
201 for(i = 0; i <= p->m; i++)
203 for(i = 1; i <= p->n; i++)
204 s->times[(p->m+1)*i] = 0;
225 ps->data = (
int*) malloc(p->n * sizeof (
int));
226 ps->pred = (
int*) malloc(p->n * sizeof (
int));
227 ps->pos = (
int*) malloc(p->n * sizeof (
int));
236void freeProblem(
struct problem *p) {
245void freeSolution(
struct solution *s) {
255void freeMove(
struct move *v) {
263void freePathState(
struct pathState *ps) {
271void printProblem(
struct problem *p) {
275 printf(
"# Permutation Flowshop Problem (%d jobs, %d machines)\n", n, m);
276 printf(
"%d %d\n", n, m);
277 for (j = 0; j < n; j++) {
278 for (i = 0; i < m; i++)
279 printf(
" %d",p->data[j*m+i]);
284void printSolution(
struct solution *s) {
286 printf(
"%dx%d Flowshop solution\n p:", s->n, s->m);
287 for(i = 0; i < s->n; i++)
288 printf(
" %d",s->data[i]);
291 for(i = 0; i < (s->eval+1)*(s->m+1); i++) {
292 printf(
" %d",s->times[i]);
293 if ((i+1) % (s->m+1) == 0)
299void printMove(
struct move *v) {
300 printf(
"%dx%d Flowshop move: %d, %d\n", v->prob->n, v->prob->m, v->data[0], v->data[1]);
303void printPathState(
struct pathState *ps) {
305 printf(
"%dx%d Flowshop path state\n", ps->n, ps->prob->m);
306 printf(
" Path length: %d\n Permutation:", ps->n - ps->in_order);
307 for (i = 0; i < ps->n; i++)
308 printf(
" %d",ps->data[i]);
309 printf(
"\n In order: %d elements\n Positions:",ps->in_order);
310 for (i = 0; i < ps->n; i++)
311 printf(
" %d",ps->pos[i]);
324 randperm(s->data, s->n);
337double getObjectiveValue(
struct solution *s) {
340 return (
double)s->objvalue;
345 dest->prob = src->prob;
348 memcpy(dest->data, src->data, src->n * sizeof (
int));
349 memcpy(dest->times, src->times, (src->n+1) * (src->m+1) * sizeof (
int));
350 dest->objvalue = src->objvalue;
351 dest->eval = src->eval;
372 for (k = i; k < j; k++)
373 s->data[k] = s->data[k+1];
375 memmove(s->data+i, s->data+i+1, (j-i) * sizeof (
int));
381 for (k = i; k > j; k--)
382 s->data[k] = s->data[k-1];
384 memmove(s->data+j+1, s->data+j, (i-j) * sizeof (
int));
423 int max = ps->in_order;
424 int i, j, k, el, r, s, n = ps->n;
425 int *pos = ps->pos, *data = ps->data;
429 v->data[0] = v->data[1] = 0;
434 r = max + randint(n-max-1);
435 i = v->data[0] = pos[r];
438 for (s = 0; s < max; s++)
439 if (data[i] <= data[pos[s]])
441 printf(
"data[i] = %d, data[pos[s]] = %d, max = %d, s = %d\n", data[i], data[pos[s]], max, s);
442 printf(
"s = %d, bs = %d, bss = %d\n\n", s, binary_search(data, pos, data[i], 0, max), binary_search(data, pos, data[pos[s]], 0, max));
444 s = binary_search(data, pos, data[i], 0, max);
449 }
else if (s == max) {
452 }
else if (i < pos[s]) {
459 j = v->data[1] = low + randint(high-low);
466 for (k = i; k < j; k++)
469 for (k = 0; k < n; k++)
470 if (pos[k] > i && pos[k] <= j)
473 for (k = s-1; k >= 0 && pos[k] > i; k--)
475 for (k = r+1; k < n && pos[k] <= j; k++)
479 for (k = i; k > j; k--)
482 for (k = 0; k < n; k++)
483 if (pos[k] >= j && pos[k] < i)
486 for (k = s; k < max && pos[k] < i; k++)
488 for (k = r-1; k >= max && pos[k] >= j; k--)
495 for (k = r; k > s; k--)
498 memmove(pos+s+1, pos+s, (r-s) *
sizeof (
int));
516 for (i = 0; i < n; i++)
517 ps->pos[s2->data[i]] = i;
518 for (i = 0; i < n; i++)
519 ps->data[i] = ps->pos[s1->data[i]];
522 ps->in_order = max = lis(ps->pred, ps->pos, ps->data, n);
526 for (i = j = n; i > 0;) {
552int getPathLength(
const struct pathState *ps) {
553 return ps->n - ps->in_order;