public static void RunTest() { ModelObservationCreator cc = new ModelObservationCreator(); List <Observation> a = cc.ReadObsModelFiles("111.txt", "111_obs.txt"); Observation bObservation = a[0]; List <Gate> allComponents = bObservation.TheModel.Components; // 11 , 22 , 33 // 44 , 22 , 55 Conflict c1 = new Conflict(new List <Gate>() { allComponents[0], allComponents[2], allComponents[3] }); Conflict c2 = new Conflict(new List <Gate>() { allComponents[1], allComponents[2], allComponents[4] }); ConflictSet cs = new ConflictSet(); cs.Conflicts = new List <Conflict>() { c1, c2 }; DiagnosisSet OUTPUT = HittingSetFinder.FindHittingSets(bObservation, cs); }
/// <summary> /// Reiter’s HS-Tree Algorithm 1 /// </summary> /// <param name="conflicts"></param> /// <returns></returns> private DiagnosisSet DiagnoseMainLoop(ConflictSet conflicts) { DiagnosisSet diagnosisSet = new DiagnosisSet(); List <HSTreePath> paths = new List <HSTreePath>(); List <HSTreeNode> newNodes = new List <HSTreeNode>(); List <HSTreeNode> nodesToExpand = HSHelper.ConvertConflictSetToNodes(conflicts); //nodesToExpand.RemoveAt(1); //Not sure if this should be empty or not.... //conflicts = new ConflictSet(); //conflicts.Conflicts = new List<Conflict>(); while (nodesToExpand.Count > 0) { newNodes = new List <HSTreeNode>(); HSTreeNode node = nodesToExpand[0]; nodesToExpand.RemoveAt(0); for (int i = 0; i < node.Conflict.TheConflict.Count; i++) { Gate c = node.Conflict.TheConflict[i]; Expand(node, c, diagnosisSet, paths, conflicts, newNodes); } if (nodesToExpand.Count > 3000) { Debug.WriteLine("Nodes to expand over 3000! ignore this obs"); return(diagnosisSet); } //Adding new nodes foreach (HSTreeNode nodeToAdd in newNodes) { bool foundSameNode = false; foreach (HSTreeNode existingNode in nodesToExpand) { foundSameNode = existingNode.CompareTo(nodeToAdd) == 0; if (foundSameNode) { break; } } if (!foundSameNode) { nodesToExpand.Add(nodeToAdd); } } //nodesToExpand.AddRange(newNodes); } return(diagnosisSet); }
public void TestFindDiagnosisHaltByFirstDiagnosis() { List <HashSet <int> > mockDiagnosisList = ConstraintSystemSolverMock.getInstance().GetDiagnosisSet(); DiagnosisSet diagnosisSet = SwitchingDiagnosticEngine.FindDiagnosisHaltByFirstDiagnosis(_observations[TestingEnvironment.ObservationIndex], _initialConflictSet, null); PrintSetList(diagnosisSet, "Diagnosis_" + TestingEnvironment.SystemFile); Assert.IsTrue(diagnosisSet.Count >= 1); }
/// <summary> /// Reiter’s HS-Tree Algorithm 1 /// </summary> /// <param name="conflicts"></param> /// <returns></returns> private DiagnosisSet DiagnoseMainLoop(ConflictSet conflicts) { DiagnosisSet diagnosisSet = new DiagnosisSet(); List <HSTreePath> paths = new List <HSTreePath>(); List <HSTreeNode> newNodes = new List <HSTreeNode>(); List <HSTreeNode> nodesToExpand = HSHelper.ConvertConflictSetToNodes(conflicts); //nodesToExpand.RemoveAt(1); //Not sure if this should be empty or not.... //conflicts = new ConflictSet(); //conflicts.Conflicts = new List<Conflict>(); int size = 1; int lastSize = 0; while (size != lastSize || Process.GetCurrentProcess().Threads.OfType <ProcessThread>().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running).Count() != 1) { //for (int j = 0; j < (size - lastSize); j++) Parallel.For(0, size - lastSize, j => { HSTreeNode node = nodesToExpand[lastSize + j]; // Parallelize the outer loop to partition the source array by rows. Parallel.For(0, node.Conflict.TheConflict.Count, i => { Gate c = node.Conflict.TheConflict[i]; Expand(node, c, diagnosisSet, paths, conflicts, nodesToExpand); }); // Parallel.For }); lastSize = size; /* * for (int i = 0; i < nodesToExpand.Count; i++) * { * HSTreeNode node = nodesToExpand[i]; * System.Diagnostics.Debug.WriteLine(node.ToString()); * }*/ size = nodesToExpand.Count; if (size - lastSize > 3000) { Debug.WriteLine("Nodes to expand over 3000! ignore this obs"); return(diagnosisSet); } } return(diagnosisSet); }
public void TestFindDiagnosis() { List <HashSet <int> > mockDiagnosisList = ConstraintSystemSolverMock.getInstance().GetDiagnosisSet(); SwitchingAlgorithm switchingAlgorithm = new SwitchingAlgorithm(_observations[TestingEnvironment.ObservationIndex], _initialConflictSet, null, NumOfDiagnosisRequired); DiagnosisSet diagnosisSet = switchingAlgorithm.FindDiagnosis(-1); PrintSetList(diagnosisSet, "diagnosis.txt"); //printSetList(mockDiagnosisList); Assert.AreEqual(diagnosisSet.Count, mockDiagnosisList.Count); }
/// <summary> /// Reiter’s HS-Tree Algorithm 1 /// </summary> /// <param name="conflicts"></param> /// <returns></returns> private DiagnosisSet DiagnoseMainLoop(ConflictSet conflicts) { DiagnosisSet diagnosisSet = new DiagnosisSet(); List <HSTreePath> paths = new List <HSTreePath>(); List <HSTreeNode> newNodes = new List <HSTreeNode>(); List <HSTreeNode> nodesToExpand = HSHelper.ConvertConflictSetToNodes(conflicts); //nodesToExpand.RemoveAt(1); //Not sure if this should be empty or not.... //conflicts = new ConflictSet(); //conflicts.Conflicts = new List<Conflict>(); int size = 1; int lastSize = 0; while (size != lastSize || Process.GetCurrentProcess().Threads.OfType <ProcessThread>().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running).Count() != 1) { for (int j = 0; j < (size - lastSize); j++) { HSTreeNode node = nodesToExpand[lastSize + j]; for (int i = 0; i < node.Conflict.TheConflict.Count; i++) { Gate c = node.Conflict.TheConflict[i]; Thread thread = new Thread(() => Expand(node, c, diagnosisSet, paths, conflicts, nodesToExpand)); thread.Start(); } } lastSize = size; /* * for (int i = 0; i < nodesToExpand.Count; i++) * { * HSTreeNode node = nodesToExpand[i]; * System.Diagnostics.Debug.WriteLine(node.ToString()); * }*/ _waitEvent.WaitOne(); size = nodesToExpand.Count; if (size - lastSize > 3000) { Debug.WriteLine("Nodes to expand over 3000! ignore this obs"); return(diagnosisSet); } } return(diagnosisSet); }
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); }
/// <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); } } } }
/// <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); }
/// <summary> /// Is Subset Of Path Does Exit In Diagnosis /// For example: /// path = {'a', 'b'} diagnosis.TheDiagnosis = {'a','b','c'} ===> Return true /// path = {'a', 'b'} diagnosis.TheDiagnosis = {'a','c','d'} ===> Return false /// </summary> /// <param name="newPathLabel"></param> /// <param name="diagnosisSet"></param> /// <returns></returns> public static bool IsSubsetOfPathDoesExitInDiagnosis(HSTreePath newPathLabel, DiagnosisSet diagnosisSet) { //Check if in any diagnosis: for (int j = 0; j < diagnosisSet.Diagnoses.Count; j++) { Diagnosis diagnosis = diagnosisSet.Diagnoses[j]; List <Gate> path = newPathLabel.Path; //Check if path is in diagnosis: if (!diagnosis.TheDiagnosis.Except(path).Any()) { return(true); } } return(false); }
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); }
private static void RunAllTest(string fileModel, string fileObs) { fileModel = "SystemFiles/" + fileModel; fileObs = "SystemFiles/" + fileObs; ModelObservationCreator cc = new ModelObservationCreator(); List <Observation> allObservetions = cc.ReadObsModelFiles(fileModel, fileObs); int index = 1; foreach (Observation observation in allObservetions) { if (index > 50) { break; } Debug.WriteLine("****************************************************************** Obs number = " + index + " CAL CONFLICATES"); //Get Conflict Set LtmsAlgorithm ltmsAlgorithm = new LtmsAlgorithm(observation.TheModel, observation); List <List <Gate> > gatelList = ltmsAlgorithm.findConflicts(); ConflictSet conflictSet = ltmsAlgorithm.ConvertGateListToConflict(gatelList); Debug.WriteLine("****************************************************************** Obs number = " + index + " Number Of Conflicts=" + conflictSet.Conflicts.Count); index++; ConstraintSystemSolver.Reset(); Stopwatch sw = Stopwatch.StartNew(); HSOneThread s1 = new HSOneThread(); DiagnosisSet diagnosisSet1 = s1.FindHittingSets(observation, conflictSet); sw.Stop(); HittingSetStatistics statistics = new HittingSetStatistics(observation.TheModel.Id, observation.Id, conflictSet.Conflicts.Count, sw.Elapsed.TotalMilliseconds, "HSOneThread", diagnosisSet1.Count); _hittingSetStatisticses.Add(statistics); ConstraintSystemSolver.Reset(); sw = Stopwatch.StartNew(); HSMultiTasks s2 = new HSMultiTasks(); DiagnosisSet diagnosisSet2 = s2.FindHittingSets(observation, conflictSet); sw.Stop(); statistics = new HittingSetStatistics(observation.TheModel.Id, observation.Id, conflictSet.Conflicts.Count, sw.Elapsed.TotalMilliseconds, "HSMultiTasks", diagnosisSet2.Count); _hittingSetStatisticses.Add(statistics); ConstraintSystemSolver.Reset(); sw = Stopwatch.StartNew(); HSMultiThreads s3 = new HSMultiThreads(); DiagnosisSet diagnosisSet3 = s3.FindHittingSets(observation, conflictSet); sw.Stop(); statistics = new HittingSetStatistics(observation.TheModel.Id, observation.Id, conflictSet.Conflicts.Count, sw.Elapsed.TotalMilliseconds, "HSMultiThreads", diagnosisSet3.Count); _hittingSetStatisticses.Add(statistics); ConstraintSystemSolver.Reset(); } Debug.WriteLine("ModelID, ObservationID, NumberOfConflicts, AlgorithmName, NumberOfDiagnoses, RunTime"); foreach (HittingSetStatistics stat in _hittingSetStatisticses) { string prints = ""; prints += stat.ModelID + " ,"; prints += stat.ObservationID + " ,"; prints += stat.NumberOfConflicts + " ,"; prints += stat.AlgorithmName + " ,"; prints += stat.NumberOfDiagnoses + " ,"; prints += stat.RunTime; Debug.WriteLine(prints); } }
/// <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(); }
public SwitchingAlgorithm(Observation observation, ConflictSet initialConflictsSet, DiagnosisSet initialDiagnosisSet, int requiredNumOfDiagnosis) { this._observation = observation; this._requiredNumOfDiagnosis = requiredNumOfDiagnosis; this._conflictsSetDataStructure = new SetsDataStructure("Conflicts"); //this._conflictsSetDataStructure = new CompSetTree.CompSetTree(); if (initialConflictsSet != null) { foreach (var conflict in initialConflictsSet.Conflicts) { _conflictsSetDataStructure.AddSet(conflict.TheConflict); } } this._diagnosisesSetDataStructure = new SetsDataStructure("Diagnosises"); //this._diagnosisesSetDataStructure = new CompSetTree.CompSetTree(); if (initialDiagnosisSet != null) { foreach (var diagnosis in initialDiagnosisSet.Diagnoses) { _diagnosisesSetDataStructure.AddSet(diagnosis.TheDiagnosis); } } if (IdToGates.Count == 0) { BuildIdToGateDictionary(observation.TheModel.Components); } }
public static DiagnosisSet FindDiagnosisHaltByQuantiy(Observation observation, ConflictSet initialConflictsSet, DiagnosisSet initialDiagnosisSet, int quantity) { var switchingAlgorithm = new SwitchingAlgorithm(observation, initialConflictsSet, initialDiagnosisSet, quantity); return(switchingAlgorithm.FindDiagnosis(-1)); }
public static DiagnosisSet FindDiagnosisHaltByTime(Observation observation, ConflictSet initialConflictsSet, DiagnosisSet initialDiagnosisSet, int timeOutMillis) { var switchingAlgorithm = new SwitchingAlgorithm(observation, initialConflictsSet, initialDiagnosisSet, Int32.MaxValue); return(switchingAlgorithm.FindDiagnosis(timeOutMillis)); }
public static DiagnosisSet FindDiagnosisHaltByFirstDiagnosis(Observation observation, ConflictSet initialConflictsSet, DiagnosisSet initialDiagnosisSet) { var switchingAlgorithm = new SwitchingAlgorithm(observation, initialConflictsSet, initialDiagnosisSet, 1); return(switchingAlgorithm.FindDiagnosis(-1)); }