All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
knapsack_unbounded_example.cpp
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
18 
19 #include <vector>
20 #include <iostream>
21 
22 int main() {
23  using Objects = std::vector<std::pair<int, int>>;
24  Objects objects{{1, 3}, {2, 2} , {3, 65} ,
25  {1, 1} , {2, 2} , {4, 3} , {1, 1} , {10, 23}};
26  const int capacity = 6;
27  auto size = [](std::pair<int, int> object) {return object.first;};
28  auto value = [](std::pair<int, int> object) {return object.second;};
29 
30  Objects result;
31  std::cout << "Knapsack unbounded" << std::endl;
32  auto maxValue = paal::knapsack_unbounded(objects,
33  capacity, std::back_inserter(result),
34  size, value);
35 
36  std::cout << "Max value " << maxValue.first << ", Total size "
37  << maxValue.second << std::endl;
38  for(auto o : result) {
39  std::cout << "{ size = " << o.first << ", value = " << o.second << "} ";
40  }
41  std::cout << std::endl;
42 
43 
44  return 0;
45 }
detail::knapsack_base< Objects, ObjectSizeFunctor, ObjectValueFunctor >::return_type knapsack_unbounded(Objects &&objects, detail::FunctorOnRangePValue< ObjectSizeFunctor, Objects > capacity, OutputIterator out, ObjectSizeFunctor size, ObjectValueFunctor value=ObjectValueFunctor())
Solution to the knapsack problem.