-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgoritmo_2.cpp
More file actions
299 lines (221 loc) · 8 KB
/
algoritmo_2.cpp
File metadata and controls
299 lines (221 loc) · 8 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// Utilización:
// Primer parámetro = ruta fichero entrada
// Segundo parámetro = ruta fichero salida
//
#include <iostream>
#include <list>
#include <set>
#include <fstream>
#include <map>
using namespace std;
const int MAX_SIZE=100000;
struct conection{
int id_endpoint;
int id_cache;
int id_video;
};
bool operator< (const conection& lhs, const conection& rhs){
return lhs.id_endpoint*1000000+lhs.id_cache+1000+lhs.id_video < rhs.id_endpoint*1000000+rhs.id_cache+1000+rhs.id_video;
}
struct cache{
int free_size;
set<int> videos;
};
//Nombres ficheros
char *nombre_fichero_entrada;
char *nombre_fichero_salida;
//Numeros de datos
int num_videos;
int num_caches;
int num_endpoints;
//latencias ordenadas
//key->saved_time*requests value->connections video
map<int,set<conection>> latencias_ordenadas;
//videos index->num value valor
int size_of_videos[MAX_SIZE];
//endpoints
//key->endpoint value->connected caches
map<int,set<int>> endpoints_cache;
//endpoints_videos
//key->endpoint value->videos visto por el endpoint en cuestion
//map<int,set<int>> endpoints_videos;
//requests
//Index-> Endpoint First->video Second->Num_Requests
map<int,int> numrequ_endp_videos[MAX_SIZE];
//Registro de los caches
cache cache_register[MAX_SIZE];
//Variables error
bool error_cargar=false;
bool error_ejecuacion=false;
bool error_guardar=false;
void cargarRequests(){
ifstream f;
int num_requests;
int size_caches;
char rubish[100];
f.open(nombre_fichero_entrada);
if (f.is_open()) {
cout << "Fichero de entrada abierto" << endl;
f >> num_videos >> num_endpoints >> num_requests >> num_caches >> size_caches;
f.getline(rubish, 1);
int rubish_int;
for (int i = 0; i < num_videos; ++i) {//cargar videos
f >> rubish_int;
}
f.getline(rubish, 1);
for (int i = 0; i < num_endpoints; ++i) {
int number_of_connected_caches;
int datacenter_latency;
f >> datacenter_latency;
f >> number_of_connected_caches;
f.getline(rubish, 1);
for (int j = 0; j < number_of_connected_caches; ++j) {
int lat;
int cach;
f >> cach;
f >> lat;
f.getline(rubish, 1);
}
}
for (int i = 0; i < num_requests; ++i) {
int vid;
int end;
int req;
f >> vid >> end >> req;
if(numrequ_endp_videos[end].find(vid)!=numrequ_endp_videos[end].end()){
(numrequ_endp_videos[end])[vid]=(numrequ_endp_videos[end])[vid]+req;
}else{
(numrequ_endp_videos[end])[vid]=req;
}
f.getline(rubish, 1);
}
cout << "Requests cargados" << endl;
}else{
cout << "Error: Fichero de entrada no abierto" <<endl;
error_cargar= true;
}
f.close();
}
void cargarDatos() {
ifstream f;
int num_requests;
int size_caches;
char rubish[100];
map<int,set<conection>> latemcias_edpoint_provisional;
f.open(nombre_fichero_entrada);
if (f.is_open()) {
cout << "Fichero de entrada abierto" << endl;
f >> num_videos >> num_endpoints >> num_requests >> num_caches >> size_caches;
f.getline(rubish, 1);
for (int i = 0; i < num_videos; ++i) {//cargar videos
f >> size_of_videos[i];
}
f.getline(rubish, 1);
for (int i=0; i < num_caches; ++i) {// Seteo caches
cache_register[i].free_size=size_caches;
}
for (int i = 0; i < num_endpoints; ++i) {
int number_of_connected_caches;
int datacenter_latency;
f >> datacenter_latency;
f >> number_of_connected_caches;
f.getline(rubish, 1);
for (int j = 0; j < number_of_connected_caches; ++j) {
int lat;
int cach;
f >> cach;
f >> lat;
f.getline(rubish, 1);
lat=lat-datacenter_latency;//Mayores latencias posiciones mas bajas
if(lat >= 0){//ese cache no tiene sentido anyadirlo
continue;
}
endpoints_cache[i].insert(cach);
conection insertar;
insertar.id_endpoint=i;
insertar.id_cache=cach;
//Inseto todos los video
map<int,int>::const_iterator iter_numrequ_endp_videos = numrequ_endp_videos[insertar.id_endpoint].begin();
while(iter_numrequ_endp_videos!=numrequ_endp_videos[insertar.id_endpoint].end()){
insertar.id_video=iter_numrequ_endp_videos->first;
latencias_ordenadas[lat*(iter_numrequ_endp_videos->second)].insert(insertar);
++iter_numrequ_endp_videos;
}
}
}
cout << "Datos cargados" << endl;
}else{
cout << "Error: Fichero de entrada no abierto" <<endl;
error_cargar= true;
}
f.close();
}
void distribuirVideos(){
cout << "Inicio distribucion videos" << endl;
map<int,set<conection>>::const_iterator iter_latencias_ordenadas = latencias_ordenadas.begin();
int num_endpoints_actual_processed=0;
int num_max_endpoints_to_process=latencias_ordenadas.size();
//Realizo primera vuelta
while (iter_latencias_ordenadas != latencias_ordenadas.end()){//recorro todas las latencias
cout << num_endpoints_actual_processed << "/" << num_max_endpoints_to_process <<endl;
++num_endpoints_actual_processed;
set<conection>::const_iterator iter_latencias_ordenadas_subset = iter_latencias_ordenadas->second.begin();
while (iter_latencias_ordenadas_subset != iter_latencias_ordenadas->second.end()) {//recorro todas las latencias del subgrupo
bool video_almacenado = false;
for (int j : endpoints_cache.find(iter_latencias_ordenadas_subset->id_endpoint)->second) {
if(cache_register[j].videos.find(iter_latencias_ordenadas_subset->id_video)!=cache_register[j].videos.end())
{
video_almacenado=true;
break;
}
}
if(!video_almacenado){
if (cache_register[iter_latencias_ordenadas_subset->id_cache].free_size>=size_of_videos[iter_latencias_ordenadas_subset->id_video]){
cache_register[iter_latencias_ordenadas_subset->id_cache].free_size-= size_of_videos[iter_latencias_ordenadas_subset->id_video];
cache_register[iter_latencias_ordenadas_subset->id_cache].videos.insert(iter_latencias_ordenadas_subset->id_video);
}
}
++iter_latencias_ordenadas_subset;
}
++iter_latencias_ordenadas;
}
cout << "Videos distribuidos" << endl;
}
void salida() {
ofstream f;
f.open(nombre_fichero_salida);
if (f.is_open()) {
cout << "Fichero salida creado" << endl;
int num_caches_usados=0;
for (int j = 0; j < num_caches ; ++j) {
if(cache_register[j].videos.size()>0) ++num_caches_usados;
}
f << num_caches_usados << endl;
for (int j = 0; j < num_caches ; ++j) {//cargar video
if(cache_register[j].videos.size()>0){
f << j ;
for (set<int>::iterator i = cache_register[j].videos.begin(); i != cache_register[j].videos.end(); ++i) {
f << " " << *i ;
}
f << endl;
}
}
}else{
cout << "Error: Al crear el fichero de salida" << endl;
error_guardar=true;
}
f.close();
}
int main(int argc, char *argv[]) {
if(argc!=3) cout << "Numero de argumentos incorrecto"<<endl;
nombre_fichero_entrada = argv[1];
nombre_fichero_salida = argv[2];
cargarRequests();
cargarDatos();
if(error_cargar) return -1;
distribuirVideos();
if(error_ejecuacion) return -1;
salida();
return 0;
}