From: Caspar Safarlou Date: Wed, 19 Nov 2014 01:46:30 +0000 (+0100) Subject: hp.pl vervangen door isHittingSet.pl en report en comments bij code bijgewerkt X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=9eaf3d9c0923e8749bb1ba2e113f0fb0868b1a80;p=ker1415-1.git hp.pl vervangen door isHittingSet.pl en report en comments bij code bijgewerkt --- diff --git a/report/additions.tex b/report/additions.tex index f61b39a..c31046c 100644 --- a/report/additions.tex +++ b/report/additions.tex @@ -7,12 +7,16 @@ correct so we had to rewrite that entire section after hacking in prolog to make it work. \subsection{Assignment 1-2} -Assignment 2 took us a week, it was much more difficult our knowledge of prolog +Assignment 2 took us a week, it was much more difficult, but our knowledge of prolog was increased significantly so we weren't stopped by trivial prolog problems and -could work on it much more efficiently. +could work on it much more efficiently. On the other hand, the algorithm is in its +difficulty programming a very steep climb in prolog programming and we +would definitly recommend splitting this assignment up more. \subsection{General remarks} -In our opinion the amount of information provided about the hittingset -algorithm and situation calculus is out of proportion concidering the very +In our opinion the amount of information provided about the hitting-set +algorithm and situation calculus is out of proportion considering the very important part it plays. For example situation calculus barely gets one page in -the reader and hitting set is also very sparsely described. +the reader and hitting set is also very sparsely described. We would really like +to have more clear information about each part of this algorithm. Some details, +such as the tp function, took hours of staring at code to finally understand it. diff --git a/report/ass2.tex b/report/ass2.tex index fbf780d..d65fa56 100644 --- a/report/ass2.tex +++ b/report/ass2.tex @@ -1,11 +1,36 @@ \subsection{Implementation of the hitting-set algorithm} \subsubsection{Task 12: Generate conflict} Can be found in \textit{./src/task12.pl} or in Listing~\ref{task12}. +\\ \\The tp command runs through a list of commands, which we will discuss here in the same order: +\\Subtract: removes the components assumed to be abnormal from the set of all components and puts these in NormComp (the predicates assumed to be normal.). +\\Maplist: maps the function Normal over all pairs of NormComp (the predicates assumed to be normal) into S, letting S be NormComp, but with all predicates X becoming ~ab(X). +\\Append3: appends SD (list of first-order formula), S (normal predicates) and OBS (the observed (ground) facts) into Theory (that which is actually going to be tested, SD ∪ OBS ∪ {¬Ab(c) | c ∈ CS}, needed to be proven inconsistent if CS is going to be a valid conflict set). +\\Conjunction: turns every element of Theory into conjunctions. These conjunctions are F (the formula that needs to be tested for inconsistency). +\\Ground: constructs the ground formula that needs to be proven if it’s inconsistent. It establishes all logical symbols in the new formula F2 using all the logic rules in order and instantiates the existential and global variables using COMP (the set of all components). +\\Break: stopping the prolog stack here so no backtracking will be done. +\\Prove/1: Tries to establish the truth of the negated F2 formula and keeps backtracking until some solution is found. +\\Prove/2: Finds the proof established in the statement above and puts the proof into Proof. +\\Flatten: Breaks down the structure of the proof into its elements into FlatProof. +\\Include: filters the abnormal elements from the FlatProof and puts them into Abs. +\\Maplist: removes the ab() predicate from each component and puts them into CompProof. +\\List-to-set: turns CompProof into CompSorted by turning the list(CompProof) into a set (CompSorted) that is sorted (which is not necessary) but which does not contain any duplicate elements. +\\Subtract: removes any elements that were already considered abnormal from CompSorted and puts them in the final result of the function: CS. +\\Break: another break to prevent prolog from backtracking after a result has been found. +\\ \\In conclusion, tp determines the conflict set for a certain system with certain observations done on it and with certain assumptions on parts of the system. +\newpage \subsubsection{Task 13: Define your data structure} -Can be found in \textit{./src/hs.pl} or in Listing~\ref{hs}. +We use the nodes and leaves for our data structure. A node contains two lists. The first one contains a list of edge labels. The second one contains a list of new nodes or leaves. Both lists in the node predicate need to have the same size, since each edge label corresponds to a branch. +\\ \\Good examples: +\\Tree 1: node([a,b,],[leaf,leaf]). +\\Tree 2: node([a,b],[node([c,d],[leaf,leaf]),leaf]). +\\Wrong examples: +\\Tree 3: node([a,b],[node([c,a],[leaf,leaf]),leaf]). +\\Tree 4: node([a,b,c],[node([b,c],[leaf,leaf]),leaf]). +\\ \\Tree 3 has a duplicate edge label in a path from root to leaf and thus is an incorrect hitting-set tree. Tree 4 is missing an entire branch for the edge label in the root. +\\ \\Our isHittingSetTree(Tree) can be found in \textit{./src/hs.pl} or in Listing~\ref{hs}. \begin{figure}[H] - \caption{Examples of good hitting set trees} + \caption{Tree 1 and Tree 2} \begin{tikzpicture} [grow=down] \node[bag] {\{$a, b\}$} child { @@ -45,7 +70,7 @@ Can be found in \textit{./src/hs.pl} or in Listing~\ref{hs}. \end{figure} \begin{figure}[H] - \caption{Examples of bad hitting set trees due to duplicate edge labels} + \caption{Tree 3 and Tree 4} \begin{tikzpicture} [grow=down] \node[bag] {\{$a, b\}$} child { @@ -69,8 +94,49 @@ Can be found in \textit{./src/hs.pl} or in Listing~\ref{hs}. node[right] {$b$} }; \end{tikzpicture} +\begin{tikzpicture} [grow=down] + \node[bag] {\{$a, b,c\}$} + child { + node[bag] {$\{b, c\}$} + child { + node[end, label=below:$\checkmark$]{} + edge from parent + node[left] {$b$} + } + child { + node[end, label=below:$\checkmark$]{} + edge from parent + node[left] {$c$} + } + edge from parent + node[left] {$a$} + } + child { + node[end, label=below:$\checkmark$]{} + edge from parent + node[right] {$b$} + }; + \end{tikzpicture} \end{figure} +\newpage \subsubsection{Task 14: Implementation} -Can be found in \textit{./src/task14part1.pl} or in Listing~\ref{task14part1}. -Can be found in \textit{./src/task14part2.pl} or in Listing~\ref{task14part2}. +How we generate hitting-set trees can be found in \textit{./src/task14part1.pl} or in Listing~\ref{task14part1}. +\\How we extract diagnoses and get the subset minimal diagnoses can be found in \textit{./src/task14part2.pl} or in Listing~\ref{task14part2}. +\\Problem 1: +\\Hitting-set tree = node([a1], [node([a2], [leaf])]), +\\Diagnoses = [[a1, a2]] +\\Smallest Diagnoses = [[a1, a2]] +\\ \\Problem 2: +\\HS = node([a1, a2], [leaf, leaf]), +\\DIAG = [[a1], [a2]] +\\MIN = [[a1], [a2]] +\\ \\Problem 3: +\\HS = node([a1, o1, a2], [node([a2, o1], [leaf, leaf]), leaf, node([a1, o1], [leaf, leaf])]), +\\DIAG = [[a1, a2], [a1, o1], [o1], [a2, a1], [a2, o1]], +\\MIN = [[o1]] +\\ \\Fulladder: +\\HS = node([a1, x1, a2, r1, x2], [node([a2, x2, x1], [node([x1, x2], [leaf, leaf]), node([a2, r1, x1], [leaf, leaf, leaf]), leaf]), leaf, node([a1, x1, x2], [node([x1, x2], [leaf, leaf]), leaf, leaf]), node([a1, x1, a2|...], [node([a2|...], [node(..., ...)|...]), leaf, node(..., ...)|...]), node([a1, x1|...], [node([...|...], [...|...]), leaf|...])]), +\\DIAG = [[a1, a2, x1], [a1, a2, x2], [a1, x2, a2], [a1, x2, r1], [a1, x2, x1], [a1, x1], [x1], [a2|...], [...|...]|...], +\\MIN = [[x1]] +\\ \\We had problems implementing how we can get subset-minimal subsets from all possible diagnoses, so we just tried to find the smallest diagnoses as another challenge. All our results are correct, since they correspond to our calculations done by hand or to the answers on the slides. A big problem we had was how to generate conflict set using tp. With some help of the internet, we found the prolog if-then-else construction, which is nice, but doesn't feel like real logic programming. Thus this is a point where improvement is possible (be it maybe not an optimalization). We think we lost track of the getting subset-minimal subsets when we first made the function that extracts all possible diagnoses, maybe we had to get those while running through the hitting set tree. Our code looks very efficient on the outside, at least to us, but when we were tracing calls we found alot of repetitions sometimes, so maybe break statements to reduce backtracking would be a nice improvement in some places. And of course, other improvements for, for example pruning the hitting-set tree, can be implemented. \ No newline at end of file diff --git a/report/src/hs.pl b/report/src/hs.pl index 53f3158..597e2e6 100644 --- a/report/src/hs.pl +++ b/report/src/hs.pl @@ -1,7 +1,21 @@ -isHittingSetTree(_, leaf). -isHittingSetTree(_, node([], [])). -isHittingSetTree(VisitedLabels, node([CurLabel|Labels], [CurChild|Children])) :- - not(member(CurLabel, VisitedLabels)), - append(VisitedLabels, [CurChild], UpdatedVisitedLabels), - isHSTree(UpdatedVisitedLabels, CurChild), - isHSTree(VisitedLabels, node(Labels, Children)). +:- [diagnosis]. +%Function to add variable for uniqueness checking. +isHittingSetTree(Tree) :- + isHittingSetTree2(Tree, []). + +%When the end of branch has been reached. +isHittingSetTree2(leaf, _). +%When a node turns empty. +isHittingSetTree2(node([], []), _). +%When a node isn't empty. +isHittingSetTree2(node([CurLabel|Labels], [CurChild|Children]), VisitedLabels) :- + %Check if label has already been used up in the tree. + not(member(CurLabel,VisitedLabels)), + %Append label to list of checked labels + append(VisitedLabels, [CurLabel], UpdatedVisitedLabels), + %Recursively search into depth of branch. + isHittingSetTree2(CurChild, Appended), + %Recursively search into width of node. + isHittingSetTree2(node(Labels, UpdatedVisitedLabels), VisitedLabels). +%If there are more or less labels then children, the tree will not match and +%the function will rightfully report false. diff --git a/report/src/task14part1.pl b/report/src/task14part1.pl index bb1aa53..68bf7cb 100644 --- a/report/src/task14part1.pl +++ b/report/src/task14part1.pl @@ -1,32 +1,33 @@ :- [diagnosis]. -%test with problem1(SD,COMP,OBS), +%Test with problem1(SD,COMP,OBS), %generateHittingSetTree(SD,COMP,OBS,[],HSTREE). This needs to report node([a1], %[node([a2], [leaf])]). -%second part of the OR is needed to translate an empty conflict set into a leaf. +%Second part of the OR is needed to translate an empty conflict set into a leaf. generateHittingSetTree(SD, COMP, OBS, HS, HSTREE) :- + %Prolog version of: if -> then ; else, here used to generate conflict sets. tp(SD, COMP, OBS, HS, CS) -> generateParts(SD, COMP, OBS, HS, CS, HSTREE); generateParts(_,_,_,_, [], HSTREE). -%generates leaf if at the end of all possible conflict sets in a branch +%Generates leaf if at the end of all possible conflict sets in a branch generateParts(_,_,_,_, [],leaf). -%generates node if the conflict set isn't empty and goes on. +%Generates node if the conflict set isn't empty and goes on. generateParts(SD, COMP, OBS, HS, CS, node(CS, RESTOFTREE)) :- - %repairs the branch by branching out for each item in the conflict set and + %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, CS, RESTOFTREE). -%single item left in conflict set +%Single item left in conflict set: repairBranch(SD, COMP, OBS, HS, [CONFLICTITEM], [BRANCH]) :- - %add the used conflict set item for this branch to the new hitting set + %Add the used conflict set item for this branch to the new hitting set append(HS, [CONFLICTITEM], HSNEW), - %find the next new conflict set with the new hitted item + %Find the next new conflict set with the new hitted item added builds next tree-part. generateHittingSetTree(SD, COMP, OBS, HSNEW, BRANCH). -%multiple items left in conflict set +%Multiple items left in conflict set: repairBranch(SD, COMP, OBS, HS, [CSHEAD|CSTAIL], [BRANCHHEAD|BRANCHTAIL]) :- - %add the used conflict set item for this branch to the new hitting set + %Add the used conflict set item for this branch to the new hitting set. append(HS, [CSHEAD], HSNEW), - %find the next new conflict set with the new hitted item + %Find the next new conflict set with the new hitted item added and builds next tree-part. generateHittingSetTree(SD, COMP, OBS, HSNEW, BRANCHHEAD), - %goes on in recursion for each item in the conflict set of the current node + %Goes on in recursion for each item in the conflict set of the current node (the width of the branch). repairBranch(SD, COMP, OBS, HS, CSTAIL, BRANCHTAIL). diff --git a/report/src/task14part2.pl b/report/src/task14part2.pl index ebbe8fa..26c15bf 100644 --- a/report/src/task14part2.pl +++ b/report/src/task14part2.pl @@ -1,6 +1,6 @@ :- [task14part1]. -% Adding a variable for the current path +% Adding a variable for the current path. extractDiagnoses(HSTree,Diagnoses):- extractDiagnoses2(HSTree, Diagnoses, []). @@ -25,32 +25,33 @@ getLengthSmallestSet([Diagnosis], DiagnosisLength):- % Calls length single diagnosis length(Diagnosis,DiagnosisLength). getLengthSmallestSet([HeadDiagnosis|RestOfDiagnoses], Minimal):- - % Recursively gets the length of tail + % Recursively gets the length of tail. getLengthSmallestSet(RestOfDiagnoses, RestLength), - % Initialises length of the head diagnosis + % Initialises length of the head diagnosis. length(HeadDiagnosis,HeadLength), - % Gets minimal length of diagnoses + % Gets minimal length of diagnoses. Minimal is min(HeadLength, RestLength). filterLength(X,Y):- length(Y, LengthY), - %checks if the set isn't bigger than the checked set + %Checks if the set isn't bigger than the smallest set. X \== LengthY. -% Deze functie moet opnieuw geschreven worden, hij moet de subsetminimal -% opleveren en niet de kleinste diagnoses + getSubsetMinimalDiagnoses(DiagnosesSets, MinimalDiagnosesSets):- + %Gets the length of the smallest set to prune the diagnoses. + %Break so that there will be no backtracking if something goes wrong. getLengthSmallestSet(DiagnosesSets,Length),!, % Excludes sets that are bigger than the smallest set exclude(filterLength(Length), DiagnosesSets, MinimalDiagnosesSets). -% Function that returns minimal hitting sets from a problem -subsetMinimalDiagnoses(SD, COMP, OBS, MinimalDiagnosesSets) :- - % Generate hitting tree +% Function that returns minimal hitting sets from a problem. +subsetMinimalDiagnoses(SD, COMP, OBS, HsTree, DiagnosesSets, MinimalDiagnosesSets) :- + % Generate hitting tree. generateHittingSetTree(SD, COMP, OBS, [], HsTree), write(HsTree), - % Get diagnoses out of the hitting tree + % Get diagnoses out of the hitting tree. extractDiagnoses(HsTree, DiagnosesSets), write(DiagnosesSets), - % Determine subset minimal diagnoses + % Determine smallest diagnoses. getSubsetMinimalDiagnoses(DiagnosesSets, MinimalDiagnosesSets).