-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.cpp
116 lines (98 loc) · 2.66 KB
/
cluster.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include "house.h"
#include "cluster.h"
Cluster::Cluster(int size, int max, float opt, float sub)
{
for(int i = 0; i < size; i++)
{
houses.push_back(new House());
}
maxPower = max;
optimal = opt;
suboptimal = sub;
}
void Cluster::toQueue(Appliance* a)
{
queue.push_back(a);
}
void Cluster::tick()
{
int W = getPower();
int heatersOn = 0;
int heatersConstant = 5;
for(int i = 0; i < houses.size(); i++){
std::vector<Appliance*> a = houses[i]->getAppliances();
for(int j = 0; j < a.size(); j++){
if(a[j]->getType() == TYPE_HEATING && (a[j]->getPowerState() == POWER_FORCED_ON || a[j]->getPowerState() == POWER_GRANTED_ON )){
heatersOn++;
std::cout << heatersOn << "\n";
}
}
}
for(int i = 0; i < queue.size(); i++)
{
/*queue[i]->turnOn(false);
queue.erase(queue.begin() + i);
i--;*/
if(queue[i]->getType() == TYPE_HEATING )
{
if(heatersOn < heatersConstant)
{
W += queue[i]->getPower();
queue[i]->turnOn(false);
queue.erase(queue.begin() + i);
i--;
heatersOn++;
std::cout << "Toimii :O \n";
continue;
}
}
if(queue[i]->getPowerState() == POWER_QUEUED && abs((maxPower * optimal) - (W + queue[i]->getPower())) < abs((maxPower * optimal) - W))
{
W += queue[i]->getPower();
queue[i]->turnOn(false);
if(queue[i]->getType() == TYPE_HEATING) heatersOn++;
queue.erase(queue.begin() + i);
i--;
}
else if(queue[i]->getPowerState() == POWER_FORCED_ON && abs((maxPower * suboptimal) - (W + queue[i]->getPower())) < abs((maxPower * suboptimal) - W))
{
queue[i]->turnOn(false);
if(queue[i]->getType() == TYPE_HEATING) heatersOn++;
queue.erase(queue.begin() + i);
i--;
}
}
for(int i = 0; i < houses.size(); i++)
{
houses[i]->tick(this);
}
std::cout << getPower() << ", " << getForced() << std::endl;
}
int Cluster::getPower()
{
int ret = 0;
for(int i = 0; i < houses.size(); i++)
{
ret += houses[i]->getPower();
}
return ret;
}
int Cluster::getPower(TYPE_ENUM type)
{
int ret = 0;
for(int i = 0; i < houses.size(); i++)
{
ret += houses[i]->getPower(type);
}
return ret;
}
int Cluster::getForced()
{
int ret = 0;
for(int i = 0; i < houses.size(); i++)
{
ret += houses[i]->getForced();
}
return ret;
}