version 1.11, 2005/02/16 10:04:23
|
version 1.12, 2005/02/16 10:27:53
|
|
|
#include<fstream> | #include<fstream> |
#include<string> | #include<string> |
#include <queue> | #include <queue> |
|
#include <stack> |
#include <stdio.h> | #include <stdio.h> |
// yes I could use just one i/o lib | // yes I could use just one i/o lib |
using namespace std; | using namespace std; |
|
|
// their parent object was in the priority queue, so static sized | // their parent object was in the priority queue, so static sized |
// arrays were used | // arrays were used |
| |
#define MAX_ITEMS 7000 |
#define MAX_ITEMS 65536 |
|
//the above bound comes from the fixed value for the boolean array TODO REMOVE LIMITATION |
| |
typedef long ITEM_MASS; | typedef long ITEM_MASS; |
typedef long INDEX_TYPE; | typedef long INDEX_TYPE; |
|
|
} | } |
| |
| |
printf("result : %d\n",temp_key.getWeight()); |
if ( temp_key.getWeight() == targetvalue ) { |
|
|
|
|
|
|
int totalitemsinserted = 0; | int totalitemsinserted = 0; |
int first=1; | int first=1; |
for (int i = 0; this->totalItems > i; i++) { | for (int i = 0; this->totalItems > i; i++) { |
|
|
printf("%d", this->item_array[i].getWeight()); | printf("%d", this->item_array[i].getWeight()); |
} | } |
} | } |
|
} |
|
else { printf("CANNOT FILL PALLET\n"); } |
} | } |
| |
/* | /* |
|
|
| |
char * read_buffer = (char *) malloc (length); | char * read_buffer = (char *) malloc (length); |
ITEM_MASS * inventory = (ITEM_MASS * ) malloc (length); | ITEM_MASS * inventory = (ITEM_MASS * ) malloc (length); |
|
// could use stack instead of array --- doesn't require malloc, more 'safe' avoids overflows, esp if |
|
// input file is funky --- its better software engineering, but its slower. Given that this is a pretty |
|
// small system, I'll stick with the array |
inputfs.getline (read_buffer, length-1); | inputfs.getline (read_buffer, length-1); |
| |
char * theToken; INDEX_TYPE index = 0; | char * theToken; INDEX_TYPE index = 0; |
|
|
| |
INDEX_TYPE size_inventory = index; //remember this is 1 based, not 0 | INDEX_TYPE size_inventory = index; //remember this is 1 based, not 0 |
| |
|
int SUPER_MODE = 0; |
|
if (inventory[0]==1) { SUPER_MODE=0;} |
|
|
if (DEBUG_MODE) { | if (DEBUG_MODE) { |
printf("Line 1 read - %d products from warehouse\n", size_inventory); | printf("Line 1 read - %d products from warehouse\n", size_inventory); |
|
if (SUPER_MODE) printf("Working in Superincreasing mode!\n"); |
} | } |
| |
// qsort ( inventory, size_inventory, sizeof(int), intcomp); // we now have a sorted list | // qsort ( inventory, size_inventory, sizeof(int), intcomp); // we now have a sorted list |
|
|
} | } |
| |
inputfs.close(); | inputfs.close(); |
|
free(read_buffer); |
|
|
|
if |
|
|
| |
// ckeanup, release memeory, close files here | // ckeanup, release memeory, close files here |
| |
|
|
| |
} | } |
knapsackOne.store_item_array(); | knapsackOne.store_item_array(); |
|
free(inventory); |
knapsackOne.branch_and_bound(DEBUG_MODE,palette_size); | knapsackOne.branch_and_bound(DEBUG_MODE,palette_size); |
| |
printf("\n"); | printf("\n"); |
} | } |
| |
| |
|
|
|
|
|
void super ( ITEM_MASS target_value , INDEX_TYPE size_inventory, int DEBUG_MODE ) { |
|
// input, a super increasing set, lowest to highest. |
|
// the number of elements in the list [1-ordinal] |
|
// a target value. |
|
//debugmode |
|
|
|
// need to output values that sum to target |
|
// need another place to store. --- not another array, need LIFO structure. A stack of course. |
|
// we have to count downwards during super increasing, or we could qsort the list. (not going to do that) |
|
|
|
stack<ITEM_MASS> packages_to_load; |
|
|
|
if (DEBUG_MODE) { |
|
printf("Extracting values, highest to lowest, doing reduction\n"); |
|
} |
|
for ( INDEX_TYPE store_index=size_inventory-1; size_inventory<=0;store_index-- ) { |
|
if (DEBUG_MODE) { |
|
printf("value %d, is it smaller than %d? If so, subtract and store.\n",inventory[store_index],target_value); |
|
} |
|
if ( inventory[store_index] <= target_value ) { |
|
target_value = target_value - inventory[store_index]; |
|
packages_to_load.push(inventory[store_index]); |
|
} |
|
} |
|
if ( target_value == 0 ) { |
|
int first = 1; |
|
while ( ! packages_to_load.empty() ) { |
|
if (first==0) { printf(","); } |
|
else {first = 0;} // there has GOT to be a better way! |
|
printf("%d",packages_to_load.top()); |
|
packages_to_load.pop(); |
|
} |
|
else { printf("CANNOT FILL PALLET\n"); } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/* | /* |
Modifications to the original branch-and-bound algorithim/approach | Modifications to the original branch-and-bound algorithim/approach |
| |