public IEnumerable <OptimalSelectionWithUncertainty> GetOptimalSelections(double minCost, Goal goal, BDDBasedUncertaintyPropagator propagator) { var timer = Stopwatch.StartNew(); var optimalSelections = new List <OptimalSelectionWithUncertainty> (); double bestVU = 1; double bestUS = double.MaxValue; int tested_count = 0; var countermeasure_goals = _model.Resolutions().Select(x => x.ResolvingGoalIdentifier).Distinct(); foreach (var cg in GetAllCombinations(countermeasure_goals.ToList())) { var cost = cg.Count(); if (minCost > 0 && cost > minCost) { continue; } tested_count++; var activeResolutions = _model.Resolutions().Where(x => cg.Contains(x.ResolvingGoalIdentifier)); foreach (var resolution in activeResolutions) { integrator.Integrate(resolution); } var sr = (SimulatedSatisfactionRate)propagator.GetESR(goal); foreach (var resolution in activeResolutions) { integrator.Remove(resolution); } double vu = sr.ViolationUncertainty(goal.RDS); double us = sr.UncertaintySpread(goal.RDS); if (vu < (bestVU - EPSILON)) { bestVU = vu; optimalSelections = new List <OptimalSelectionWithUncertainty> (); optimalSelections.Add(new OptimalSelectionWithUncertainty(activeResolutions, cost, vu, us)); } else if (Math.Abs(vu - bestVU) < EPSILON) { optimalSelections.Add(new OptimalSelectionWithUncertainty(activeResolutions, cost, vu, us)); } } timer.Stop(); stats.TimeToComputeSelection = timer.Elapsed; stats.NbTestedSelectionsForOptimality = tested_count; return(optimalSelections); }
public Dictionary <HashSet <Obstacle>, UncertaintyCriticalScore> GetObstacleScores(int size) { var dict = new Dictionary <HashSet <Obstacle>, UncertaintyCriticalScore>(); var obstacles = _model.LeafObstacles().ToList(); foreach (var combination in GetAllCombinations(obstacles, size)) { var d = (SimulatedSatisfactionRate)_propagator.GetESR(_root, combination); var key = combination.ToHashSet(); var val = new UncertaintyCriticalScore(d.UncertaintySpread(rsr), d.ViolationUncertainty(rsr)); dict.Add(key, val); } return(dict); }
public static void Main(string [] args) { Console.WriteLine("*** This is CMSelectorUncertainty from KAOSTools. ***"); Console.WriteLine("*** For more information on KAOSTools see <https://github.com/ancailliau/KAOSTools> ***"); Console.WriteLine("*** Please report bugs to <https://github.com/ancailliau/KAOSTools/issues> ***"); Console.WriteLine(); Console.WriteLine("*** Copyright (c) 2017, Université catholique de Louvain ***"); Console.WriteLine(""); string rootname = "root"; options.Add("root=", "Specify the root goal for which the selection shall be optimized. (Default: root)", v => rootname = v); int n_samples = 1000; options.Add("n_samples=", $"Specify the number of samples to use to perform Monte-Carlo simulation. (Default: {n_samples})", v => n_samples = int.Parse(v)); string outfile = null; options.Add("outfile=", "Specify the file to export the raw values, in csv.", v => outfile = v); bool doCombine = false; var combine = ExpertCombinationMethod.MendelSheridan; options.Add("combine=", "Combine the estimates from the experts using the specified method.", v => { doCombine = true; if (v.Equals("cook")) { combine = ExpertCombinationMethod.Cook; } else if (v.Equals("mendel-sheridan") | v.Equals("mendelsheridan") | v.Equals("ms")) { combine = ExpertCombinationMethod.MendelSheridan; } else { PrintError($"Combination method '{v}' is not known"); Environment.Exit(1); } }); Init(args); var root = model.Goal(rootname); if (root == null) { PrintError("The goal '" + rootname + "' was not found"); } try { if (doCombine) { var expert_combination = new ExpertCombinator(model, combine); expert_combination.Combine(); } var optimizer = new NaiveCountermeasureSelectionOptimizerWithUncertainty(model, .05); var propagator = new BDDBasedUncertaintyPropagator(model, n_samples); var sr = (SimulatedSatisfactionRate)propagator.GetESR(root); Console.WriteLine("Violation Uncertainty without countermeasures: " + sr.ViolationUncertainty(root.RDS)); Console.WriteLine("Uncertainty Spread without countermeasures: " + sr.UncertaintySpread(root.RDS)); Console.WriteLine("Required Satisfaction Rate: " + root.RDS); var minimalCost = optimizer.GetMinimalCost(root, propagator); Console.WriteLine("Minimal cost to guarantee RSR: " + minimalCost); if (minimalCost >= 0) { var optimalSelections = optimizer.GetOptimalSelections(minimalCost, root, propagator); if (optimalSelections.Count() == 0) { Console.WriteLine("Optimal selections: No countermeasure to select."); Console.WriteLine(); } else { Console.WriteLine($"Optimal selections ({optimalSelections.Count ()}):"); foreach (var o in optimalSelections.Distinct()) { Console.WriteLine("* {0} (vu={1}, us={2})", o, o.ViolationUncertainty, o.UncertaintySpread); } } } else { var optimalSelections = optimizer.GetOptimalSelections(minimalCost, root, propagator); Console.WriteLine($"The best that can be achived ({optimalSelections.Count ()}):"); foreach (var o in optimalSelections.Distinct()) { Console.WriteLine("* {0} (vu={1}, us={2})", o, o.ViolationUncertainty, o.UncertaintySpread); } } var stat = optimizer.GetStatistics(); Console.WriteLine(); Console.WriteLine("--- Statistics ---"); Console.WriteLine("Number of countermeasure goals: " + stat.NbResolvingGoals); Console.WriteLine("Number of possible selections: " + stat.NbSelections); Console.WriteLine("Number of safe selections: " + stat.NbSafeSelections); Console.WriteLine("Number of tested selections (for minimal cost): " + stat.NbTestedSelections); Console.WriteLine("Number of tested selections (for optimal selection): " + stat.NbTestedSelectionsForOptimality); Console.WriteLine("Maximal safe cost: " + stat.MaxSafeCost); Console.WriteLine("Time to compute minimal cost: " + stat.TimeToComputeMinimalCost); Console.WriteLine("Time to compute optimal selections: " + stat.TimeToComputeMinimalCost); } catch (Exception e) { PrintError("An error occured during the computation. (" + e.Message + ").\n" + "Please report this error to <https://github.com/ancailliau/KAOSTools/issues>.\n" + "----------------------------\n" + e.StackTrace + "\n----------------------------\n"); } }
public static void Main(string[] args) { Console.WriteLine("*** This is UncertaintyPropagator from KAOSTools. ***"); Console.WriteLine("*** For more information on KAOSTools see <https://github.com/ancailliau/KAOSTools> ***"); Console.WriteLine("*** Please report bugs to <https://github.com/ancailliau/KAOSTools/issues> ***"); Console.WriteLine(); Console.WriteLine("*** Copyright (c) 2017, Université catholique de Louvain ***"); Console.WriteLine(""); string rootname = "root"; options.Add("root=", "Specify the root goal for which to compute the satisfaction rate. (Default: root)", v => rootname = v); int n_samples = 1000; options.Add("n_samples=", $"Specify the number of samples to use to perform Monte-Carlo simulation. (Default: {n_samples})", v => n_samples = int.Parse(v)); string outfile = null; options.Add("outfile=", "Specify the file to export the raw values, in csv.", v => outfile = v); bool doCombine = false; var combine = ExpertCombinationMethod.MendelSheridan; options.Add("combine=", "Combine the estimates from the experts using the specified method.", v => { doCombine = true; if (v.Equals("cook")) { combine = ExpertCombinationMethod.Cook; } else if (v.Equals("mendel-sheridan") | v.Equals("mendelsheridan") | v.Equals("ms")) { combine = ExpertCombinationMethod.MendelSheridan; } else { PrintError($"Combination method '{v}' is not known"); Environment.Exit(1); } }); Init(args); KAOSCoreElement root = model.Goal(rootname); if (root == null) { root = model.Obstacle(rootname); if (root == null) { PrintError("The goal or obstacle '" + rootname + "' was not found."); } } try { if (doCombine) { expert_combination = new ExpertCombinator(model, combine); expert_combination.Combine(); } IPropagator _propagator; _propagator = new BDDBasedUncertaintyPropagator(model, n_samples); SimulatedSatisfactionRate esr = null; if (root is Goal g) { esr = (SimulatedSatisfactionRate)_propagator.GetESR(g); } else if (root is Obstacle o) { esr = (SimulatedSatisfactionRate)_propagator.GetESR(o); } Console.WriteLine(root.FriendlyName + ":"); Console.WriteLine(" Mean: {0:P}", esr.Mean); if (root is Goal g2) { Console.WriteLine(" Required Satisfaction Rate: {0:P}", g2.RDS); Console.WriteLine(" Violation Uncertainty: {0:P}", esr.ViolationUncertainty(g2.RDS)); Console.WriteLine(" Uncertainty Spread: {0}", esr.UncertaintySpread(g2.RDS)); } if (doCombine && combine == ExpertCombinationMethod.Cook) { var stats = (CookFrameworkStatistics)expert_combination.GetStatistics(); Console.WriteLine(); Console.WriteLine("-- Information score (Cook):"); foreach (var item in stats.InformationScores) { Console.WriteLine($" {item.Key} : {item.Value}"); } Console.WriteLine("-- Calibration score (Cook):"); foreach (var item in stats.CalibrationScores) { Console.WriteLine($" {item.Key} : {item.Value}"); } Console.WriteLine("-- Combination weights (Cook):"); foreach (var item in stats.Weights) { Console.WriteLine($" {item.Key.Name} : {item.Value}"); } } if (!string.IsNullOrEmpty(outfile)) { var out_filename = Environment.ExpandEnvironmentVariables(outfile); using (var f = File.CreateText(out_filename)) { f.WriteLine(root.Identifier); f.Write(string.Join("\n", esr.Values)); } } } catch (Exception e) { PrintError("An error occured during the computation. (" + e.Message + ").\n" + "Please report this error to <https://github.com/ancailliau/KAOSTools/issues>.\n" + "----------------------------\n" + e.StackTrace + "\n----------------------------\n"); } }
/// <summary> /// Returns the minimal cost ensuring the goal RDS. Returns -1 if no /// selection satisfy the RDS. /// </summary> /// <param name="goal">The goal</param> /// <returns>The minimal cost.</returns> public double GetMinimalCost(Goal goal, BDDBasedUncertaintyPropagator propagator) { var timer = Stopwatch.StartNew(); int minCost = -1; var sr = (SimulatedSatisfactionRate)propagator.GetESR(goal); if (sr.ViolationUncertainty(goal.RDS) < threshold) { return(0); } int count = 0; int tested_count = 0; int safe_count = 0; var countermeasure_goals = _model.Resolutions().Select(x => x.ResolvingGoalIdentifier).Distinct(); foreach (var cg in GetAllCombinations(countermeasure_goals.ToList())) { count++; var cost = cg.Count(); //if (minCost >= 0 && cost > minCost) continue; tested_count++; var r = _model.Resolutions().Where(x => cg.Contains(x.ResolvingGoalIdentifier)); foreach (var resolution in r) { integrator.Integrate(resolution); } sr = (SimulatedSatisfactionRate)propagator.GetESR(goal); // Console.WriteLine ( new OptimalSelection (r, cost, sr.SatisfactionRate) ); foreach (var resolution in r) { integrator.Remove(resolution); } if (sr.ViolationUncertainty(goal.RDS) < threshold) { safe_count++; stats.MaxSafeCost = Math.Max(stats.MaxSafeCost, cost); if (minCost == -1 || cost < minCost) { minCost = cost; } } } timer.Stop(); stats.TimeToComputeMinimalCost = timer.Elapsed; stats.NbResolvingGoals = countermeasure_goals.Count(); stats.NbSelections = count; stats.NbTestedSelections = tested_count; stats.NbSafeSelections = safe_count; return(minCost); }