use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class EvaluationTestCase method testEvaluation.
@Test
public void testEvaluation() {
EvaluationTester tester = new EvaluationTester();
tester.assertEvaluates(0.5, "0.5");
tester.assertEvaluates(-0.5, "-0.5");
tester.assertEvaluates(0.5, "one_half");
tester.assertEvaluates(-0.5, "-one_half");
tester.assertEvaluates(0, "nonexisting");
tester.assertEvaluates(0.75, "0.5 + 0.25");
tester.assertEvaluates(0.75, "one_half + a_quarter");
tester.assertEvaluates(1.25, "0.5 - 0.25 + one");
tester.assertEvaluates(9.0, "3 ^ 2");
// String
tester.assertEvaluates(1, "if(\"a\"==\"a\",1,0)");
// Precedence
tester.assertEvaluates(26, "2*3+4*5");
tester.assertEvaluates(1, "2/6+4/6");
tester.assertEvaluates(2 * 3 * 4 + 3 * 4 * 5 - 4 * 200 / 10, "2*3*4+3*4*5-4*200/10");
tester.assertEvaluates(3, "1 + 10 % 6 / 2");
tester.assertEvaluates(10.0, "3 ^ 2 + 1");
tester.assertEvaluates(18.0, "2 * 3 ^ 2");
// Conditionals
tester.assertEvaluates(2 * (3 * 4 + 3) * (4 * 5 - 4 * 200) / 10, "2*(3*4+3)*(4*5-4*200)/10");
tester.assertEvaluates(0.5, "if( 2<3, one_half, one_quarter)");
tester.assertEvaluates(0.25, "if( 2>3, one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( 1==1, one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( 1<=1, one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( 1<=1.1, one_half, a_quarter)");
tester.assertEvaluates(0.25, "if( 1>=1.1, one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( 0.33333333333333333333~=1/3, one_half, a_quarter)");
tester.assertEvaluates(0.25, "if( 0.33333333333333333333~=1/35, one_half, a_quarter)");
tester.assertEvaluates(5.5, "if(one_half in [one_quarter,one_half], one_half+5,log(one_quarter) * one_quarter)");
tester.assertEvaluates(0.5, "if( 1 in [1,2 , 3], one_half, a_quarter)");
tester.assertEvaluates(0.25, "if( 1 in [ 2,3,4], one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( \"foo\" in [\"foo\",\"bar\"], one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( foo in [\"foo\",\"bar\"], one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( \"foo\" in [foo,\"bar\"], one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( foo in [foo,\"bar\"], one_half, a_quarter)");
tester.assertEvaluates(0.25, "if( \"foo\" in [\"baz\",\"boz\"], one_half, a_quarter)");
tester.assertEvaluates(0.5, "if( one in [0, 1, 2], one_half, a_quarter)");
tester.assertEvaluates(0.25, "if( one in [2], one_half, a_quarter)");
tester.assertEvaluates(2.5, "if(1.0, 2.5, 3.5)");
tester.assertEvaluates(3.5, "if(0.0, 2.5, 3.5)");
tester.assertEvaluates(2.5, "if(1.0-1.1, 2.5, 3.5)");
tester.assertEvaluates(3.5, "if(1.0-1.0, 2.5, 3.5)");
// Conditionals with branch probabilities
RankingExpression e = tester.assertEvaluates(3.5, "if(1.0-1.0, 2.5, 3.5, 0.3)");
assertEquals(0.3d, (double) ((IfNode) e.getRoot()).getTrueProbability(), tolerance);
// Conditionals as expressions
tester.assertEvaluates(new BooleanValue(true), "2<3");
tester.assertEvaluates(new BooleanValue(false), "2>3");
tester.assertEvaluates(new BooleanValue(false), "if (3>2, 2>3, 5.0)");
// The result of 2>3 is converted to 0, which is <1
tester.assertEvaluates(new BooleanValue(true), "2>3<1");
tester.assertEvaluates(2.5, "if(2>3<1, 2.5, 3.5)");
tester.assertEvaluates(2.5, "if(1+1>3<1+0, 2.5, 3.5)");
// Functions
tester.assertEvaluates(0, "sin(0)");
tester.assertEvaluates(1, "cos(0)");
tester.assertEvaluates(8, "pow(4/2,min(cos(0)*3,5))");
// Random feature (which is also a tensor function) (We expect to be able to parse it and look up a zero)
tester.assertEvaluates(0, "random(1)");
tester.assertEvaluates(0, "random(foo)");
// Combined
tester.assertEvaluates(1.25, "5*if(1>=1.1, one_half, if(min(1,2)<max(1,2),if (\"foo\" in [\"foo\",\"bar\"],a_quarter,3000), 0.57345347))");
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class NeuralNetEvaluationTestCase method assertEvaluates.
private RankingExpression assertEvaluates(Value value, String expressionString, Context context) {
try {
RankingExpression expression = new RankingExpression(expressionString);
assertEquals(expression.toString(), value, expression.evaluate(context));
return expression;
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class TypeResolutionTestCase method assertIncompatibleType.
private void assertIncompatibleType(String expression, TypeContext<Reference> context) {
try {
new RankingExpression(expression).type(context);
fail("Expected type incompatibility exception");
} catch (IllegalArgumentException expected) {
assertEquals("An if expression must produce compatible types in both alternatives, " + "but the 'true' type is tensor(x[]) while the 'false' type is tensor(y[])", expected.getMessage());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class TestableTensorFlowModel method evaluateMacro.
private void evaluateMacro(Context context, TensorFlowModel model, String macroName) {
if (!context.names().contains(macroName)) {
RankingExpression e = model.macros().get(macroName);
evaluateMacroDependencies(context, model, e.getRoot());
context.put(macroName, new TensorValue(e.evaluate(context).asTensor()));
}
}
use of com.yahoo.searchlib.rankingexpression.RankingExpression in project vespa by vespa-engine.
the class ConstantDereferencerTestCase method testConstantDereferencer.
@Test
public void testConstantDereferencer() throws ParseException {
ConstantDereferencer c = new ConstantDereferencer();
Map<String, Value> constants = new HashMap<>();
constants.put("a", Value.parse("1.0"));
constants.put("b", Value.parse("2"));
constants.put("c", Value.parse("3.5"));
TransformContext context = new TransformContext(constants);
assertEquals("1.0 + 2.0 + 3.5", c.transform(new RankingExpression("a + b + c"), context).toString());
assertEquals("myMacro(1.0,2.0)", c.transform(new RankingExpression("myMacro(a, b)"), context).toString());
}
Aggregations