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