You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by to...@apache.org on 2015/03/06 10:30:38 UTC

svn commit: r1664569 - /jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/LMSEstimator.java

Author: tommaso
Date: Fri Mar  6 09:30:37 2015
New Revision: 1664569

URL: http://svn.apache.org/r1664569
Log:
OAK-2584 - added a threshold to lms estimator

Modified:
    jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/LMSEstimator.java

Modified: jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/LMSEstimator.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/LMSEstimator.java?rev=1664569&r1=1664568&r2=1664569&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/LMSEstimator.java (original)
+++ jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/LMSEstimator.java Fri Mar  6 09:30:37 2015
@@ -23,33 +23,38 @@ import org.apache.solr.common.SolrDocume
 
 /**
  * A very simple estimator for no. of entries in the index using least mean square update method but not the full stochastic
- * gradient descent algorithm (yet?).
+ * gradient descent algorithm (yet?), on a linear interpolation model.
  */
 class LMSEstimator {
 
     private double[] weights;
     private final double alpha;
+    private final long threshold;
 
-    public LMSEstimator(double alpha, double[] weights) {
+    LMSEstimator(double alpha, double[] weights, long threshold) {
         this.alpha = alpha;
         this.weights = weights;
+        this.threshold = threshold;
     }
 
-    public LMSEstimator(double[] weights) {
-        this.weights = weights;
-        this.alpha = 0.03;
+    LMSEstimator(double[] weights) {
+        this(0.03, weights, 5);
     }
 
-    public LMSEstimator() {
-        this.weights = new double[5];
-        this.alpha = 0.03;
+    LMSEstimator() {
+        this(0.03, new double[5], 5);
     }
 
     synchronized void update(Filter filter, SolrDocumentList docs) {
         double[] updatedWeights = new double[weights.length];
-        for (int i = 0; i < updatedWeights.length; i++) {
-            double errors = (docs.getNumFound() - estimate(filter)) * getInput(filter, i);
-            updatedWeights[i] = weights[i] + alpha * errors;
+        long estimate = estimate(filter);
+        long numFound = docs.getNumFound();
+        long delta = numFound - estimate;
+        if (Math.abs(delta) > threshold) {
+            for (int i = 0; i < updatedWeights.length; i++) {
+                double errors = delta * getInput(filter, i);
+                updatedWeights[i] = weights[i] + alpha * errors;
+            }
         }
         // weights updated
         weights = Arrays.copyOf(updatedWeights, 5);
@@ -63,6 +68,20 @@ class LMSEstimator {
         return estimatedEntryCount + 1; // smoothing
     }
 
+    /**
+     * Get the input value for a certain feature (by index) in the given filter.
+     * <p/>
+     * A filter is represented as a vector in R^5 where
+     * i_0 : no. of property restrictions
+     * i_1 : 1 if any native constraint exists in the filter, 0 otherwise
+     * i_2 : the path restriction ordinal
+     * i_3 : the depth of the path restriction if set, 0 otherwise
+     * i_4 : the precedence of the dominant full text constraint if present, 0 otherwise
+     *
+     * @param filter the filter
+     * @param i      the index of the filter vector feature to retrieve
+     * @return the feature value
+     */
     private long getInput(Filter filter, int i) {
         assert i < 5;
         if (i == 0) {