michael_hartman_cz

MI-PAA HW01 GreedyHeuristic

Oct 18th, 2016
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <algorithm>    // std::sort
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class CInstance
  10.  {
  11.     public:
  12.         vector<unsigned int>    weight;
  13.         vector<unsigned int>    cost;
  14.         vector<bool>            solution;
  15.         int                     id;
  16.         int                     quantity;
  17.         int                     capacity;
  18.         int                     total_cost;
  19.  };
  20.  
  21.  
  22. int readInstance ( CInstance & i, string line )
  23.  {
  24.     int tmp;
  25.     stringstream ss;
  26.     ss << line;
  27.    
  28.     ss >> i . id;
  29.     ss >> i . quantity;
  30.     ss >> i . capacity;
  31.     for ( int a = 0; a < i . quantity; ++a )
  32.      {
  33.         ss >> tmp;
  34.         i . weight . push_back ( tmp );
  35.         ss >> tmp;
  36.         i . cost . push_back ( tmp );
  37.      }
  38.     i . solution . resize ( i . weight . size(), false );
  39.     return 0;
  40.  }
  41.  
  42. int printInstance ( CInstance & i )
  43.  {
  44.     cout << i . id << " " << i . quantity << " " << i . capacity << " ";
  45.    
  46.     for ( int a = 0; a < i . quantity; ++a )
  47.      {
  48.         cout << i . cost [ a ] << " " << i . weight [ a ] << " ";
  49.      }
  50.     cout << endl;
  51.     return 0;
  52.  }
  53.  
  54. int printSolution ( CInstance & i )
  55.  {
  56.     cout << i . id << " " << i . quantity << " " << i . total_cost << "  ";
  57.    
  58.     for ( int a = 0; a < i . quantity; ++a )
  59.      {
  60.          //cout << ( i . solution [ a ] ? "0" : "1" ) << " ";
  61.          cout << (int) i . solution [ a ] << " ";
  62.      }
  63.     cout << endl;
  64.  }
  65.  
  66. int countInstanceCost ( CInstance & i )
  67.  {
  68.     int tmp_cost = 0;
  69.     for ( size_t idx = 0; idx < i . cost . size(); ++idx )
  70.         if ( i . solution [ idx ] )
  71.             tmp_cost += i . cost [ idx ];
  72.     return tmp_cost;
  73.  
  74.  }
  75.  
  76. //broken / unfinished
  77. int countNextSolution ( CInstance & i )
  78.  {
  79.  
  80.     for ( size_t idx = 0; idx < i . solution . size(); ++idx )
  81.      {
  82.         if ( i . solution [ idx ] == false )
  83.          {
  84.             i . solution [ idx ] = true;
  85.             return 0;
  86.          }
  87.        
  88.      }
  89.  }
  90.  
  91.  
  92. int recursiveBruteForce ( int n, int currentWeight, int currentValue, vector<bool> s, CInstance & i )
  93.  {
  94.     if ( n == -1 && currentWeight <= i . capacity && currentValue > i . total_cost )
  95.      {
  96.         i . total_cost = currentValue;
  97.         for ( size_t idx = 0; idx < i . solution . size(); ++idx )
  98.             i . solution [ idx ] = s [ idx ];
  99.      }
  100.    
  101.     if ( n == -1 )
  102.         return 0;
  103.    
  104.     //dont pack this
  105.     s [ n ] = false;
  106.     recursiveBruteForce ( n-1, currentWeight, currentValue, s, i );
  107.    
  108.     //pack this
  109.     s [ n ] = true;
  110.     recursiveBruteForce ( n-1, currentWeight + i . weight [ n ], currentValue + i . cost [ n ], s, i );
  111.  }
  112.  
  113. bool compareIdValuePairbyValue ( pair<int,double> a, pair<int,double> b )
  114.  {
  115.     return ( a . second > b . second );
  116.  }
  117.  
  118.  
  119. int greedyHeuristic ( CInstance & i )
  120.  {
  121.     vector< pair<int,double> > ratio;
  122.     ratio . resize ( i . quantity );
  123.     for ( size_t idx = 0; idx < i . quantity; ++ idx )
  124.      {
  125.         ratio [ idx ] . first = idx;
  126.         ratio [ idx ] . second = (double) i . cost [ idx ] / (double) i . weight [ idx ]; //suppose positive weights...
  127.      }
  128.    
  129.     //high to low, is best to worst
  130.     sort ( ratio.begin(), ratio.end(), compareIdValuePairbyValue );
  131.    
  132.     int currentWeight = 0;
  133.     i . total_cost = 0;
  134.     for ( size_t idx = 0; idx < i . quantity; ++ idx )
  135.      {
  136.         if ( currentWeight +  i . weight [ ratio [ idx ] . first ] <= i . capacity ) // another item still fits
  137.          {
  138.             currentWeight +=  i . weight [ ratio [ idx ] . first ];
  139.             i . total_cost += i . cost [ ratio [ idx ] . first ];
  140.             i . solution [ ratio [ idx ] . first ] = true;
  141.          }      
  142.      }
  143.    
  144.  }
  145.  
  146. int processInstance ( CInstance & i )
  147.  {  
  148.     vector<bool> initSol ( i . solution );
  149.     //recursiveBruteForce ( i . quantity - 1, 0, 0, initSol, i );
  150.     greedyHeuristic ( i );
  151.     return 0;
  152.  }
  153.  
  154. int main ( int argc, char ** argv )
  155. {
  156.     string line;
  157.     vector<CInstance> instances;
  158.     while ( getline ( cin, line ) )
  159.      {
  160.         instances . resize ( instances . size () + 1 );
  161.         readInstance ( instances [ instances . size () - 1 ], line );
  162.      }
  163.    
  164.     for ( size_t i = 0; i < instances . size(); ++i )
  165.      {
  166.         processInstance ( instances [ i ] );
  167.      }
  168.  
  169.     for ( size_t i = 0; i < instances . size(); ++i )
  170.      {
  171.         printSolution ( instances [ i ] );
  172.      }
  173.  
  174.  
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment