001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.algorithms.qtl.heuristics;
020
021import java.util.Set;
022
023import org.dllearner.algorithms.qtl.datastructures.impl.EvaluatedRDFResourceTree;
024import org.dllearner.core.ComponentAnn;
025import org.dllearner.core.ComponentInitException;
026import org.dllearner.learningproblems.Heuristics;
027import org.dllearner.learningproblems.QueryTreeScore;
028import org.semanticweb.owlapi.model.OWLIndividual;
029
030/**
031 * A simple heuristic based which just takes the accuracy into account.
032 * @author Lorenz Buehmann
033 *
034 */
035@ComponentAnn(name = "QueryTreeHeuristic", shortName = "qtree_heuristic_simple", version = 0.1)
036public class QueryTreeHeuristicSimple extends QueryTreeHeuristic {
037        
038        /* (non-Javadoc)
039         * @see org.dllearner.core.Component#init()
040         */
041        @Override
042        public void init() throws ComponentInitException {
043                initialized = true;
044        }
045        
046        @Override
047        public double getScore(EvaluatedRDFResourceTree tree){
048                QueryTreeScore treeScore = tree.getTreeScore();
049                
050                Set<OWLIndividual> truePositives = treeScore.getCoveredPositives();
051                Set<OWLIndividual> trueNegatives = treeScore.getNotCoveredNegatives();
052                Set<OWLIndividual> falsePositives = treeScore.getNotCoveredPositives();
053                Set<OWLIndividual> falseNegatives = treeScore.getCoveredNegatives();
054                
055                double tp = truePositives.size();
056                double tn = trueNegatives.size();
057                double fp = falsePositives.size();
058                double fn = falseNegatives.size();
059                
060                double score = 0;
061                switch(heuristicType){
062                        case FMEASURE : 
063                                score = Heuristics.getFScore(tp/(tp+fn), tp/(tp+fp), posExamplesWeight);break;
064                        case PRED_ACC : 
065                                score = (1/posExamplesWeight * tp + tn) / (1/posExamplesWeight * (tp + fn) + (tn + fp));break;
066                        case ENTROPY  :{
067                                double total = tp + fn;
068                                double pp = tp / total;
069                                double pn = fn / total;
070                                score = pp * Math.log(pp) + pn * Math.log(pn);
071                                break;}
072                        case MATTHEWS_CORRELATION  : // a measure between -1 and 1
073                                double denominator = Math.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));
074                                if(denominator == 0) { // 0 means not better than random prediction
075                                        return 0;
076//                                      denominator = 1;
077                                }
078                                score = (tp * tn - fp * fn) / denominator;break;
079                        case YOUDEN_INDEX : score = tp / (tp + fn) + tn / (fp + tn) - 1;break;
080                default:
081                        break;
082                        
083                }
084                
085                return score;
086        }
087}