public override bool Equals(object obj) { if (obj is Diagnosis) { Diagnosis diag = (Diagnosis)obj; if (diag == null || diag.TheDiagnosis == null) { return(false); } if (this.TheDiagnosis == null || this.TheDiagnosis.Count == diag.TheDiagnosis.Count) { return(false); } foreach (Gate g in this.TheDiagnosis) { if (!diag.TheDiagnosis.Contains(g)) { return(false); } } return(true); } else { return(base.Equals(obj)); } }
private List <Diagnosis> generateDiagsFromGrounded(List <List <Gate> > grounded) { List <Diagnosis> ans = new List <Diagnosis>(); int numOfGroups = 0; grounded.ForEach(list => { if (list.Count > 1) { numOfGroups++; } }); if (numOfGroups == 0) { List <Diagnosis> diagnoses = new List <Diagnosis>(); Diagnosis diag = new Diagnosis(); grounded.ForEach(x => { if (x.Count == 1) { diag.AddCompToDiagnosis(x.First()); } }); diagnoses.Add(diag); return(diagnoses); } else if (numOfGroups == 1) { List <Diagnosis> diagnoses = new List <Diagnosis>(); List <Gate> bigGroup = grounded.Find(x => x.Count > 1); foreach (Gate g in bigGroup) { Diagnosis diag = new Diagnosis(); diag.AddCompToDiagnosis(g); grounded.Where(x => x.Count == 1).ToList().ForEach(x => { diag.AddCompToDiagnosis(x.First()); }); diagnoses.Add(diag); } return(diagnoses); } else { List <Diagnosis> diagnoses = new List <Diagnosis>(); List <Gate> bigGroup = grounded.Find(x => x.Count > 1); List <List <Gate> > newGrounded = new List <List <Gate> >(grounded); newGrounded.Remove(bigGroup); foreach (Gate g in bigGroup) { List <Gate> single = new List <Gate>(); single.Add(g); newGrounded.Add(single); diagnoses.AddRange(generateDiagsFromGrounded(newGrounded)); newGrounded.Remove(single); } return(diagnoses); } }
private Comp Random(DiagnosisSet diagnoses) { int index = rnd.Next(0, diagnoses.Count - 1); Diagnosis d = diagnoses.Diagnoses.ToList()[index]; index = rnd.Next(0, d.Comps.Count - 1); return(d.Comps.ToList()[index]); }
public void CreateObsReal(string fileModel, string fileObs, bool minCard) { List <Observation> observationsList = parser.ReadObsModelFiles(fileModel, fileObs); if (observationsList == null || observationsList.Count == 0) { return; } Dictionary <int, Gate> compDic = observationsList[0].TheModel.CreateCompDic(); StreamWriter sw; string filePath; if (minCard) { filePath = "groundedDiagnoses/"; } else { filePath = "diagnoses/"; } Dictionary <int, string> ans = new Dictionary <int, string>(); foreach (Observation obs in observationsList) { if (ans.ContainsKey(obs.Id)) { continue; } DiagnosisSet diagnoses = null; if (minCard) { string diagFileName = filePath + obs.TheModel.Id + "_iscas85_" + obs.Id + ".all"; diagnoses = parser.ReadGroundedDiagnosesFile(diagFileName, compDic); } else { string diagFileName = filePath + obs.TheModel.Id + "_" + obs.Id + "_Diag.txt"; diagnoses = parser.ReadDiagnosisFile(diagFileName, compDic); } Diagnosis chosenDiag = chooseReal(diagnoses); ans.Add(obs.Id, chosenDiag.ToString()); } string realFileName = observationsList[0].TheModel.Id + ""; if (minCard) { realFileName += "_minCard"; } realFileName += "_Real.txt"; sw = new StreamWriter(realFileName, false); foreach (int ob in ans.Keys) { string toWrite = ob + ":" + ans[ob]; sw.WriteLine(toWrite); } sw.Close(); }
public void AddDiagnosis(Diagnosis diagnosis) { if (diagnosis != null && diagnosis.TheDiagnosis != null && diagnosis.TheDiagnosis.Count != 0) { Diagnoses.Add(diagnosis); if (diagnosis.Probability == 0) { diagnosis.CalcAndSetProb(); } SetProbability += diagnosis.Probability; } }
public override RepairAction Plan(SystemState state) { if (state == null || state.Diagnoses == null || state.Diagnoses.Count == 0) { return(null); } int numOfDiagnoses = state.Diagnoses.Count; int diagIndex = rnd.Next(numOfDiagnoses); Diagnosis chosenDiag = state.Diagnoses.Diagnoses.ToArray()[diagIndex]; return(new RepairAction(chosenDiag.Comps)); }
private Comp BestDiagnosis(DiagnosisSet diagnoses) { Diagnosis bestDiag = null; double bestDiagProb = 0; foreach (Diagnosis diag in diagnoses.Diagnoses) { if (bestDiag == null || diag.Probability > bestDiagProb) { bestDiag = diag; bestDiagProb = diag.Probability; } } int j = rnd.Next(bestDiag.Comps.Count - 1); return(bestDiag.Comps.ToList()[j]); }
public bool IsConsistent(Observation observation, Diagnosis diagnosis) { observation.TheModel.SetValue(observation.InputValues); foreach (Gate g in diagnosis.Comps) { function.Operate(g); } bool[] modelOutput = observation.TheModel.GetValue(); for (int i = 0; i < observation.OutputValues.Length; i++) { if (modelOutput[i] != observation.OutputValues[i]) { return(false); } } return(true); }
public void AddDiagnosis(Diagnosis diagnosis) { if (diagnosis != null && diagnosis.Comps != null && diagnosis.Comps.Count != 0 && !Diagnoses.Contains(diagnosis)) { Diagnoses.Add(diagnosis); if (diagnosis.Probability == 0) { diagnosis.CalcAndSetProb(); } SetProbability += diagnosis.Probability; foreach (Comp c in diagnosis.Comps) { if (!Components.Contains(c)) { Components.Add(c); } } } }
public override RepairActionsSet ComputePossibleAcions(SystemState state) { int k = K; if (state == null || state.Diagnoses == null || state.Diagnoses.Count == 0) { return(null); } RepairActionsSet ans; if (RATrie) { ans = new RepairActionsTrie(); } else { ans = new RepairActionsHashSet(); } List <Diagnosis> diagnoses = state.Diagnoses.Diagnoses.ToList(); double diagProb = 0; bool cropDiagnoses = false; if (diagnoses.Count > 2000) { cropDiagnoses = true; } if (diagnoses.Count < k) //need to correct this!! { k = diagnoses.Count; } int maxIndex = diagnoses.Count - 1; SortedSet <int> currPermutation = new SortedSet <int>(); for (int size = 1; size <= k; size++) { if (cropDiagnoses && diagProb > 0.999) { break; } if (currPermutation == null) { currPermutation = new SortedSet <int>(); } for (int i = 0; i < size; i++) //computing first permutation { currPermutation.Add(i); } while (currPermutation != null && currPermutation.Count != 0) { if (cropDiagnoses && diagProb > 0.999) { break; } SortedSet <Comp> newAction = new SortedSet <Comp>(new Comp.CompComparer()); foreach (int index in currPermutation) { Diagnosis diag = diagnoses[index]; diagProb += diag.Probability / state.Diagnoses.SetProbability; foreach (Comp c in diag.Comps) { if (!newAction.Contains(c)) { newAction.Add(c); } } } ans.AddAction(newAction); currPermutation = nextPermutation(currPermutation, maxIndex); } } return(ans); }
public DiagnosisSet ReadDiagnosisFile(string fileName, Dictionary <int, Gate> compDic) { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(fs); string allText = reader.ReadToEnd(); fs.Close(); reader.Close(); fs = null; reader = null; DiagnosisSet ans = new DiagnosisSet(); char[] delrow = new char[2]; delrow[0] = '\n'; delrow[1] = '\r'; List <string> rows = allText.Split(delrow, StringSplitOptions.RemoveEmptyEntries).ToList(); if (rows == null || rows.Count == 0) { return(null); } foreach (string row in rows) { char[] del = new char[1]; del[0] = ' '; List <string> sdiag = row.Split(del, StringSplitOptions.RemoveEmptyEntries).ToList(); if (sdiag == null || sdiag.Count == 0) { continue; } List <int> list = new List <int>(); bool addDiag = true; for (int i = 0; i < sdiag.Count; i++) { int ComponentID = 0; if (!Int32.TryParse(sdiag[i], out ComponentID)) { addDiag = false; break; } list.Add(ComponentID); } if (addDiag) { //convert list to diagnosis List <Gate> diag = new List <Gate>(); foreach (int gid in list) { Gate gate; if (compDic.TryGetValue(gid, out gate)) { diag.Add(gate); } } if (diag.Count > 0) { Diagnosis diagnosis = new Diagnosis(new List <Comp>(diag)); ans.AddDiagnosis(diagnosis); } } } return(ans); }
public DiagnosisSet ReadTLDiagnosisFile(string fileName, Dictionary <int, Cone> conesDic) { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(fs); string allText = reader.ReadToEnd(); fs.Close(); reader.Close(); fs = null; reader = null; DiagnosisSet ans = new DiagnosisSet(); char[] delrow = new char[2]; delrow[0] = '\n'; delrow[1] = '\r'; List <string> rows = allText.Split(delrow, StringSplitOptions.RemoveEmptyEntries).ToList(); if (rows == null || rows.Count == 0) { return(null); } foreach (string row in rows) { if (!row.StartsWith("[gate")) { continue; } List <string> delDiag = new List <string>(); delDiag.Add("["); delDiag.Add("]"); delDiag.Add("."); delDiag.Add(","); delDiag.Add("gate"); List <string> diagString = row.Split(delDiag.ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); Diagnosis diag = new Diagnosis(); bool addDiag = true; foreach (string comp in diagString) { int compId = 0; if (!Int32.TryParse(comp, out compId)) { //in case of not numeric id: string toascii = ""; foreach (char c in comp) { int i = c; toascii += i; } if (!Int32.TryParse(toascii, out compId)) { Console.WriteLine("Parsing error"); return(null); } } if (conesDic.ContainsKey(compId)) { diag.AddCompToDiagnosis(conesDic[compId]); } else { addDiag = false; break; } } if (addDiag) { ans.AddDiagnosis(diag); } } return(ans); }
public bool IsConsistent(Observation observation, Diagnosis diagnosis) { return(searchAlgorithm.IsConsistent(observation, diagnosis)); }