version 1.11, 2005/02/16 10:04:23
|
version 1.13, 2005/02/16 10:44:24
|
|
|
#include<fstream> | #include<fstream> |
#include<string> | #include<string> |
#include <queue> | #include <queue> |
|
#include <stack> |
|
#include <vector> |
#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"); } |
} | } |
| |
/* |
|
// have to deal with non int #s | // have to deal with non int #s |
| |
int intcomp (const void * a, const void * b) |
void super_increase_algo ( vector<ITEM_MASS> inventory, ITEM_MASS target_value , INDEX_TYPE size_inventory, int DEBUG_MODE ) { |
{ |
stack<ITEM_MASS> packages_to_load; |
return ( *(int*)a - *(int*)b ); |
|
}*/ |
if (DEBUG_MODE) { |
|
printf("Extracting values, highest to lowest, doing reduction from index %d\n",size_inventory); |
|
} |
|
for ( INDEX_TYPE store_index=size_inventory-1; store_index>=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(); |
|
} |
|
printf("\n"); |
|
} |
|
else { printf("CANNOT FILL PALLET\n"); } |
|
} |
|
|
| |
void syntaxmsg(){ | void syntaxmsg(){ |
printf("Usage:\n"); | printf("Usage:\n"); |
|
|
length = inputfs.tellg(); | length = inputfs.tellg(); |
inputfs.seekg (0, ios::beg); | inputfs.seekg (0, ios::beg); |
| |
max_inventory= 1 + length/2; // Elements cannot be more than this amount |
|
|
|
char * read_buffer = (char *) malloc (length); | char * read_buffer = (char *) malloc (length); |
ITEM_MASS * inventory = (ITEM_MASS * ) malloc (length); |
vector<ITEM_MASS> inventory; |
|
// 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 --- oh even better -- a vector! |
inputfs.getline (read_buffer, length-1); | inputfs.getline (read_buffer, length-1); |
| |
char * theToken; INDEX_TYPE index = 0; | char * theToken; INDEX_TYPE index = 0; |
|
|
// assume 1+ items | // assume 1+ items |
while ( theToken != NULL ) { | while ( theToken != NULL ) { |
//printf("tokendebug %d %f\n",theToken,theToken); | //printf("tokendebug %d %f\n",theToken,theToken); |
inventory[index] = atol(theToken); |
inventory.push_back(atol(theToken)); |
//printf("index %d, token %s, value %d\n",index,theToken,inventory[index]); | //printf("index %d, token %s, value %d\n",index,theToken,inventory[index]); |
index++; | index++; |
theToken = strtok (NULL,","); | theToken = strtok (NULL,","); |
} | } |
| |
INDEX_TYPE size_inventory = index; //remember this is 1 based, not 0 |
INDEX_TYPE size_inventory = inventory.size(); //remember this is 1 based, not 0 |
|
|
|
int SUPER_MODE = 0; |
|
if (inventory[0]==1) { SUPER_MODE=1;} |
| |
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 |
|
|
printf("Line 2 read - %d weight units can fit onto palette\n",palette_size); | printf("Line 2 read - %d weight units can fit onto palette\n",palette_size); |
} | } |
| |
inputfs.close(); |
if (SUPER_MODE) { |
|
super_increase_algo (inventory, palette_size , size_inventory, DEBUG_MODE ); |
// ckeanup, release memeory, close files here |
return 0; |
|
} |
| |
|
inputfs.close(); |
|
free(read_buffer); |
| |
// I could use a class to extrapolate out the file loading function like my 499 project.... | // I could use a class to extrapolate out the file loading function like my 499 project.... |
| |
|
|
| |
} | } |
knapsackOne.store_item_array(); | knapsackOne.store_item_array(); |
|
inventory.clear(); |
knapsackOne.branch_and_bound(DEBUG_MODE,palette_size); | knapsackOne.branch_and_bound(DEBUG_MODE,palette_size); |
| |
printf("\n"); | printf("\n"); |
} | } |
| |
| |
|
|
|
|
|
|
|
|
|
|
|
|
/* | /* |
Modifications to the original branch-and-bound algorithim/approach | Modifications to the original branch-and-bound algorithim/approach |
| |