Esempio n. 1
0
        public void HS(string fileName1, string fileName2)
        {
            List <Observation> observationsList = MOCreator.ReadObsModelFiles(fileName1, fileName2);

            if (observationsList == null || observationsList.Count == 0)
            {
                return;
            }
            FlipFunction       flip     = new FlipFunction();
            TimeSpan           x        = new TimeSpan(0, 5, 0);
            IterativeDeepening salgo    = new IterativeDeepening(flip, x);
            ConesAlgorithm     algo     = new ConesAlgorithm(salgo);
            Stopwatch          stopwtch = new Stopwatch();
            CSVExport          myExport = new CSVExport();

            salgo.agenda = DiagnosesSearcher.Agenda.helthState;
            foreach (Observation obs in observationsList)
            {
                stopwtch.Start();
                DiagnosisSet diagnoses = algo.FindDiagnoses(obs);
                stopwtch.Stop();
                if (diagnoses != null)
                {
                    myExport.AddRow();
                    myExport["System"]      = obs.TheModel.Id;
                    myExport["Observation"] = obs.Id;
                    myExport["# diagnoses"] = diagnoses.Count;
                    myExport["Runtime"]     = stopwtch.Elapsed;
                }
                stopwtch.Reset();
            }
            myExport.ExportToFile(observationsList.First().TheModel.Id + "HS.csv");
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
 public override bool Equals(object obj)
 {
     if (obj is DiagnosisSet)
     {
         DiagnosisSet diagSet = (DiagnosisSet)obj;
         if (diagSet == null || diagSet.Diagnoses == null)
         {
             return(false);
         }
         if (this.Diagnoses == null || this.Diagnoses.Count != diagSet.Diagnoses.Count)
         {
             return(false);
         }
         foreach (Diagnosis diag in this.Diagnoses)
         {
             if (!diagSet.Diagnoses.Contains(diag))
             {
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         return(base.Equals(obj));
     }
 }
Esempio n. 6
0
        public void CalcHelthState(DiagnosisSet diagnoses)
        {
            if (diagnoses == null || diagnoses.Count == 0)
            {
                return;
            }
            List <double> compHS = new List <double>();

            for (int i = 0; i < Components.Count; i++)
            {
                compHS.Add(0);
            }
            foreach (Diagnosis diag in diagnoses.Diagnoses)
            {
                foreach (Gate comp in diag.TheDiagnosis)
                {
                    int i = Components.IndexOf(comp); //check
                    if (i >= 0 && i < compHS.Count)
                    {
                        compHS[i] += (diag.Probability / diagnoses.SetProbability);
                    }
                }
            }
            CurrentHelthState = compHS;
            DiagnosesCounter  = diagnoses.Count;
        }
Esempio n. 7
0
        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]);
        }
Esempio n. 8
0
        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();
        }
Esempio n. 9
0
        public void SetNextState(RepairAction action)
        {
            DiagnosisSet nextDiagSet = computeNextState(action);

            if (nextDiagSet == null)
            {
                return;
            }
            Diagnoses = nextDiagSet;
        }
Esempio n. 10
0
        public void SetNextState(List <Gate> action)
        {
            DiagnosisSet nextDiagSet = computeNextState(action);

            if (nextDiagSet == null)
            {
                return;
            }
            Diagnoses = nextDiagSet;
        }
Esempio n. 11
0
 public HealthStateVector(List <Comp> components, DiagnosisSet diagnoses)
 {
     if (components != null)
     {
         Components = components;
     }
     else
     {
         Components = new List <Comp>();
     }
     CurrentHealthState = new List <double>();
     DiagnosesCounter   = 0;
     CalcHealthState(diagnoses);
 }
Esempio n. 12
0
        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]);
        }
Esempio n. 13
0
        private bool CheckHSDistance(int num, double distance, DiagnosisSet diagnoses)
        {
            if (diagnoses.Count - hSVector.DiagnosesCounter < num)
            {
                return(false);
            }
            bool          ans         = false;
            List <double> oldHSVector = hSVector.CurrentHelthState;

            hSVector.CalcHelthState(diagnoses);
            double dis = CalcHSVectorDistance(oldHSVector);

            if (dis <= distance)
            {
                ans = true;
            }
            return(ans);
        }
Esempio n. 14
0
        public SystemState GetNextState(List <Gate> action)
        {
            DiagnosisSet nextDiagSet = computeNextState(action);

            if (nextDiagSet == null)
            {
                return(this); // return null?
            }
            SystemState nextState = null;

            if (this.HelthState == null)
            {
                nextState = new SystemState(null);
            }
            else
            {
                nextState = new SystemState(HelthState.Components);
            }
            nextState.Diagnoses = nextDiagSet;
            return(nextState);
        }
