1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.util;
25
26 import java.util.*;
27 import java.awt.*;
28
29 public class Profile {
30 protected Hashtable table = new Hashtable();
31 protected static final double TINY = 1.0E-8;
32
33 class Data { double cost = 0.0;
34 int reject = 0;
35 int accept = 0;
36 };
37
38 public Profile() {
39 }
40 public double p(double x) {
41 String index = Integer.toString((int)x);
42 Data data = (Data)table.get(index);
43 if ( data == null ) {
44 data = new Data();
45 table.put(index,data);
46 }
47 return (data.reject + data.accept == 0)
48 ? 1.0
49 : 1.0*data.accept/((double)(data.reject + data.accept));
50 }
51 public double c(double x) {
52 String index = Integer.toString((int)x);
53 Data data = (Data)table.get(index);
54 if ( data == null ) {
55 data = new Data();
56 table.put(index,data);
57 }
58 return data.cost;
59 }
60 public double e(double x) {
61 double p = p(x);
62 return (p < TINY) ? Double.MAX_VALUE : c(x)/p;
63 }
64 public void update(double x, double c) {
65 String index = Integer.toString((int)x);
66 Data data = (Data)table.get(index);
67 data.cost = (data.accept*data.cost+c)/(data.accept+1);
68 data.accept += 1;
69 }
70 public void update(double x) {
71 String index = Integer.toString((int)x);
72 Data data = (Data)table.get(index);
73 data.reject += 1;
74 }
75 public String toString() {
76 Data data;
77 String key;
78 Enumeration keys = table.keys();
79 String s = "";
80 while( keys.hasMoreElements() ) {
81 key = (String)keys.nextElement();
82 data = (Data)table.get(key);
83 if ( Math.abs(data.cost-0.0) > TINY )
84 s += "Time:" + key + "\t" +
85 "Accept:" + data.accept + "\t" +
86 "Reject:" + data.reject + "\t" +
87 "Cost:" + Misc.decimalPlaces(data.cost,2) + "\n\t\t";
88 }
89 return s;
90 }
91
92 public static void main(String[] args) {
93 Profile pr = new Profile();
94 double t = 4.2;
95 double c = 125;
96 double e = pr.e(t);
97 System.out.println(e + " " + pr);
98 pr.update(t,c);
99 e = pr.e(t);
100 System.out.println(e + " " + pr);
101 t = 4.8;
102 c = 300;
103 e = pr.e(t);
104 System.out.println(e + " " + pr);
105 pr.update(t,c);
106 e = pr.e(t);
107 System.out.println(e + " " + pr);
108 System.out.println("-----------");
109 pr.update(t);
110 e = pr.e(t);
111 System.out.println(e + " " + pr);
112 pr.update(t);
113 e = pr.e(t);
114 System.out.println(e + " " + pr);
115 pr.update(t);
116 e = pr.e(t);
117 System.out.println(e + " " + pr);
118 pr.update(t);
119 e = pr.e(t);
120 System.out.println(e + " " + pr);
121 System.out.println("-----------");
122 pr = new Profile();
123 e = pr.e(t);
124 System.out.println(e + " " + pr);
125 pr.update(t);
126 e = pr.e(t);
127 System.out.println(e + " " + pr);
128 pr.update(t);
129 e = pr.e(t);
130 System.out.println(e + " " + pr);
131 pr.update(t);
132 e = pr.e(t);
133 System.out.println(e + " " + pr);
134 pr.update(t,c);
135 e = pr.e(t);
136 System.out.println(e + " " + pr);
137 pr.update(t,c);
138 e = pr.e(t);
139 System.out.println(e + " " + pr);
140
141 }
142 }