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