📄 simulate.java
字号:
import java.io.*;
public class Simulate{ public static void main(String[] args) { double lambda_a = 19.0; double lambda_s = 20.0; double t_end = 7.0; //Sim1(lambda_a, lambda_s, t_end); SimMult(lambda_a, lambda_s, t_end,3); } // IO definition
private static BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut =
new PrintWriter(System.out, true);
private static PrintWriter stdErr =
new PrintWriter(System.err, true);
public static void Sim1(double lambda_a, double lambda_s, double t_end) { MyRandom r = new MyRandom(12342); // sets up random number generator // r.nextErlang(3,20) // returns an Erlang-3 distributed random number with E(r) = 1/20 // r.nextExponential(20) // returns an Exponential distributed random number with E(r) = 1/20 // Start writing your solution for Task I here
Queue queue = new Queue();
boolean server_free = true;
final int ARRIVAL = 1;
final int COMPLETION = 2;
int action_code = ARRIVAL;
double clock = 0.0;
double tna = 0.0;
double tnc = Double.POSITIVE_INFINITY;
double last_completion = 0;
double total_free = 0.0;
int line_num = 1;
int max_queue_length = 0;
int tt_arrival = 0;
int tt_morethan6m = 0;
double tt_waitingtime = 0.0;
stdOut.println("# event ql clock\n-----------------------------------");
while (tna < t_end) {
if (tna <= tnc) {
action_code = ARRIVAL;
clock = tna;
} else {
action_code = COMPLETION;
clock = tnc;
}
if (action_code == ARRIVAL) {
tna = clock + r.nextExponential(lambda_a);
if (server_free) {
stdOut.println("Server is free for: " + (clock - last_completion) + " tt server free yet: " + (total_free +=
(clock - last_completion)));
server_free = false;
stdOut.println(line_num++ + " " + "Arivl " + 0 + " " + clock);
stdOut.println("Guy is serverd, has waited:0.0 ");
tnc = clock + r.nextErlang(3,lambda_s);
} else {
if (queue.length() > max_queue_length) {
max_queue_length = queue.length();
}
queue.put(clock);
stdOut.println(line_num++ + " " + "Arivl " + queue.length() + " " + clock);
}
tt_arrival++;
} else if (action_code == COMPLETION) {
last_completion = clock;
if (queue.length() > max_queue_length) {
max_queue_length = queue.length();
}
stdOut.println(line_num++ + " " + "Compl " + queue.length() + " " + clock);
if (!queue.isEmpty()) {
double waiting_time = clock - queue.get();
tt_waitingtime += waiting_time;
if (waiting_time > 0.1) tt_morethan6m++;
stdOut.println("Guy is served, has waited: " + waiting_time);
tnc = clock + r.nextErlang(3,lambda_s);
} else {
server_free = true;
tnc = Double.POSITIVE_INFINITY;
}
} else {
tna = Double.POSITIVE_INFINITY;
}
}
while (!queue.isEmpty()) {
clock = tnc;
double waiting_time = clock - queue.get();
tt_waitingtime += waiting_time;
if (waiting_time > 0.1) tt_morethan6m++;
stdOut.println(line_num++ + " " + "Compl " + queue.length() + " " + clock);
if (!queue.isEmpty()) {
stdOut.println("Guy is served, has waited: " + waiting_time);
}
tnc = clock + r.nextErlang(3,lambda_s);
}
stdOut.println("Average waiting time: " + tt_waitingtime / (tt_arrival - queue.length()));
stdOut.println("Server free fraction: " + total_free / clock);
stdOut.println("Maximum queue length: " + max_queue_length);
stdOut.println("tt_arrivals : " + tt_arrival);
stdOut.println("tt_wait_time : " + tt_waitingtime);
stdOut.println("tt_served : " + (tt_arrival - queue.length()));
stdOut.println("tt wait>6min : " + tt_morethan6m);
stdOut.println("fraction > 6min : " + ((double)(tt_morethan6m) / (tt_arrival - queue.length())) * 100 + "%" );
}
//=======================================================================================
public static void SimMult(double lambda_a, double lambda_s,
double t_end, int nserv){
MyRandom r = new MyRandom(12342); // sets up random number generator
// r.nextErlang(3,20)
// returns an Erlang-3 distributed random number with E(r) = 1/20
// r.nextExponential(20)
// returns an Exponential distributed random number with E(r) = 1/20
// Start writing your solution for Task I here
Queue queue = new Queue();
int server_free_num = nserv;
final int ARRIVAL = 1;
final int COMPLETION = 2;
int action_code = ARRIVAL;
double clock = 0.0;
double tna = 0.0;
double tnc = Double.POSITIVE_INFINITY;
double last_completion = 0;
double total_free = 0.0;
int line_num = 0;
int max_queue_length = 0;
int tt_arrival = 0;
int tt_morethan6m = 0;
double tt_waitingtime = 0.0;
double all_finish = 0.0;
stdOut.println("# event ql clock\n-----------------------------------");
while (tna < t_end) {
if (tna <= tnc) {
action_code = ARRIVAL;
clock = tna;
} else {
action_code = COMPLETION;
clock = tnc;
}
if (action_code == ARRIVAL) {
tna = clock + r.nextExponential(lambda_a);
if (server_free_num > 0) {
stdOut.println(server_free_num + " Server(s) are free for: " + (clock - last_completion) + " tt server free yet: " + (total_free +=
(clock - last_completion)));
server_free_num--;
stdOut.println(line_num++ + " " + "Arivl " + 0 + " " + clock);
//stdOut.println("Guy is serverd, has waited:0.0 ");
tnc = clock + r.nextErlang(3,lambda_s);
} else {
if (queue.length() > max_queue_length) {
max_queue_length = queue.length();
}
queue.put(clock);
stdOut.println(line_num++ + " " + "Arivl " + queue.length() + " " + clock);
}
tt_arrival++;
} else if (action_code == COMPLETION) {
//last_completion = clock;
if (queue.length() > max_queue_length) {
max_queue_length = queue.length();
}
if (server_free_num > 0) {
stdOut.println(server_free_num + " Server(s) are free for: " + (clock - last_completion) + " tt server free yet: " + (total_free +=
(clock - last_completion)*server_free_num));
last_completion = clock;
}
stdOut.println(line_num++ + " " + "Compl " + queue.length() + " " + clock);
if (!queue.isEmpty()) {
double waiting_time = clock - queue.get();
tt_waitingtime += waiting_time;
if (waiting_time > 0.1) tt_morethan6m++;
stdOut.println("Guy is served, has waited: " + waiting_time);
tnc = clock + r.nextErlang(3,lambda_s);
} else {
server_free_num++;
tnc = Double.POSITIVE_INFINITY;
}
} else {
tna = Double.POSITIVE_INFINITY;
}
}
while (!queue.isEmpty()) {
clock = tnc;
double waiting_time = clock - queue.get();
tt_waitingtime += waiting_time;
if (waiting_time > 0.1) tt_morethan6m++;
stdOut.println(line_num++ + " " + "Compl " + queue.length() + " " + clock);
if (!queue.isEmpty()) {
stdOut.println("Guy is served, has waited: " + waiting_time);
}
tnc = clock + r.nextErlang(3,lambda_s);
}
stdOut.println("Average waiting time: " + tt_waitingtime / (tt_arrival - queue.length()));
stdOut.println("Server free fraction: " + total_free / clock);
stdOut.println("Maximum queue length: " + max_queue_length);
stdOut.println("tt_arrivals : " + tt_arrival);
stdOut.println("tt_wait_time : " + tt_waitingtime);
stdOut.println("tt_served : " + (tt_arrival - queue.length()));
stdOut.println("tt wait>6min : " + tt_morethan6m);
stdOut.println("fraction > 6min : " + ((double)(tt_morethan6m) / (tt_arrival - queue.length())) * 100 + "%" );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -