From: <tc...@us...> - 2012-05-13 17:14:22
|
Revision: 3706 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3706&view=rev Author: tcanvn Date: 2012-05-13 17:14:12 +0000 (Sun, 13 May 2012) Log Message: ----------- 1. Add PADCEL (PArallel Divide & Conquer Expression Learning) algorithm with the new splitting strategy used in PADCEL 2. Add abstract class for PADCELEx (PADCEL with Exceptions) algorithm (for new CLI reference). The implementation of this algorithm is being checked and committed Added Paths: ----------- trunk/components-core/src/main/java/nz/ trunk/components-core/src/main/java/nz/ac/ trunk/components-core/src/main/java/nz/ac/massey/ trunk/components-core/src/main/java/nz/ac/massey/abd/ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELAbstract.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELComplenessComparator.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCompletenessComparator.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCorrectnessComparator.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoverageGreedyReducer.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoveredNegativeExampleComparator.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefaultHeuristic.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionGenerationTimeComparator.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthComparator.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthReducer.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELEvaluationResult.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELExtraNode.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELGenerationTimeReducer.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELHeuristic.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELImprovedCovegareGreedyReducer.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELNode.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELOntologyUtil.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELPosNegLP.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELReducer.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELRefinementOperatorFactory.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELRefinementOperatorPool.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELScore.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELStringUtilities.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELTaskComparator.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELWorker.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELWorkerThreadFactory.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELearnerMBean.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/split/ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/split/PADCELDoubleSplitterAbstract.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/split/PADCELDoubleSplitterV1.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/split/ValueCount.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/split/ValuesSet.java trunk/components-core/src/main/java/org/dllearner/algorithms/PADCELEx/ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCELEx/PADCELExAbstract.java Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELAbstract.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELAbstract.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELAbstract.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,74 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.Set; +import java.util.SortedSet; +import java.util.concurrent.ConcurrentSkipListSet; + +import org.dllearner.core.AbstractCELA; +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.owl.Description; + +/** + * Abstract class for all PDL-Learner algorithms family + * + * @author An C. Tran + * + */ +public abstract class PADCELAbstract extends AbstractCELA { + + /** + * All generated descriptions thread-safe set is used to avoid concurrently + * accessing + */ + protected ConcurrentSkipListSet<Description> allDescriptions = null; + + /** + * Search tree. It hold all evaluated descriptions that are not correct and + * not weak ==> candidate for partial definitions Nodes in the search tree + * must be ordered using heuristic so that it can help the searching more + * efficiently (best search rather than 'blind' breadth first of depth + * first) NOTE: node = (description + accuracy/correctness/completeness/... + * value) + */ + protected ConcurrentSkipListSet<PADCELNode> searchTree = null; // thread safe + // set + + /** + * partial definitions (they should be sorted so that we can get the best + * partial definition at any time + */ + protected SortedSet<PADCELExtraNode> partialDefinitions = null; + + /** + * Default constructor + */ + public PADCELAbstract() { + super(); + } + + /** + * ======================================================================== + * Constructor for the learning algorithm + * + * @param learningProblem + * Must be a PDLLPosNegLP + * @param reasoningService + * A reasoner + */ + public PADCELAbstract(PADCELPosNegLP learningProblem, AbstractReasonerComponent reasoningService) { + super(learningProblem, reasoningService); + } + + public abstract Description getUnionCurrenlyBestDescription(); + + public abstract int getNoOfCompactedPartialDefinition(); + + public abstract Set<PADCELExtraNode> getPartialDefinitions(); + + public abstract double getCurrentlyOveralMaxCompleteness(); + + public long getNumberOfPartialDefinitions() { + return this.partialDefinitions.size(); + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELAbstract.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELComplenessComparator.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELComplenessComparator.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELComplenessComparator.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,37 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.Comparator; + +import org.dllearner.utilities.owl.ConceptComparator; + +/** + * Comparator for PDLLNode based on the completeness of the description If two + * nodes has the same completeness, description length will be taken into the + * consideration and finally the ConceptComparator + * + * @author An C. Tran + * + */ + +public class PADCELComplenessComparator implements Comparator<PADCELNode> { + + @Override + public int compare(PADCELNode node1, PADCELNode node2) { + int v1 = node1.getCoveredPositiveExamples().size(); + int v2 = node2.getCoveredPositiveExamples().size(); + if (v1 > v2) + return -1; + else if (v1 < v2) + return 1; + else { + if (node1.getDescription().getLength() < node2.getDescription().getLength()) + return -1; + else if (node1.getDescription().getLength() > node2.getDescription().getLength()) + return 1; + else + return new ConceptComparator().compare(node1.getDescription(), + node2.getDescription()); + } + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELComplenessComparator.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCompletenessComparator.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCompletenessComparator.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCompletenessComparator.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,38 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.Comparator; + +import org.dllearner.utilities.owl.ConceptComparator; + +/** + * Use to compare 2 ExtraPLOENode nodes based on their completeness (coverage). The description + * length and ConceptComparator will be used it they have equal coverage + * + * @author An C. Tran + * + */ +public class PADCELCompletenessComparator implements Comparator<PADCELExtraNode> { + + @Override + public int compare(PADCELExtraNode node1, PADCELExtraNode node2) { + + int v1 = node1.getCoveredPositiveExamples().size(); + int v2 = node2.getCoveredPositiveExamples().size(); + + if (v1 > v2) + return -1; + else if (v1 < v2) + return 1; + else { + int len1 = node1.getDescription().getLength(); + int len2 = node2.getDescription().getLength(); + if (len1 < len2) + return -1; + else if (len1 > len2) + return 1; + else + return new ConceptComparator().compare(node1.getDescription(), + node2.getDescription()); + } + } +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCompletenessComparator.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCorrectnessComparator.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCorrectnessComparator.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCorrectnessComparator.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,38 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.Comparator; + +import org.dllearner.utilities.owl.ConceptComparator; + +/** + * Use to compare 2 ExtraPLOENode nodes based on their correctness. The description length and + * ConceptComparator will be used it they have equal coverage + * + * @author An C. Tran + * + */ +public class PADCELCorrectnessComparator implements Comparator<PADCELExtraNode> { + + @Override + public int compare(PADCELExtraNode node1, PADCELExtraNode node2) { + double correctness1 = node1.getCorrectness(); + double correctness2 = node2.getCorrectness(); + + if (correctness1 > correctness2) + return -1; // smaller will be on the top + else if (correctness1 < correctness2) + return 1; + else { + int len1 = node1.getDescription().getLength(); + int len2 = node2.getDescription().getLength(); + + if (len1 < len2) + return -1; + else if (len1 > len2) + return 1; + else + return new ConceptComparator().compare(node1.getDescription(), + node2.getDescription()); + } + } +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCorrectnessComparator.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoverageGreedyReducer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoverageGreedyReducer.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoverageGreedyReducer.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,82 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.owl.Individual; + +/** + * This class implements a simple strategy for compacting the partial definition set In this + * strategy, the partial definition will be chosen based on their accuracy. The partial definition + * with the best accuracy will be chosen first and the rests will not be re-calculated before the + * next reduction + * + * @author An C. Tran + * + */ +public class PADCELCoverageGreedyReducer implements PADCELReducer { + + /** + * Compact partial definitions + * + * @param partialDefinitions + * Set of partial definitions + * @param positiveExamples + * Set of positive examples (used to check whether partial definition is useful + * @param uncoveredPositiveExamples + * Number of uncovered positive examples allowed + * + * @return Subset of partial definitions that cover all positive examples + */ + @Override + public SortedSet<PADCELExtraNode> compact(SortedSet<PADCELExtraNode> partialDefinitions, + Set<Individual> positiveExamples) { + return compact(partialDefinitions, positiveExamples, 0); + } + + /** + * Compact partial definition with noise allowed + * + * @param partialDefinitions + * Set of partial definitions + * @param positiveExamples + * Set of positive examples (used to check whether partial definition is useful + * @param uncoveredPositiveExamples + * Number of uncovered positive examples allowed + * + * @return Subset of partial definitions that cover (positive examples \ uncovered positive + * examples) + */ + @Override + public SortedSet<PADCELExtraNode> compact(SortedSet<PADCELExtraNode> partialDefinitions, + Set<Individual> positiveExamples, int uncoveredPositiveExamples) { + + Set<Individual> positiveExamplesTmp = new HashSet<Individual>(); + positiveExamplesTmp.addAll(positiveExamples); + + TreeSet<PADCELExtraNode> minimisedPartialDefinition = new TreeSet<PADCELExtraNode>( + new PADCELCorrectnessComparator()); + + Iterator<PADCELExtraNode> partialDefinitionIterator = partialDefinitions.iterator(); + while ((positiveExamplesTmp.size() > uncoveredPositiveExamples) + && (partialDefinitionIterator.hasNext())) { + PADCELExtraNode node = partialDefinitionIterator.next(); + + int positiveExamplesRemoved = positiveExamplesTmp.size(); + positiveExamplesTmp.removeAll(node.getCoveredPositiveExamples()); + + positiveExamplesRemoved -= positiveExamplesTmp.size(); + + if (positiveExamplesRemoved > 0) { + node.setCorrectness(positiveExamplesRemoved); + minimisedPartialDefinition.add(node); + } + } + + return minimisedPartialDefinition; + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoverageGreedyReducer.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoveredNegativeExampleComparator.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoveredNegativeExampleComparator.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoveredNegativeExampleComparator.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,38 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.Comparator; + +import org.dllearner.utilities.owl.ConceptComparator; + +/** + * Use to compare 2 ExtraPLOENode nodes based on the number of covered negative examples. The + * description length and ConceptComparator will be used it they have equal coverage + * + * @author An C. Tran + * + */ +public class PADCELCoveredNegativeExampleComparator implements Comparator<PADCELExtraNode> { + + @Override + public int compare(PADCELExtraNode node1, PADCELExtraNode node2) { + int coveredNeg1 = node1.getCoveredNegativeExamples().size(); + int coveredNeg2 = node2.getCoveredPositiveExamples().size(); + + if (coveredNeg1 > coveredNeg2) + return -1; // smaller will be on the top + else if (coveredNeg1 < coveredNeg2) + return 1; + else { + int len1 = node1.getDescription().getLength(); + int len2 = node2.getDescription().getLength(); + + if (len1 < len2) + return -1; + else if (len1 > len2) + return 1; + else + return new ConceptComparator().compare(node1.getDescription(), + node2.getDescription()); + } + } +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELCoveredNegativeExampleComparator.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefaultHeuristic.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefaultHeuristic.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefaultHeuristic.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,95 @@ +package org.dllearner.algorithms.PADCEL; + +import org.dllearner.utilities.owl.ConceptComparator; + +/** + * Implements the heuristic used to expand the search tree. Dimensions used: <br> + * + correctness: main value<br> + * + horizontal expansion: penalty<br> + * + accuracy gained from the parent node: bonus<br> + * + refinement nodes: penalty<br> + * + concept type + name (org.dllearner.utilities.owl.ConceptComparator) + * + * @author An C. Tran + * + */ +public class PADCELDefaultHeuristic implements PADCELHeuristic { + + // penalty for long descriptions + protected double expansionPenaltyFactor = 0.1; + + // bonus for gained accuracy + protected double gainBonusFactor = 0.2; + + // penalty if a node description has very many refinements since exploring + // such a node is computationally very expensive + protected double nodeRefinementPenalty = 0.0001; + + // award for node with high accuracy + protected double accuracyAwardFactor = 0.01; + + // syntactic comparison as final comparison criterion + protected ConceptComparator conceptComparator = new ConceptComparator(); + + /** + * Compare two node + * + * @param node1 + * Node to compare + * @param node2 + * Node to compare + * + * @return 1 if node1 "greater" than node2 and vice versa + */ + public int compare(PADCELNode node1, PADCELNode node2) { + double diff = getNodeScore(node1) - getNodeScore(node2); + + if (diff > 0) { // node1 has better score than node2 + return 1; + } else if (diff < 0) { + return -1; + } else { + int comp = conceptComparator.compare(node1.getDescription(), node2.getDescription()); + + // this allows duplicate descriptions exists in the set + if (comp != 0) + return comp; + else + return -1; + + } + } + + /** + * Calculate score for a node which is used as the searching heuristic + * + * @param node + * Node to be scored + * + * @return Score of the node + */ + protected double getNodeScore(PADCELNode node) { + + // the scoring mainly bases on correctness + double score = node.getCorrectness(); + + // bonus for the accuracy gained + if (!node.isRoot()) { + double parentAccuracy = ((PADCELNode) (node.getParent())).getAccuracy(); + score += (parentAccuracy - node.getAccuracy()) * gainBonusFactor; + } + + // award node with high accuracy + score += node.getAccuracy() * accuracyAwardFactor; + + // penalty for horizontal expansion + score -= node.getHorizontalExpansion() * expansionPenaltyFactor; + // score -= node.getDescription().getLength() * expansionPenaltyFactor; + + return score; + } + + public double getScore(PADCELNode node) { + return this.getNodeScore(node); + } +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefaultHeuristic.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionGenerationTimeComparator.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionGenerationTimeComparator.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionGenerationTimeComparator.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,37 @@ +package org.dllearner.algorithms.PADCEL; + +/** + * Compare two node based on their generation time. + * This will be used in the Generation Time Greedy Compactness strategy + * GOLR + * + * @author An C. Tran + */ + +import java.util.Comparator; + +import org.dllearner.utilities.owl.ConceptComparator; + +public class PADCELDefinitionGenerationTimeComparator implements Comparator<PADCELExtraNode> { + + @Override + public int compare(PADCELExtraNode node1, PADCELExtraNode node2) { + double genTime1 = node1.getGenerationTime(); + double genTime2 = node2.getGenerationTime(); + + if (genTime1 < genTime2) + return -1; + else if (genTime1 > genTime2) + return 1; + else { + if (node1.getDescription().getLength() < node2.getDescription().getLength()) + return -1; + else if (node1.getDescription().getLength() > node2.getDescription().getLength()) + return 1; + else + return new ConceptComparator().compare(node1.getDescription(), + node2.getDescription()); + } + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionGenerationTimeComparator.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthComparator.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthComparator.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthComparator.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,29 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.Comparator; + +import org.dllearner.utilities.owl.ConceptComparator; + +/** + * Compare two node based on their definition length. This will be used in the Definition Length + * Greedy Compactness strategy + * + * @author An C. Tran + * + */ + +public class PADCELDefinitionLengthComparator implements Comparator<PADCELExtraNode> { + + @Override + public int compare(PADCELExtraNode node1, PADCELExtraNode node2) { + int len1 = node1.getDescription().getLength(); + int len2 = node2.getDescription().getLength(); + if (len1 < len2) + return -1; + else if (len1 > len2) + return 1; + else + return new ConceptComparator().compare(node1.getDescription(), node2.getDescription()); + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthComparator.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthReducer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthReducer.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthReducer.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,59 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.owl.Individual; + +/** + * Compact set of partial definitions using Definition Length Grredy Reduction strategy + * + * @author An C. Tran + * + */ +public class PADCELDefinitionLengthReducer implements PADCELReducer { + + @Override + public SortedSet<PADCELExtraNode> compact(SortedSet<PADCELExtraNode> partialDefinitions, + Set<Individual> positiveExamples) { + return compact(partialDefinitions, positiveExamples, 0); + } + + @Override + public SortedSet<PADCELExtraNode> compact(SortedSet<PADCELExtraNode> partialDefinitions, + Set<Individual> positiveExamples, int uncoveredPositiveExamples) { + Set<Individual> positiveExamplesTmp = new HashSet<Individual>(); + positiveExamplesTmp.addAll(positiveExamples); + + TreeSet<PADCELExtraNode> newSortedPartialDefinitions = new TreeSet<PADCELExtraNode>( + new PADCELDefinitionLengthComparator()); + synchronized (partialDefinitions) { + newSortedPartialDefinitions.addAll(partialDefinitions); + } + + TreeSet<PADCELExtraNode> minimisedPartialDefinition = new TreeSet<PADCELExtraNode>( + new PADCELDefinitionGenerationTimeComparator()); + + Iterator<PADCELExtraNode> partialDefinitionIterator = newSortedPartialDefinitions.iterator(); + while ((positiveExamplesTmp.size() > uncoveredPositiveExamples) + && (partialDefinitionIterator.hasNext())) { + PADCELExtraNode node = partialDefinitionIterator.next(); + + int positiveExamplesRemoved = positiveExamplesTmp.size(); + positiveExamplesTmp.removeAll(node.getCoveredPositiveExamples()); + + positiveExamplesRemoved -= positiveExamplesTmp.size(); + + if (positiveExamplesRemoved > 0) { + node.setCorrectness(positiveExamplesRemoved); + minimisedPartialDefinition.add(node); + } + } + + return minimisedPartialDefinition; + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELDefinitionLengthReducer.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELEvaluationResult.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELEvaluationResult.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELEvaluationResult.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,132 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.Set; + +import org.dllearner.core.owl.Individual; + +/** + * This class represents the result of an evaluation result return from the PDLL learning problem + * (PDLLPosNegLP) <br> + * Information included in a PDLL evaluation includes: + * <ol> + * <li>- accuracy: double</li> + * <li>- correctness: double</li> + * <li>- completeness: double</li> + * <li>- covered positive examples: Set<Individual></li> + * <li>- covered negative examples: Set<Individual></li> + * </ol> + * + * @author An C. Tran + * + */ +public class PADCELEvaluationResult { + + protected double accuracy = -1; + protected double correctness = -1; + protected double completeness = -1; + protected Set<Individual> coveredPossitiveExamples = null; + protected Set<Individual> coveredNegativeExamples = null; + + /** + * Default constructor, use default value for all properties + */ + public PADCELEvaluationResult() { + + } + + /** + * This use to create an evaluation result that is not for a correct definition. So, we don't + * need to hold the set of covered positive examples + * + * @param accuracy + * @param correctness + */ + public PADCELEvaluationResult(double accuracy, double correctness, double completeness) { + this.accuracy = accuracy; + this.correctness = correctness; + this.completeness = completeness; + } + + /** + * Used to create an evaluation result for a correct definition.<br> + * Covered positive examples have to be kept to be used in the result compactness later on.<br> + * This is usually used in case of partial definition + * + * @param accuracy + * @param correctness + * @param completeness + * @param coveredPossitiveExamples + */ + public PADCELEvaluationResult(double accuracy, double correctness, double completeness, + Set<Individual> coveredPossitiveExamples) { + this.accuracy = accuracy; + this.correctness = correctness; + this.completeness = completeness; + this.coveredPossitiveExamples = coveredPossitiveExamples; + } + + /** + * Used to create an evaluation result for a correct definition.<br> + * Both covered positive examples and covered negative examples will be kept to be used in the + * result compactness later on. + * + * @param accuracy + * @param correctness + * @param completeness + * @param coveredPositiveExamples + * @param coveredNegativeExamples + */ + public PADCELEvaluationResult(double accuracy, double correctness, double completeness, + Set<Individual> coveredPositiveExamples, Set<Individual> coveredNegativeExamples) { + + this.accuracy = accuracy; + this.correctness = correctness; + this.completeness = completeness; + this.coveredPossitiveExamples = coveredPositiveExamples; + this.coveredNegativeExamples = coveredNegativeExamples; + } + + // --------------------------------------- + // Getters and setters + // --------------------------------------- + public double getAccuracy() { + return accuracy; + } + + public void setAccuracy(double accuracy) { + this.accuracy = accuracy; + } + + public double getCorrectness() { + return correctness; + } + + public void setCorrectness(double correctness) { + this.correctness = correctness; + } + + public double getCompleteness() { + return completeness; + } + + public void setCompleteness(double completeness) { + this.completeness = completeness; + } + + public Set<Individual> getCoveredPossitiveExamples() { + return coveredPossitiveExamples; + } + + public void setCoveredPossitiveExamples(Set<Individual> coveredPossitiveExamples) { + this.coveredPossitiveExamples = coveredPossitiveExamples; + } + + public Set<Individual> getCoveredNegativeExamples() { + return coveredNegativeExamples; + } + + public void setCoveredNegativeExamples(Set<Individual> coveredNegativeExamples) { + this.coveredNegativeExamples = coveredNegativeExamples; + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELEvaluationResult.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELExtraNode.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELExtraNode.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELExtraNode.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,132 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.HashSet; +import java.util.Set; + +import org.dllearner.algorithms.celoe.OENode; +import org.dllearner.core.owl.Description; +import org.dllearner.core.owl.Individual; + +/** + * Generation time and node type is added into PADCELNode. This information is necessary for some + * reduction algorithms + * + * @author An C. Tran + * + */ +public class PADCELExtraNode extends PADCELNode { + + private double generationTime = Double.MIN_VALUE; // time in ms that the node was generated + private int type = -1; + + Set<OENode> compositeNodes = new HashSet<OENode>(); + + /** + * ============================================================================================ + * Constructor with correctness of the description + * + * @param parentNode + * Parent node of this node + * @param description + * Description of the node + * @param accuracy + * Accuracy of the node + * @param distance + * Distance between the node and the learning problem + * @param correctness + * Correctness of the node + */ + public PADCELExtraNode(OENode parentNode, Description description, double accuracy, + double correctness) { + super(parentNode, description, accuracy, correctness); + } + + public PADCELExtraNode(OENode parentNode, Description description, double accuracy) { + super(parentNode, description, accuracy); + } + + public PADCELExtraNode(PADCELNode node) { + super(node.getParent(), node.getDescription(), node.getAccuracy(), node.getCorrectness()); + setCoveredPositiveExamples(node.getCoveredPositiveExamples()); + setCoveredNegativeExamples(node.getCoveredNegativeExamples()); + } + + public PADCELExtraNode(PADCELNode node, Set<Individual> cp, double generationTime) { + super(node.getParent(), node.getDescription(), node.getAccuracy(), node.getCorrectness()); + super.coveredPositiveExamples = cp; + this.generationTime = generationTime; + } + + /** + * ============================================================================================ + * Constructor with the correctness and the generation time of the description + * + * @param parentNode + * Parent node of this node + * @param description + * Description of the node + * @param accuracy + * Accuracy of the node + * @param distance + * Distance between the node and the learning problem + * @param correctness + * Correctness of the node + * @param genTime + * Time in ms that the work used to generate this node + */ + public PADCELExtraNode(PADCELNode parentNode, Description description, double accuracy, + double correctness, double genTime) { + super(parentNode, description, accuracy, correctness); + this.coveredPositiveExamples = null; + this.generationTime = genTime; + } + + /** + * ============================================================================================ + * Constructor with the set of positive examples covered by the description of the node + * + * @param parentNode + * @param description + * @param accuracy + * @param distance + * @param correctness + * @param cn + * Covered positive examples + */ + public PADCELExtraNode(PADCELNode parentNode, Description description, double accuracy, + double correctness, Set<Individual> cp) { + super(parentNode, description, accuracy, correctness); + super.setCoveredPositiveExamples(cp); + } + + // ------------------------- + // getters and setters + // ------------------------- + public void setCoveredPositiveExamples(Set<Individual> cpn) { + super.setCoveredPositiveExamples(cpn); + } + + public double getGenerationTime() { + return generationTime; + } + + public void setGenerationTime(double d) { + this.generationTime = d; + } + + public void setType(int t) { + this.type = t; + } + + public int getType() { + return this.type; + } + + public void setCompositeList(Set<PADCELExtraNode> compositeNodes) { + this.compositeNodes.addAll(compositeNodes); + } + + public Set<OENode> getCompositeNodes() { + return this.compositeNodes; + } +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELExtraNode.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELGenerationTimeReducer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELGenerationTimeReducer.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELGenerationTimeReducer.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,62 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.owl.Individual; + +/** + * Compact two a partial definitions set using Generation Time Greedy strategy + * + * @author An C. Tran + * + */ + +public class PADCELGenerationTimeReducer implements PADCELReducer { + + @Override + public SortedSet<PADCELExtraNode> compact(SortedSet<PADCELExtraNode> partialDefinitions, + Set<Individual> positiveExamples) { + return compact(partialDefinitions, positiveExamples, 0); + } + + @Override + public SortedSet<PADCELExtraNode> compact(SortedSet<PADCELExtraNode> partialDefinitions, + Set<Individual> positiveExamples, int uncoveredPositiveExamples) { + Set<Individual> positiveExamplesTmp = new HashSet<Individual>(); + positiveExamplesTmp.addAll(positiveExamples); + + TreeSet<PADCELExtraNode> newSortedPartialDefinitions = new TreeSet<PADCELExtraNode>( + new PADCELDefinitionGenerationTimeComparator()); + + synchronized (partialDefinitions) { + newSortedPartialDefinitions.addAll(partialDefinitions); + } + + TreeSet<PADCELExtraNode> minimisedPartialDefinition = new TreeSet<PADCELExtraNode>( + new PADCELDefinitionGenerationTimeComparator()); + + Iterator<PADCELExtraNode> partialDefinitionIterator = newSortedPartialDefinitions.iterator(); + + while ((positiveExamplesTmp.size() > uncoveredPositiveExamples) + && (partialDefinitionIterator.hasNext())) { + PADCELExtraNode node = partialDefinitionIterator.next(); + + int positiveExamplesRemoved = positiveExamplesTmp.size(); + positiveExamplesTmp.removeAll(node.getCoveredPositiveExamples()); + + positiveExamplesRemoved -= positiveExamplesTmp.size(); + + if (positiveExamplesRemoved > 0) { + node.setCorrectness(positiveExamplesRemoved); + minimisedPartialDefinition.add(node); + } + } + + return minimisedPartialDefinition; + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELGenerationTimeReducer.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELHeuristic.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELHeuristic.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELHeuristic.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,16 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.Comparator; + +/** + * Interface for heuristics used in PADCEL + * + * @author An C. Tran + * + */ +public interface PADCELHeuristic extends Comparator<PADCELNode> { + + public int compare(PADCELNode node1, PADCELNode node2); + + public double getScore(PADCELNode node); +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELHeuristic.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELImprovedCovegareGreedyReducer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELImprovedCovegareGreedyReducer.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELImprovedCovegareGreedyReducer.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,115 @@ +package org.dllearner.algorithms.PADCEL; + +import java.util.HashSet; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.apache.log4j.Logger; +import org.dllearner.core.owl.Individual; + +/** + * This class implements "wise" coverage greedy strategy for compacting the partial definitions In + * this strategy, the partial definitions will be chosen based on their coverage. When a partial + * definition has been chosen, coverage of other partial definition will be recalculated + * + * @author An C. Tran + * + */ + +public class PADCELImprovedCovegareGreedyReducer implements PADCELReducer { + + Logger logger = Logger.getLogger(this.getClass()); + + /** + * Compact partial definition with noise allowed + * + * @param partialDefinitions + * Set of partial definitions + * @param positiveExamples + * Set of positive examples (used to check whether partial definition is useful + * @param uncoveredPositiveExamples + * Number of uncovered positive examples allowed + * + * @return Subset of partial definitions that cover all positive examples + */ + @Override + public SortedSet<PADCELExtraNode> compact(SortedSet<PADCELExtraNode> partialDefinitions, + Set<Individual> positiveExamples) { + return this.compact(partialDefinitions, positiveExamples, 0); + } + + /** + * Compact partial definition with noise allowed + * + * @param partialDefinitions + * Set of partial definitions + * @param positiveExamples + * Set of positive examples (used to check whether partial definition is useful + * @param uncoveredPositiveExamples + * Number of uncovered positive examples allowed + * + * @return Subset of partial definitions that cover (positive examples \ uncovered positive + * examples) + */ + @Override + public SortedSet<PADCELExtraNode> compact(SortedSet<PADCELExtraNode> partialDefinitions, + Set<Individual> positiveExamples, int uncoveredPositiveExamples) { + Set<Individual> positiveExamplesTmp = new HashSet<Individual>(); + positiveExamplesTmp.addAll(positiveExamples); + + TreeSet<PADCELExtraNode> reducedPartialDefinition = new TreeSet<PADCELExtraNode>( + new PADCELCompletenessComparator()); + + if (partialDefinitions.size() == 0) + return reducedPartialDefinition; + + synchronized (partialDefinitions) { + Object[] partialDefs = partialDefinitions.toArray(); + + // the highest accurate partial definition + // reducedPartialDefinition.add((PDLLExtraNode)partialDefs[0]); + // positiveExamplesTmp.removeAll(((PDLLExtraNode)partialDefs[0]).getCoveredPositiveExamples()); + + for (int i = 0; (positiveExamplesTmp.size() > uncoveredPositiveExamples) + && (i < partialDefinitions.size()); i++) { + + // count the number of different positive examples covered + int counti = 0; + for (Individual indi : ((PADCELExtraNode) partialDefs[i]) + .getCoveredPositiveExamples()) { + if (positiveExamplesTmp.contains(indi)) + counti++; + } // count the number of different covered positive examples by + // i + + for (int j = i + 1; j < partialDefinitions.size(); j++) { + int countj = 0; + + for (Individual indj : ((PADCELExtraNode) partialDefs[j]) + .getCoveredPositiveExamples()) + if (positiveExamplesTmp.contains(indj)) + countj++; + + // TODO: revise this code: Swapping should be done only one + // time at the end + // swap the partial definition so that the "best" partial + // definition will be in the top + if (countj > counti) { + PADCELExtraNode tmp = (PADCELExtraNode) partialDefs[j]; + partialDefs[j] = partialDefs[i]; + partialDefs[i] = tmp; + counti = countj; + } + } + + reducedPartialDefinition.add((PADCELExtraNode) partialDefs[i]); + positiveExamplesTmp.removeAll(((PADCELExtraNode) partialDefs[i]) + .getCoveredPositiveExamples()); + } + } + + return reducedPartialDefinition; + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELImprovedCovegareGreedyReducer.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELNode.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELNode.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELNode.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,113 @@ +package org.dllearner.algorithms.PADCEL; + +import java.text.DecimalFormat; +import java.util.HashSet; +import java.util.Set; + +import org.dllearner.algorithms.celoe.OENode; +import org.dllearner.core.owl.Description; +import org.dllearner.core.owl.Individual; + +/** + * Represents a node in the search tree used in the PDLLeanring<br> + * A node includes description and its corresponding properties such as: correctness, accuracy, + * distance between the description the leaning problem, parent node of the description. It also + * contains a flag which indicates the node is processed or not. + * + * @author An C. Tran + * + */ +public class PADCELNode extends OENode { + + private double correctness = -1.0; + private double completeness = -1.0; + + protected Set<Individual> coveredPositiveExamples = new HashSet<Individual>(); + protected Set<Individual> coveredNegativeExamples = new HashSet<Individual>(); + + private DecimalFormat dfPercent = new DecimalFormat("0.00%"); + + public PADCELNode(OENode parentNode, Description description, double accuracy) { + super(parentNode, description, accuracy); + } + + public PADCELNode(OENode parentNode, Description description, double accuracy, double correctness) { + super(parentNode, description, accuracy); + this.correctness = correctness; + } + + public PADCELNode(OENode parentNode, Description description, double accuracy, + double correctness, double completeness) { + super(parentNode, description, accuracy); + this.correctness = correctness; + this.completeness = completeness; + } + + public PADCELNode(OENode parentNode, Description description) { + super(parentNode, description, 0); + } + + public PADCELNode(OENode parentNode, Description description, + Set<Individual> coveredPositiveExamples, Set<Individual> coveredNegativeExamples) { + super(parentNode, description, 0); + this.coveredPositiveExamples.addAll(coveredPositiveExamples); + this.coveredNegativeExamples.addAll(coveredNegativeExamples); + } + + public void setCorrectness(double cor) { + this.correctness = cor; + } + + public double getCorrectness() { + return this.correctness; + } + + public void setCompleteness(double comp) { + this.completeness = comp; + } + + public double getCompleteness() { + return this.completeness; + } + + public void setAccuracy(double acc) { + this.accuracy = acc; + } + + public Set<Individual> getCoveredPositiveExamples() { + return this.coveredPositiveExamples; + } + + public Set<Individual> getCoveredNegativeExamples() { + return this.coveredNegativeExamples; + } + + public void setCoveredPositiveExamples(Set<Individual> coveredPositiveExamples) { + if (coveredPositiveExamples != null) + this.coveredPositiveExamples.addAll(coveredPositiveExamples); + else + this.coveredPositiveExamples.clear(); + } + + public void setCoveredNegativeExamples(Set<Individual> coveredNegativeExamples) { + if (coveredNegativeExamples != null) + this.coveredNegativeExamples.addAll(coveredNegativeExamples); + else + this.coveredNegativeExamples.clear(); + } + + @Override + public String toString() { + String ret = this.getDescription().toString(null, null) + " ["; + ret += "acc:" + dfPercent.format(this.getAccuracy()) + ", "; + ret += "cor:" + dfPercent.format(this.getCorrectness()) + ", "; + ret += "comp:" + dfPercent.format(this.completeness) + "]"; + return ret; + + } + + public void setDescription(Description newDescription) { + this.description = newDescription; + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELNode.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELOntologyUtil.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELOntologyUtil.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELOntologyUtil.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,87 @@ +package org.dllearner.algorithms.PADCEL; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.OWLOntologyStorageException; + +/** + * Some utility functions for ontology manipulation + * + * @author An C. Tran + * + */ + +public class PADCELOntologyUtil { + + /** + * ========================================================================================== + * Get ontology given its file path + * + * @param ontologyFilePath + * + * @return Opened ontology + * @throws OWLOntologyCreationException + */ + public static OWLOntology loadOntology(String ontologyFilePath) + throws OWLOntologyCreationException { + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); + OWLOntology ontology; + + String flash = (System.getProperty("os.name").contains("Windows")) ? "/" : ""; + + File f = new File(ontologyFilePath); + + if (!ontologyFilePath.contains("file:")) + ontologyFilePath = "file:" + flash + f.getAbsolutePath(); + + ontologyFilePath = ontologyFilePath.replace('\\', '/'); + + ontology = manager.loadOntology(IRI.create(ontologyFilePath)); + + return ontology; + } + + /** + * ============================================================================================ + * Persist the ontology + * + * @param ontology + * Ontology which need to be persisted + * + * @throws OWLOntologyStorageException + */ + public static void persistOntology(OWLOntology ontology) throws OWLOntologyStorageException { + OWLOntologyManager manager = ontology.getOWLOntologyManager(); + manager.saveOntology(ontology); + } + + /** + * ============================================================================================ + * Persist ontology to another ontology + * + * @param ontology + * Ontology contains changes + * @param newFilePath + * Path to the new ontology file + * + * @throws OWLOntologyStorageException + * @throws IOException + */ + public static void persistOntology(OWLOntology ontology, String newFilePath) + throws OWLOntologyStorageException, IOException { + OWLOntologyManager manager = ontology.getOWLOntologyManager(); + + File f = new File(newFilePath); + FileOutputStream fo = new FileOutputStream(f); + + manager.saveOntology(ontology, fo); + fo.close(); + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELOntologyUtil.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELPosNegLP.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELPosNegLP.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/PADCEL/PADCELPosNegLP.java 2012-05-13 17:14:12 UTC (rev 3706) @@ -0,0 +1,493 @@ +package org.dllearner.algorithms.PADCEL; + +/** + * PDLL Learning problem: provides correctness, completeness, and accuracy calculation. + * Predictive accuracy calculation is used. + * + * @author An C. Tran + */ + +import java.util.Set; + +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; + +import org.apache.log4j.Logger; +import org.dllearner.core.AbstractLearningProblem; +import org.dllearner.core.ComponentAnn; +import org.dllearner.core.ComponentInitException; +import org.dllearner.core.AbstractReasonerComponent; +import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.options.ConfigOption; +import org.dllearner.core.options.StringSetConfigOption; +import org.dllearner.core.owl.Description; +import org.dllearner.core.owl.Individual; + +@ComponentAnn(name = "PDLLPosNegLP", shortName = "pdllPosNegLP", version = 0.1, description = "PDLL positive negative learning problem") +public class PADCELPosNegLP extends AbstractLearningProblem { + + protected Set<Individual> positiveExamples; + protected Set<Individual> negativeExamples; + + protected Set<Individual> uncoveredPositiveExamples; + + private Logger logger = Logger.getLogger(this.getClass()); + + // reasoner component is declared in AbstractLearningProblem class + + /** + * Constructor, used in case that positive and negative examples are provided when this + * component is initialized + * + * @param reasoningService + * Reasoner, provides reasoning service. Used to checking the instance type + * @param positiveExamples + * Positive examples + * @param negativeExamples + * Negative examples + */ + public PADCELPosNegLP(AbstractReasonerComponent reasoningService, + Set<Individual> positiveExamples, Set<Individual> negativeExamples) { + super(reasoningService); + this.positiveExamples = positiveExamples; + this.negativeExamples = negativeExamples; + this.uncoveredPositiveExamples = this.positiveExamples; + } + + /** + * This constructor is used when the learning configuration file is used + * + * @param reasoningService + */ + public PADCELPosNegLP(AbstractReasonerComponent reasoningService) { + super(reasoningService); + } + + /** + * This constructor can be used by SpringDefinition to create bean object Properties of new bean + * may be initialised later using setters + */ + public PADCELPosNegLP() { + super(); + + if (logger.isDebugEnabled()) + logger.debug("Learning problem created: " + this); + } + + /** + * Get list of positive examples covered by a description + * + * @param description + * Description + * + * @return Set of positive examples covered by the description + */ + protected Set<Individual> coveredPositiveExamples(Description description) { + Set<Individual> coveredPositiveExamples = new HashSet<Individual>(); + + for (Individual example : positiveExamples) + if (reasoner.hasType(description, example)) + coveredPositiveExamples.add(example); + + return coveredPositiveExamples; + } + + /** + * Get number of positive examples covered by a description + * + * @param description + * Description + * @return Number if positive examples covered by the description + */ + protected int getNumberCoveredPositiveExamples(Description description) { + int coveredPos = 0; + + for (Individual example : positiveExamples) + if (reasoner.hasType(description, example)) + coveredPos++; + + return coveredPos; + } + + /** + * Get number of negative examples covered by a description + * + * @param description + * Description to test + * + * @return Number of negative examples covered by the description + */ + protected int getNumberOfCoveredNegativeExamples(Description description) { + int coveredNeg = 0; + for (Individual example : negativeExamples) { + if (reasoner.hasType(description, example)) { + coveredNeg++; + } + } + + return coveredNeg; + } + + /** + * Calculate predictive accuracy of a description pred-accuracy(D) = + * (covered-positive-examples(D) + uncovered-negative-examples(D)) / all-examples + * + * @param description + * Description which will ve calculated the accuracy + * + * @return Predictive accuracy of a description + */ + protected double accuracy_cal(Description description) { + // double accuracy = (positiveExamples.size() - notCoveredPos + notCoveredNeg)/ + // (double)(positiveExamples.size() + negativeExamples.size()); + int cp = this.getNumberCoveredPositiveExamples(description); + int un = this.negativeExamples.size() + - this.getNumberOfCoveredNegativeExamples(description); + + return (cp + un) / (double) (positiveExamples.size() + negativeExamples.size()); + } + + /** + * Calculate the correctness of a description + * + * @param description + * Description to calculate + * + * @return Correctness of the description + */ + protected double correctness_cal(Description description) { + int un = this.negativeExamples.size() + - this.getNumberOfCoveredNegativeExamples(description); + return un / (double) this.negativeExamples.size(); + } + + /** + * Calculate the completeness of a description + * + * @param description + * Description to calculate + * + * @return Complete if the description + */ + protected double completeness_cal(Description description) { + int cp = this.getNumberCoveredPositiveExamples(description); + return cp / (double) this.positiveExamples.size(); + } + + /** + * Calculate accuracy and correctness, calculated by:<br> + * correctness(D) = not-covered-examples(D) / all-negative-examples<br> + * completeness(D) = covered-positive-examples / all-positive-examples<br> + * accuracy(D) = [covered-positive-examples(D) + not-covered-negative-examples(D)] / + * all-examples<br> + * Noise has not been supported in the current version + * + * + * @param description + * Description to be calculated accuracy and correctness + * + * @return A PDLLEvaluationResult object. If the description is weak, its accuracy will be -1 + * + * NOTE: do we need "weak" concept with the value of -1? How if we just simply assign 0 + * for it? + */ + public PADCELEvaluationResult getAccuracyAndCorrectness(Description description) { + + int notCoveredPos = 0; + int notCoveredNeg = 0; + Set<Individual> coveredPositiveExamples = new HashSet<Individual>(); + + for (Individual example : positiveExamples) { + if (!reasoner.hasType(description, example)) + notCoveredPos++; + else + coveredPositiveExamples.add(example); + } + + if (coveredPositiveExamples.size() > 0) { + + notCoveredNeg = negativeExamples.size() + - getNumberOfCoveredNegativeExamples(description); + + double correctness = (double) notCoveredNeg / (double) negativeExamples.size(); + double completeness = (double) coveredPositiveExamples.size() / positiveExamples.size(); + + if (correctness < 1.0d) + coveredPositiveExamples = null; + + double accuracy = (positiveExamples.size() - notCoveredPos + notCoveredNeg) + / (double) (positiveExamples.size() + negativeExamples.size()); + + // accuracy = (covered positive examples + not covered negative examples) / all examples + // (completeness + correctness) + return new PADCELEvaluationResult(accuracy, correctness, completeness, + coveredPositiveExamples); + + } else { + // a node will be considered as "weak" if it covers none of the positive example and + // the accuracy will be assigned -1 + return new PADCELEvaluationResult(-1, notCoveredNeg / (double) negativeExamples.size(), 0); + } + + } + + /** + * In this accuracy calculation, the accuracy value is based on the current uncovered positive + * examples but the covered positive examples returned still takes all positive examples into + * account + * + * @param description + * Description to be calculated + * @return + */ + public PADCELEvaluationResult getAccuracyAndCorrectness2(Description descript... [truncated message content] |