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.ocel;
020
021import com.google.common.collect.ComparisonChain;
022import org.dllearner.core.ComponentAnn;
023import org.dllearner.core.ComponentInitException;
024import org.dllearner.core.config.ConfigOption;
025import org.dllearner.utilities.owl.OWLClassExpressionUtils;
026
027/**
028 * This heuristic compares two nodes by computing a score
029 * using the number of covered negatives and the horizontal
030 * expansion factor of a node as input. Using this score
031 * it decides which one of the nodes seems to be more promising.
032 * The heuristic is flexible, because it offers a tradeoff
033 * between accurary and horizontal expansion (concept length).
034 * In contrast to the lexicographic heuristic this means that
035 * it sometimes prefers worse classifiers with low horizontal
036 * expansion over a better classifier with high horizontal
037 * expansion.
038 * 
039 * It can be configured by using the "percentPerLenghtUnit" 
040 * constructor argument. A higher
041 * value means that the algorithm is more likely to search in
042 * unexplored areas (= low horizontal expansion) of the search 
043 * space vs. looking in promising but already explored (= high
044 * horizontal expansion) areas of the search space.
045 * 
046 * @author Jens Lehmann
047 *
048 */
049@ComponentAnn(name = "Flexible Heuristic", shortName = "flexheuristic", version = 0.1)
050public class FlexibleHeuristic implements ExampleBasedHeuristic {
051
052        @ConfigOption(description = "the number of negative examples")
053        private int nrOfNegativeExamples;
054        @ConfigOption(description = "score percent to deduct per expression length", required = true)
055        private double percentPerLengthUnit;
056        // 5% sind eine Verlängerung um 1 wert
057        // double percentPerLengthUnit = 0.05;
058
059        public int getNrOfNegativeExamples() {
060                return nrOfNegativeExamples;
061        }
062
063        public void setNrOfNegativeExamples(int nrOfNegativeExamples) {
064                this.nrOfNegativeExamples = nrOfNegativeExamples;
065        }
066
067        public double getPercentPerLengthUnit() {
068                return percentPerLengthUnit;
069        }
070
071        public void setPercentPerLengthUnit(double percentPerLengthUnit) {
072                this.percentPerLengthUnit = percentPerLengthUnit;
073        }
074
075        
076        public FlexibleHeuristic(int nrOfNegativeExamples, double percentPerLengthUnit) {
077                this.nrOfNegativeExamples = nrOfNegativeExamples;
078                this.percentPerLengthUnit = percentPerLengthUnit;
079        }
080
081        public FlexibleHeuristic() {
082        }
083        
084        // implementiert einfach die Definition in der Diplomarbeit
085        @Override
086        public int compare(ExampleBasedNode n1, ExampleBasedNode n2) {
087                
088                // sicherstellen, dass Qualität ausgewertet wurde
089                if(n1.isQualityEvaluated() && n2.isQualityEvaluated() && !n1.isTooWeak() && !n2.isTooWeak()) {
090                        
091                        // alle scores sind negativ, größere scores sind besser
092                        double score1 = -n1.getCoveredNegatives().size()/(double)nrOfNegativeExamples;
093                        score1 -= percentPerLengthUnit * OWLClassExpressionUtils.getLength(n1.getConcept());
094                        
095                        double score2 = -n2.getCoveredNegatives().size()/(double)nrOfNegativeExamples;
096                        score2 -= percentPerLengthUnit * OWLClassExpressionUtils.getLength(n2.getConcept());
097
098                        return ComparisonChain.start()
099                                        .compare(score1, score2)
100                                        .compare(n1.getConcept(), n2.getConcept())
101                                        .result();
102                }
103                
104                throw new RuntimeException("Cannot compare nodes, which have no evaluated quality or are too weak.");
105        }
106
107        @Override               
108        public boolean equals(Object o) {
109                return (o instanceof FlexibleHeuristic);
110        }
111
112        /* (non-Javadoc)
113         * @see org.dllearner.core.Component#init()
114         */
115        @Override
116        public void init() throws ComponentInitException {
117        }
118
119        @Override
120        public double getNodeScore(ExampleBasedNode n1) {
121                double score1 = -n1.getCoveredNegatives().size()/(double)nrOfNegativeExamples;
122                score1 -= percentPerLengthUnit * OWLClassExpressionUtils.getLength(n1.getConcept());
123                return score1;
124        }
125}