-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMorrisAlgorithm.java
More file actions
240 lines (196 loc) · 7.39 KB
/
MorrisAlgorithm.java
File metadata and controls
240 lines (196 loc) · 7.39 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
package com.fordham.cisc.project;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MorrisAlgorithm {
static Logger LOGGER = Logger.getLogger("GenEvents");
static final String fileName = "events.txt";
static Random random =new Random();
public static void main(String[] args) {
// TODO Auto-generated method stub
// Validate the Input Arguments
validateInputArgs(args);
String fileName = args[0];
double epsilon = Double.valueOf( args[1] );
double delta = Double.valueOf( args[2] );
double reportTime = Double.valueOf( args[3] );
Stream<String> fileStream = readFile();
List<String> eventList = fileStream.collect(Collectors.toList());
double timer = 0;
morris( eventList );
closeStream( fileStream );
}
/**
* This method implements the morris algorithm
* @param predictedN
* @return the updated predictedN counter
*/
public static void morris(List<String> eventList){
double timer = 0;
int predictedN = 0;
int predictedCount = 0;
for(int actualN=0; actualN < eventList.size(); actualN ++){
// System.out.println("######################## Counter == "+actualN);
timer += Double.valueOf(eventList.get(actualN));
// updating the predicted N for the first iteration
if (actualN == 0)
predictedN += 1;
// end
// Getting 1/2^x probability to update the predicted counter
// boolean probibility = random.nextInt((int) Math.pow(2, predictedN)) == 0;
// Another way of getting the probability
boolean probibility = ThreadLocalRandom.current().nextInt( (int) Math.pow(2, predictedN) ) == 0;
//System.out.println("********** Probability == " + probibility);
// update the predicted counter with the probability (1/2^x)
if (probibility)
predictedN += 1;
predictedCount = ((int) Math.pow(2, predictedN)) - 1;
// Outputing the result at the report time, setting up the timer back to "0" to get the next report time.
if(timer >= 1.0){
System.out.println("Output result Actual "+ actualN );
System.out.println("Output result Morris = "+ predictedCount );
System.out.println(" ************************************************** ");
timer = 0;
}
}
// System.out.println("********* " + timer);
System.out.println("Output result Actual "+ eventList.size() );
System.out.println("Output result Morris = "+ predictedCount );
}
/**
* This method implements the morris algorithm
* @param predictedN
* @return the updated predictedN counter
*/
public static void morrisPlus(List<String> eventList, double epsilon, double delta){
double timer = 0;
int predictedN = 0;
int predictedCount = 0;
// This is "S" independent copy for Morris+ :- S > (1 / 2^(e^2)*delta) = O(1/delta)
int noOfCopy = (int) Math.ceil( 1 / (2 * (Math.pow(epsilon, 2.0) * delta)));
for(int actualN=0; actualN < eventList.size(); actualN ++){
// System.out.println("######################## Counter == "+actualN);
timer += Double.valueOf(eventList.get(actualN));
// updating the predicted N for the first iteration
if (actualN == 0)
predictedN += 1;
// end
// Getting 1/2^x probability to update the predicted counter
// boolean probibility = random.nextInt((int) Math.pow(2, predictedN)) == 0;
// Another way of getting the probability
boolean probibility = ThreadLocalRandom.current().nextInt( (int) Math.pow(2, predictedN) ) == 0;
//System.out.println("********** Probability == " + probibility);
// update the predicted counter with the probability (1/2^x)
if (probibility)
predictedN += 1;
predictedCount = ((int) Math.pow(2, predictedN)) - 1;
// Outputing the result at the report time, setting up the timer back to "0" to get the next report time.
if(timer >= 1.0){
System.out.println("Output result Actual "+ actualN );
System.out.println("Output result Morris = "+ predictedCount );
System.out.println(" ************************************************** ");
timer = 0;
}
}
// System.out.println("********* " + timer);
System.out.println("Output result Actual "+ eventList.size() );
System.out.println("Output result Morris = "+ predictedCount );
}
public static Stream<String> readFile(){
Stream<String> stream = null;
try {
stream = Files.lines(Paths.get(fileName));
/*stream.forEach( (string) -> {
System.out.println("== " + string);
});*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stream;
}
public static void validateInputArgs(String[] args){
if(!(args.length == 4) ){
errorMessage("Please pass the required argument to the program, in the following order - (fileName ,epsilpon, delta , How_Often) ", true, 0);
}
String fileName = "";
try {
fileName = args[0];
Path filePath = Paths.get(fileName);
boolean isFileExist = Files.exists( filePath );
if(!isFileExist){
errorMessage("File doesn't exist", true, 0);
}
} catch (Exception e) {
// TODO Auto-generated catch block
errorMessage("Error Occored while accessing the file", true, null);
e.printStackTrace();
// Program terminating message
errorMessage(null, true, 0);
}
double epsilon = 0;
try {
epsilon = Double.valueOf(args[1]);
if(!(0 < epsilon && epsilon <= 1.0)){
errorMessage("Epsilon (Allowable Error) has to be a floating point number greater than 0 and less than 1", true, 0);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
errorMessage("Epsilon (Allowable Error) has to be a floating point number greater than 0 and less than 1", true, 0);
// e.printStackTrace();
// Program terminating message
//errorMessage("", true, 0);
}
double delta = 0;
try {
delta = Double.valueOf(args[2]);
if(!(0 < delta && delta <= 1.0)){
errorMessage("Delta (Failure Probability) has to be a floating point number greater than 0 and less than 1", true, 0);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
errorMessage("Delta (Failure Probability) has to be a floating point number greater than 0 and less than 1", true, 0);
// e.printStackTrace();
// Program terminating message
//errorMessage("", true, 0);
}
double reportTime = 0;
try {
reportTime = Double.valueOf(args[3]);
if(0 >= reportTime){
errorMessage("Report time (How_Often) has to be a positve number", true, 0);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
errorMessage("Report time (Hoe_Often) has to be a positve number", true, 0);
// e.printStackTrace();
// Program terminating message
//errorMessage("", true, 0);
}
}
public static void errorMessage(String msg, boolean isError, Integer exitCode) {
if (msg != null && !(msg.equals(""))) {
if (isError)
LOGGER.log(Level.SEVERE, msg);
else
LOGGER.log(Level.WARNING, msg);
}
if (exitCode != null) {
LOGGER.log(Level.SEVERE, "******* Terminating program ******* ");
System.exit(0);
}
}
public static void closeStream(Stream<String> stream){
stream.close();
}
}