version 1.7, 2005/02/16 08:15:50
|
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! |
} ; | } ; |
| |
// ****************************************************************** | // ****************************************************************** |
|
|
// * 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 * |
|
|
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; |
} | } |
| |
|
|
// ****************************************************************** | // ****************************************************************** |
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 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 |
|
|
|
|
|
|
|
|
|
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 |
| |