From: Caspar Safarlou Date: Tue, 18 Nov 2014 02:05:44 +0000 (+0100) Subject: added diagnoses extraction from tree X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=8a789fcec63a7729881561e6f57f8ffe3792786e;p=ker1415-1.git added diagnoses extraction from tree --- diff --git a/report/ass2.tex b/report/ass2.tex index 8ea5d8c..a158e77 100644 --- a/report/ass2.tex +++ b/report/ass2.tex @@ -81,5 +81,10 @@ \subsubsection{Task 14: Implementation} \begin{listing}[H] \caption{Code for generating a hitting set tree} - \prologcode{./src/task14.pl} + \prologcode{./src/task14part1.pl} \end{listing} + +\begin{listing}[H] + \caption{Code for generating a minimal hitting sets} + \prologcode{./src/task14part2.pl} +\end{listing} \ No newline at end of file diff --git a/report/src/task14part1.pl b/report/src/task14part1.pl new file mode 100644 index 0000000..74f01a7 --- /dev/null +++ b/report/src/task14part1.pl @@ -0,0 +1,18 @@ +:- [diagnosis]. + +generateHittingSetTree(SD, COMP, OBS, HS, HSTREE) :- %test with problem1(SD,COMP,OBS), generateHittingSetTree(SD,COMP,OBS,[],HSTREE). This needs to report node([a1], [node([a2], [leaf])]). + tp(SD, COMP, OBS, HS, CS) -> generateParts(SD, COMP, OBS, HS, CS, HSTREE) ; generateParts(_,_,_,_, [], HSTREE). %second part of the OR is needed to translate an empty conflict set into a leaf. + +generateParts(_,_,_,_, [],leaf).%generates leaf if at the end of all possible conflict sets in a branch +generateParts(SD, COMP, OBS, HS, CS, node(CS, RESTOFTREE)) :- %generates node if the conflict set isn't empty and goes on. + repairBranch(SD, COMP, OBS, HS, CS, RESTOFTREE). %repairs the branch by branching out for each item in the conflict set and generating new conflict sets for the next node + + +repairBranch(SD, COMP, OBS, HS, [CONFLICTITEM], [BRANCH]) :- %single item left in conflict set + append(HS, [CONFLICTITEM], HSNEW), %add the used conflict set item for this branch to the new hitting set + generateHittingSetTree(SD, COMP, OBS, HSNEW, BRANCH). %find the next new conflict set with the new hitted item +repairBranch(SD, COMP, OBS, HS, [CSHEAD|CSTAIL], [BRANCHHEAD|BRANCHTAIL]) :- %multiple items left in conflict set + append(HS, [CSHEAD], HSNEW), %add the used conflict set item for this branch to the new hitting set + generateHittingSetTree(SD, COMP, OBS, HSNEW, BRANCHHEAD), %find the next new conflict set with the new hitted item + repairBranch(SD, COMP, OBS, HS, CSTAIL, BRANCHTAIL). %goes on in recursion for each item in the conflict set of the current node + \ No newline at end of file diff --git a/report/src/task14part2.pl b/report/src/task14part2.pl new file mode 100644 index 0000000..1a5dd26 --- /dev/null +++ b/report/src/task14part2.pl @@ -0,0 +1,33 @@ +:- [task14part1] + + +extractDiagnoses(HSTree,Diagnoses):- + extractDiagnoses2(HSTree, Diagnoses, []). %adding a variable for the current path + +extractDiagnoses2(leaf, [PATHEND], PATHEND). %set path in set and end recursion when leaf had been reached +extractDiagnoses2(node([], []), [], PATH). %finished clearing empty node +extractDiagnoses2(node([CSHEAD|CSTAIL], [CSHEADBRANCH|CSTAILBRANCHES]), DIAGNOSESSET, CURRENTPATH) :- + append(CURRENTPATH, [CSHEAD], NEWPATH), %add new visited conflict item to the visited path + append(DEPTHPATHS, WIDTHPATHS, DIAGNOSESSET), %recursively collects all paths from the recursive calls into the depth and the width and puts them into the DIAGNOSESSET + extractDiagnoses2(CSHEADBRANCH,DEPTHPATHS,NEWPATH), %continues into the depth of the branch + extractDiagnoses2(node(CSTAIL, CSTAILBRANCHES), WIDTHPATHS, CURRENTPATH). %calls on all conflict items in the width of the branch + +getLengthSmallestSet([DIAGNOSIS], DIAGNOSISLENGTH):- %computes length of smallest diagnosis + length(DIAGNOSIS,DIAGNOSISLENGTH). %calls length single diagnosis +getLengthSmallestSet([HEADDIAGNOSIS|RESTOFDIAGNOSES], Minimal):- + getLengthSmallestSet(RESTOFDIAGNOSES, RESTLENGTH), %recursively gets the length of tail + length(HEADDIAGNOSIS,HEADLENGTH), %initialises length of the head diagnosis + Minimal is min(HEADLENGTH, RESTLENGTH). %gets minimal length of diagnoses + +filterLength(X,Y):- + length(Y, LENGTHY), + X \== LENGTHY. %checks if the set isn't bigger than the checked set + +getSubsetMinimalDiagnoses(DIAGNOSESSETS, MINIMALDIAGNOSESSETS):- %deze functie moet opnieuw geschreven worden, hij moet de subsetminimal opleveren en niet de kleinste diagnoses + getLengthSmallestSet(DIAGNOSESSETS,LENGTH),!, + exclude(filterLength(LENGTH), DIAGNOSESSETS, MINIMALDIAGNOSESSETS). %excludes sets that are bigger than the smallest set + +subsetMinimalDiagnoses(SD, COMP, OBS, MINIMALDIAGNOSESSETS) :- %function that returns minimal hitting sets from a problem + generateHittingSetTree(SD, COMP, OBS, [], HSTREE), %generate hitting tree + extractDiagnoses(HSTREE, DIAGNOSESSETS), %get diagnoses out of the hitting tree + getSubsetMinimalDiagnoses(DIAGNOSESSETS, MINIMALDIAGNOSESSETS). %determine subset minimal diagnoses \ No newline at end of file