Exemplo n.º 1
0
extern stype combo(item *f, item *l, stype c, stype lb, stype ub,
            boolean def, boolean relx)
/* f,l : first, last item                                               */
/* c   : capacity of knapsack                                           */
/* lb  : lower bound. Solution vector is only updated if better z found */
/* ub  : upper bound. When upper bound is reached terminate immediately */
/* def : should solution vector be defined or just find objective value */
/* relx: relaxed problem is solved (no more relaxations will be made)   */
/* returns the objective value of the problem                           */
{
  allinfo a;
  interval *inttab;
  boolean heur, rudi;

  if ((ub != 0) && (lb == ub)) return lb;

  heur     = FALSE;
  rudi     = FALSE;
  inttab   = palloc(sizeof(interval), SORTSTACK);
  a.intv1b = &inttab[0];
  a.intv2b = &inttab[SORTSTACK - 1];
  a.intv1  = a.intv1b;
  a.intv2  = a.intv2b;
  a.fitem  = f;
  a.litem  = l;
  a.c      = c;
  a.z      = lb;
  a.lb     = lb;
  a.relx   = relx;
  a.master = (def && !relx);
  a.coresize = 0;

  partsort(&a, a.fitem, a.litem, 0, a.c, PARTITION);
  findbreak(&a);
  a.ub = (ub == 0 ? a.dantzig : ub);

  /* find and enumerate core */
  findcore(&a);
  reduceset(&a);

  while ((a.d.size > 0) && (a.z < a.ub)) {
    if (a.t <= a.lsort) {
      if (haschance(&a, a.t, RIGHT)) multiply(&a, a.t,  RIGHT);
       a.t++;
    }
    reduceset(&a);
    if (a.s >= a.fsort) {
      if (haschance(&a, a.s, LEFT)) multiply(&a, a.s,  LEFT);
      a.s--;
    }
    reduceset(&a);

    /* find better lower bound when needed */
    if ((!heur) && (a.d.size > MINHEUR)) { heuristic(&a); heur = TRUE; }

    /* find tight bound when needed */
    if ((!relx) && (a.d.size > MINSET)) { surrelax(&a); relx = TRUE; }

    /* use rudimentary divisibility to decrease c */
    if ((!rudi) && (a.d.size > MINRUDI)) { rudidiv(&a); rudi = TRUE; }
  }
  pfree(a.d.set1);
  pfree(inttab);

  if ((def) && (a.z > a.lb)) definesolution(&a);
  pfree(a.ffull);

  return a.z;
}
Exemplo n.º 2
0
stype minknap(int n, int *p, int *w, int *x, int c)
{
  allinfo a;
  item* tab;//[KNAPSIZE];
  static item tabmem[KNAPSIZE];
  interval* inttab;//[KNAPSIZE];
  static interval inttabmem[SORTSTACK];

  /* allocate space for internal representation */
  //tab = (item *) palloc(sizeof(item) * n);
  tab = tabmem;//(item *) palloc(sizeof(item) * n);
  a.fitem = &tab[0]; a.litem = &tab[n-1];
  copyproblem(a.fitem, a.litem, p, w, x);
  a.n           = n;
  a.cstar       = c;

  a.iterates    = 0;
  a.simpreduced = 0;
  a.pireduced   = 0;
  a.pitested    = 0;
  a.maxstates   = 0;
  a.coresize    = 0;

  //inttab  = (interval*) palloc(sizeof(interval) * SORTSTACK);
  inttab  = inttabmem;
  a.intv1 = a.intv1b = &inttab[0];
  a.intv2 = a.intv2b = &inttab[SORTSTACK - 1];
  a.fsort = a.litem; a.lsort = a.fitem;
  partsort(&a, a.fitem, a.litem, 0, PARTIATE);
  findbreak(&a);

  a.ub        = a.dantzig;
  a.firsttime = TRUE;

  for (;;) {
    a.iterates++;

    a.s = a.b-1;
    a.t = a.b;
    initfirst(&a, a.psumb, a.wsumb);
    initvect(&a);
    reduceset(&a);

    while ((a.d.size > 0) && (a.z < a.ub)) {
      if (a.t <= a.lsort) {
	if (haschance(&a, a.t, RIGHT)) multiply(&a, a.t, RIGHT);
	(a.t)++;
      }
      reduceset(&a);
      if (a.s >= a.fsort) {
	if (haschance(&a, a.s, LEFT)) multiply(&a, a.s, LEFT);
	(a.s)--;
      }
      reduceset(&a);
    }
    //pfree(a.d.set1);

    definesolution(&a);
    if (a.welldef) break;
  }
  //pfree(tab);
  //pfree(inttab);
  return a.zstar;
}