-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomStartPopulationGenerator.h
More file actions
32 lines (27 loc) · 999 Bytes
/
RandomStartPopulationGenerator.h
File metadata and controls
32 lines (27 loc) · 999 Bytes
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
#pragma once
#include "AbstractStartPopulationGenerator.h"
#include <algorithm> // std::random_shuffle
#include <cstdlib> // std::rand, std::srand
using namespace std;
class RandomStartPopulationGenerator :
public AbstractStartPopulationGenerator
{
public:
RandomStartPopulationGenerator(weightvalue maxWeight, int randomSeed) :AbstractStartPopulationGenerator(maxWeight) { srand(randomSeed); }
ByteVector Generate(ObjectVector objectVector)
{
random_shuffle(objectVector.begin(), objectVector.end());
weightvalue currentWeight = 0;
ByteVector generatedVector(objectVector.size());
for (auto object : objectVector) {
if (currentWeight+object.object.weight<=maxWeight)
{
currentWeight += object.object.weight;
generatedVector.setByte(object.index, 1);
}
// clog << object.index << " " << object.object.weight << " " << object.object.cost << " curW:" << currentWeight << endl;
}
return generatedVector;
}
~RandomStartPopulationGenerator();
};