use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class SimplifierTestCase method testParenthesisPreservation.
@Test
public void testParenthesisPreservation() throws ParseException {
Simplifier s = new Simplifier();
TransformContext c = new TransformContext(Collections.emptyMap());
CompositeNode transformed = (CompositeNode) s.transform(new RankingExpression("a + (b + c) / 100000000.0"), c).getRoot();
assertEquals("a + (b + c) / 100000000.0", transformed.toString());
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class SimplifierTestCase method testSimplify.
@Test
public void testSimplify() throws ParseException {
Simplifier s = new Simplifier();
TransformContext c = new TransformContext(Collections.emptyMap());
assertEquals("a + b", s.transform(new RankingExpression("a + b"), c).toString());
assertEquals("6.5", s.transform(new RankingExpression("1.0 + 2.0 + 3.5"), c).toString());
assertEquals("6.5", s.transform(new RankingExpression("1.0 + ( 2.0 + 3.5 )"), c).toString());
assertEquals("6.5", s.transform(new RankingExpression("( 1.0 + 2.0 ) + 3.5 "), c).toString());
assertEquals("6.5", s.transform(new RankingExpression("1.0 + ( 2.0 + 3.5 )"), c).toString());
assertEquals("7.5", s.transform(new RankingExpression("1.0 + ( 2.0 + 3.5 ) + 1"), c).toString());
assertEquals("6.5 + a", s.transform(new RankingExpression("1.0 + ( 2.0 + 3.5 ) + a"), c).toString());
assertEquals("7.5", s.transform(new RankingExpression("7.5 + ( 2.0 + 3.5 ) * 0.0"), c).toString());
assertEquals("7.5", s.transform(new RankingExpression("7.5 + ( 2.0 + 3.5 ) * (0.0)"), c).toString());
assertEquals("7.5", s.transform(new RankingExpression("7.5 + ( 2.0 + 3.5 ) * (1.0 - 1.0)"), c).toString());
assertEquals("7.5", s.transform(new RankingExpression("if (2 > 0, 3.5 * 2 + 0.5, a *3 )"), c).toString());
assertEquals("0.0", s.transform(new RankingExpression("0.0 * (1.3 + 7.0)"), c).toString());
assertEquals("6.4", s.transform(new RankingExpression("max(0, 10.0-2.0)*(1-fabs(0.0-0.2))"), c).toString());
assertEquals("(query(d) + query(b) - query(a)) * query(c) / query(e)", s.transform(new RankingExpression("(query(d) + query(b) - query(a)) * query(c) / query(e)"), c).toString());
assertEquals("14.0", s.transform(new RankingExpression("5 + (2 + 3) + 4"), c).toString());
assertEquals("28.0 + bar", s.transform(new RankingExpression("7.0 + 12.0 + 9.0 + bar"), c).toString());
assertEquals("1.0 - 0.001 * attribute(number)", s.transform(new RankingExpression("1.0 - 0.001*attribute(number)"), c).toString());
assertEquals("attribute(number) * 1.5 - 0.001 * attribute(number)", s.transform(new RankingExpression("attribute(number) * 1.5 - 0.001 * attribute(number)"), c).toString());
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class StreamEvaluationBenchmark method run.
private void run(String expressionString, List<Map<String, Double>> features, int iterations) throws ParseException {
// Optimize
RankingExpression expression = new RankingExpression(expressionString);
DoubleOnlyArrayContext contextPrototype = new DoubleOnlyArrayContext(expression, true);
OptimizationReport forestOptimizationReport = new ExpressionOptimizer().optimize(expression, contextPrototype);
System.out.println(forestOptimizationReport);
System.out.println("Optimized expression: " + expression.getRoot());
// Warm up
out("Warming up ...");
double total = 0;
total += benchmarkIterations(expression, contextPrototype, features, Math.max(iterations / 5, 1));
oul("done");
// Benchmark
out("Running " + iterations + " of 'stream' ...");
long tStartTime = System.currentTimeMillis();
total += benchmarkIterations(expression, contextPrototype, features, iterations);
long totalTime = System.currentTimeMillis() - tStartTime;
oul("done");
oul(" Total time running 'stream': " + totalTime + " ms (" + totalTime * 1000 / (iterations * features.size()) + " microseconds/expression)");
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression 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);
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class GBDTOptimizerTestCase method testFeatureNamesWithDots.
public void testFeatureNamesWithDots() throws ParseException {
String gbdtString = "if (a.b < 1.72971, 0.0697159, if (a.b.c < 0.10496, if (a.c < 0.0329127, 0.151257, 0.117501), if (a < 18.5, 0.0897622, 0.0756903))) + 1";
RankingExpression gbdt = new RankingExpression(gbdtString);
// Regular evaluation
MapContext arguments = new MapContext();
arguments.put("a.b", 1d);
arguments.put("a.b.c", 0.1d);
arguments.put("a.c", 0.01d);
arguments.put("a", 19d);
double result = gbdt.evaluate(arguments).asDouble();
// Optimized evaluation
ArrayContext fArguments = new ArrayContext(gbdt);
OptimizationReport report = new OptimizationReport();
new GBDTOptimizer().optimize(gbdt, fArguments, report);
assertEquals("Optimization result is as expected:\n" + report, 1, report.getMetric("Optimized GDBT trees"));
fArguments.put("a.b", 1d);
fArguments.put("a.b.c", 0.1d);
fArguments.put("a.c", 0.01d);
fArguments.put("a", 19d);
double oResult = gbdt.evaluate(fArguments).asDouble();
// Assert the same results are produced
assertEquals(result, oResult);
}
Aggregations