Esempio n. 1
0
bool IOService::IsConnected(int connectionId) const
{
    if (Find(removedListeners, connectionId) != std::end(removedListeners)) {
        return false;
    }
    const auto equal = [&connectionId](const Listener& listener)-> bool {
        return listener.id == connectionId;
    };
    if (FindIf(listeners, equal) != std::end(listeners)) {
        return true;
    }
    if (FindIf(addedListeners, equal) != std::end(addedListeners)) {
        return true;
    }
    return false;
}
static void CalcShapValuesRecursive(const TObliviousTrees& forest,
                                    const TVector<int>& binFeaturesMapping,
                                    const TVector<ui8>& binFeaturesValues,
                                    size_t treeIdx,
                                    int depth,
                                    const TVector<TVector<size_t>>& subtreeSizes,
                                    int dimension,
                                    size_t nodeIdx,
                                    const TVector<TFeaturePathElement>& oldFeaturePath,
                                    double zeroPathsFraction,
                                    double onePathsFraction,
                                    int feature,
                                    TVector<double>* shapValuesPtr) {
    TVector<double>& shapValues = *shapValuesPtr;
    TVector<TFeaturePathElement> featurePath = ExtendFeaturePath(oldFeaturePath, zeroPathsFraction, onePathsFraction, feature);

    if (depth == forest.TreeSizes[treeIdx]) {
        for (size_t elementIdx = 1; elementIdx < featurePath.size(); ++elementIdx) {
            TVector<TFeaturePathElement> unwoundPath = UnwindFeaturePath(featurePath, elementIdx);
            double weightSum = 0.0;
            for (const TFeaturePathElement& unwoundPathElement : unwoundPath) {
                weightSum += unwoundPathElement.Weight;
            }
            const TFeaturePathElement& element = featurePath[elementIdx];
            const int approxDimension = forest.ApproxDimension;

            shapValues[element.Feature] += weightSum * (element.OnePathsFraction - element.ZeroPathsFraction)
                                         * forest.LeafValues[treeIdx][nodeIdx * approxDimension + dimension];
        }
    } else {
        const TRepackedBin& split = forest.GetRepackedBins()[forest.TreeStartOffsets[treeIdx] + depth];
        const int splitBinFeature = split.FeatureIndex;
        const int splitFlatFeature = binFeaturesMapping[splitBinFeature];
        const ui8 threshold = split.SplitIdx;
        const ui8 xorMask = split.XorMask;

        double newZeroPathsFraction = 1.0;
        double newOnePathsFraction = 1.0;

        const auto sameFeatureElement = FindIf(featurePath.begin(), featurePath.end(), [splitFlatFeature](const TFeaturePathElement& element) {return element.Feature == splitFlatFeature;});

        if (sameFeatureElement != featurePath.end()) {
            const size_t sameFeatureIndex = sameFeatureElement - featurePath.begin();
            newZeroPathsFraction = featurePath[sameFeatureIndex].ZeroPathsFraction;
            newOnePathsFraction = featurePath[sameFeatureIndex].OnePathsFraction;
            featurePath = UnwindFeaturePath(featurePath, sameFeatureIndex);
        }

        const size_t goNodeIdx = nodeIdx | (((binFeaturesValues[splitBinFeature] ^ xorMask) >= threshold) << depth);
        const size_t skipNodeIdx = goNodeIdx ^ (1 << depth);

        if (subtreeSizes[depth + 1][goNodeIdx] > 0) {
            CalcShapValuesRecursive(forest, binFeaturesMapping, binFeaturesValues, treeIdx, depth + 1, subtreeSizes, dimension,
                                    goNodeIdx, featurePath,
                                    newZeroPathsFraction * subtreeSizes[depth + 1][goNodeIdx] / subtreeSizes[depth][nodeIdx],
                                    newOnePathsFraction, splitFlatFeature,
                                    &shapValues);
        }

        if (subtreeSizes[depth + 1][skipNodeIdx] > 0) {
            CalcShapValuesRecursive(forest, binFeaturesMapping, binFeaturesValues, treeIdx, depth + 1, subtreeSizes, dimension,
                                    skipNodeIdx, featurePath,
                                    newZeroPathsFraction * subtreeSizes[depth + 1][skipNodeIdx] / subtreeSizes[depth][nodeIdx],
                                    /*onePathFraction*/ 0, splitFlatFeature,
                                    &shapValues);
        }
    }
}