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