Search in sources :

Example 1 with ExpressionOptimizer

use of com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer in project vespa by vespa-engine.

the class GBDTOptimizerTestCase method testNodeOptimization.

public void testNodeOptimization() throws ParseException {
    String gbdtString = "if (LW_NEWS_SEARCHES_RATIO < 1.72971, 0.0697159, if (LW_USERS < 0.10496, if (SEARCHES < 0.0329127, 0.151257, 0.117501), if (SUGG_OVERLAP < 18.5, 0.0897622, 0.0756903))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.73156, if (NEWS_USERS < 0.0737993, -0.00481646, 0.00110018), if (LW_USERS < 0.0844616, 0.0488919, if (SUGG_OVERLAP < 32.5, 0.0136917, 9.85328E-4))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.74451, -0.00298257, if (LW_USERS < 0.116207, if (SEARCHES < 0.0329127, 0.0676105, 0.0340198), if (NUM_WORDS < 1.5, -8.55514E-5, 0.0112406))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.72995, if (NEWS_USERS < 0.0737993, -0.00407515, 0.00139088), if (LW_USERS == 0.0509035, 0.0439466, if (LW_USERS < 0.325818, 0.0187156, 0.00236949)))";
    RankingExpression gbdt = new RankingExpression(gbdtString);
    // Regular evaluation
    MapContext arguments = new MapContext();
    arguments.put("LW_NEWS_SEARCHES_RATIO", 1d);
    arguments.put("SUGG_OVERLAP", 17d);
    double result1 = gbdt.evaluate(arguments).asDouble();
    arguments.put("LW_NEWS_SEARCHES_RATIO", 2d);
    arguments.put("SUGG_OVERLAP", 20d);
    double result2 = gbdt.evaluate(arguments).asDouble();
    arguments.put("LW_NEWS_SEARCHES_RATIO", 2d);
    arguments.put("SUGG_OVERLAP", 40d);
    double result3 = gbdt.evaluate(arguments).asDouble();
    // Optimized evaluation
    ArrayContext fArguments = new ArrayContext(gbdt);
    ExpressionOptimizer optimizer = new ExpressionOptimizer();
    optimizer.getOptimizer(GBDTForestOptimizer.class).setEnabled(false);
    OptimizationReport report = optimizer.optimize(gbdt, fArguments);
    assertEquals(4, report.getMetric("Optimized GDBT trees"));
    fArguments.put("LW_NEWS_SEARCHES_RATIO", 1d);
    fArguments.put("SUGG_OVERLAP", 17d);
    double oResult1 = gbdt.evaluate(fArguments).asDouble();
    fArguments.put("LW_NEWS_SEARCHES_RATIO", 2d);
    fArguments.put("SUGG_OVERLAP", 20d);
    double oResult2 = gbdt.evaluate(fArguments).asDouble();
    fArguments.put("LW_NEWS_SEARCHES_RATIO", 2d);
    fArguments.put("SUGG_OVERLAP", 40d);
    double oResult3 = gbdt.evaluate(fArguments).asDouble();
    // Assert the same results are produced
    assertEquals(result1, oResult1);
    assertEquals(result2, oResult2);
    assertEquals(result3, oResult3);
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ArrayContext(com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext) OptimizationReport(com.yahoo.searchlib.rankingexpression.evaluation.OptimizationReport) ExpressionOptimizer(com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext)

Example 2 with ExpressionOptimizer

use of com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer in project vespa by vespa-engine.

the class ContextReuseTestCase method testIt.

public void testIt() throws ParseException, IOException {
    // Prepare
    RankingExpression expression = new RankingExpression(IOUtils.readFile(new File("src/test/files/s-expression.vre")));
    ArrayContext contextPrototype = new ArrayContext(expression);
    new ExpressionOptimizer().optimize(expression, contextPrototype);
    // Execute
    ArrayContext context = contextPrototype.clone();
    for (String contextValueString : contextString.split("\n")) {
        String[] contextValueParts = contextValueString.split("=");
        context.put(contextValueParts[0].trim(), Double.valueOf(contextValueParts[1].trim()));
    }
    assertEquals(-2.3450294999999994, expression.evaluate(context).asDouble());
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ArrayContext(com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext) ExpressionOptimizer(com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer) File(java.io.File)

Example 3 with ExpressionOptimizer

use of com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer in project vespa by vespa-engine.

the class GBDTOptimizerTestCase method testSimpleNodeOptimization.

public void testSimpleNodeOptimization() throws ParseException {
    RankingExpression gbdt = new RankingExpression("if (a < 2, if (b < 2, 5, 6), 4) + if (a < 3, 7, 8)");
    // Optimized evaluation
    ArrayContext arguments = new ArrayContext(gbdt);
    ExpressionOptimizer optimizer = new ExpressionOptimizer();
    optimizer.getOptimizer(GBDTForestOptimizer.class).setEnabled(false);
    OptimizationReport report = optimizer.optimize(gbdt, arguments);
    assertEquals(2, report.getMetric("Optimized GDBT trees"));
    arguments.put("a", 1d);
    arguments.put("b", 2d);
    assertEquals(13.0, gbdt.evaluate(arguments).asDouble());
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ArrayContext(com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext) OptimizationReport(com.yahoo.searchlib.rankingexpression.evaluation.OptimizationReport) ExpressionOptimizer(com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer)

Aggregations

RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)3 ArrayContext (com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext)3 ExpressionOptimizer (com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer)3 OptimizationReport (com.yahoo.searchlib.rankingexpression.evaluation.OptimizationReport)2 MapContext (com.yahoo.searchlib.rankingexpression.evaluation.MapContext)1 File (java.io.File)1