public override double StateCost(SystemState state) { return(Overhead); }
public override RepairAction Plan(SystemState state) { ResetProperties(); AStarVertex bestRepairAction = null; //its cost is the upper bound of the optimal solution PriorityQueue <AStarVertex> openList = new PriorityQueue <AStarVertex>(); //a priority queue with fvalue as the priority List <AStarVertex> closedList = new List <AStarVertex>(); double fmax = 0; //the lower bound of the optimal solution double fmin = -1; //initial state foreach (Diagnosis diag in state.Diagnoses.Diagnoses) { AStarVertex v = new AStarVertex(new RepairAction(diag.Comps), costEstimator, state, 1); if (bestRepairAction == null || v.Wastedcost < bestRepairAction.Wastedcost) { bestRepairAction = v; } if (fmin == -1) { fmin = v.FVal; } else if (v.FVal < fmin) { fmin = v.FVal; } openList.Enqueue(v, v.FVal); } while (!openList.IsEmpty()) { AStarVertex bestV = (AStarVertex)openList.Dequeue(); //extract the best vertex from OPEN if (closedList.Contains(bestV)) { continue; } //update fmax if (bestV.FVal > fmax) { fmax = bestV.FVal; } //halting condition if (bestRepairAction != null && fmax >= bestRepairAction.Wastedcost) { break; } if (bestV.FVal == bestV.Wastedcost) //no point to create his children { continue; //insert to closed list? } currentDepth = bestV.Depth; if (Stop()) { break; } //create all of best V childs and insert them to OPEN foreach (Diagnosis diag in state.Diagnoses.Diagnoses) { if (Stop()) { break; } List <Comp> listd = new List <Comp>(bestV.Action.R); foreach (Comp c in diag.Comps) { if (!listd.Contains(c)) { listd.Add(c); } } if (listd.Count == bestV.Action.R.Count) { continue; } AStarVertex v = new AStarVertex(new RepairAction(listd), costEstimator, state, bestV.Depth + 1); openList.Enqueue(v, v.FVal); if (v.Wastedcost < bestRepairAction.Wastedcost) { bestRepairAction = v; } } closedList.Add(bestV); //add best vertex to CLOSED } FillIterationDetails(bestRepairAction.Action, closedList.Count + 1, bestRepairAction.Wastedcost); ((AStarIterationDetails)IterationDetails).ChosenFval = bestRepairAction.FVal; ((AStarIterationDetails)IterationDetails).Fminmax = fmax; ((AStarIterationDetails)IterationDetails).Fminstart = fmin; return(bestRepairAction.Action); }
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 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"); }
public abstract RepairActionsSet ComputePossibleAcions(SystemState state);
public override RepairActionsSet ComputePossibleAcions(SystemState state) { int k = K; if (state == null || state.Diagnoses == null || state.Diagnoses.Count == 0) { return(null); } List <List <Comp> > ans = new List <List <Comp> >(); List <Comp> comps = new List <Comp>(); // hash set? foreach (Diagnosis diag in state.Diagnoses.Diagnoses) { foreach (Comp c in diag.Comps) { if (!comps.Contains(c)) { comps.Add(c); List <Comp> list = new List <Comp>(); list.Add(c); ans.Add(list); } } } while (k > 1) { List <List <Comp> > temp = new List <List <Comp> >(); foreach (List <Comp> list in ans) { foreach (Comp c in comps) { if (!list.Contains(c)) { List <Comp> newList = new List <Comp>(list); newList.Add(c); temp.Add(newList); } } } ans.AddRange(temp); k--; } RepairActionsSet actions; if (k == 1) { actions = new RepairActionsHashSet(); } else { actions = new RepairActionsTrie(); } foreach (List <Comp> list in ans) { SortedSet <Comp> action = new SortedSet <Comp>(new Comp.CompComparer()); foreach (Comp c in list) { action.Add(c); } actions.AddAction(action); } return(actions); }
public abstract double StateCost(SystemState state);
//this function calculates the fvalue of this vertex private void calcFval(BatchCostEstimator costEstimator, SystemState state) { double overhead = costEstimator.Overhead; List <double> l_fp = new List <double>(); List <double> l_sr = new List <double>(); foreach (Comp c in state.Diagnoses.Components) { if (!Action.Contains(c)) { double hs = state.HealthState.GetCompHealthState(c); double cost = ((1 - hs) * c.Cost); l_fp.Add(cost); l_sr.Add(hs); } } l_sr.Sort(); l_sr.Reverse(); l_fp.Sort(); double fpv = costEstimator.FPCost(Action, state.HealthState); double srv = state.SystemRepair(Action); double fmin = Wastedcost; double sum_hs = l_sr.Sum(); double sum_sr = 0; double sum_dfp = 0; int b = Bound - Depth; int size = Math.Min(l_fp.Count, l_sr.Count); if (b > size || !Bounded) { b = size; } for (int i = 0; i < b; i++) { sum_dfp += l_fp[i]; sum_sr += l_sr[i]; double srvi = sum_sr + srv; if (srvi > 1) { srvi = 1; } double fnvi = (sum_hs - sum_sr) * overhead; if (fpv + ((1 - srv) * fnvi) > Wastedcost) { fnvi = overhead; } if (fpv + ((1 - srv) * fnvi) > Wastedcost) { fnvi = 0; } double fvi = fpv + sum_dfp + ((1 - srvi) * fnvi); if (fvi < fmin) { fmin = fvi; } } FVal = fmin; }
public AStarVertex(RepairAction repairAction, BatchCostEstimator costEstimator, SystemState state) : this(repairAction, costEstimator, state, 0) { }
public AStarVertex(RepairAction repairAction, BatchCostEstimator costEstimator, SystemState state, int depth) { if (repairAction == null || repairAction.Count == 0) { Action = null; FVal = -1; Depth = 0; } else { Action = repairAction; Wastedcost = costEstimator.WastedCostUtility(repairAction, state); Depth = depth; if (Bounded && Depth == Bound) { FVal = Wastedcost; } else { calcFval(costEstimator, state); } } }
public abstract RepairAction Plan(SystemState state);
public override double FNCost(RepairAction repairAction, SystemState state) { return(Overhead); }