Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: marf/Classification/RandomClassification/RandomClassification.java


1   package marf.Classification.RandomClassification;
2   
3   import java.io.FileInputStream;
4   import java.io.FileNotFoundException;
5   import java.io.FileOutputStream;
6   import java.io.ObjectInputStream;
7   import java.io.ObjectOutputStream;
8   import java.util.Random;
9   import java.util.Vector;
10  import java.util.zip.GZIPInputStream;
11  import java.util.zip.GZIPOutputStream;
12  
13  import marf.MARF;
14  import marf.Classification.Classification;
15  import marf.Classification.ClassificationException;
16  import marf.FeatureExtraction.IFeatureExtraction;
17  import marf.Storage.Result;
18  import marf.Storage.StorageException;
19  import marf.util.Debug;
20  
21  
22  /**
23   * <p>Random Classification Module is for testing purposes.</p>
24   *
25   * <p>This represents the bottomline of the classification results.
26   * All the other modules should be better than this 99% of the time.
27   * If they are not, debug them.</p>
28   *
29   * <p>$Id: RandomClassification.java,v 1.15 2005/08/14 01:15:55 mokhov Exp $</p>
30   *
31   * @author Serguei Mokhov
32   * @version $Revision: 1.15 $
33   * @since 0.2.0
34   */
35  public class RandomClassification
36  extends Classification
37  {
38    /**
39     * For serialization versioning.
40     * When adding new members or make other structural
41     * changes regenerate this number with the
42     * <code>serialver</code> tool that comes with JDK.
43     * @since 0.3.0.4
44     */
45    private static final long serialVersionUID = -6770780209979417110L;
46  
47  
48    /**
49     * Vector of integer IDs.
50     */
51    private Vector oIDs = new Vector();
52  
53    /**
54     * RandomClassification Constructor.
55     * @param poFeatureExtraction FeatureExtraction module reference
56     */
57    public RandomClassification(IFeatureExtraction poFeatureExtraction)
58    {
59      super(poFeatureExtraction);
60  
61      this.strFilename =
62        getClass().getName() + "." +
63        MARF.getPreprocessingMethod() + "." +
64        MARF.getFeatureExtractionMethod() + "." +
65        getDefaultExtension();
66    }
67  
68    /**
69     * Picks an ID at random.
70     * @return <code>true</code>
71     * @throws ClassificationException
72     */
73    public final boolean classify()
74    throws ClassificationException
75    {
76      try
77      {
78        int iFirstID = 0;
79        int iSecondID = 0;
80  
81        restore();
82  
83        if(this.oIDs.size() == 0)
84        {
85          Debug.debug("RandomClassification.classify() --- ID set is of 0 length.");
86  
87          this.oIDs.add(new Integer(iFirstID));
88  
89          this.oResultSet.addResult
90          (
91            iFirstID,
92            0,
93            getClass().getName() +
94            " out of the blue"
95          );
96        }
97        else
98        {
99          // Collect for stats
100         // XXX: Move to StatsCollector
101         iFirstID = ((Integer)this.oIDs.elementAt((int)(this.oIDs.size() * (new Random().nextDouble())))).intValue();
102         iSecondID = iFirstID;
103 
104         // Pick a different second best ID if there are > 1 IDs in there
105         while(iSecondID == iFirstID && this.oIDs.size() > 1)
106           iSecondID = ((Integer)this.oIDs.elementAt((int)(this.oIDs.size() * (new Random().nextDouble())))).intValue();
107 
108         // If they are still equal (in case of one ID in oIDs), just add one.
109         if(iSecondID == iFirstID)
110           iSecondID++;
111 
112         Debug.debug("RandomClassification.classify() --- ID1 = " + iFirstID + ", ID2 = " + iSecondID);
113 
114         this.oResultSet.addResult(iFirstID, 0, this.getClass().getName());
115         this.oResultSet.addResult(iSecondID, 1, this.getClass().getName());
116       }
117 
118       return true;
119     }
120     catch(StorageException e)
121     {
122       throw new ClassificationException(e);
123     }
124   }
125 
126   /**
127    * Simply stores incoming ID's.
128    * @return <code>true</code>
129    * @throws ClassificationException
130    */
131   public final boolean train()
132   throws ClassificationException
133   {
134     try
135     {
136       Integer oIntegerID = new Integer(MARF.getCurrentSubject());
137 
138       // TODO: this test isn't working -- objects are not the same, so dupes are possible
139       // maybe need to fix, though it's not of primary importance
140       if(this.oIDs.contains(oIntegerID) == false)
141       {
142         restore();
143 
144         this.oIDs.add(oIntegerID);
145 
146         dump();
147 
148         Debug.debug
149         (
150           "RandomClassification.train() --- added ID: " + MARF.getCurrentSubject() + ",\n" +
151           "all IDs: " + this.oIDs
152         );
153       }
154 
155       return true;
156     }
157     catch(StorageException e)
158     {
159       throw new ClassificationException("Exception in RandomClassification.train() --- " + e.getMessage());
160     }
161   }
162 
163   /* From Storage Manager */
164 
165   /**
166    * Dumps "training set" of IDs.
167    * @throws StorageException
168    */
169   public final void dump()
170   throws StorageException
171   {
172     try
173     {
174       FileOutputStream fos = new FileOutputStream(this.strFilename);
175       GZIPOutputStream gzos = new GZIPOutputStream(fos);
176       ObjectOutputStream out = new ObjectOutputStream(gzos);
177 
178       out.writeObject(this.oIDs);
179       out.flush();
180       out.close();
181     }
182     catch(Exception e)
183     {
184       throw new StorageException(e);
185     }
186   }
187 
188   /**
189    * Restores "training set" of IDs.
190    * @throws StorageException if there was an error loading the data file.
191    */
192   public final void restore()
193   throws StorageException
194   {
195     try
196     {
197       FileInputStream fis = new FileInputStream(this.strFilename);
198       GZIPInputStream gzis = new GZIPInputStream(fis);
199       ObjectInputStream in = new ObjectInputStream(gzis);
200 
201       this.oIDs = (Vector)in.readObject();
202 
203       in.close();
204     }
205     catch(FileNotFoundException e)
206     {
207       this.oIDs = new Vector();
208       dump();
209     }
210     catch(ClassNotFoundException e)
211     {
212       throw new StorageException
213       (
214         "RandomClassification.restore() --- ClassNotFoundException: " +
215         e.getMessage()
216       );
217     }
218     catch(Exception e)
219     {
220       throw new StorageException(e);
221     }
222   }
223 
224   /**
225    * Retrieves the classification result.
226    *
227    * @return Result object
228    *
229    * @since 0.3.0
230    */
231   public Result getResult()
232   {
233     return this.oResultSet.getMinimumResult();
234   }
235 
236   /**
237    * Returns string representation of the internals of this object.
238    * @return String
239    * @since 0.3.0
240    */
241   public String toString()
242   {
243     return
244       "Dump mode: " + this.iCurrentDumpMode + "\n" +
245       "Dump file: " + this.strFilename + "\n" +
246       "ID data: " + this.oIDs;
247   }
248 
249   /**
250    * Retrieves class' revision.
251    * @return revision string
252    * @since 0.3.0
253    */
254   public static String getMARFSourceCodeRevision()
255   {
256     return "$Revision: 1.15 $";
257   }
258 }
259 
260 // EOF