예제 #1
0
void test1() {
  size_t maxw = 4;
  size_t n = 4;
  size_t w[4] = {1,2,3,4};
  size_t v[4] = {0,1,4,2};
  size_t val, size;
  size_t* soln;
  Knapsack k = makeKnapsack(maxw, n, w, v);
  val = solve(k);
  printTable(k);
  printf("val %lu\n", val);
  assert(val == 4);
  soln = getSolution(k, &size);
  printf("size %lu soln[0] %lu\n", size, soln[0]);
  assert(size == 1);
  assert(soln[0] == 2);
  destroyKnapsack(k);
  free(soln);
}
예제 #2
0
void test2() {
  /* http://cse.unl.edu/~goddard/Courses/CSCE310J/Lectures/Lecture8-DynamicProgramming.pdf */
  size_t maxw = 5;
  size_t n = 4;
  size_t w[4] = {2,3,4,5};
  size_t v[4] = {3,4,5,6};
  size_t val, size;
  size_t* soln;
  Knapsack k = makeKnapsack(maxw, n, w, v);
  val = solve(k);
  printTable(k);
  printf("val %lu\n", val);
  assert(val == 7);
  soln = getSolution(k, &size);
  printf("size %lu soln[0] %lu soln[1] %lu\n", size, soln[0], soln[1]);
  assert(size == 2);
  assert(soln[0] == 1 && soln[1] == 0);
  destroyKnapsack(k);
  free(soln);
}
예제 #3
0
int main (int argc, char* argv[]) {
  int i; 
  pthread_t minions[MINIONS];

  /* Initialize Stuff */
  Knapsack* ks = initKnapsack(argv[1]);
  Heap* h = initHeap(DEFAULT_SIZE);
  ThreadArgs* ta = (ThreadArgs*)malloc(sizeof(ThreadArgs));
  ta->_ks = ks;
  ta->_h = h;

  /* Begin port of bb() */
  double rootUB = upperBound(ks, 0, ks->_capacity);
  Node* n = initNode(0, -1, rootUB, ks->_capacity, NULL);
  add(h, n);

  /* Create Minions */ 
  for ( i = 0; i < MINIONS; i++){
    pthread_create(&minions[i],NULL,minionDoWork,ta);
  }
  pthread_cond_signal(&ks->_cond);

  /* Return minions~ */
  for( i = 0; i < MINIONS; i++){
    pthread_join(minions[i],NULL);
  }

  /* Display Results */
  printf("Found solution: %d\n", ks->_lowerBound);
  printf("Best x=");
  for (i=0; i<ks->_nbItems-1; i++) {
    printf("%d,",ks->_bestX[i]);
  }
  printf("%d\n",ks->_bestX[ks->_nbItems-1]);
  printf("Nodes processed: %d\n", ks->_nodesProcessed);
  destroyKnapsack(ks);
  destroyHeap(h);
  free(ta);
  return 0;
}
예제 #4
0
int main (int argc, char* argv[]) {
  /* Read from file input */
  FILE *fp;
  Item* temp;
  Knapsack* ks = (Knapsack*)malloc(sizeof(Knapsack));
  char* fName = argv[1];
  fp = fopen(fName,"r");
  fscanf(fp,"%d", &ks->_nbItems);

  // We randomly chose nbItems/4 as an initial heap size.
  // It's something to tweak as we go on
  Heap* h = initHeap(ks->_nbItems/4);
  ks->_items = (Item**)malloc(sizeof(Item*)*ks->_nbItems);
  ks->_lowerBound = 0;

  // Each row of input has an unused counter variable at the beginning.
  // We'll use fscanf to dump it into j. 
  int i, j;
  /* For each of the items, read in their values and create the node */
  for (i = 0; i < ks->_nbItems; i++) {
    ks->_items[i] = (Item*)malloc(sizeof(Item));
    fscanf(fp,"%d %d %d", &j, &ks->_items[i]->_value, &ks->_items[i]->_weight);
  }
  ks->_bestX = (char*)calloc(ks->_nbItems, sizeof(char));
  fscanf(fp,"%d",&ks->_capacity);

  fclose(fp);
  /* Sort 'em */
  qsort(ks->_items, ks->_nbItems, sizeof(Item*), compare); 

  /* Begin port of bb() */
  
  double rootUB = upperBound(ks, 0, ks->_capacity);
  // I don't understand why depth is -1 here but I'll go with it
  Node* n = initNode(0, -1, rootUB, ks->_capacity, NULL);
  add(h, n);

  /* Begin port of run() */
  int nodesProcessed = 0;
  while (!isEmpty(h)) {
    n = removeMax(h);
    if (n->_depth >= (ks->_nbItems-1)) {
      if (n->_value > ks->_lowerBound) {
        printf("tighten LB to %d\n", n->_value);
        ks->_lowerBound = n->_value;
        for (i=0; i < n->_depth+1; i++) {
          ks->_bestX[i] = n->_x[i];
        }
        for (i=n->_depth+1; i<ks->_nbItems; i++) {
          ks->_bestX[i] = 0;
        }
        destroyNode(n);
      }
    } else {
      processNode(ks, h, n);
      nodesProcessed++;
    }
  }
  printf("Found solution: %d\n", ks->_lowerBound);
  printf("Best x=");
  for (i=0; i<ks->_nbItems-1; i++) {
    printf("%d,",ks->_bestX[i]);
  }
  printf("%d\n",ks->_bestX[ks->_nbItems-1]);
  printf("Nodes processed: %d\n", nodesProcessed);
  // Fix those pesky memory leaks.
  destroyKnapsack(ks);
  destroyHeap(h);
  return 0;
}