start of new file
[IRC.git] / Robust / src / Benchmarks / PERT / Tag / Estimator.java
1 public class Estimator {
2     flag estimate;
3     flag prob;
4
5     int stages;
6     int time;
7     double variance;
8     double[] probtable;
9
10     boolean partial;
11
12     public Estimator(int stages) {
13         this.stages = stages;
14         this.time = 0;
15         this.variance = 0;
16
17         this.probtable = new double[31];
18         int i = 0;
19         this.probtable[i++] = 0.5000;
20         this.probtable[i++] = 0.5398;
21         this.probtable[i++] = 0.5793;
22         this.probtable[i++] = 0.6179;
23         this.probtable[i++] = 0.6554;
24         this.probtable[i++] = 0.6915;
25         this.probtable[i++] = 0.7257;
26         this.probtable[i++] = 0.7580;
27         this.probtable[i++] = 0.7881;
28         this.probtable[i++] = 0.8159;
29         this.probtable[i++] = 0.8413;
30         this.probtable[i++] = 0.8643;
31         this.probtable[i++] = 0.8849;
32         this.probtable[i++] = 0.9032;
33         this.probtable[i++] = 0.9192;
34         this.probtable[i++] = 0.9332;
35         this.probtable[i++] = 0.9452;
36         this.probtable[i++] = 0.9554;
37         this.probtable[i++] = 0.9641;
38         this.probtable[i++] = 0.9713;
39         this.probtable[i++] = 0.9772;
40         this.probtable[i++] = 0.9821;
41         this.probtable[i++] = 0.9861;
42         this.probtable[i++] = 0.9893;
43         this.probtable[i++] = 0.9918;
44         this.probtable[i++] = 0.9938;
45         this.probtable[i++] = 0.9953;
46         this.probtable[i++] = 0.9965;
47         this.probtable[i++] = 0.9974;
48         this.probtable[i++] = 0.9981;
49         this.probtable[i++] = 0.9987;
50
51         this.partial = false;
52     }
53
54     public boolean estimate(int time, double variance2, boolean fake) {
55         if(!fake) {
56             this.time += time;
57             this.variance += variance2;
58         } else {
59             this.partial = true;
60         }
61         --this.stages;
62         if(this.stages == 0) {
63             //System.printString("variance2: " + (int)(this.variance*100) + "(/100); ");
64             this.variance = Math.sqrt(this.variance);
65             //System.printString("variance: " + (int)(this.variance*100) + "(/100)\n");
66             return true;
67         }
68         return false;
69     }
70
71     public double getProbability(int x, int y) {
72         int l = x;
73         int r = y;
74         if(x > y) {
75             l = y;
76             r = x;
77         }
78
79         double prob = prob(r) - prob(l);
80         return prob;
81     }
82
83     private double prob(int s) {
84         int tmp = (int)((s - this.time) * 10 / this.variance);
85         //System.printString(tmp + "\n");
86         int abs = (int)Math.abs(tmp);
87         double prob = 0;
88         if(abs > this.probtable.length - 1) {
89             prob = 1;
90         } else {
91             prob = this.probtable[abs];
92         }
93         if(tmp < 0) {
94             return 1.0 - prob;
95         } else {
96             return prob;
97         }
98     }
99
100     public int getTime() {
101         return this.time;
102     }
103
104     public double getVariance() {
105         return this.variance;
106     }
107
108     public boolean isPartial() {
109         return this.partial;
110     }
111 }