(file) Return to acm2.cpp CVS log (file) (dir) Up to [RizwankCVS] / acm

Diff for /acm/acm2.cpp between version 1.8 and 1.9

version 1.8, 2005/02/16 08:32:30 version 1.9, 2005/02/16 08:55:07
Line 10 
Line 10 
  
 #define MAX_ITEMS 5100 #define MAX_ITEMS 5100
  
   typedef float ITEM_MASS;
   typedef long INDEX_TYPE;
   
   
 using namespace std; using namespace std;
  
 // ****************************************************************** // ******************************************************************
Line 19 
Line 23 
 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;
 }; };
  
 // ****************************************************************** // ******************************************************************
Line 53 
Line 57 
         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;
 }; };
Line 91 
Line 95 
 // ****************************************************************** // ******************************************************************
 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;
 }; };
  
Line 191 
Line 195 
 // * 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]);
 } }
  
Line 199 
Line 203 
 // * 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;
 } }
  
Line 207 
Line 211 
 // * 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;
 } }
  
Line 223 
Line 227 
 // * 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;
 } }
  
Line 231 
Line 235 
 // * 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;
 } }
  
Line 239 
Line 243 
 // * 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;
 } }
  
  
Line 260 
Line 264 
 // * 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];
Line 272 
Line 276 
 // * 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); // sometimes this starts at 2000?          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);
 } }
  
Line 294 
Line 298 
 // * 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;
 } }
  
Line 302 
Line 306 
 // * 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;
 } }
  
Line 310 
Line 314 
 // * 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] );
 } }
  
Line 370 
Line 374 
  
         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()));
 } }
  
Line 392 
Line 396 
 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);
Line 7499 
Line 7507 
 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
Line 7526 
Line 7534 
 user    0m1.436s user    0m1.436s
 sys     0m0.015s 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? 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?


Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

Rizwan Kassim
Powered by
ViewCVS 0.9.2