public List<Observation> ReadObsModelFiles(string fileModel, string fileObs) //path? { //reading model file List<Observation> observationsList = new List<Observation>(); // if (observationsList.Count > 0) // observationsList.Clear(); FileStream fs = new FileStream(fileModel, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(fs); string model_allText = reader.ReadToEnd(); fs.Close(); reader.Close(); fs = null; reader = null; char[] delrow = new char[2]; delrow[0] = '\n'; delrow[1] = '\r'; List<string> rows = model_allText.Split(delrow, StringSplitOptions.RemoveEmptyEntries).ToList(); if(rows==null || rows.Count<4) return null; // throw //build Model SystemModel theModel; string modelID = rows[0].Substring(0, rows[0].Length - 1); List<Wire> inputs = new List<Wire>(); List<Wire> outputs = new List<Wire>(); List<Wire> internalWires = new List<Wire>(); Dictionary<Wire.WireType, Dictionary<int, Wire>> wiresDictionary = new Dictionary<Wire.WireType, Dictionary<int, Wire>>(); wiresDictionary.Add(Wire.WireType.i, new Dictionary<int, Wire>()); wiresDictionary.Add(Wire.WireType.o, new Dictionary<int, Wire>()); wiresDictionary.Add(Wire.WireType.z, new Dictionary<int, Wire>()); //model id // if (!Int32.TryParse(rows[0].Substring(0, rows[0].Length - 1), out modelID)) // return; //throw char[] del = new char[6]; del[0]='.'; del[1]=','; del[2]='['; del[3]=']'; del[4]='('; del[5]=')'; //Wire.WiresDictionary = new Dictionary<string, Wire>(); //input & output string[] inputArr = rows[1].Split(del, StringSplitOptions.RemoveEmptyEntries); string[] outputArr = rows[2].Split(del, StringSplitOptions.RemoveEmptyEntries); for (int i=0; i < inputArr.Length; i++) { //need to check if the Value is Valid? 2<=len<=3, starts with i/o/z, end with a number //need to check if theres as similar wire exist string wireName = inputArr[i]; int wid; if(Int32.TryParse(wireName.Substring(1),out wid)) { if (wireName.StartsWith("i")) { Wire w = new Wire(wid, Wire.WireType.i); inputs.Add(w); wiresDictionary[Wire.WireType.i].Add(wid, w); } } //else -- } for (int j = 0; j < outputArr.Length; j++) { string wireName = outputArr[j]; int wid; if (Int32.TryParse(wireName.Substring(1), out wid)) { if (wireName.StartsWith("o")) { Wire w = new Wire(wid, Wire.WireType.o); outputs.Add(w); wiresDictionary[Wire.WireType.o].Add(wid, w); } } } theModel = new SystemModel(modelID, inputs, outputs); //creating components for (int i = 3; i < rows.Count; i++) { if (!String.IsNullOrEmpty(rows[i])) theModel.AddComponent(CreateComponent(rows[i].Split(del, StringSplitOptions.RemoveEmptyEntries),theModel,wiresDictionary)); } //sort model theModel.SortComponents(); //reading observation fila delrow = new char[1]; delrow[0] = '.'; del = new char[7]; del[0] = '\r'; del[1] = ','; del[2] = '['; del[3] = ']'; del[4] = '('; del[5] = ')'; del[6] = '\n'; rows = null; fs = new FileStream(fileObs, FileMode.Open, FileAccess.Read); reader = new StreamReader(fs); string ob_allText = reader.ReadToEnd(); rows = ob_allText.Split(delrow, StringSplitOptions.RemoveEmptyEntries).ToList(); fs.Close(); reader.Close(); fs = null; reader = null; if (rows == null || rows.Count == 0) return null; // throw //build observation // List<Observation> obList = new List<Observation>(); for (int i = 0; i < rows.Count; i++) { string[] obArr = rows[i].Split(del, StringSplitOptions.RemoveEmptyEntries); if (obArr == null || obArr.Length == 0&& i!=rows.Count-1) return null; //throw if (obArr.Length == 2 + inputs.Count + outputs.Count) { if (!obArr[0].Equals(modelID)) return null; //throw Observation toAdd = CreateObservation(obArr); if (toAdd != null) { toAdd.TheModel = theModel; //try catch observationsList.Add(toAdd); } } //else return/throw? } return observationsList; }
public abstract DiagnosisSet FindDiagnoses(Observation observation);
private DiagnosisSet abstractDiagGrounding(Observation observation, DiagnosisSet abstractDiag) { int count = 0; Observation obs; DiagnosisSet diagnoses = new DiagnosisSet(); Dictionary <int, DiagnosisSet> coneDiagDic = new Dictionary <int, DiagnosisSet>(); List <List <Gate> > tempDiag = new List <List <Gate> >(); List <List <Gate> > temp = new List <List <Gate> >(); foreach (Diagnosis diag in abstractDiag.Diagnoses) { List <Gate> openList = diag.TheDiagnosis; // observation.TheModel.SetValue(observation.InputValues); observation.SetWiresToCorrectValue(); if (openList.Count == 0) { continue; } foreach (Cone c in openList) { if (coneDiagDic.ContainsKey(c.Id)) { continue; } count++; bool[] obIn = new bool[c.cone.Input.Count]; bool[] obOut = new bool[1]; for (int i = 0; i < obIn.Length; i++) { obIn[i] = c.cone.Input[i].Value; } obOut[0] = !c.Output.Value; obs = new Observation(observation.Id + count, obIn, obOut); obs.TheModel = c.cone; coneDiagDic.Add(c.Id, searchAlgorithm.FindDiagnoses(obs)); c.Output.Value = obOut[0]; } foreach (Cone c in openList) //X { if (coneDiagDic[c.Id] == null || coneDiagDic[c.Id].Diagnoses.Count == 0) { continue; } if (tempDiag.Count == 0) { foreach (Diagnosis d in coneDiagDic[c.Id].Diagnoses) { tempDiag.Add(new List <Gate>(d.TheDiagnosis)); } continue; } if (coneDiagDic[c.Id].Diagnoses.Count == 1) { for (int i = 0; i < tempDiag.Count; i++) { tempDiag[i].AddRange(new List <Gate>(coneDiagDic[c.Id].Diagnoses.First().TheDiagnosis)); } continue; } temp.AddRange(tempDiag); tempDiag.Clear(); foreach (Diagnosis d in coneDiagDic[c.Id].Diagnoses) { List <Gate> listD = d.TheDiagnosis; foreach (List <Gate> listTemp in temp) { List <Gate> listTempD = new List <Gate>(listTemp); listTempD.AddRange(new List <Gate>(listD)); tempDiag.Add(new List <Gate>(listTempD)); listTempD.Clear(); } } temp.Clear(); } foreach (List <Gate> list in tempDiag) { diagnoses.AddDiagnosis(new Diagnosis(list)); } tempDiag.Clear(); } return(diagnoses); }
public override DiagnosisSet FindDiagnoses(Observation observation) { if (function == null || observation == null || observation.TheModel == null || observation.TheModel.Components == null || observation.TheModel.Components.Count == 0) { return(null); //throw } this.observation = observation; DiagnosisSet diagnoses = new DiagnosisSet(); hSVector = new HelthStateVector(observation.TheModel.Components); notInDiag = new List <Gate>(); observation.TheModel.SetValue(observation.InputValues); FoundMinCard = false; FirstMinCard = new TimeSpan(); trie = new Trie <string>(); stopwatch.Start(); bool toContinue; foreach (Gate Component in observation.TheModel.Components) { if (closed.Contains(Component.Id)) { if (Component is Cone) { if (((Cone)Component).cone.Components.Count == 0) { continue; } else { toContinue = true; foreach (Gate g in ((Cone)Component).cone.Components) { if (!closed.Contains(g.Id)) { toContinue = false; break; } } if (toContinue) { continue; } } } else { continue; } } function.Operate(Component); if (isDamaged()) { List <Gate> diag = new List <Gate>(); diag.Add(Component); diagnoses.AddDiagnosis(new Diagnosis(diag)); if (diagnoses.Count == 1) { FirstMinCard = stopwatch.Elapsed; } trie.Put(Component.Id + "", Component.Id + ""); } else { notClosed.Add(Component); } Component.SetValue(); } notInDiag.AddRange(notClosed); int depth; toContinue = true; if (agenda == Agenda.helthState && CheckHSDistance(5, 0.1, diagnoses)) { toContinue = false; } if (diagnoses.Count > 0) { FoundMinCard = true; if (agenda == Agenda.minCard) { toContinue = false; } } for (depth = 2; toContinue && depth <= notClosed.Count; depth++) { if (Stop()) { break; } if (diagnoses.Count > 0) { FoundMinCard = true; if (agenda == Agenda.minCard) { break; } } foreach (Gate g in notClosed) { if (Stop() || !toContinue) { break; } openList.Add(g); trie.Matcher.ResetMatch(); toContinue = deepCheck(depth, diagnoses); openList.Remove(g); } } notClosed.Clear(); openList.Clear(); stopwatch.Stop(); stopwatch.Reset(); return(diagnoses); }
public bool IsConsistent(Observation observation, Diagnosis diagnosis) { return(searchAlgorithm.IsConsistent(observation, diagnosis)); }