-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDancigStartPopulationGenerator.h
45 lines (37 loc) · 1.46 KB
/
DancigStartPopulationGenerator.h
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
#pragma once
#include "AbstractStartPopulationGenerator.h"
#include <algorithm>
using namespace std;
class DancigStartPopulationGenerator :
public AbstractStartPopulationGenerator
{
private:
static bool compare(IndexedBackpackObject& first,IndexedBackpackObject& second) {
//Âçÿòü èç ïåðâîé ïàðû è èç âòîðîé òîëüêî èõ ñòîèìîñòè è âåñà è ñðàâíèòü
return first.object.cost / first.object.weight < second.object.cost / second.object.weight;
}
public:
DancigStartPopulationGenerator(weightvalue maxWeight) :AbstractStartPopulationGenerator(maxWeight) {}
ByteVector Generate(ObjectVector objectVector)
{
costvalue currentCost = 0;
weightvalue currentWeight = 0;
ByteVector generatedVector(objectVector.size());
std::sort(objectVector.begin(), objectVector.end(),compare);
std::reverse(objectVector.begin(), objectVector.end());
for (size_t i = 0; i < objectVector.size(); i++)
{
if (currentWeight + objectVector.at(i).object.weight <= maxWeight) {
currentWeight += objectVector.at(i).object.weight;
generatedVector.setByte(objectVector.at(i).index,1);
}
else
{
//Èíà÷å ìá óæå ñ ìèíèìàëüíûìè âåññàìè, íî ñ ñàìîé ìàëåíüêîé ñòîèìîñòüþ
}
// clog << objectVector.at(i).index << " " << objectVector.at(i).object.weight << " " << objectVector.at(i).object.cost << " " << objectVector.at(i).object.cost / objectVector.at(i).object.weight << " curW:" << currentWeight << endl;
}
return generatedVector;
}
~DancigStartPopulationGenerator();
};