Exemple #1
0
 /// <summary>
 ///Accepts as input a set of diagnoses and single diagnosis.
 ///Used trie data structure to alocate the diagnosis.
 ///Used toString function that used to represent the diagnosis as a string.
 ///Insert the single diagnosis to the trie data sturcture of all minimal diagnosis.
 /// </summary>
 /// <param name="R">The data structure that alocate all the uniq diagnosis (minimal).</param>
 /// <param name="W">The current diagnosis.</param>
 public void AddToTrie(Trie <Diagnosis> R, Diagnosis w)
 {
     if (w != null)
     {
         R.Put(diagnosisToString(w.TheDiagnosis), w);
         minimalDiagnoses.AddDiagnosis(w);
         addToCheckList(w.TheDiagnosis);
     }
 }
        private DiagnosisSet BuildDiagnosisSet()
        {
            var diagnosisSet = new DiagnosisSet();

            for (var index = 0; index < _diagnosisesSetDataStructure.GetCompSets().Count; index++)
            {
                var gates     = _diagnosisesSetDataStructure.GetCompSets()[index];
                var diagnosis = new Diagnosis(gates);
                diagnosisSet.AddDiagnosis(diagnosis);
            }
            return(diagnosisSet);
        }
Exemple #3
0
        /// <summary>
        /// Reiter’s HS-Tree Algorithm 2
        /// </summary>
        /// <param name="existingNode"></param>
        /// <param name="c"></param>
        /// <param name="diagnosisSet"></param>
        /// <param name="paths"></param>
        /// <param name="conflicts"></param>
        /// <param name="newNodes"></param>
        private void Expand(HSTreeNode existingNode, Gate c, DiagnosisSet diagnosisSet, List <HSTreePath> paths, ConflictSet conflicts, List <HSTreeNode> newNodes)
        {
            HSTreePath newPathLabel = new HSTreePath(existingNode.PathLabel, c);

            if (!HSHelper.IsSubsetOfPathDoesExitInDiagnosis(newPathLabel, diagnosisSet) &&
                CheckAndAddPath(paths, newPathLabel))
            {
                HSTreeNode node = new HSTreeNode(newPathLabel);

                Conflict S = HSHelper.IsAConflictExistConjWithPathLabelIsEmpty(conflicts, newPathLabel);

                if (S != null)
                {
                    node.Conflict = S;
                }
                else
                {
                    //consistency checker, which tests if the new node is a diagnosis or returns a minimal conflict otherwise.
                    bool IsDiagnosis = ConstraintSystemSolver.Instance.CheckConsistensy(_observation, node.PathLabel.Path);

                    //If its not a diagnosis we add it as a conflict
                    if (!IsDiagnosis)
                    {
                        node.Conflict = new Conflict(node.PathLabel.Path);
                    }
                }

                lock (_expendLock)
                {
                    if (node.Conflict != null && node.Conflict.TheConflict.Count > 0)
                    {
                        // Add if not exist
                        if (!newNodes.Any(e => e.CompareTo(node) == 0))
                        {
                            newNodes.Add(node);
                        }

                        // Add if not exist
                        if (!conflicts.Conflicts.Contains(node.Conflict))
                        {
                            conflicts.Conflicts.Add(node.Conflict);
                        }
                    }
                    else if (!HSHelper.IsSubsetOfPathDoesExitInDiagnosis(newPathLabel, diagnosisSet))
                    {
                        Diagnosis diagnosis = new Diagnosis(node.PathLabel.Path);
                        diagnosisSet.AddDiagnosis(diagnosis);
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        ///Accepts as input a set of components, a quarter of them randomly selects and creates a new set of all possible components (permutations).
        ///Returns the first permutation which is consistency with the SD and alpha.
        /// </summary>
        /// <param name="SD">The system model of the given system.</param>
        /// <param name="obs">The observation of the given system.</param>
        public Diagnosis RandomDiagnosis(SystemModel SD, Observation obs)
        {
            int         diagnosisID    = 0;
            double      quantity       = (Convert.ToDouble(SD.Components.Count)) * (0.10);
            int         numOfFaultComp = Convert.ToInt32(quantity);
            Random      random         = new Random();
            List <Gate> randomComp     = new List <Gate>();
            List <int>  usedDiagnosis  = new List <int>();

            for (int i = 0; i < numOfFaultComp; i++)
            {
                int randomNumber = random.Next(0, SD.Components.Count);
                if (!usedDiagnosis.Contains(randomNumber))
                {
                    randomComp.Add(SD.Components[randomNumber]);
                    usedDiagnosis.Add(randomNumber);
                }
                else
                {
                    numOfFaultComp++;
                }
            }
            IEnumerable <IList> permutate = Permutate(randomComp, randomComp.Count);

            foreach (var item in permutate)
            {
                List <Gate> temp  = new List <Gate>();
                Diagnosis   toAdd = new Diagnosis();
                temp = (List <Gate>)item;
                toAdd.AddCompsToDiagnosis(temp);
                possibleDiagnosis.AddDiagnosis(toAdd);
            }

            for (int i = 0; i < possibleDiagnosis.Diagnoses.Count; i++)
            {
                int rand = random.Next(0, possibleDiagnosis.Count);
                if (isConsistency(obs, possibleDiagnosis.Diagnoses[rand].TheDiagnosis))
                {
                    if (!isInTrie(possibleDiagnosis.Diagnoses[rand].TheDiagnosis))
                    {
                        diagnosisID = rand;
                        break;
                    }
                }
            }

            return(possibleDiagnosis.Diagnoses[diagnosisID]);
        }
Exemple #5
0
        /// <summary>
        /// Remove duplicate diagnosis in the all result data.
        /// </summary>
        public void removeDuplicate()
        {
            List <string> helper = new List <string>();
            DiagnosisSet  answer = new DiagnosisSet();

            foreach (var item in minimalDiagnoses.Diagnoses)
            {
                bool asf = helper.Contains(diagnosisToString(item.TheDiagnosis));
                if (!helper.Contains(diagnosisToString(item.TheDiagnosis)))
                {
                    helper.Add(diagnosisToString(item.TheDiagnosis));
                    answer.AddDiagnosis(item);
                }
            }
            minimalDiagnoses = answer;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="observation"></param>
        /// <param name="alreadyFoundDiagnosisSet"></param>
        /// <param name="conflicts"></param>
        /// <returns></returns>
        public static DiagnosisSet FindHittingSets(Observation observation, DiagnosisSet alreadyFoundDiagnosisSet, ConflictSet conflicts)
        {
            DiagnosisSet fullSet = FindHittingSets(observation, conflicts);

            DiagnosisSet temp = new DiagnosisSet();

            foreach (Diagnosis diagnosis in fullSet.Diagnoses)
            {
                if (!alreadyFoundDiagnosisSet.Diagnoses.Contains(diagnosis))
                {
                    temp.AddDiagnosis(diagnosis);
                }
            }

            return(temp);
        }
Exemple #7
0
        public void TestAddComponentToDataStructure()
        {
            Gate gate1 = new MultipleInputComponent(1, Gate.Type.and);
            Gate gate2 = new MultipleInputComponent(2, Gate.Type.or);
            Gate gate3 = new MultipleInputComponent(3, Gate.Type.xor);
            Gate gate4 = new MultipleInputComponent(4, Gate.Type.and);

            List <Gate> gateList1 = new List <Gate>();

            gateList1.Add(gate1);
            gateList1.Add(gate4);
            gateList1.Add(gate2);
            gateList1.Add(gate3);

            Diagnosis set1 = new Diagnosis(gateList1);

            SetsDataStructure diagnosiSetsDataStructure = new SetsDataStructure("Diagnosis");

            diagnosiSetsDataStructure.AddSet(set1.TheDiagnosis);

            Assert.AreEqual(diagnosiSetsDataStructure.SetIdsToSet.Count, 1);

            Gate gate5 = new MultipleInputComponent(5, Gate.Type.or);
            //trying to add new set
            List <Gate> gateList2 = new List <Gate>();

            gateList2.Add(gate1);
            gateList2.Add(gate5);
            gateList2.Add(gate4);

            Diagnosis set2 = new Diagnosis(gateList2);

            diagnosiSetsDataStructure.AddSet(set2.TheDiagnosis);

            Assert.AreEqual(diagnosiSetsDataStructure.SetIdsToSet.Count, 2);

            //trying to add super set
            List <Gate> gateList3 = new List <Gate>();

            gateList3.Add(gate1);
            gateList3.Add(gate5);
            gateList3.Add(gate4);
            gateList3.Add(gate2);
            gateList3.Add(gate3);

            Diagnosis set3 = new Diagnosis(gateList3);

            diagnosiSetsDataStructure.AddSet(set3.TheDiagnosis);

            Assert.AreEqual(diagnosiSetsDataStructure.SetIdsToSet.Count, 2);

            //trying to add sub set
            List <Gate> gateList4 = new List <Gate>();

            gateList4.Add(gate1);
            gateList4.Add(gate5);

            Diagnosis set4 = new Diagnosis(gateList4);

            diagnosiSetsDataStructure.AddSet(set4.TheDiagnosis);

            Assert.AreEqual(diagnosiSetsDataStructure.SetIdsToSet.Count, 2);

            DiagnosisSet diagnosisSet = new DiagnosisSet();

            foreach (List <Gate> gates in diagnosiSetsDataStructure.GetCompSets())
            {
                diagnosisSet.AddDiagnosis(new Diagnosis(gates));;
            }

            Assert.AreEqual(diagnosiSetsDataStructure.SetIdsToSet.Count, diagnosisSet.Count);
        }
Exemple #8
0
        /// <summary>
        /// Reiter’s HS-Tree Algorithm 2
        /// </summary>
        /// <param name="existingNode"></param>
        /// <param name="c"></param>
        /// <param name="diagnosisSet"></param>
        /// <param name="paths"></param>
        /// <param name="conflicts"></param>
        /// <param name="newNodes"></param>
        private void Expand(HSTreeNode existingNode, Gate c, DiagnosisSet diagnosisSet, List <HSTreePath> paths, ConflictSet conflicts, List <HSTreeNode> newNodes)
        {
            HSTreePath newPathLabel = new HSTreePath(existingNode.PathLabel, c);

            if (!HSHelper.IsSubsetOfPathDoesExitInDiagnosis(newPathLabel, diagnosisSet) &&
                CheckAndAddPath(paths, newPathLabel))
            {
                HSTreeNode node = new HSTreeNode(newPathLabel);

                Conflict S = HSHelper.IsAConflictExistConjWithPathLabelIsEmpty(conflicts, newPathLabel);

                if (S != null)
                {
                    node.Conflict = S;
                }
                else
                {
                    //consistency checker, which tests if the new node is a diagnosis or returns a minimal conflict otherwise.
                    bool IsDiagnosis = ConstraintSystemSolver.Instance.CheckConsistensy(_observation, node.PathLabel.Path);

                    //If its not a diagnosis we add it as a conflict
                    if (!IsDiagnosis)
                    {
                        node.Conflict = new Conflict(node.PathLabel.Path);
                    }
                }

                lock (_expendLock)
                {
                    if (node.Conflict != null && node.Conflict.TheConflict.Count > 0)
                    {
                        // Add if not exist
                        if (!newNodes.Any(e => e.CompareTo(node) == 0))
                        {
                            newNodes.Add(node);
                        }

                        // Add if not exist
                        if (!conflicts.Conflicts.Contains(node.Conflict))
                        {
                            conflicts.Conflicts.Add(node.Conflict);
                        }
                    }
                    else if (!HSHelper.IsSubsetOfPathDoesExitInDiagnosis(newPathLabel, diagnosisSet))
                    {
                        Diagnosis diagnosis = new Diagnosis(node.PathLabel.Path);
                        diagnosisSet.AddDiagnosis(diagnosis);
                        for (int i = 0; i < diagnosisSet.Diagnoses.Count; i++)
                        {
                            Diagnosis d = diagnosisSet.Diagnoses[i];

                            /// path = {'a', 'b'}   diagnosis.TheDiagnosis = {'a','b','c'}  ===> Return true
                            /// path = {'a', 'b'}   diagnosis.TheDiagnosis = {'a','c','d'}  ===> Return false
                            if (d.TheDiagnosis.Count <= newPathLabel.Path.Count)
                            {
                                continue;
                            }
                            if (d.TheDiagnosis.Except(newPathLabel.Path).Any())
                            {
                                diagnosisSet.Diagnoses.RemoveAt(i);
                                i--;
                            }
                        }
                    }
                }
            }

            _waitEvent.Set();
        }