summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestNode.java
blob: 8ee4cdbf2972066a04a8aa4aba4f299e968d2fdb (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
// Copyright 2017 Yahoo Holdings. 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.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.Deque;

/**
 * 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 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) */
    public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) {
        return "(optimized sum of condition trees of size " + (values.length*8) + " bytes)";
    }

}