-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
536 lines (488 loc) · 13.2 KB
/
Copy pathmain.cpp
File metadata and controls
536 lines (488 loc) · 13.2 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "ServerMachine.h"
#include "VirtualMachine.h"
#include "SMObject.h"
#include "VMObject.h"
#include "SortedArray.h"
#include <ctime>
#ifdef linux
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#endif
typedef long long ll;
using namespace std;
//服务器类型表
unordered_map<string, ServerMachine> serverMachines;
//三种不同类型的服务器(高核心/均衡/高内存)
vector<ServerMachine> type_SMs[3];
//虚拟机类型表
unordered_map<string, VirtualMachine> virtualMachines;
//当前购买了的服务器表
unordered_map<int, SMObject*> currentSM;
//当前购买了的三类不同的服务器(高核心/均衡/高内存)
//SortedArray<SMObject*> type_currentSMs[3];
//当前配置了的虚拟机表
unordered_map<int, VMObject*> currentVM;
//当前虚拟机表的列表
vector<VMObject*> currentVMList;
//当前的请求队列
vector<pair<int, string>> requestList;
//每日购买列表
map<string, int> dailyPurchase;
//每日迁移列表
vector<tuple<int, int, VM_NodeType> > dailyMigration;
//每日创建列表
vector<pair<int, VMObject*> > dailyCreation;
//当前的服务器ID号的最大值(虚假的id)
int max_sm_id = 0;
//真正的id
int id = 0;
//记录所有虚拟机最小的内存和最小的cpu占用
int min_VMcore = 1000000000;
int min_VMmemo = 1000000000;
int rd(int mod) //生成随机数函数
{
#ifdef WIN32
srand(time(0));
#endif
#ifdef linux
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_sec * 1000000 + tv.tv_usec / 1000000);
#endif
return rand() % mod;
}
void InitInput(ifstream* ifs)
{
int N; //服务器数量
*ifs >> N;
ifs->get();
string catcher;
string _model; //型号
int _core, _mCp, _hC, _dC;
for (int i = 0; i < N; ++i)
{
getline(*ifs, catcher);
int len = catcher.length();
catcher = catcher.substr(1, len - 2);
replace(catcher.begin(), catcher.end(), ',', ' ');
stringstream stm(catcher);
stm >> _model;
stm >> _core >> _mCp >> _hC >> _dC;
auto& sm = serverMachines[_model] = ServerMachine(_model, _core, _mCp, _hC, _dC);
switch (sm.GetMechType())
{
case MachineType::HighCore:
type_SMs[0].push_back(sm);
break;
case MachineType::Balance:
type_SMs[1].push_back(sm);
break;
case MachineType::HighMemory:
type_SMs[2].push_back(sm);
break;
}
}
for (int i = 0; i <= 2; ++i)
{
sort(type_SMs[i].begin(), type_SMs[i].end(), [](const ServerMachine& a, const ServerMachine& b)
-> bool
{
if (a.GetDailyCost() == b.GetDailyCost()) return a.GetHardwareCost() < a.GetHardwareCost();
return a.GetDailyCost() < b.GetDailyCost();
});
}
*ifs >> N; //虚拟机数量
ifs->get();
bool _tN;
for (int i = 0; i < N; ++i)
{
getline(*ifs, catcher);
int len = catcher.length();
catcher = catcher.substr(1, len - 2);
replace(catcher.begin(), catcher.end(), ',', ' ');
stringstream stm(catcher);
stm >> _model;
stm >> _core >> _mCp >> _tN;
virtualMachines[_model] = VirtualMachine(_model, _core, _mCp, _tN);
}
}
void InitInput()
{
int N; //服务器数量
cin >> N;
cin.get();
string catcher;
string _model; //型号
int _core, _mCp, _hC, _dC;
for (int i = 0; i < N; ++i)
{
getline(cin, catcher);
int len = catcher.length();
catcher = catcher.substr(1, len - 2);
replace(catcher.begin(), catcher.end(), ',', ' ');
stringstream stm(catcher);
stm >> _model;
stm >> _core >> _mCp >> _hC >> _dC;
serverMachines[_model] = ServerMachine(_model, _core, _mCp, _hC, _dC);
}
cin >> N; //虚拟机数量
cin.get();
bool _tN;
for (int i = 0; i < N; ++i)
{
getline(cin, catcher);
int len = catcher.length();
catcher = catcher.substr(1, len - 2);
replace(catcher.begin(), catcher.end(), ',', ' ');
stringstream stm(catcher);
stm >> _model;
stm >> _core >> _mCp >> _tN;
virtualMachines[_model] = VirtualMachine(_model, _core, _mCp, _tN);
if (_core < min_VMcore)
min_VMcore = _core;
if (_mCp < min_VMmemo)
min_VMmemo = _mCp;
}
}
//决定为当前的虚拟机购买哪一款服务器
ServerMachine& ChooseServer(const VirtualMachine& vm_property)
{
int vm_core = vm_property.GetCore();
int vm_memory = vm_property.GetMemoryCapacity();
int type;
if (vm_core / vm_memory >= SMstandrad * 2)
{
type = 0;
}
else if (vm_memory / vm_core >= SMstandrad * 2)
{
type = 2;
}
else type = 1;
int maxsearch = type_SMs[type].size() >> 1;
//先在本类里寻找
while (maxsearch--)
{
int _rand = rd(type_SMs[type].size());
ServerMachine& serverMachine = type_SMs[type][_rand];
int sm_core = serverMachine.GetCore();
int sm_memory = serverMachine.GetMemoryCapacity();
if (!vm_property.IsTwoNode())
{
sm_core >>= 1;
sm_memory >>= 1;
}
if (vm_core <= sm_core && vm_memory <= sm_memory) return serverMachine;
}
//广撒网
while (true)
{
int _rand = rd(serverMachines.size());
auto iter = serverMachines.begin();
while (--_rand && iter != serverMachines.end()) ++iter;
if (iter == serverMachines.end()) iter = serverMachines.begin();
ServerMachine& serverMachine = iter->second;
int sm_core = serverMachine.GetCore();
int sm_memory = serverMachine.GetMemoryCapacity();
if (!vm_property.IsTwoNode())
{
sm_core >>= 1;
sm_memory >>= 1;
}
if (vm_core <= sm_core && vm_memory <= sm_memory) return serverMachine;
}
}
char GetStringFromNodeType(const VM_NodeType& nodeType)
{
switch (nodeType)
{
case VM_NodeType::A:
return 'A';
case VM_NodeType::B:
return 'B';
default:
return ' ';
}
}
//从当前的虚拟机表中随机洗出数个元素
void Shuffle(vector<VMObject*>& list, int num)
{
unordered_set<int> hasChose;
int totalnum = currentVMList.size();
while (num)
{
int trand = rd(totalnum);
auto elem = currentVMList[trand];
if (hasChose.find(elem->GetID()) != hasChose.end())
{
continue;
}
list.push_back(elem);
hasChose.insert(elem->GetID());
--num;
}
}
/*void DailyMigration(int maxMigrationNum)
{
if (!maxMigrationNum) return;
vector<VMObject*> list;
Shuffle(list, maxMigrationNum);
for (int i = 0; i < maxMigrationNum; ++i)
{
VMObject* cur = list[i];
const VirtualMachine& tproperty = cur->GetProperty();
MachineType type;
if (tproperty.GetCore() / tproperty.GetMemoryCapacity() >= SMstandrad * 4)
{
type = MachineType::HighCore;
}
else if (tproperty.GetMemoryCapacity() / tproperty.GetCore() >= SMstandrad * 4)
{
type = MachineType::HighMemory;
}
else
{
type = MachineType::Balance;
}
auto iter = currentSM.begin();
for (; iter != currentSM.end(); ++iter)
{
if (iter->second->GetProperty().GetMechType() == type)
{
if (iter->second->GetId() != cur->GetFatherID() && iter->second->AddChild(cur))
{
dailyMigration.push_back(tuple<int, int, VM_NodeType>(cur->GetID(), iter->second->GetTrueId(), cur->GetNodeType()));
}
}
}
}
}*/
void DailyMigration(int maxMigrationNum)
{
if (!maxMigrationNum) return;
bool isFull = false;
for (auto it = currentSM.begin();!isFull && it != currentSM.end(); it++)
{
if (it->second->GetACore() > min_VMcore && it->second->GetBCore() > min_VMcore && it->second->GetAMemo() > min_VMmemo && it->second->GetBMemo() > min_VMmemo)
{
for (auto it2 = currentVM.begin(); !isFull && it2 != currentVM.end(); it2++)
{
if (it2->second->GetFatherID() != it->second->GetId() && it->second->AbleToAddChild(it2->second))
{
dailyMigration.push_back(tuple<int, int, VM_NodeType>(it2->second->GetID(), it->second->GetId(), it2->second->GetNodeType()));
if (dailyMigration.size() >= maxMigrationNum)
break;
}
}
}
}
for (auto iter = dailyMigration.begin(); iter != dailyMigration.end(); ++iter)
{
int vm_id = get<0>(*iter);
int sm_id = get<1>(*iter);
currentSM[sm_id]->AddChild(currentVM[vm_id]);
}
}
void DailyOutput()
{
int Q = dailyPurchase.size();
printf("(purchase,%d)\n", Q);
for (auto iter = dailyPurchase.begin(); iter != dailyPurchase.end(); ++iter)
{
printf("(%s,%d)\n", iter->first.c_str(), iter->second);
for (auto it = currentSM.begin(); it != currentSM.end(); it++)
{
if (it->second->Gettype() == iter->first.c_str() && it->second->GetTrueId() == -1)
{
it->second->SetTrueId(id++);
}
}
}
int W = dailyMigration.size();
printf("(migration,%d)\n", W);
for (auto iter = dailyMigration.begin(); iter != dailyMigration.end(); ++iter)
{
int vm_id = get<0>(*iter);
int sm_id = get<1>(*iter);
VM_NodeType& nodeType = get<2>(*iter);
sm_id = currentSM[sm_id]->GetTrueId();
if (nodeType == VM_NodeType::Both)
{
printf("(%d,%d)\n", vm_id, sm_id);
}
else
{
char s_nodeType = GetStringFromNodeType(nodeType);
printf("(%d,%d,%c)\n", vm_id, sm_id, s_nodeType);
}
}
for (auto iter = dailyCreation.begin(); iter != dailyCreation.end(); ++iter)
{
int sm_id = iter->second->GetFatherTureID();
VM_NodeType nodeType = iter->second->GetNodeType();
if (nodeType == VM_NodeType::Both)
{
printf("(%d)\n", sm_id);
}
else
{
char s_nodeType = GetStringFromNodeType(nodeType);
printf("(%d,%c)\n", sm_id, s_nodeType);
}
}
}
void DailyClear()
{
requestList.clear();
dailyPurchase.clear();
dailyMigration.clear();
dailyCreation.clear();
}
int main()
{
//clock_t start = clock();
//ifstream in("C:/Users/14014/Desktop/Simianchuge/SiMianChuGe-main/training-1.txt");
InitInput();
int T; //天数
int TotalT; //总天数
int R; //每天的请求数
string catcher;
string _model;
string _opType;
int _VM_id;
cin >> T;
TotalT = T;
while (T--)
{
if (T == TotalT >> 1)
{
for (int i = 0; i <= 2; ++i)
{
sort(type_SMs[i].begin(), type_SMs[i].end(), [](const ServerMachine& a, const ServerMachine& b)
-> bool
{
if (a.GetHardwareCost() == b.GetDailyCost()) return a.GetDailyCost() < b.GetDailyCost();
return a.GetHardwareCost() < a.GetHardwareCost();
});
}
}
cin >> R;
cin.get();
//获取当日的创建申请
for (int i = 0; i < R; ++i)
{
getline(cin, catcher);
int len = catcher.length();
catcher = catcher.substr(1, len - 2);
replace(catcher.begin(), catcher.end(), ',', ' ');
stringstream stm(catcher);
stm >> _opType;
if (_opType == "add")
{
stm >> _model >> _VM_id;
//创建一台虚拟机
requestList.push_back({ _VM_id,_model });
}
else //del
{
stm >> _VM_id;
requestList.push_back({ _VM_id,"" });
}
}
//尝试迁移
//int maxMigrateNum = currentVM.size() /1000;
//DailyMigration(maxMigrateNum);
//为请求队列寻找目标服务器/删除目标虚拟机
for (auto iter = requestList.begin(); iter != requestList.end(); ++iter)
{
if (iter->second == "")
{
VMObject* vm = currentVM[iter->first];
vm->LeaveFather();
currentVM.erase(iter->first);
VMObject* theback = currentVMList.back();
currentVMList[vm->index] = theback;
theback->index = vm->index;
currentVMList.pop_back();
delete vm;
}
else
{
VirtualMachine vm_property = virtualMachines[iter->second];
VMObject* vmObject = new VMObject(vm_property, iter->first, nullptr);
currentVM[iter->first] = vmObject;
vmObject->index = currentVMList.size();
currentVMList.push_back(vmObject);
//改进部分:按照类型来选择服务器
auto iter2 = currentSM.begin();
auto it3 = currentSM.end();
for (; iter2 != currentSM.end(); ++iter2)
{
int vm_core = vm_property.GetCore();
int vm_memory = vm_property.GetMemoryCapacity();
MachineType type;
if (vm_core / vm_memory >= SMstandrad * 2)
{
type = MachineType::HighCore;
}
else if (vm_memory / vm_core >= SMstandrad * 2)
{
type = MachineType::HighMemory;
}
else type = MachineType::Balance;
if (type==iter2->second->GetProperty().GetMechType()&&iter2->second->AddChild(vmObject))
{
//找到了可以塞的下的服务器
dailyCreation.push_back(make_pair(iter2->first, vmObject));
break;
}
else
if(it3==currentSM.end()&&iter2->second->AbleToAddChild(vmObject))//保留随机选择结果
it3 = iter2;
}
//找不到可以塞的下的服务器
if (iter2 == currentSM.end())
{
if (it3 != currentSM.end())//还原随机选择结果
{
it3->second->AddChild(vmObject);
dailyCreation.push_back(make_pair(it3->first, vmObject));
continue;
}
ServerMachine& sm_property = ChooseServer(vm_property);
SMObject* smObject = new SMObject(sm_property, max_sm_id);
smObject->AddChild(vmObject);
dailyCreation.push_back(make_pair(max_sm_id, vmObject));
currentSM[max_sm_id] = smObject;
if (dailyPurchase.find(sm_property.GetModelType()) != dailyPurchase.end())
{
++dailyPurchase[sm_property.GetModelType()];
}
else
{
dailyPurchase[sm_property.GetModelType()] = 1;
}
++max_sm_id;
}
}
}
//输出
DailyOutput();
//清理垃圾
DailyClear();
}
fflush(stdout);
//clock_t finish = clock();
//cout << (finish - start) / 1000 << "s" << endl;
}