001    /**
002     * Copyright (C) 2007-2011, 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     */
019    
020    package org.dllearner.algorithms.refinement;
021    
022    import org.dllearner.utilities.owl.ConceptComparator;
023    
024    /**
025     * This heuristic compares two nodes by computing a score
026     * using the number of covered negatives and the horizontal
027     * expansion factor of a node as input. Using this score
028     * it decides which one of the nodes seems to be more promising.
029     * The heuristic is flexible, because it offers a tradeoff
030     * between accurary and horizontal expansion (concept length).
031     * In contrast to the lexicographic heuristic this means that
032     * it sometimes prefers worse classifiers with low horizontal
033     * expansion over a better classifier with high horizontal
034     * expansion.
035     * 
036     * It can be configured by using the "percentPerLenghtUnit" 
037     * constructor argument. A higher
038     * value means that the algorithm is more likely to search in
039     * unexplored areas (= low horizontal expansion) of the search 
040     * space vs. looking in promising but already explored (= high
041     * horizontal expansion) areas of the search space.
042     * 
043     * @author Jens Lehmann
044     *
045     */
046    public class NodeComparator2 implements Heuristic {
047    
048            // Vergleich von Konzepten, falls alle anderen Kriterien fehlschlagen
049            private ConceptComparator conceptComparator = new ConceptComparator();
050            private int nrOfNegativeExamples;
051            private double percentPerLengthUnit;
052            
053            // 5% sind eine Verlängerung um 1 wert
054            // double percentPerLengthUnit = 0.05;
055            
056            public NodeComparator2(int nrOfNegativeExamples, double percentPerLengthUnit) {
057                    this.nrOfNegativeExamples = nrOfNegativeExamples;
058                    this.percentPerLengthUnit = percentPerLengthUnit;
059            }
060            
061            // implementiert einfach die Definition in der Diplomarbeit
062            public int compare(Node n1, Node n2) {
063                    
064                    // sicherstellen, dass Qualität ausgewertet wurde
065                    if(n1.isQualityEvaluated() && n2.isQualityEvaluated() && !n1.isTooWeak() && !n2.isTooWeak()) {
066                            
067                            // alle scores sind negativ, größere scores sind besser
068                            double score1 = -n1.getCoveredNegativeExamples()/(double)nrOfNegativeExamples;
069                            score1 -= percentPerLengthUnit * n1.getConcept().getLength();
070                            
071                            double score2 = -n2.getCoveredNegativeExamples()/(double)nrOfNegativeExamples;
072                            score2 -= percentPerLengthUnit * n2.getConcept().getLength();
073                            
074                            double diff = score1 - score2;
075                            
076                            if(diff>0)
077                                    return 1;
078                            else if(diff<0)
079                                    return -1;
080                            else
081                                    return conceptComparator.compare(n1.getConcept(), n2.getConcept());
082                    }
083                    
084                    throw new RuntimeException("Cannot compare nodes, which have no evaluated quality or are too weak.");
085            }
086    
087            @Override               
088            public boolean equals(Object o) {
089                    return (o instanceof NodeComparator2);
090            }
091            
092    }