-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimDemo1.cpp
More file actions
110 lines (88 loc) · 2.4 KB
/
simDemo1.cpp
File metadata and controls
110 lines (88 loc) · 2.4 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
/*
* Simulacny model vleku
*
*/
#include "simulator.h"
#include <iostream>
const double jedna_cesta = 4.0;
Store Kotvy("Sklad kotev", 40);
Facility Stanoviste("Stavoviste");
Histogram dobaCesty("Doba stravena lyzarem u vleku", 0, 1, 15);
Histogram pocetPokusu("Pocet pokusu nastoupit", 1, 1, 10);
Statistics cekaniZavodniku("Doba cekani zavodniku");
// proces volne ujizdejici kotvy
class KotvaBezi : public Process {
public:
KotvaBezi(int t) : Process() { T=t; } // T=1 - jedna cesta, T=2 - cesta tam a zpet
void Behavior() {
scheduleAt(Time() + jedna_cesta*T, SLOT(KotvaBezi::VratKotvu) );
}
void VratKotvu() {
Leave(Kotvy, 1);
}
int T;
};
class Lyzar : public Process {
public:
Lyzar(int pri) : Process(pri) {}
void Behavior() {
cekaniTime = Time();
Seize(Stanoviste, SLOT(Lyzar::StanovisteSeized));
}
void StanovisteSeized() {
cekaniZavodniku.Record(Time()-cekaniTime);
//opak:
Enter(Kotvy, SLOT(Lyzar::EnterKotvy), 1);
}
void EnterKotvy()
{
scheduleAt(Time() + Exponential(0.5), SLOT(Lyzar::WaitContinue));
}
void WaitContinue(){
pokusu++;
if (Random()<=0.1) {
// nezdareny start, kotva jede sama dve cesty (nahoru, dolu)
(new KotvaBezi(2))->scheduleAt(Time());
//goto opak
StanovisteSeized();
return;
}
pocetPokusu.Sample(pokusu);
pokusu = 0;
Release(Stanoviste);
scheduleAt(Time() + jedna_cesta, SLOT(Lyzar::KotvaDole) );
}
void KotvaDole(){
dobaCesty.Sample(Time() - cekaniTime);
(new KotvaBezi(1))->scheduleAt(Time()); // nahore opousti kotvu a ta jede sama dolu do skladu
}
private:
double cekaniTime = 0;
int pokusu = 0;
};
// generuje dva typy lyzaru. Obecny predpis
class Generator : public Event {
public:
Generator(double interv, int pri) : Event() {
Interval = interv;
Pri = pri;
}
void Behavior() {
(new Lyzar(Pri))->scheduleAt(Time());
scheduleAt(Time() + Exponential(Interval));
}
double Interval;
int Pri;
};
int main()
{
InitTime(0, 1000);
(new Generator(1,0))->scheduleAt(Time());
(new Generator(10,1))->scheduleAt(Time());
Run();
Kotvy.Output();
Stanoviste.Output();
dobaCesty.Output();
pocetPokusu.Output();
cekaniZavodniku.Output();
}