-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglapa.cpp
More file actions
72 lines (65 loc) · 2.23 KB
/
glapa.cpp
File metadata and controls
72 lines (65 loc) · 2.23 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
#include "glapa.h"
namespace Glapa {
Output solve() {
Output o;
for (auto r : requestDescriptions) {
for (auto cache : endpoints[r.endpointId].cachesConnectedToThisEndpoint) {
if (o.addVideoToCache(r.videoId, cache.cacheId))
break;
}
}
return o;
}
const double D = 3;
double multiplier(int cacheId, Output & o) {
double rt = (double)o.capacitiesTaken[cacheId]/(double)nCacheCapacity;
return pow((1.0 / (1 + 1e-8 - rt)),3);
}
double calculateGain(int endpointId, Output& o) {
double gain = 0;
int lb = endpoints[endpointId].latencyToBase;
for (auto cache : endpoints[endpointId].cachesConnectedToThisEndpoint) {
gain = max(gain, lb - cache.cacheLatency * multiplier(cache.cacheId, o));
}
return gain;
}
bool eq(double a, double b) {
return abs(a-b) < 1;
}
double calculateScore(int requestId, Output& o) {
int vid = requestDescriptions[requestId].videoId;
return requestDescriptions[requestId].numberOfRequests * calculateGain(requestDescriptions[requestId].endpointId, o)/(double)videoSizes[vid];
}
Output solve2() {
Output o;
priority_queue<pair<double, int>> q;
for (int i = 0; i < nRequestsDescriptions; i++) {
q.emplace(calculateScore(i, o), i);
}
while (!q.empty()) {
double sc; int rid;
tie(sc, rid) = q.top();
q.pop();
double nsc = calculateScore(rid, o);
if (!eq(sc, nsc)) {
q.emplace(nsc, rid);
continue;
}
auto & r = requestDescriptions[rid];
bool present = false;
for (auto cache : endpoints[r.endpointId].cachesConnectedToThisEndpoint)
if (o.videosInCaches[cache.cacheId].count(r.videoId))
present = true;
if (!present) {
auto& caches = endpoints[r.endpointId].cachesConnectedToThisEndpoint;
sort(caches.begin(), caches.end(), [&] (const pair<int,int>&a, const pair<int,int>& b){return a.cacheLatency*multiplier(a.cacheId,o) < b.cacheLatency*multiplier(b.cacheId, o);});
for (auto cache : caches) {
if (o.addVideoToCache(r.videoId, cache.cacheId))
break;
}
}
}
cerr << o.getScore() << endl;
return o;
}
}