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.learningproblems;
021    
022    import java.util.Set;
023    
024    import org.dllearner.core.owl.Individual;
025    
026    /**
027     * Calculates accuracy and score (with respect to some length penalty) of
028     * a class description. 
029     * 
030     * TODO: In fact, a score value influencing a learning algorithm
031     * should not be calculated here, but rather in a separate heuristic
032     * as there are many methods to calculate such a value. This class
033     * should only be used for computing example coverage, accuracy etc.
034     * 
035     * @author Jens Lehmann
036     *
037     */
038    public class ScoreTwoValued extends ScorePosNeg {
039    
040            private static final long serialVersionUID = 6264873890324824550L;
041            
042            private Set<Individual> posAsPos; 
043        private Set<Individual> posAsNeg;
044        private Set<Individual> negAsPos;
045        private Set<Individual> negAsNeg;    
046        private double score;
047        private double accuracy;
048        private int nrOfExamples;
049        private int conceptLength;
050        private double percentPerLengthUnit;
051            
052            public ScoreTwoValued(Set<Individual> posAsPos, Set<Individual> posAsNeg, Set<Individual> negAsPos, Set<Individual> negAsNeg) {
053                    this(0,0,posAsPos,posAsNeg,negAsPos,negAsNeg);
054            }    
055        
056            @Deprecated
057            public ScoreTwoValued(int conceptLength, double percentPerLengthUnit, Set<Individual> posAsPos, Set<Individual> posAsNeg, Set<Individual> negAsPos, Set<Individual> negAsNeg) {
058            this.conceptLength = conceptLength;
059            this.percentPerLengthUnit = percentPerLengthUnit;
060                    this.posAsPos = posAsPos;
061                    this.posAsNeg = posAsNeg;
062                    this.negAsPos = negAsPos;
063                    this.negAsNeg = negAsNeg;
064                    nrOfExamples = posAsPos.size()+posAsNeg.size()+negAsPos.size()+negAsNeg.size();
065                    computeScore();
066            }
067            
068            public ScoreTwoValued(int conceptLength, double percentPerLengthUnit, Set<Individual> posAsPos, Set<Individual> posAsNeg, Set<Individual> negAsPos, Set<Individual> negAsNeg, double accuracy) {
069            this.conceptLength = conceptLength;
070            this.percentPerLengthUnit = percentPerLengthUnit;
071                    this.posAsPos = posAsPos;
072                    this.posAsNeg = posAsNeg;
073                    this.negAsPos = negAsPos;
074                    this.negAsNeg = negAsNeg;
075                    nrOfExamples = posAsPos.size()+posAsNeg.size()+negAsPos.size()+negAsNeg.size();
076                    this.accuracy = accuracy;
077                    score = accuracy - 1 - percentPerLengthUnit * conceptLength;
078            }
079            
080            // score should not be computed within this class anymore, but directly within learning problem (to support
081            // different functions like predictive accuracy, F-measure etc.)
082            @Deprecated
083            private void computeScore() {
084                    // compute accuracy
085                    accuracy = posAsPos.size() + negAsNeg.size();
086                    accuracy = accuracy / (double) nrOfExamples;
087                    // compute score
088                    score = accuracy - 1 - percentPerLengthUnit * conceptLength;
089            }
090            
091            @Override
092            public double getAccuracy() {
093                    return accuracy;
094            }       
095            
096            /**
097             * score = accuracy - 1 - length * length penalty
098             */
099            @Override
100            public double getScoreValue() {
101                    return score;
102            }
103    
104            @Override
105            public String toString() {
106                    String str = "";
107                    str += "score: " + score + "\n";
108                    str += "accuracy: " + accuracy + "\n";
109                    str += "posAsPos (" + posAsPos.size() + "): " + posAsPos + "\n";
110                    str += "positive examples classified as negative (" + posAsNeg.size() + "): " + posAsNeg + "\n";
111                    str += "negative examples classified as positive (" + negAsPos.size() + "): " + negAsPos + "\n";
112                    return str;
113            }
114    
115            @Override
116            public Set<Individual> getCoveredNegatives() {
117                    return negAsPos;
118            }
119    
120            @Override
121            public Set<Individual> getCoveredPositives() {
122                    return posAsPos;
123            }
124            
125            @Override
126            public Set<Individual> getNotCoveredPositives() {
127                    return posAsNeg;
128            }
129            
130            @Override
131            public Set<Individual> getNotCoveredNegatives() {
132                    return negAsNeg;
133            }               
134            
135            @Override
136            public ScorePosNeg getModifiedLengthScore(int newLength) {
137                    return new ScoreTwoValued(newLength, percentPerLengthUnit, posAsPos, posAsNeg, negAsPos, negAsNeg);
138            }
139    
140    }