michael_hartman_cz

MI-PAA HW01 BruteForce

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