-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.cpp
198 lines (168 loc) · 6.63 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
188
189
190
191
192
193
194
195
196
197
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: cancian
*
* Created on 21 de Junho de 2018, 12:47
*/
#include <cstdlib>
#include <iostream>
#include "Simulator.h"
#include "Listener.h"
#include "Traits.h"
// Simulator objects
#include "Sampler_if.h" // old MMC
#include "Fitter_if.h"
// Tools and Utils
#include "Collector_if.h"
#include "Statistics_if.h"
#include "Integrator_if.h"
#include "HypothesisTester_if.h"
#include "ProbDistrib.h"
// Model Components
#include "Create.h"
#include "Delay.h"
#include "Dispose.h"
#include "Seize.h"
#include "Release.h"
using namespace std;
void traceHandler(TraceEvent e) {
std::cout << e.getText() << std::endl;
}
void traceSimulationHandler(TraceSimulationEvent e) {
std::cout << e.getText() << std::endl;
}
void buildSimpleCreateDelayDisposeModel(Model* model) {
// traces handle simulation events to output them
model->addTraceListener(&traceHandler);
model->addTraceReportListener(&traceHandler);
model->addTraceSimulationListener(&traceSimulationHandler);
// create and insert model components to the model
Create* create1 = new Create(model);
create1->setTimeBetweenCreationsExpression("1.5");
create1->setTimeUnit(Util::TimeUnit::TU_minute);
Delay* delay1 = new Delay(model);
delay1->setDelayExpression("30");
Dispose* dispose1 = new Dispose(model);
List<ModelComponent*>* components = model->getComponents();
components->insert(create1);
components->insert(delay1);
components->insert(dispose1);
// connect model components to create a "workflow"
// should always start from a SourceModelComponent and end at a SinkModelComponent (it will be checked)
create1->getNextComponents()->insert(delay1);
delay1->getNextComponents()->insert(dispose1);
}
void buildSimpleCreateSeizeDelayReleaseDisposeModel(Model* model) {
// traces handle simulation events to output them
model->addTraceListener(&traceHandler);
model->addTraceReportListener(&traceHandler);
model->addTraceSimulationListener(&traceSimulationHandler);
// create and insert model components to the model
Create* create1 = new Create(model);
create1->setTimeBetweenCreationsExpression("1.5");
create1->setTimeUnit(Util::TimeUnit::TU_minute);
Seize* seize1 = new Seize(model);
seize1->setResourceName("Máquina 1");
Delay* delay1 = new Delay(model);
delay1->setDelayExpression("30");
Release* release1 = new Release(model);
Dispose* dispose1 = new Dispose(model);
List<ModelComponent*>* components = model->getComponents();
components->insert(create1);
components->insert(seize1);
components->insert(delay1);
components->insert(release1);
components->insert(dispose1);
// connect model components to create a "workflow"
// should always start from a SourceModelComponent and end at a SinkModelComponent (it will be checked)
create1->getNextComponents()->insert(seize1);
seize1->getNextComponents()->insert(delay1);
delay1->getNextComponents()->insert(release1);
release1->getNextComponents()->insert(dispose1);
}
void buildModel(Model* model) {
// change next command to build different models
//buildSimpleCreateDelayDisposeModel(model);
buildSimpleCreateSeizeDelayReleaseDisposeModel(model);
}
void buildSimulationSystem() {
Simulator* simulator = new Simulator();
Model* model = new Model(simulator);
buildModel(model);
simulator->getModels()->insert(model);
if (model->check()) {
model->startSimulation();
model->showReports();
}
}
void testStudentSoftwareDevelopments() {
Simulator* simulator = new Simulator();
Sampler_if* mmc = simulator->getSampler(); // Sampler is the new MMC
Collector_if* collector = new Traits<Collector_if>::Implementation();
collector->setDataFilename("./datafile.txt");
// generate a datafile with a thousand values that should be normal distributed == NORM(5000,350)
double value;
for (unsigned int i = 0; i < 1e3; i++) {
value = mmc->sampleNormal(5000, 350);
collector->addValue(value);
}
// generate statistics about that datafile
Statistics_if* statistics = new Traits<Statistics_if>::Implementation();
statistics->setDataFilename(collector->getDataFilename());
double statVal;
statVal = statistics->numElements();
statVal = statistics->average();
statVal = statistics->stddeviation();
statVal = statistics->halfWidthConfidenceInterval(0.05);
statVal = statistics->quartil(2);
statVal = statistics->mediane();
unsigned int numclasses = statistics->histogramNumClasses();
for (unsigned int i = 0; i < numclasses; i++) {
statVal = statistics->histogramClassFrequency(i);
}
// fit datafile to different probability distributions
Fitter_if* fitter = simulator->getFitter();
fitter->setDataFilename(collector->getDataFilename());
double sqrerror, p1, p2, p3, p4;
std::string distribName;
bool res;
res = fitter->isNormalDistributed(0.95);
fitter->fitUniform(&sqrerror, &p1, &p2);
fitter->fitTriangular(&sqrerror, &p1, &p2, &p3);
fitter->fitNormal(&sqrerror, &p1, &p2);
fitter->fitAll(&sqrerror, &distribName);
// get some values from the PDF (Probability Density Functions)
value = ProbDistrib::uniform(0.25, 0.0, 1.0);
value = ProbDistrib::uniform(0.5, 0.0, 1.0); // should return the same value than above
value = ProbDistrib::uniform(0.75, 0.0, 1.0); // should return the same value than above
value = ProbDistrib::normal(10.0, 50.0, 20.0);
value = ProbDistrib::normal(30.0, 50.0, 20.0); // should return a greater value than above
value = ProbDistrib::normal(50.0, 50.0, 20.0); // should return a greater value than above
// calculate some integrals just for fun
Integrator_if* integrator = new Traits<Integrator_if>::Implementation();
value = integrator->integrate(0.0, 0.4, &ProbDistrib::uniform, 0.0, 0.1); // should return 0.4
value = integrator->integrate(0.0, 100, &ProbDistrib::normal, 100, 10); // should return 0.5
// Test some hypothesis about the datafile
HypothesisTester_if* tester = new Traits<HypothesisTester_if>::Implementation();
tester->setDataFilename(collector->getDataFilename());
res = tester->testAverage(0.95, 5500, HypothesisTester_if::GREATER_THAN);
res = tester->testAverage(0.95, 4500, HypothesisTester_if::LESS_THAN);
res = tester->testAverage(0.95, 3000, HypothesisTester_if::EQUAL);
res = tester->testAverage(0.95, 5000, HypothesisTester_if::EQUAL);
res = tester->testAverage(0.95, 5100, HypothesisTester_if::DIFFERENT);
res = tester->testVariance(0.95, 350*350, HypothesisTester_if::EQUAL);
res = tester->testVariance(0.95, 350*350, HypothesisTester_if::LESS_THAN);
}
/*
*
*/
int main(int argc, char** argv) {
//buildSimulationSystem();
testStudentSoftwareDevelopments();
return 0;
}