View Javadoc

1   package net.obsearch.pivots.bustos.impl;
2   
3   import java.util.Arrays;
4   import java.util.HashMap;
5   
6   import hep.aida.bin.StaticBin1D;
7   
8   import net.obsearch.Index;
9   import net.obsearch.dimension.DimensionFloat;
10  import net.obsearch.exception.IllegalIdException;
11  import net.obsearch.exception.OBException;
12  import net.obsearch.pivots.Pivotable;
13  import net.obsearch.pivots.bustos.AbstractIncrementalBustosNavarroChavez;
14  import net.obsearch.pivots.IncrementalPivotSelector;
15  
16  import net.obsearch.ob.OBFloat;
17  
18  import com.sleepycat.je.DatabaseException;
19  
20  /*
21   OBSearch: a distributed similarity search engine This project is to
22   similarity search what 'bit-torrent' is to downloads. 
23   Copyright (C) 2008 Arnoldo Jose Muller Molina
24  
25   This program is free software: you can redistribute it and/or modify
26   it under the terms of the GNU General Public License as published by
27   the Free Software Foundation, either version 3 of the License, or
28   (at your option) any later version.
29  
30   This program is distributed in the hope that it will be useful,
31   but WITHOUT ANY WARRANTY; without even the implied warranty of
32   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33   GNU General Public License for more details.
34  
35   You should have received a copy of the GNU General Public License
36   along with this program.  If not, see <http://www.gnu.org/licenses/>.
37   */
38  
39  /**
40   * IncrementalBustosNavarroChavezFloat is an implementation 
41   * for OBFloat objects
42   * @author Arnoldo Jose Muller Molina
43   */
44  //*************************************************************************
45  //****** Warning: this is a generated file ********************************
46  //****** The source file is: IncrementalBustosNavarroChavez.java    
47  //*************************************************************************
48  public class IncrementalBustosNavarroChavezFloat<O extends OBFloat>
49          extends AbstractIncrementalBustosNavarroChavez<O> implements IncrementalPivotSelector<O> {
50      
51      /**
52       * Keeps track of the SMAP values of objects.
53       */
54      private transient HashMap<Long, float[]> smapCache;
55      
56      /**
57       * Receives the object that accepts pivots as possible candidates. Selects l
58       * pairs of objects to compare which set of pivots is better, and selects m
59       * possible pivot candidates from the data set.
60       * @param pivotable
61       * @param l pairs of objects to select
62       * @param m m possible pivot candidates to be randomly picked.
63       */
64      public IncrementalBustosNavarroChavezFloat(Pivotable < O > pivotable,
65              int l, int m) {
66          super(pivotable, l , m);
67          smapCache = new HashMap<Long, float[]>(l);
68      }
69  
70      @Override
71      protected double calculateMedian(long[] pivots, long[] x, long[] y, Index<O> index) throws DatabaseException,
72      IllegalIdException, IllegalAccessException, InstantiationException,
73      OBException{
74          StaticBin1D data = new StaticBin1D();  
75          int i = 0;
76          while ( i < x.length){
77              float[] tupleA = getTuple(pivots, x[i], (Index<OBFloat>)index);
78              float[] tupleB = getTuple(pivots, y[i], (Index<OBFloat>)index);
79              float distance = DimensionFloat.lInfinite(tupleA, tupleB);
80              data.add(distance);
81              i++;
82          }                
83          return data.mean();
84      }
85      
86      protected void resetCache(int x){
87          smapCache = new HashMap<Long, float[]>(x);
88      }
89      
90      private float[] getTuple(long[] pivots, long id, Index<OBFloat>index  )throws DatabaseException,
91      IllegalIdException, IllegalAccessException, InstantiationException,
92      OBException{
93          float[] t = smapCache.get(id);
94          if(t == null){
95              t = new float[pivots.length];
96              smapCache.put(id, t);
97              assert pivots.length == 1;
98          }else{          
99              float [] td = new float[pivots.length];
100             System.arraycopy(t, 0, td, 0, t.length );
101             smapCache.put(id, td);
102             t = td;
103         }
104         int i = pivots.length-1;
105         t[i] = index.getObject(id).distance(index.getObject(pivots[i]));
106         return t;
107     }
108 
109     /* (non-Javadoc)
110      * @see net.obsearch.result.index.pivotselection.AbstractIncrementalBustosNavarroChavez#validatePivots(int[], int)
111      */
112     @Override
113     protected boolean validatePivots(long[] pivots, long id, Index<O> index) throws DatabaseException,
114     IllegalIdException, IllegalAccessException, InstantiationException,
115     OBException {
116         float[] real = DimensionFloat.getPrimitiveTuple(pivots, id, index);
117         float[] localTuple = smapCache.get(id);
118         return Arrays.equals(real, localTuple);
119     }
120     
121     
122 
123 }
124