-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
187 lines (164 loc) · 3.98 KB
/
main.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
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
#include <queue>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include "MetodoCongruente.h"
using namespace std;
#define HUGE_VAL 1000000000000000000000000000000000000000000000000000000.0
#define T_MAX 1000.0
static int ID = 0;
enum Server {
IDLE, BUSY
};
class Task {
public:
double sim_time = 0.0;
int ID;
Task() {
}
Task(double sim_time) {
this->sim_time = sim_time;
}
};
class Statistics {
private:
double arrivalA;
double arrivalB;
double arrivalTime;
double departureTime;
double requisitionInterval[1000000];
double firstArrival;
double lastDeparture;
double totalBusyTime;
int aux = 0;
MetodoCongruente crandA = MetodoCongruente(1103515245,12345,2147483648,23);
MetodoCongruente crandB = MetodoCongruente(1103515245,12345,2147483648,17);
public:
Statistics() {
}
void setfirstArrival(double value){
this->firstArrival = value;
}
void setlastDeparture(double value){
this->lastDeparture = value;
}
void setarrivalTime(double value){
this->arrivalTime = value;
}
void setarrivalA(double value){
this->arrivalA = value;
}
void setarrivalB(double value){
this->arrivalB = value;
}
void setdepartureTime(double value){
this->departureTime = value;
}
int getaux(){
return this->aux;
}
void update() {
requisitionInterval[aux] = arrivalB - arrivalA;
aux++;
arrivalA = 0.0;
arrivalB = 0.0;
//departureTime = 0.0;
}
//intervalo entre requisições
double intervalMean(){
int i = 0;
double j = 0;
while(i < aux){
j = requisitionInterval[i] + j;
i++;
}
return j/aux;
}
//tempo de medio de todas as task no sistema
float taskMean(queue<Task> queue){
double i = 0;
float j = 0;
int h = queue.size();
cout << "Tasks: " << h << endl;
while(i < h){
Task task = queue.front();
j = task.sim_time + j;
queue.pop();
i++;
}
this->totalBusyTime = j;
return j/h;
}
//tempo medio do servico
float serviceMeanTime(){
return totalBusyTime/(lastDeparture-firstArrival);
}
//gerador A
double randA_exp(double lambda){
double y = - 1 + ((double) this->crandA.GeraNumeroAleatorio() / (RAND_MAX)) + 1;
double x;
return x = -lambda*log(y);
}
//gerador B
double randB_exp(double lambda){
double y = - 1 + ((double) this->crandB.GeraNumeroAleatorio() / (RAND_MAX)) + 1;
double x;
return x = -lambda*log(y);
}
};
int main() {
Server s = IDLE;
Statistics stats;
double sim_time = 0.0;
double next_departure = HUGE_VAL;
double mean_arrival = 1.0;
double mean_processing = 0.9;
double next_arrival = stats.randA_exp(mean_arrival);
stats.setfirstArrival(next_arrival);
queue<Task> queue;
std::queue<Task> queueStatistics;
int numberOfTasks = 0;
// while (sim_time < T_MAX) {
while (numberOfTasks <= 10000) {
if (next_arrival < next_departure) {
sim_time = next_arrival;
stats.setarrivalA(next_arrival);
if (s == IDLE) {
s = BUSY;
stats.setarrivalTime(next_arrival);
next_departure = sim_time + stats.randB_exp(mean_processing);
stats.setdepartureTime(next_departure);
cout << "BUSY" << endl;
} else {
Task task;
task.ID = ID++;
task.sim_time = sim_time;
numberOfTasks++;
queue.push(task);
cout << "Entrando: " << queue.size() << endl;
}
next_arrival = sim_time + stats.randA_exp(mean_arrival);
stats.setarrivalB(next_arrival);
if(!queue.empty()) stats.update();
} else {
sim_time = next_departure;
if (queue.empty()) {
s = IDLE;
next_departure = HUGE_VAL;
} else {
Task task = queue.front();
task.sim_time = sim_time - task.sim_time;
queueStatistics.push(task);
queue.pop();
cout << "Saindo..." << endl;
next_departure = sim_time + stats.randB_exp(mean_processing);
}
}
}
stats.setlastDeparture(sim_time);
cout << "1 - Intervalo medio de Tasks no sistema: " << stats.taskMean(queueStatistics) << endl;
cout << "2 - Intervalo medio de requisicoes: " << stats.intervalMean() << endl;
cout << "3 - Tempo médio de serviço: " << stats.serviceMeanTime() << endl;
}