Esempio n. 15
0
        public void temp(string fileModel, string fileObs, string fileReal)
        {
            createNewDiagnoser();
            RepairActionSearcher          ras              = new PowersetBasedSearcher();
            BatchCostEstimator            bce              = new PessimisticEstimator();
            MDPPlanner                    planner          = new MDPPlanner(ras, bce, 2);
            List <Observation>            observationsList = MOCreator.ReadObsModelFiles(fileModel, fileObs);
            Dictionary <int, List <int> > obsReal          = MOCreator.ReadRealObsFiles(fileReal);

            if (observationsList == null || observationsList.Count == 0 || obsReal == null || obsReal.Count == 0)
            {
                return;
            }
            Observation  obs       = observationsList[0];
            List <int>   realComp  = new List <int>(obsReal[obs.Id]);
            DiagnosisSet diagnoses = Diagnoser.FindDiagnoses(obs);
            SystemState  state     = new SystemState(obs.TheModel.Components);

            state.Diagnoses = diagnoses;
            List <Gate> repairAction = planner.Plan(state);
        }
Esempio n. 16
0
        public void CreateDiagnosesFiles(string fileModel, string fileObs)
        {
            createNewDiagnoser();
            List <Observation> observationsList = parser.ReadObsModelFiles(fileModel, fileObs);

            foreach (Observation obs in observationsList)
            {
                obs.TheModel.SetValue(obs.InputValues);
                DiagnosisSet diagnoses = Diagnoser.FindDiagnoses(obs);
                if (diagnoses == null || diagnoses.Count == 0)
                {
                    continue;
                }
                StreamWriter sw = new StreamWriter(obs.TheModel.Id + "_" + obs.Id + "_Diag.txt");
                foreach (Diagnosis diag in diagnoses.Diagnoses)
                {
                    sw.WriteLine(diag.ToString());
                }
                sw.Close();
            }
        }
Esempio n. 17
0
        public void CalcProbabilityMass(string fileModel, string fileObs)
        {
            createNewDiagnoser();
            ConesAlgorithm         conesAlgo        = (ConesAlgorithm)Diagnoser;
            List <Observation>     observationsList = parser.ReadObsModelFiles(fileModel, fileObs);
            CSVExport              myExport         = new CSVExport();
            SystemModel            model            = observationsList[0].TheModel;
            Dictionary <int, Gate> compDic          = model.CreateCompDic();
            string diagnosesFilesPath   = "diagnoses/";
            string tldiagnosesFilesPath = "tldiagnoses/";

            model.createCones();
            Dictionary <int, Cone> conesDic = new Dictionary <int, Cone>();

            foreach (Cone c in model.Cones)
            {
                if (!conesDic.ContainsKey(c.Id))
                {
                    conesDic.Add(c.Id, c);
                }
            }
            foreach (Observation obs in observationsList)
            {
                string       diagFileName     = diagnosesFilesPath + model.Id + "_" + obs.Id + "_Diag.txt";
                DiagnosisSet allDiagnoses     = parser.ReadDiagnosisFile(diagFileName, compDic);
                string       tldFileName      = tldiagnosesFilesPath + model.Id + "_iscas85_" + obs.Id + ".tld"; //!!! txt?
                DiagnosisSet tlDiagnoses      = parser.ReadTLDiagnosisFile(tldFileName, conesDic);
                DiagnosisSet minCardDiagnoses = conesAlgo.AbstractDiagGrounding(obs, tlDiagnoses);
                myExport.AddRow();
                myExport["Observation"]                    = obs.Id;
                myExport["# all diagnoses"]                = allDiagnoses.Count;
                myExport["all diagnoses probability"]      = allDiagnoses.SetProbability;
                myExport["# min card diagnoses"]           = minCardDiagnoses.Count;
                myExport["min card diagnoses probability"] = minCardDiagnoses.SetProbability;
            }
            myExport.ExportToFile(model.Id + "_probabilityMass.csv");
        }
Esempio n. 18
0
        private Diagnosis chooseReal(DiagnosisSet diagnoses)
        {
            double val = rnd.NextDouble();

            if (val == 1)
            {
                return(diagnoses.Diagnoses.Last());
            }
            else if (val == 0)
            {
                return(diagnoses.Diagnoses.First());
            }
            double sum = 0;

            foreach (Diagnosis diag in diagnoses.Diagnoses)
            {
                sum += (diag.Probability / diagnoses.SetProbability);
                if (sum >= val)
                {
                    return(diag);
                }
            }
            return(diagnoses.Diagnoses.Last());
        }
Esempio n. 19
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);
        }
Esempio n. 20
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);
        }
Esempio n. 21
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);
        }
Esempio n. 22
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);
        }
Esempio n. 23
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");
        }
Esempio n. 24
0
        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);
        }
Esempio n. 25
0
        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);
        }