aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestNode.java
blob: c8b20e774b5f5196eb7b22b2ffd35c480bb2e87a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.evaluation.gbdtoptimization;

import com.yahoo.searchlib.rankingexpression.Reference;
import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;
import com.yahoo.searchlib.rankingexpression.rule.CompositeNode;
import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode;
import com.yahoo.searchlib.rankingexpression.rule.SerializationContext;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.TypeContext;

import java.util.Arrays;
import java.util.Deque;
import java.util.Objects;

/**
 * An optimized version of a sum of consecutive decision trees.
 *
 * @author bratseth
 */
public class GBDTForestNode extends ExpressionNode {

    private final double[] values;

    public GBDTForestNode(double[] values) {
        this.values=values;
    }

    @Override
    public final TensorType type(TypeContext<Reference> context) { return TensorType.empty; }

    @Override
    public final Value evaluate(Context context) {
        int pc = 0;
        double treeSum = 0;
        while (pc < values.length) {
            int nextTree = (int)values[pc++];
            treeSum += GBDTNode.evaluate(values, pc, context);
            pc += nextTree;
        }
        return new DoubleValue(treeSum);
    }

    /** Returns (optimized sum of condition trees) */
    @Override
    public StringBuilder toString(StringBuilder string, SerializationContext context, Deque<String> path, CompositeNode parent) {
        return string.append("(optimized sum of condition trees of size ").append(values.length*8).append(" bytes)");
    }

    @Override
    public int hashCode() { return Objects.hash("gbdtForest", Arrays.hashCode(values)); }

}