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.Iterator;
10  import jwutil.util.Assert;
11  import net.sf.bddbddb.BDDInferenceRule;
12  import net.sf.bddbddb.FindBestDomainOrder;
13  import net.sf.bddbddb.order.WekaInterface.OrderAttribute;
14  import net.sf.bddbddb.order.WekaInterface.OrderInstance;
15  import weka.core.Instance;
16  
17  
18  public class TrialInstance extends OrderInstance implements Comparable {
19  
20      /***
21       * Version ID for serialization.
22       */
23      private static final long serialVersionUID = 3689626995428701492L;
24  
25      public static TrialInstance construct(TrialInfo ti, Order o, double cost, TrialInstances dataSet) {
26          return construct(ti, o, cost, dataSet, 1);
27      }
28  
29      public static TrialInstance construct(TrialInfo ti, Order o, double cost, TrialInstances dataSet, double weight) {
30          double[] d = new double[dataSet.numAttributes()];
31          for (int i = 0; i < d.length; ++i) {
32              d[i] = Instance.missingValue();
33          }
34        
35          for (Iterator i = o.getConstraints().iterator(); i.hasNext();) {
36              OrderConstraint oc = (OrderConstraint) i.next();
37              // TODO: use a map from Pair to int instead of building String and doing linear search.
38       
39              
40              String cName = oc.getFirst() + "," + oc.getSecond();
41              OrderAttribute oa = (OrderAttribute) dataSet.attribute(cName);
42              if (oa != null) {
43                  if(oc.getFirst().equals(oc.getSecond()) && d[oa.index()] == WekaInterface.INTERLEAVE) 
44                      continue;
45                 /* TODO should only one type of constraint for
46                  * when first == second and they are not interleaved
47                  */  
48                  d[oa.index()] = WekaInterface.getType(oc);
49              } else {
50                  System.out.println("Warning: cannot find constraint "+oc+" order "+ti.order+" in dataset "+dataSet);
51                  Assert.UNREACHABLE();
52                  return null;
53              }
54          }
55          d[d.length - 1] = cost;
56          return new TrialInstance(weight, d, o, ti);
57      }
58  
59      public TrialInfo ti;
60  
61      public TrialInstance(double weight, double[] d, Order o, TrialInfo ti) {
62          super(weight, d, o);
63          this.ti = ti;
64      }
65  
66      protected TrialInstance(TrialInstance that) {
67          super(that);
68          this.ti = that.ti;
69      }
70  
71      public TrialInfo getTrialInfo() { return ti; }
72  
73      public double getCost() {
74          return value(numAttributes() - 1);
75      }
76      
77      public void setCost(double cost){
78          this.setValue(numAttributes() -1 , cost);
79      }
80      
81      public void recomputeCost(double best){
82          setCost((double) (ti.cost + 1) / best);
83      }
84  
85      public Object copy() {
86          return new TrialInstance(this);
87      }
88  
89      public int compareTo(Object arg0) {
90          TrialInstance that = (TrialInstance) arg0;
91          return FindBestDomainOrder.compare(this.getCost(), that.getCost());
92      }
93  
94      public boolean isMaxTime() {
95          return ti.cost >= BDDInferenceRule.LONG_TIME;
96      }
97      
98      public static TrialInstance cloneInstance(TrialInstance instance){
99          return new TrialInstance(instance.weight(), instance.toDoubleArray(), instance.getOrder(), instance.getTrialInfo());
100     }
101     
102     /* just tests if the orders are equal */
103     public boolean equals(Object o){
104         return this.ti.order.equals(((TrialInstance) o).ti.order);
105     }
106 
107     public int hashCode() {
108         return this.ti.hashCode();
109     }
110 }