public double SystemRepair(RepairAction repairAction) { double sysRepair = 0; if (m_diagnoses == null || repairAction == null || repairAction.Count == 0) { return(sysRepair); } if (m_diagnoses.Count == 0) { return(1); //system is repaired } foreach (Diagnosis diag in m_diagnoses.Diagnoses) { if (diag.Comps == null || diag.Comps.Count == 0 || diag.Comps.Count > repairAction.Count) { continue; } //double p = diag.Probability; double p = diag.Probability / m_diagnoses.SetProbability; //changes foreach (Comp g in diag.Comps) { if (!repairAction.Contains(g)) { p = 0; break; } } sysRepair += p; } return(sysRepair); }
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); }
public override bool Equals(object obj) { if (obj == null) { return(false); } if (obj is RepairAction) { RepairAction action = (RepairAction)obj; if (action.R == null || R == null) { return(false); } if (R.Count != action.R.Count) { return(false); } foreach (Comp c in R) { if (!action.R.Contains(c)) { return(false); } } return(true); } else { return(base.Equals(obj)); } }
public MDPState ComputeNextState(MDPState state, RepairAction action) { bool systemNotFixed = stateTransition(state, action); if (systemNotFixed) { if (nextStatesDic.ContainsKey(state)) { if (nextStatesDic[state].ContainsKey(action)) { return(nextStatesDic[state][action]); } } else { nextStatesDic.Add(state, new Dictionary <RepairAction, MDPState>()); } SystemState nextSysState = state.State.GetNextState(action); MDPState nextState = new MDPState(nextSysState, state.Level + 1); nextStatesDic[state].Add(action, nextState); return(nextState); } else { MDPState nextState = new MDPState(); nextState.SystemRepairedState = true; nextState.Level = state.Level + 1; return(nextState); } }
public void SetNextState(RepairAction action) { DiagnosisSet nextDiagSet = computeNextState(action); if (nextDiagSet == null) { return; } Diagnoses = nextDiagSet; }
public virtual void FillIterationDetails(RepairAction chosenAction, int counter, double wastedCost) { iteraionDetailsFilled = true; totalStopWatch.Stop(); IterationDetails.RunTime = totalStopWatch.Elapsed.TotalMilliseconds; IterationDetails.ChosenRepairAction = chosenAction; IterationDetails.Cost = costEstimator.ComputeRepairCost(chosenAction); IterationDetails.FoundOpt = foundOpt; IterationDetails.NumOfExpanded = counter; IterationDetails.WastedCost = wastedCost; }
public double WastedCostUtility(RepairAction repairAction, SystemState state) { if (state == null || repairAction == null) { return(0); } double fp = FPCost(repairAction, state.HealthState); double sysNotRep = (1 - state.SystemRepair(repairAction)); double fn = FNCost(repairAction, state); return(fp + (sysNotRep * fn)); }
public int GetNumOfTimesChosen(RepairAction action) { int nb; if (numberOfTimesChosen.TryGetValue(action, out nb)) { return(numberOfTimesChosen[action]); } else { return(0); } }
private double calcFNCostNextState(RepairAction repairAction, SystemState state) { SystemState nextState = state.GetNextState(repairAction); if (nextState.Diagnoses.Count != state.Diagnoses.Count) { return(calcFNCostRegular(repairAction, nextState.HealthState)); } else { return(calcFNCostRegular(repairAction, state.HealthState)); } }
public void UpdateStateActionValue(RepairAction action, double value) { if (!numberOfTimesChosen.ContainsKey(action)) { numberOfTimesChosen.Add(action, 0); } numberOfTimesChosen[action]++; if (!stateActionValue.ContainsKey(action)) { stateActionValue.Add(action, 0); } stateActionValue[action] += value; }
public double FPCost(RepairAction repairAction, HealthStateVector HealthState) { double ans = 0; if (repairAction == null || repairAction.Count == 0 || HealthState == null || HealthState.Count == 0) { return(ans); } foreach (Comp c in repairAction.R) { ans += ((1 - HealthState.GetCompHealthState(c)) * c.Cost); } return(ans); }
private MDPState descendByUCB(MDPState state) { RepairActionsSet actions = repairActionSearcher.ComputePossibleAcions(state.State); if (actions == null) { return(null); } RepairAction currentAction = actions.NextAction(); double U; RepairAction bestAction = null; double ans = double.MinValue; while (currentAction != null && currentAction.Count != 0) { int actionNb = state.GetNumOfTimesChosen(currentAction); if (actionNb == 0) { U = (-costEstimator.FPCost(currentAction, state.State.HealthState)) / (currentAction.Count); } else { double alpha = state.GetStateActionValue(currentAction); U = alpha + numOfDiag * (Math.Sqrt(Math.Log10(state.NumberOfVisits) / actionNb)); } if (U > ans) //update best action { bestAction = currentAction; ans = U; } currentAction = actions.NextAction(); } MDPState nextState = null; if (bestAction != null) { state.LatestChosenAction = bestAction; nextState = ComputeNextState(state, bestAction); } state.NumberOfVisits++; return(nextState); }
private double calcFNCostWorstCase(RepairAction repairAction, List <Comp> components) { double ans = 0; if (components == null || components.Count == 0) { return(ans); } foreach (Comp c in components) { if (!repairAction.Contains(c)) { ans += Overhead; } } return(ans); }
public override RepairAction Plan(SystemState state) { InitialParameters(); if (state == null || state.HealthState == null || state.HealthState.Count == 0) { return(null); } List <Comp> ans = new List <Comp>(); List <double> hsVector = new List <double>(state.HealthState.CurrentHealthState); int counter = 0; double value = 0; for (int i = 0; i < k; i++) { double max = 0; Comp comp = null; int index = -1; for (int j = 0; j < hsVector.Count; j++) { counter++; Comp c = state.HealthState.Components[j]; double hs = hsVector[j]; if (hs > max) { max = hs; comp = c; index = j; } } if (comp != null) { value += max; ans.Add(comp); hsVector[index] = 0; } } if (ans.Count == 0) { return(null); } RepairAction bestRepairAction = new RepairAction(ans); FillIterationDetails(bestRepairAction, counter, value); return(bestRepairAction); }
public double ComputeRepairCost(RepairAction repairAction) { double ans = 0; if (repairAction == null || repairAction.Count == 0) { return(ans); } foreach (Comp c in repairAction.R) { if (c != null) { ans += c.Cost; } } ans += Overhead; return(ans); }
public override double FNCost(RepairAction repairAction, SystemState state) { if (state == null || repairAction == null || repairAction.Count == 0) { return(0); } if (type == PessimisticEstimationType.regular) { return(calcFNCostRegular(repairAction, state.HealthState)); } else if (type == PessimisticEstimationType.worstCase) { return(calcFNCostWorstCase(repairAction, state.Diagnoses.Components)); } else { return(calcFNCostNextState(repairAction, state)); } }
public double GetStateActionValue(RepairAction action) { double value; int actionNb; if (stateActionValue.TryGetValue(action, out value)) { double ans = stateActionValue[action]; if (numberOfTimesChosen.TryGetValue(action, out actionNb)) { ans = ans / actionNb; } return(ans); } else { return(0); } }
public override RepairAction Plan(SystemState state) { if (state == null || state.Diagnoses == null || state.Diagnoses.Count == 0) { return(null); } double max = -1; RepairAction ans = null; foreach (Diagnosis diag in state.Diagnoses.Diagnoses) { if (diag.Probability > max) { ans = new RepairAction(diag.Comps); max = diag.Probability; } } return(ans); }
public SystemState GetNextState(RepairAction action) { DiagnosisSet nextDiagSet = computeNextState(action); if (nextDiagSet == null) { return(this); // return null? } SystemState nextState = null; if (HealthState == null) { nextState = new SystemState(null); } else { nextState = new SystemState(HealthState.Components); } nextState.Diagnoses = nextDiagSet; return(nextState); }
public override RepairAction Plan(SystemState state) { ResetProperties(); nodesCounter = 1; RepairAction chosenAction = null; nextStatesDic.Clear(); numOfDiag = state.Diagnoses.Count; if (state == null || state.Diagnoses == null || numOfDiag == 0) { return(chosenAction); } if (numOfDiag == 1) { chosenAction = new RepairAction(state.Diagnoses.Diagnoses.First().Comps); FillIterationDetails(chosenAction, nodesCounter, costEstimator.WastedCostUtility(chosenAction, state)); return(chosenAction); } MDPState initMDPState = new MDPState(state, 0); for (int j = 0; j < Iterations; j++) { if (Stop()) { break; } performOneIteration(initMDPState); } chosenAction = initMDPState.GetBestValueAction(); FillIterationDetails(chosenAction, nodesCounter, costEstimator.WastedCostUtility(chosenAction, state)); return(chosenAction); }
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); } } }
private bool stateTransition(MDPState state, RepairAction action) //return true if the system is not fixed in the next state { double sysRepair = state.State.SystemRepair(action); if (sysRepair == 0) { return(true); } else if (sysRepair == 1) { return(false); } else { if (rnd.NextDouble() > sysRepair) { return(true); } else { return(false); } } }
public override RepairAction Plan(SystemState state) { ResetProperties(); if (state == null || state.Diagnoses == null || state.Diagnoses.Count == 0) { return(null); } RepairActionsSet actions = repairActionSearcher.ComputePossibleAcions(state); if (actions == null) { return(null); } double min = double.MaxValue; RepairAction bestRepairAction = null; RepairAction action = actions.NextAction(); int counter = 0; while (action != null && action.Count > 0) { counter++; if (Stop()) { break; } double val = costEstimator.WastedCostUtility(action, state); if (val < min) { min = val; bestRepairAction = action; } action = actions.NextAction(); } FillIterationDetails(bestRepairAction, counter, min); return(bestRepairAction); }
public RepairAction GetBestValueAction() //the largest { if (stateActionValue.Count == 0) { return(LatestChosenAction); } RepairAction bestValAction = null; double bestVal = -10000000; foreach (RepairAction action in stateActionValue.Keys) { double val = GetStateActionValue(action); if (bestValAction == null || val > bestVal) { bestVal = val; bestValAction = action; } } if (bestValAction == null) { return(LatestChosenAction); } return(bestValAction); }
private double calcFNCostRegular(RepairAction repairAction, HealthStateVector HealthState) { double FN = 0; double FFP = 0; if (HealthState == null || HealthState.Count == 0) { return(0); } for (int i = 0; i < HealthState.Count; i++) { Comp c = HealthState.Components[i]; double h = HealthState.CurrentHealthState[i]; if (h == 0) { continue; } if (!repairAction.Contains(c)) { FN += (h * Overhead); if (ffpType == FFPType.FFP) { FFP += ((1 - h) * (c.Cost)); } else if (ffpType == FFPType.FFPplusOverhead) { FFP += ((1 - h) * (c.Cost + Overhead)); } } } if (ffpType != FFPType.noFFP) { return(FN + FFP); } return(FN); }
public override double FNCost(RepairAction repairAction, SystemState state) { return(Overhead); }
public AStarVertex(RepairAction repairAction, BatchCostEstimator costEstimator, SystemState state) : this(repairAction, costEstimator, state, 0) { }
public abstract double FNCost(RepairAction repairAction, SystemState state);
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 override RepairAction Plan(SystemState state) { ResetProperties(); if (state == null || state.Diagnoses == null || state.Diagnoses.Count == 0) { return(null); } //improve the code appearance List <Comp> bestRepairAction = new List <Comp>(); SystemState currState = state; double totalUtility = double.MaxValue; List <Comp> chosenAction; int expandedCounter = 0; while (!Stop()) { double min = double.MaxValue; chosenAction = null; foreach (Diagnosis diagnosis in currState.Diagnoses.Diagnoses) { if (Stop()) { break; } List <Comp> action = new List <Comp>(diagnosis.Comps); if (action == null || action.Count == 0) { continue; } List <Comp> testedAction = new List <Comp>(action); if (bestRepairAction != null && bestRepairAction.Count > 0) { testedAction.AddRange(bestRepairAction); } double utility = costEstimator.WastedCostUtility(new RepairAction(testedAction), state); //one option: action,currState, second option: testedAction,state if (utility < min) { min = utility; chosenAction = action; } } currentDepth++; expandedCounter++; if (chosenAction != null && chosenAction.Count > 0) { if (totalUtility < min || min == 0) { if (bestRepairAction.Count == 0) { bestRepairAction.AddRange(chosenAction); } break; } bestRepairAction.AddRange(chosenAction); currState = currState.GetNextState(new RepairAction(chosenAction)); totalUtility = min; } else { break; } } RepairAction chosenRepairAction = new RepairAction(bestRepairAction); FillIterationDetails(chosenRepairAction, expandedCounter, totalUtility); return(chosenRepairAction); }