-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0029knapsac.cpp
More file actions
74 lines (59 loc) · 1.74 KB
/
0029knapsac.cpp
File metadata and controls
74 lines (59 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdlib>
#include <cassert>
#include <fstream>
#include <string>
#include <vector>
#include "Timer.h"
#include "Project2.h"
using namespace std;
// the maximum weight
const int MAX_WEIGHT = 65536;
// the number of packages to examine
const int NUM_PACKAGES = 24;
int main()
{
// declare variables
Timer timer;
Project2 proj2;
ifstream infile;
PackageStats access;
vector<PackageStats> packages;
vector<PackageStats> bestCombo;
int totPackages = 0;
// open file & make sure it exists
infile.open("knapsack_packages.txt");
if(infile.fail())
{
cerr<<"nCant find file!n";
exit(1);
}
// get the total number of packages from the file
infile >> totPackages;
// make sure there are enough packages in the file
assert(NUM_PACKAGES <= totPackages);
// display stats
cerr<<"n = "<<NUM_PACKAGES<<", W = "<<MAX_WEIGHT
<<"nn-- Exhaustive Search Solution --nn";
// get the remaining info from the file
// std::string int int
for(int x=0; (infile >> access.name >> access.votes >> access.size)&& x < NUM_PACKAGES; ++x)
{
packages.push_back(access);
}
infile.close();
// start the timer
timer.Start();
// find the best knapsack subset solution
bestCombo = proj2.ExhaustiveKnapsack(packages, MAX_WEIGHT);
// stop the timer
timer.Stop();
// display the best solution packages
proj2.Display(bestCombo, bestCombo.size());
// display the size and total votes
cout<<"nTotal Size = "<<proj2.TotalSize(bestCombo)<<" -- Total Votes = "
<<proj2.TotalVotes(bestCombo)<<endl;
// display the elapsed time
cout<<"nIt took "<<timer.Elapsed()*1000
<<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl;
return 0;
}