-
Notifications
You must be signed in to change notification settings - Fork 1
/
Brain.php
173 lines (149 loc) · 5.39 KB
/
Brain.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
class Brain
{
//
public $videos = [];
//
public $endpoints = [];
// $left_spaces_to_each_cache [ CACHE_ID => SPACE_ID];
public $left_spaces_to_each_cache = [];
// $videos_at_cache = [ CACHE_ID => [VIDEO_ID => VIDEO_ID]]; for quick search we make assoc array
public $videos_at_cache = [];
public $answer_string = "";
// Initialization
public function __construct($cache_numbers = 0, $cache_capacity)
{
if ($cache_numbers && $cache_capacity) {
$this->left_spaces_to_each_cache = array_fill(0, $cache_numbers, $cache_capacity);
}
}
public function solution1()
{
$this->sortByRequestCounts();
$this->_calculateAndSaveInAnswerString();
}
public function solution2()
{
$this->sortByTotalRequests();
$this->_calculateAndSaveInAnswerString();
}
public function solution3()
{
$this->sortByLatencyToDataCenter();
$this->_calculateAndSaveInAnswerString();
}
public function solution4()
{
$this->sortByInterestingVideos();
$this->sortByDifferenceBetweenCacheAndDataCenter();
$this->_calculateAndSaveInAnswerString();
}
public function solution5()
{
/**
* Under Development
*
* take interesting videos and put in every possible cache where users are interested
*/
}
public function solution6()
{
$this->removeUnimportantVideos();
$this->solution4();
}
public function removeUnimportantVideos()
{
foreach ($this->videos as $video_id => $video) {
if ($video->size > Cache::$capacity || $video->number_of_requests == 0) {
unset($this->videos[$video_id]);
}
}
}
public function sortByInterestingVideos()
{
uasort($this->videos, function ($v1, $v2) {
$interesting_1 = $v1->number_of_requests / $v1->size * $v1->interested_endpoints_number;
$interesting_2 = $v2->number_of_requests / $v2->size * $v2->interested_endpoints_number;
return $interesting_1 < $interesting_2;
});
}
public function sortByRequestCounts()
{
foreach ($this->endpoints as $endpoint_id => $endpoint) {
arsort($endpoint->statistics);
$this->endpoints[$endpoint_id] = $endpoint;
}
}
public function sortByTotalRequests()
{
uasort($this->endpoints, function ($e1, $e2) {
return $e1->number_of_total_requests < $e2->number_of_total_requests;
});
}
public function sortByLatencyToDataCenter()
{
uasort($this->endpoints, function ($e1, $e2) {
return $e1->latency_to_datacenter < $e2->latency_to_datacenter;
});
}
public function sortByDifferenceBetweenCacheAndDataCenter()
{
uasort($this->endpoints, function ($e1, $e2) {
$dif_e1 = $e1->latency_to_datacenter - end($e1->latency_to_cache);
$dif_e2 = $e2->latency_to_datacenter - end($e2->latency_to_cache);
return $dif_e1 < $dif_e2;
});
}
public function sortByVideoRequestCounts()
{
uasort($this->videos, function ($v1, $v2) {
return $v1->number_of_requests < $v2->number_of_requests;
});
}
private function _calculateAndSaveInAnswerString()
{
$this->sortByRequestCounts();
foreach ($this->endpoints as $endpoint) {
foreach ($endpoint->statistics as $video_id => $request_amount) {
$video_size = $this->videos[$video_id]->size;
// If our video is bigger then cache capacity we don't care for such video
/*if ($video_size > Cache::$capacity) {
//unset($this->videos[$video_id]);
continue;
}*/
// if the video is to another cache from this endpoint then skip
// Pass video_id and all the neighbour caches
if ($this->_videoIsOnAnotherCache($video_id, $endpoint->latency_to_cache)) {
continue;
}
// Now if the video is good then store it in the available cache server. PS this cache data are sorted
foreach ($endpoint->latency_to_cache as $cache_id => $latency) {
if ($this->left_spaces_to_each_cache[$cache_id] >= $video_size) {
$this->left_spaces_to_each_cache[$cache_id] -= $video_size;
$this->videos_at_cache[$cache_id][$video_id] = $video_id;
}
}
}
}
$this->_saveInAnswerString($this->videos_at_cache);
}
private function _saveInAnswerString($videos_at_cache)
{
$str = (string) count($videos_at_cache) . "\n";
foreach ($videos_at_cache as $cache_id => $video_ids) {
$str .= $cache_id . " ";
$tmp_str = implode($video_ids, ' ');
$str .= $tmp_str . "\n";
}
$this->answer_string = $str;
}
private function _videoIsOnAnotherCache($video_id, $latency_to_caches){
$videos_at_cache = $this->videos_at_cache;
foreach ($latency_to_caches as $cache_id => $latency) {
if (isset($videos_at_cache[$cache_id]) && isset($videos_at_cache[$cache_id][$video_id])) {
return true;
}
}
return false;
}
}