class AveragePrecision extends AbstractRecommendationMetric<U, I> {

    @Override
    public double evaluate(Recommendation<U, I> recommendation) {
        UserIdealRelevanceModel<U, I> userRelModel = relModel.getModel(recommendation.getUser());

        int nRelItems = min(cutoff, userRelModel.getRelevantItems().size());
        if (nRelItems == 0) {
            return 0.0;
        }

        double ap = 0;
        int relCount = 0;
        int rank = 0;

        for (Tuple2od<I> pair : recommendation.getItems()) {
            rank++;
            if (userRelModel.isRelevant(pair.v1)) {
                relCount++;
                ap += relCount / (double) rank;
            }
            if (rank == cutoff) {
                break;
            }
        }

        return ap / nRelItems;
    }

    public  AveragePrecision(int cutoff, IdealRelevanceModel<U, I> relevanceModel);

}