-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.cpp
More file actions
57 lines (42 loc) · 1.61 KB
/
Knapsack.cpp
File metadata and controls
57 lines (42 loc) · 1.61 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
#include <fstream>
#include <string>
#include <random>
#include <limits>
using namespace std;
#include "GeneticCode.h"
#include "Knapsack.h"
Knapsack::Knapsack(string fileName, unsigned int sizeCapacity, unsigned int weightCapacity):
sizeCapacity(sizeCapacity), weightCapacity(weightCapacity) {
ofstream file(fileName, ofstream::out);
// Fandom number generator
default_random_engine generator; // random_device{}()
uniform_int_distribution<unsigned char> randValue (1, numeric_limits<unsigned char>::max());
uniform_int_distribution<unsigned char> randWeight (0, this->weightCapacity / 10);
uniform_int_distribution<unsigned char> randSize (0, this->sizeCapacity / 10);
// Generate random items and record to file
for(unsigned char i = 0; i < 32; i++) {
struct Item item = {randValue(generator), randWeight(generator), randSize(generator)};
this->items.push_back(item);
file << (unsigned int) i << ", " << (unsigned int) item.value << ", " << (unsigned int) item.weight << ", " << (unsigned int) item.size << endl;
}
file.close();
}
unsigned int Knapsack::evaluateCode(unsigned int geneticCode) const {
unsigned int totalValue = 0;
unsigned int totalSize = 0;
unsigned int totalWeight = 0;
// Iterate through each bit of the code
for(unsigned int i = 0; i < 32; i++) {
unsigned int wtf = GeneticCode::readBit(geneticCode, i);
if(wtf == 1) {
// Include the item
totalValue += this->items[i].value;
totalSize += this->items[i].size;
totalWeight += this->items[i].weight;
}
}
if(totalSize > this->sizeCapacity || totalWeight > this->weightCapacity) {
return 0;
}
return totalValue;
}