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.utilities.owl;
021    
022    import java.util.Collection;
023    import java.util.Iterator;
024    import java.util.LinkedList;
025    import java.util.List;
026    import java.util.TreeSet;
027    
028    import org.dllearner.core.EvaluatedDescription;
029    import org.dllearner.core.AbstractLearningProblem;
030    import org.dllearner.core.owl.Description;
031    import org.dllearner.learningproblems.EvaluatedDescriptionPosNeg;
032    
033    /**
034     * A set of evaluated descriptions, which is bound by a maximum
035     * size. Can be used by algorithms to store the most promising
036     * n class descriptions.
037     * 
038     * @author Jens Lehmann
039     *
040     */
041    public class EvaluatedDescriptionSet {
042    
043            private EvaluatedDescriptionComparator comp = new EvaluatedDescriptionComparator();
044            
045            private TreeSet<EvaluatedDescription> set = new TreeSet<EvaluatedDescription>(comp);
046    
047            private int maxSize;
048            
049            public EvaluatedDescriptionSet(int maxSize) {
050                    this.maxSize = maxSize;
051            }
052            
053            public void add(Description description, double accuracy, AbstractLearningProblem problem) {
054                    // bug http://sourceforge.net/tracker/?func=detail&atid=986319&aid=3029181&group_id=203619
055                    // -> set should be filled up to max size before we compare acc. with the worst result
056                    if(set.size()<maxSize || getWorst().getAccuracy() <= accuracy) {
057                            set.add(problem.evaluate(description));
058                    }       
059                    if(set.size()>maxSize) {
060                            // delete the worst element
061                            Iterator<EvaluatedDescription> it = set.iterator();
062                            it.next();
063                            it.remove();
064                    }               
065            }
066            
067            public void add(EvaluatedDescription ed) {
068                    set.add(ed);
069                    if(set.size()>maxSize) {
070                            Iterator<EvaluatedDescription> it = set.iterator();
071                            it.next();
072                            it.remove();
073                    }
074            }
075    
076            public void addAll(Collection<EvaluatedDescriptionPosNeg> eds) {
077                    for(EvaluatedDescriptionPosNeg ed : eds) {
078                            add(ed);
079                    }
080            }       
081            
082            public boolean isFull() {
083                    return (set.size() >= maxSize);
084            }
085            
086            public int size() {
087                    return set.size();
088            }
089            
090            public EvaluatedDescription getBest() {
091                    return set.size()==0 ? null : set.last();
092            }
093            
094            public double getBestAccuracy() {
095                    return set.size()==0 ? Double.NEGATIVE_INFINITY : set.last().getAccuracy();
096            }
097            
098            public EvaluatedDescription getWorst() {
099                    return set.size()==0 ? null : set.first();
100            }
101            
102            /**
103             * @return the set
104             */
105            public TreeSet<EvaluatedDescription> getSet() {
106                    return set;
107            }
108            
109            public List<Description> toDescriptionList() {
110                    List<Description> list = new LinkedList<Description>();
111                    for(EvaluatedDescription ed : set.descendingSet()) {
112                            list.add(ed.getDescription());
113                    }
114                    return list;
115            }
116            
117            @Override
118            public String toString() {
119                    return set.toString();
120            }
121    
122            /**
123             * @return the maxSize
124             */
125            public int getMaxSize() {
126                    return maxSize;
127            }
128    }