version 1.6, 2005/02/16 07:49:34
|
version 1.8, 2005/02/16 08:32:30
|
|
|
// ****************************************************************** | // ****************************************************************** |
class item { | class item { |
public: | public: |
float getRatio(); |
// float getRatio(); |
unsigned short getWeight(); | unsigned short getWeight(); |
unsigned short getCost(); | unsigned short getCost(); |
unsigned short getNumber(); | unsigned short getNumber(); |
|
|
private: | private: |
unsigned short weight; | unsigned short weight; |
unsigned short cost; | unsigned short cost; |
float ratio; |
// float ratio; |
unsigned short number; | unsigned short number; |
}; | }; |
| |
|
|
// ****************************************************************** | // ****************************************************************** |
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! |
} ; | } ; |
| |
// ****************************************************************** | // ****************************************************************** |
|
|
} | } |
else { | else { |
temp = (float)((this->myBackpack->get_maxWeight()) - cur_weight); | temp = (float)((this->myBackpack->get_maxWeight()) - cur_weight); |
temp = temp * (this->myBackpack->get_Item(this->nextitem)).getRatio(); |
// temp = temp * (this->myBackpack->get_Item(this->nextitem)).getRatio(); (ratio = 1!) |
} | } |
this->upper_bound = cur_value + temp; | this->upper_bound = cur_value + temp; |
| |
|
|
// * 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 * |
|
|
void item::setData(unsigned short weightage, unsigned short costage, unsigned short numerage){ | void item::setData(unsigned short weightage, unsigned short costage, unsigned short 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->number = numerage; | this->number = numerage; |
} | } |
| |
|
|
// ****************************************************************** | // ****************************************************************** |
void backpack::putItem(unsigned short weight, unsigned short cost){ | void backpack::putItem(unsigned short weight, unsigned short 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); |
} | } |
| |
|
|
} | } |
printf("======================================================\n"); | printf("======================================================\n"); |
printf("Totals: %2d %2d %2d\n",totalitemsinserted, temp_key.getWeight(), temp_key.getValue()); | printf("Totals: %2d %2d %2d\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())); |
} | } |
| |
// ****************************************************************** | // ****************************************************************** |
|
|
| |
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(); |
| |
|
|
user 0m0.062s | user 0m0.062s |
sys 0m0.000s | sys 0m0.000s |
| |
|
|
Now, lets really ramp it up so that we can see optimzation effects | Now, lets really ramp it up so that we can see optimzation effects |
n=2500! won't even care to list the numbers, no point! | n=2500! won't even care to list the numbers, no point! |
| |
|
|
| |
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 - |
|
move all the unsigned shorts to (other) |
|
the unsigned shorts are now ITEM_TYPE, a def earlier on (we could have done typedef here too, of course) |
|
real 0m2.928s |
|
user 0m1.999s |
|
sys 0m0.061s |
|
Slight increase. |
|
|
|
Bad things happen when we try to make it into floats, I just tired. floats can't be [] contents for an array ;p |
|
Doing unsigned long for now. |
|
goal 1 = stop ratio calculation and comparisons if at all possible! |
|
real time shot up! |
|
real 0m3.994s |
|
user 0m2.390s |
|
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 |
|
|
|
|
|
|
|
|
|
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 |
| |
| |
*/ | */ |