Example #1
0
        private DiagnosisSet cropHS(SystemState systemState)
        {
            DiagnosisSet newDiagSet           = new DiagnosisSet();
            PriorityQueue <Diagnosis> diagPrQ = new PriorityQueue <Diagnosis>();

            foreach (Diagnosis diag in systemState.Diagnoses.Diagnoses)
            {
                double sumHS = 0;
                foreach (Comp c in diag.Comps)
                {
                    sumHS += systemState.HealthState.GetCompHealthState(c);
                }
                diagPrQ.Enqueue(diag, -sumHS);
            }
            while (newDiagSet.Count < DiagCropLimit && !diagPrQ.IsEmpty())
            {
                Diagnosis diag = (Diagnosis)diagPrQ.Dequeue();
                if (diag != null)
                {
                    newDiagSet.AddDiagnosis(diag);
                }
            }
            int compCount = systemState.Diagnoses.Components.Count;

            while (newDiagSet.Components.Count < compCount && !diagPrQ.IsEmpty())
            {
                Diagnosis diag = (Diagnosis)diagPrQ.Dequeue();
                if (diag != null)
                {
                    newDiagSet.AddDiagnosis(diag);
                }
            }
            return(newDiagSet);
        }
Example #2
0
        private DiagnosisSet computeNextState(List <Gate> action)
        {
            if (m_diagnoses == null || m_diagnoses.Count == 0 || action == null || action.Count == 0)
            {
                return(null);
            }
            DiagnosisSet ans = new DiagnosisSet();

            foreach (Diagnosis diag in m_diagnoses.Diagnoses)
            {
                Diagnosis toAdd = new Diagnosis();
                foreach (Gate g in diag.TheDiagnosis)
                {
                    if (!action.Contains(g))
                    {
                        toAdd.AddCompToDiagnosis(g);
                    }
                }
                if (toAdd.TheDiagnosis.Count == diag.TheDiagnosis.Count) //to save space and time
                {
                    toAdd = diag;
                }
                if (toAdd.TheDiagnosis.Count != 0)
                {
                    ans.AddDiagnosis(toAdd);
                }
            }
            return(ans);
        }
Example #3
0
        private DiagnosisSet computeNextState(RepairAction action) //the resulted diagnoses could be not subset minimal!
        {
            if (m_diagnoses == null || m_diagnoses.Count == 0 || action == null || action.Count == 0)
            {
                return(null);
            }
            DiagnosisSet ans = new DiagnosisSet();

            foreach (Diagnosis diag in m_diagnoses.Diagnoses)
            {
                Diagnosis toAdd = new Diagnosis();
                foreach (Comp g in diag.Comps)
                {
                    if (!action.Contains(g))
                    {
                        toAdd.AddCompToDiagnosis(g);
                    }
                }
                if (toAdd.Comps.Count != 0)
                {
                    ans.AddDiagnosis(toAdd);
                }
            }
            return(ans);
        }
        private bool deepCheck(int depth, DiagnosisSet diagnoses)
        {
            bool ans   = true;
            int  index = notClosed.IndexOf(openList.Last()); //index in notClosed of last Component that added to openlist

            for (int i = index + 1; ans == true && i >= 0 && i < notClosed.Count; i++)
            {
                Gate Component = notClosed[i];
                //maybe add check - openlist.last.id < Component
                openList.Add(Component);
                if (depth == 2)
                {
                    if (openList.Count > 2 && isInTrie(openList))
                    {
                        openList.Remove(Component);
                        continue;
                    }
                    string      str  = "";
                    List <Gate> diag = new List <Gate>();
                    foreach (Gate g in openList)
                    {
                        function.Operate(g);

                        if (g != openList.Last())
                        {
                            str += g.Id + " ";
                        }
                        else
                        {
                            str += g.Id;
                        }
                        diag.Add(g);
                    }
                    if (isDamaged())
                    {
                        diagnoses.AddDiagnosis(new Diagnosis(diag));
                        if (diagnoses.Count == 1)
                        {
                            FirstMinCard = stopwatch.Elapsed;
                        }
                        trie.Put(str, str);
                        foreach (Gate g in openList)
                        {
                            if (notInDiag.Contains(g))
                            {
                                notInDiag.Remove(g);
                            }
                        }
                        if (agenda == Agenda.helthState && CheckHSDistance(5, 0.1, diagnoses))
                        {
                            ans = false;
                        }
                    }
                    foreach (Gate g in openList)
                    {
                        g.SetValue();
                    }
                }

                else if (!isInTrie(openList))
                {
                    ans = deepCheck(depth - 1, diagnoses);
                }

                openList.Remove(Component);
            }
            return(ans);
        }
        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);
        }
