View Javadoc

1   /*
2    * Created on Jan 15, 2005
3    *
4    * TODO To change the template for this generated file go to
5    * Window - Preferences - Java - Code Style - Code Templates
6    */
7   package net.sf.bddbddb.order;
8   
9   import java.util.Map;
10  
11  import net.sf.bddbddb.BDDInferenceRule;
12  import net.sf.bddbddb.FindBestDomainOrder;
13  import net.sf.bddbddb.order.EpisodeCollection.Episode;
14  
15  import org.jdom.Element;
16  
17  
18  /***
19   * Information about a particular trial.
20   * 
21   * @author John Whaley
22   * @version $Id: TrialInfo.java 551 2005-05-19 01:53:49Z cs343 $
23   */
24  public class TrialInfo implements Comparable {
25      /***
26       * Order tried.
27       */
28      public Order order;
29  
30      /***
31       * Cost of this trial.
32       */
33      public long cost;
34  
35      /***
36       * Collection that contains this trial.
37       */
38      public Episode episode;
39  
40      /***
41       * The predicted results for this trial.
42       */
43      public TrialPrediction pred;
44  
45      public long timestamp;
46      /***
47       * Construct a new TrialInfo.
48       * 
49       * @param o  order
50       * @param p predict value for this trial
51       * @param c  cost
52       */
53      public TrialInfo(Order o, TrialPrediction p, long c, Episode ep, long timestamp) {
54          this.order = o;
55          this.pred = p;
56          this.cost = c;
57          this.episode = ep;
58          this.timestamp = timestamp;
59      }
60  
61      /***
62       * @return Returns the trial collection that this is a member of.
63       */
64      public EpisodeCollection getCollection() {
65          return episode.getEpisodeCollection();
66      }
67  
68      /* (non-Javadoc)
69       * @see java.lang.Object#toString()
70       */
71      public String toString() {
72          return order + ": cost " + cost;
73      }
74  
75      /* (non-Javadoc)
76       * @see java.lang.Comparable#compareTo(java.lang.Object)
77       */
78      public int compareTo(Object arg0) {
79          return compareTo((TrialInfo) arg0);
80      }
81  
82      /***
83       * Comparison operator for TrialInfo objects.  Comparison is based on cost.
84       * If the cost is equal, we compare the order lexigraphically.
85       * 
86       * @param that  TrialInfo to compare to
87       * @return  -1, 0, or 1 if this TrialInfo is less than, equal to, or greater than the other
88       */
89      public int compareTo(TrialInfo that) {
90          if (this == that) return 0;
91          int result = FindBestDomainOrder.signum(this.cost - that.cost);
92          if (result == 0) {
93              result = this.order.compareTo(that.order);
94          }
95          if(result == 0){
96              result = FindBestDomainOrder.signum(this.timestamp - that.timestamp);
97          }
98          return result;
99      }
100 
101     public boolean isMax() {
102         return cost == BDDInferenceRule.LONG_TIME;
103     }
104 
105     public static String PREDICTION_VAR1 = "LowerBound";
106 
107     public static String PREDICTION_VAR2 = "UpperBound";
108 
109     /***
110      * Returns this TrialInfo as an XML element.
111      * 
112      * @return XML element
113      */
114     public Element toXMLElement() {
115         Element dis = new Element("trialInfo");
116         dis.setAttribute("order", order.toString());
117         dis.setAttribute("cost", Long.toString(cost));
118         dis.setAttribute("timestamp", Long.toString(timestamp));
119         dis.setAttribute("var" + PREDICTION_VAR1, Double.toString(pred.getVarLowerBound()));
120         dis.setAttribute("var" + PREDICTION_VAR2, Double.toString(pred.getVarUpperBound()));
121         dis.setAttribute("attr" + PREDICTION_VAR1, Double.toString(pred.getAttrLowerBound()));
122         dis.setAttribute("attr" + PREDICTION_VAR2, Double.toString(pred.getAttrUpperBound()));
123         dis.setAttribute("dom" + PREDICTION_VAR1, Double.toString(pred.getDomLowerBound()));
124         dis.setAttribute("dom" + PREDICTION_VAR2, Double.toString(pred.getDomUpperBound()));
125         return dis;
126     }
127 
128     public static TrialInfo fromXMLElement(Element e, Map nameToVar, Episode ep) {
129         String o_str = e.getAttributeValue("order");
130         if(o_str == null) throw new IllegalArgumentException();
131         Order o = Order.parse(o_str, nameToVar);
132         
133         long c = Long.parseLong(e.getAttributeValue("cost"));
134         long timestamp = Long.parseLong(e.getAttributeValue("timestamp"));
135         String score1 = e.getAttributeValue("score");
136         double score = score1 == null ? 0 : Double.parseDouble(score1);
137         String var1 = e.getAttributeValue("var" + PREDICTION_VAR1);
138         String var2 = e.getAttributeValue("var" + PREDICTION_VAR2);
139         double vVar1 = var1 == null ? 0 : Double.parseDouble(var1);
140         double vVar2 = var2 == null ? Double.MAX_VALUE : Double.parseDouble(var2);
141         var1 = e.getAttributeValue("attr" + PREDICTION_VAR1);
142         var2 = e.getAttributeValue("attr" + PREDICTION_VAR2);
143         double aVar1 = var1 == null ? 0 : Double.parseDouble(var1);
144         double aVar2 = var2 == null ? Double.MAX_VALUE : Double.parseDouble(var2);
145         var1 = e.getAttributeValue("dom" + PREDICTION_VAR1);
146         var2 = e.getAttributeValue("dom" + PREDICTION_VAR2);
147         double dVar1 = var1 == null ? 0 : Double.parseDouble(var1);
148         double dVar2 = var2 == null ? Double.MAX_VALUE : Double.parseDouble(var2);
149         return new TrialInfo(o, new TrialPrediction(score, vVar1, vVar2, aVar1, aVar2, dVar1, dVar2), c, ep, timestamp);
150     }
151 }