version 1.7, 2005/02/16 08:15:50
|
version 1.9, 2005/02/16 08:55:07
|
|
|
| |
#define MAX_ITEMS 5100 | #define MAX_ITEMS 5100 |
| |
|
typedef float ITEM_MASS; |
|
typedef long INDEX_TYPE; |
|
|
|
|
using namespace std; | using namespace std; |
| |
// ****************************************************************** | // ****************************************************************** |
|
|
// ****************************************************************** | // ****************************************************************** |
class item { | class item { |
public: | public: |
float getRatio(); |
// float getRatio(); |
unsigned short getWeight(); |
ITEM_MASS getWeight(); |
unsigned short getCost(); |
ITEM_MASS getCost(); |
unsigned short getNumber(); |
// ITEM_MASS getNumber(); |
void setData(unsigned short,unsigned short,unsigned short); |
void setData(ITEM_MASS,ITEM_MASS); |
private: | private: |
unsigned short weight; |
ITEM_MASS weight; |
unsigned short cost; |
ITEM_MASS cost; |
float ratio; |
// float ratio; |
unsigned short number; |
// ITEM_MASS number; |
}; | }; |
| |
// ****************************************************************** | // ****************************************************************** |
|
|
void addCurItem(); | void addCurItem(); |
bool doneItems(); | bool doneItems(); |
float getBound(); | float getBound(); |
bool checkItem(unsigned short); |
bool checkItem(INDEX_TYPE); |
unsigned short getWeight(); |
ITEM_MASS getWeight(); |
unsigned short getValue(); |
ITEM_MASS getValue(); |
private: | private: |
void calcBound(); | void calcBound(); |
bool inserted[MAX_ITEMS]; | bool inserted[MAX_ITEMS]; |
unsigned short nextitem; |
INDEX_TYPE nextitem; |
unsigned short cur_weight; |
ITEM_MASS cur_weight; |
unsigned short cur_value; |
ITEM_MASS cur_value; |
float upper_bound; | float upper_bound; |
backpack *myBackpack; | backpack *myBackpack; |
}; | }; |
|
|
// ****************************************************************** | // ****************************************************************** |
struct item_comparator { | struct item_comparator { |
bool operator()( item left, item right ) const | bool operator()( item left, item right ) const |
{ return ( left.getRatio() < right.getRatio() ) ; } |
// { return ( left.getRatio() < right.getRatio() ) ; } |
|
{ return 0 ; } //same ratio to all, don't actually do a compare! |
} ; | } ; |
| |
// ****************************************************************** | // ****************************************************************** |
|
|
// ****************************************************************** | // ****************************************************************** |
class backpack { | class backpack { |
public: | public: |
void initBackpack(unsigned short, unsigned short); |
void initBackpack(INDEX_TYPE, ITEM_MASS); |
void putItem(unsigned short, unsigned short); |
void putItem(ITEM_MASS, ITEM_MASS); |
void store_item_array(); | void store_item_array(); |
void branch_and_bound(); | void branch_and_bound(); |
unsigned short get_totalItems(); |
ITEM_MASS get_totalItems(); |
unsigned short get_maxWeight(); |
ITEM_MASS get_maxWeight(); |
item get_Item(unsigned short); |
item get_Item(INDEX_TYPE); |
private: | private: |
priority_queue< item, vector<item> , item_comparator> item_queue; | priority_queue< item, vector<item> , item_comparator> item_queue; |
item *item_array; | item *item_array; |
priority_queue< key, vector<key>, key_comparator> key_queue; | priority_queue< key, vector<key>, key_comparator> key_queue; |
unsigned short totalItems; |
INDEX_TYPE totalItems; |
unsigned short maxWeight; |
ITEM_MASS maxWeight; |
long addnodeCount, worknodeCount; | long addnodeCount, worknodeCount; |
}; | }; |
| |
|
|
// * FUNCTION : checkItem IN : CLASS key * | // * FUNCTION : checkItem IN : CLASS key * |
// * Checks if a given item is listed as inserted. * | // * Checks if a given item is listed as inserted. * |
// ****************************************************************** | // ****************************************************************** |
bool key::checkItem(unsigned short num) { |
bool key::checkItem(INDEX_TYPE num) { |
return (this->inserted[num]); | return (this->inserted[num]); |
} | } |
| |
|
|
// * FUNCTION : getValue IN : CLASS key * | // * FUNCTION : getValue IN : CLASS key * |
// * Gets the Value of the current key. * | // * Gets the Value of the current key. * |
// ****************************************************************** | // ****************************************************************** |
unsigned short key::getValue(){ |
ITEM_MASS key::getValue(){ |
return this->cur_value; | return this->cur_value; |
} | } |
| |
|
|
// * FUNCTION : getWeight IN : CLASS key * | // * FUNCTION : getWeight IN : CLASS key * |
// * Gets the Weight of the current key. * | // * Gets the Weight of the current key. * |
// ****************************************************************** | // ****************************************************************** |
unsigned short key::getWeight(){ |
ITEM_MASS key::getWeight(){ |
return this->cur_weight; | return this->cur_weight; |
} | } |
| |
|
|
// * FUNCTION : getRatio IN : CLASS item * | // * FUNCTION : getRatio IN : CLASS item * |
// * Gets the Ratio for the current item. * | // * Gets the Ratio for the current item. * |
// ****************************************************************** | // ****************************************************************** |
float item::getRatio(){ |
//float item::getRatio(){ |
return ratio; |
// return ratio; |
} |
//} |
| |
// ****************************************************************** | // ****************************************************************** |
// * FUNCTION : getWeight IN : CLASS item * | // * FUNCTION : getWeight IN : CLASS item * |
// * Gets the Weight of the current item. * | // * Gets the Weight of the current item. * |
// ****************************************************************** | // ****************************************************************** |
unsigned short item::getWeight(){ |
ITEM_MASS item::getWeight(){ |
return this->weight; | return this->weight; |
} | } |
| |
|
|
// * FUNCTION : getCost IN : CLASS item * | // * FUNCTION : getCost IN : CLASS item * |
// * Gets the Value of the current item. * | // * Gets the Value of the current item. * |
// ****************************************************************** | // ****************************************************************** |
unsigned short item::getCost(){ |
ITEM_MASS item::getCost(){ |
return this->cost; | return this->cost; |
} | } |
| |
|
|
// * FUNCTION : getNumber IN : CLASS item * | // * FUNCTION : getNumber IN : CLASS item * |
// * Gets the Index of the current item. * | // * Gets the Index of the current item. * |
// ****************************************************************** | // ****************************************************************** |
unsigned short item::getNumber(){ |
//ITEM_MASS item::getNumber(){ |
return this->number; |
// return this->number; |
} |
//} |
| |
// ****************************************************************** | // ****************************************************************** |
// * FUNCTION : setData IN : CLASS item * | // * FUNCTION : setData IN : CLASS item * |
// * Sets all the data for an item. * | // * Sets all the data for an item. * |
// ****************************************************************** | // ****************************************************************** |
void item::setData(unsigned short weightage, unsigned short costage, unsigned short numerage){ |
void item::setData(ITEM_MASS weightage, ITEM_MASS costage){ //, ITEM_MASS numerage){ |
this->cost = costage; | this->cost = costage; |
this->weight = weightage; | this->weight = weightage; |
// this->ratio = ( (float)(cost)/(float)(weight) ); | // this->ratio = ( (float)(cost)/(float)(weight) ); |
this->ratio = 1; //ratio = 1 |
// this->ratio = 1; //ratio = 1 |
this->number = numerage; |
// this->number = numerage; |
} | } |
| |
| |
|
|
// * FUNCTION : initBackpack IN : CLASS backpack * | // * FUNCTION : initBackpack IN : CLASS backpack * |
// * Initalizes the backpack values and creates the item array * | // * Initalizes the backpack values and creates the item array * |
// ****************************************************************** | // ****************************************************************** |
void backpack::initBackpack(unsigned short total, unsigned short max){ |
void backpack::initBackpack(INDEX_TYPE total, ITEM_MASS max){ |
this->totalItems = total; | this->totalItems = total; |
this->maxWeight = max; | this->maxWeight = max; |
item_array = new item[total]; | item_array = new item[total]; |
|
|
// * FUNCTION : putItem IN : CLASS backpack * | // * FUNCTION : putItem IN : CLASS backpack * |
// * Creates an item and places it into the priority queue * | // * Creates an item and places it into the priority queue * |
// ****************************************************************** | // ****************************************************************** |
void backpack::putItem(unsigned short weight, unsigned short cost){ |
void backpack::putItem(ITEM_MASS weight, ITEM_MASS cost){ |
item temp_item; | item temp_item; |
temp_item.setData(weight,cost,(int)(this->item_queue.size())+1); |
temp_item.setData(weight,cost);//,(int)(this->item_queue.size())+1); // sometimes this starts at 2000? |
this->item_queue.push(temp_item); | this->item_queue.push(temp_item); |
} | } |
| |
|
|
// * FUNCTION : get_totalItems IN : CLASS backpack * | // * FUNCTION : get_totalItems IN : CLASS backpack * |
// * Returns the number of items for consideration. * | // * Returns the number of items for consideration. * |
// ****************************************************************** | // ****************************************************************** |
unsigned short backpack::get_totalItems(){ |
ITEM_MASS backpack::get_totalItems(){ |
return this->totalItems; | return this->totalItems; |
} | } |
| |
|
|
// * FUNCTION : get_maxWeight IN : CLASS backpack * | // * FUNCTION : get_maxWeight IN : CLASS backpack * |
// * Returns the maximum weight for this backpack. * | // * Returns the maximum weight for this backpack. * |
// ****************************************************************** | // ****************************************************************** |
unsigned short backpack::get_maxWeight(){ |
ITEM_MASS backpack::get_maxWeight(){ |
return this->maxWeight; | return this->maxWeight; |
} | } |
| |
|
|
// * FUNCTION : get_Item IN : CLASS backpack * | // * FUNCTION : get_Item IN : CLASS backpack * |
// * Returns a particular item from the item array * | // * Returns a particular item from the item array * |
// ****************************************************************** | // ****************************************************************** |
item backpack::get_Item(unsigned short index){ |
item backpack::get_Item(INDEX_TYPE index){ |
return ( this->item_array[index] ); | return ( this->item_array[index] ); |
} | } |
| |
|
|
| |
printf("\nObjects Chosen \n"); | printf("\nObjects Chosen \n"); |
| |
printf(" Objects Weights Values\n"); |
printf("\t\tWeights\tValues\n"); |
int totalitemsinserted = 0; | int totalitemsinserted = 0; |
for (int i = 0; this->totalItems > i; i++) { | for (int i = 0; this->totalItems > i; i++) { |
if ( temp_key.checkItem(i) ) { | if ( temp_key.checkItem(i) ) { |
printf(" %2d %2d %2d\n", this->item_array[i].getNumber(), this->item_array[i].getWeight(), this->item_array[i].getCost()); |
printf("\t\t%4.2f\t%4.2f\n", this->item_array[i].getWeight(), this->item_array[i].getCost()); |
totalitemsinserted++; |
totalitemsinserted++; // this->item_array[i].getNumber(), removed |
} | } |
} | } |
printf("======================================================\n"); | printf("======================================================\n"); |
printf("Totals: %2d %2d %2d\n",totalitemsinserted, temp_key.getWeight(), temp_key.getValue()); |
printf("Totals:\t%3d\t%4.2f\t%4.2f\n",totalitemsinserted, temp_key.getWeight(), temp_key.getValue()); |
printf("Ratio : %2.5f\n", ((float)temp_key.getValue()/(float)temp_key.getWeight())); |
// printf("Ratio : %2.5f\n", ((float)temp_key.getValue()/(float)temp_key.getWeight())); |
} | } |
| |
// ****************************************************************** | // ****************************************************************** |
|
|
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) |
{ | { |
item temp_item; | item temp_item; |
printf("CS331_Project 4, by Rizwan Kassim.\n"); |
printf("Entry into UCLA ACM Feb. 2005 Coding Competition\n"); |
printf("Version 3\n"); |
printf("Based on code developed (myself) for CS331 Project 4 at CSU Pomona\n"); |
printf("All compiled / source code are (C) Rizwan Kassim 2003\n\n"); |
printf("Version 4\n"); |
|
printf("All compiled / source code are (C) Rizwan Kassim 2005\n\n"); |
| |
| |
printf("============================ KNAPSACK ONE ================================\n"); | printf("============================ KNAPSACK ONE ================================\n"); |
backpack knapsackOne; | backpack knapsackOne; |
| |
knapsackOne.initBackpack(5000,50983); // 5 total items, 17 total weight | knapsackOne.initBackpack(5000,50983); // 5 total items, 17 total weight |
|
|
|
|
|
knapsackOne.putItem(2,2); |
|
|
knapsackOne.putItem(7567,7567); | knapsackOne.putItem(7567,7567); |
knapsackOne.putItem(2728,2728); |
|
knapsackOne.putItem(6052,6052); | knapsackOne.putItem(6052,6052); |
knapsackOne.putItem(6389,6389); | knapsackOne.putItem(6389,6389); |
knapsackOne.putItem(2402,2402); | knapsackOne.putItem(2402,2402); |
|
|
| |
knapsackOne.store_item_array(); | knapsackOne.store_item_array(); |
| |
|
/* |
for ( int i = 0; knapsackOne.get_totalItems() > i; i++){ | for ( int i = 0; knapsackOne.get_totalItems() > i; i++){ |
temp_item=knapsackOne.get_Item(i); | temp_item=knapsackOne.get_Item(i); |
printf("Item Number %2d : %2d cost for %2d weight at ratio %2.3f\n", temp_item.getNumber(), temp_item.getCost(), temp_item.getWeight(), temp_item.getRatio()); |
printf("Item Number %2d : %2d cost for %2d weight at ratio %2.3f\n", temp_item.getNumber(), temp_item.getCost(), temp_item.getWeight(), 1); //temp_item.getRatio()); |
} | } |
printf("\n"); | printf("\n"); |
|
*/ //--- we don't really want it all printed out, seriously. |
| |
knapsackOne.branch_and_bound(); | knapsackOne.branch_and_bound(); |
| |
|
|
Given that this is about the largest we can hope to achieve before INTMAX because a massive issue (we should really typedefine the container class), lets see what optimizations we can make to this code! | Given that this is about the largest we can hope to achieve before INTMAX because a massive issue (we should really typedefine the container class), lets see what optimizations we can make to this code! |
| |
must do 1 - | must do 1 - |
move all the unsigned shorts to (other) |
move all the ITEM_MASSs to (other) |
the unsigned shorts are now ITEM_TYPE, a def earlier on (we could have done typedef here too, of course) |
the ITEM_MASSs are now ITEM_TYPE, a def earlier on (we could have done typedef here too, of course) |
real 0m2.928s | real 0m2.928s |
user 0m1.999s | user 0m1.999s |
sys 0m0.061s | sys 0m0.061s |
|
|
user 0m2.390s | user 0m2.390s |
sys 0m0.046s | sys 0m0.046s |
| |
|
(that being said, timse don't seem to be consistent. CVS is storing our versions, lets plow on) |
|
|
|
remove the comparator in the structcomparator |
|
real 0m2.830s |
|
user 0m2.030s |
|
sys 0m0.015s |
|
|
|
remove the printout of the items as inserted |
|
real 0m1.423s |
|
user 0m1.436s |
|
sys 0m0.015s |
|
|
|
remove the tracking of the item "number", we don't need to deal with it with this project. |
|
|
|
after moved all unsigned ints to typedefs, |
|
typedef float ITEM_MASS; |
|
typedef long INDEX_TYPE; |
|
|
|
and modified the printfs to handle floating ! |
|
|
|
real 0m1.591s |
|
user 0m1.562s |
|
sys 0m0.000s |
|
|
|
inserting a float does : |
|
|
|
acm2.cpp: In function `int main(int, char**)': |
|
acm2.cpp:410: error: no matching function for call to `backpack::putItem( |
|
double, int, int)' |
|
acm2.cpp:279: error: candidates are: void backpack::putItem(float, float) |
|
make: *** [acm2] Error 1 |
|
|
|
(and it locks up if we have a decimal in seeked value) |
|
|
|
|
|
we could really move the pqueue to a queue --- although don't we want to push bigger items to the top to fill it up faster? |
| |
goal 1 - lets remove the double call to the creator function, weight=value ratio=1 | goal 1 - lets remove the double call to the creator function, weight=value ratio=1 |
| |