Example #6
0
        public void BatchRepair(string diagPath, string fileModel, string fileObs, string fileReal, BatchPlanner planner, double overhead, bool minCard, int maxNumOfDiag) //do it more generic
        {
            bool findDiagnoses = false;                                                                                                                                    //

            List <Observation>            observationsList = parser.ReadObsModelFiles(fileModel, fileObs);
            Dictionary <int, List <int> > obsReal          = parser.ReadRealObsFiles(fileReal);

            if (observationsList == null || observationsList.Count == 0 || obsReal == null || obsReal.Count == 0)
            {
                return;
            }
            createNewDiagnoser();
            SystemModel            model   = observationsList[0].TheModel;
            Dictionary <int, Gate> compDic = model.CreateCompDic();
            Stopwatch stopwatch            = new Stopwatch();
            CSVExport myExport             = new CSVExport();

            foreach (Observation obs in observationsList)
            {
                if (!obsReal.ContainsKey(obs.Id))
                {
                    continue;
                }
                List <int> realComp      = new List <int>(obsReal[obs.Id]);
                int        counter       = 0;
                double     cost          = 0;
                int        numberOfFixed = 0;
                stopwatch.Start();
                DiagnosisSet diagnoses = null;

                if (findDiagnoses) //
                {
                    diagnoses = Diagnoser.FindDiagnoses(obs);
                }
                else
                {
                    if (minCard)
                    {
                        string diagFileName = diagPath + model.Id + "_iscas85_" + obs.Id + ".all";
                        diagnoses = parser.ReadGroundedDiagnosesFile(diagFileName, compDic);
                    }
                    else
                    {
                        string diagFileName = diagPath + model.Id + "_" + obs.Id + "_Diag.txt";
                        diagnoses = parser.ReadDiagnosisFile(diagFileName, compDic);
                    }
                }
                if (diagnoses.Count == 1)
                {
                    continue;
                }
                if (maxNumOfDiag > 0 && diagnoses.Count > maxNumOfDiag)
                {
                    continue;
                }
                SystemState state = new SystemState(new List <Comp>(model.Components));
                state.Diagnoses = diagnoses;

                while (realComp.Count != 0)
                {
                    if (state.Diagnoses != null && state.Diagnoses.Count != 0)
                    {
                        RepairAction repairAction = planner.Plan(state);
                        if (repairAction != null)
                        {
                            counter++;
                            cost += overhead;
                            state.SetNextState(repairAction);
                            foreach (Gate gate in repairAction.R)
                            {
                                cost += gate.Cost;
                                numberOfFixed++;
                                if (realComp.Contains(gate.Id))
                                {
                                    realComp.Remove(gate.Id);
                                }
                            }
                            obs.TheModel.SetValue(obs.InputValues);
                            if (realComp.Count == 0)//the system is fixed
                            {
                                planner.ExportIterationDetails(model.Id, obs.Id, counter, true);
                                break;
                            }
                            else
                            {
                                planner.ExportIterationDetails(model.Id, obs.Id, counter, false);
                            }
                            foreach (int gid in realComp)
                            {
                                Gate g;
                                if (compDic.TryGetValue(gid, out g))
                                {
                                    gateFunc.Operate(g);
                                }
                            }
                            obs.OutputValues = model.GetValue();
                            DiagnosisSet newDiagnoses = new DiagnosisSet();
                            foreach (Diagnosis diag in state.Diagnoses.Diagnoses)
                            {
                                if (Diagnoser.IsConsistent(obs, diag))
                                {
                                    newDiagnoses.AddDiagnosis(diag);
                                }
                            }
                            state.Diagnoses = newDiagnoses;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                stopwatch.Stop();
                if (realComp.Count > 0)
                {
                    continue;
                }

                TimeSpan time = stopwatch.Elapsed;
                stopwatch.Reset();
                myExport.AddRow();
                myExport["System"]    = model.Id;
                myExport["Algorithm"] = planner.Algorithm();
                if (planner.Bounded)
                {
                    myExport["Bound"] = planner.Bound;
                }
                else
                {
                    myExport["Bound"] = "No Bound";
                }
                myExport["Objective Function"] = planner.ObjectiveFunction();
                myExport["Overhead"]           = overhead;
                myExport["Observation"]        = obs.Id;
                myExport["# Diagnoses"]        = diagnoses.Count;
                myExport["Runtime"]            = time;
                myExport["# Iterations"]       = counter;
                myExport["Cost"] = cost;
                myExport["# Fixed Components"] = numberOfFixed;
            }
            string fileName = model.Id + "_" + planner.Type() + "_o=" + overhead;

            if (maxNumOfDiag > 0)
            {
                fileName += "_MaxDiag" + maxNumOfDiag;
            }
            myExport.ExportToFile(fileName + ".csv");
            planner.CreateIterationDetailsFile(fileName + "_IterationDetails");
        }
Example #7
0
        public DiagnosisSet ReadGroundedDiagnosesFile(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)
            {
                if (!row.StartsWith("[[gate"))
                {
                    continue;
                }
                string        sDiags  = row.Remove(row.Length - 2, 2).Substring(1);
                List <string> delDiag = new List <string>();
                delDiag.Add("[");
                delDiag.Add("]");
                List <string>       diagsString = sDiags.Split(delDiag.ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
                List <List <Gate> > grounded    = new List <List <Gate> >();
                foreach (string d in diagsString)
                {
                    delDiag.Add(".");
                    delDiag.Add(",");
                    delDiag.Add("gate");
                    List <string> scomps = d.Split(delDiag.ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
                    List <Gate>   comps  = new List <Gate>();
                    foreach (string comp in scomps)
                    {
                        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 (compDic.ContainsKey(compId))
                        {
                            comps.Add(compDic[compId]);
                        }
                    }
                    grounded.Add(comps);
                }
                grounded = grounded.Where(x => x.Count > 0).ToList();
                List <Diagnosis> diagnoses = generateDiagsFromGrounded(grounded);
                diagnoses.ForEach(x => ans.AddDiagnosis(x));
            }

            return(ans);
        }
Example #8
0
        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);
        }
Example #9
0
        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);
        }
Example #10
0
        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);
        }