public void TestCECountermeasureSelectionFDS() { var input = File.ReadAllText("/Users/acailliau/Google Drive/PhD/Dissertation/running-example-fds/model2.kaos"); ModelBuilder parser = new ModelBuilder(); var model = parser.Parse(input); var optimizer = new MOCECountermeasureSelectionOptimizer(model); var propagator = new BDDBasedPropagator(model); var root = model.Goal("locals_warned_when_risk_imminent"); var sr = (DoubleSatisfactionRate)propagator.GetESR(root); Console.WriteLine("Satisfaction Rate without countermeasures: " + sr.SatisfactionRate); Console.WriteLine("Required Satisfaction Rate: " + root.RDS); var optimalSelections = optimizer.GetOptimalSelections(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().OrderBy(x => x.Cost).ThenBy(x => x.SatisfactionRate)) { Console.WriteLine("* " + o); } } }
public void TestMilestoneGoalRefinement() { var options = new RandomModelOptions { MinGoalBranchingFactor = 2, MaxGoalBranchingFactor = 4, NbGoals = 2, MinObstacleANDBranchingFactor = 2, MaxObstacleANDBranchingFactor = 4, }; var generator = new RandomModelGenerator(options); var model = generator.Generate(); var p1 = new PatternBasedPropagator(model); var p2 = new BDDBasedPropagator(model); Console.WriteLine("Generated goals: " + model.Goals().Count()); Console.WriteLine("Generated obstacles: " + model.Obstacles().Count()); var root = model.RootGoals().Single(); var sr1 = (DoubleSatisfactionRate)p1.GetESR(root); var sr2 = (DoubleSatisfactionRate)p2.GetESR(root); Assert.AreEqual(sr1.SatisfactionRate, sr2.SatisfactionRate, 0.0001); }
public void TestRemoveObstructedGoal() { var input = @"declare goal [ anchor ] refinedby child1, child2 end declare goal [ child1 ] obstructedby o1 end declare goal [ child2 ] obstructedby o2 end declare obstacle [ o1 ] probability .1 resolvedby [substitution:anchor] cm1 end declare obstacle [ o2 ] refinedby so1 refinedby so2 end declare obstacle [ so1 ] probability .2 resolvedby [substitution:anchor] cm2 end declare obstacle [ so2 ] probability .3 end declare goal [ cm1 ] end declare goal [ cm2 ] obstructedby o_cm end declare obstacle [ o_cm ] probability .4 end"; var parser = new ModelBuilder(); var model = parser.Parse(input); var resolutions = model.Resolutions(); foreach (var r in resolutions) { r.Name = "R_" + r.ResolvingGoalIdentifier; } var anchor = model.Goal("anchor"); var p1 = new BDDBasedPropagator(model); System.Console.WriteLine(p1.GetESR(anchor)); HashSet <Resolution> hashSet = new HashSet <Resolution> (model.Resolutions()); }
public Dictionary <HashSet <Obstacle>, CriticalScore> GetObstacleScores(int combination_size) { var dict = new Dictionary <HashSet <Obstacle>, CriticalScore>(); var obstacles = _model.LeafObstacles().ToList(); foreach (var combination in GetAllCombinations(obstacles, combination_size)) { var d = (DoubleSatisfactionRate)_propagator.GetESR(_root, combination); var key = combination.ToHashSet(); double combination_probability = combination.Aggregate(1d, (arg1, arg2) => { var sat_rate = (DoubleSatisfactionRate)_model.satisfactionRateRepository.GetObstacleSatisfactionRate(arg2.Identifier); if (sat_rate == null) { throw new InvalidOperationException($"The obstacle '{arg2.Identifier}' was not estimated"); } return(arg1 * sat_rate.SatisfactionRate); }); var val = new CriticalScore(combination_probability, d.ViolationSeverity(rsr)); dict.Add(key, val); } return(dict); }
private double GetScore(IEnumerable <Goal> goals, IPropagator propagator, HashSet <string> selection, out IEnumerable <string> rootsSatisfied) { var r = _model.Resolutions().Where(x => selection.Contains(x.ResolvingGoalIdentifier)); var rootGoalsSatisfied = new HashSet <string> (); integrator = new SoftResolutionIntegrator(_model); Console.WriteLine("---- Computing score for " + string.Join(",", selection) + "."); // Integrate the selection foreach (var resolution in r) { Console.WriteLine("Integrate " + resolution.ResolvingGoalIdentifier); integrator.Integrate(resolution); } var p2 = new BDDBasedPropagator(_model); //var all_goals_satisfied = true; var avg_sat_rate = 0.0; foreach (var goal in goals) { var satRate = ((DoubleSatisfactionRate)p2.GetESR(goal)); avg_sat_rate += satRate.SatisfactionRate; if (satRate.SatisfactionRate >= goal.RDS) { rootGoalsSatisfied.Add(goal.Identifier); } Console.WriteLine("Cost " + selection.Count); Console.WriteLine("Selected cm: " + string.Join(",", selection)); Console.WriteLine($"{goal.Identifier} : {satRate.SatisfactionRate} >= {goal.RDS}."); } //var all_goals_satisfied = (satRate.SatisfactionRate > goal.RDS); var cost = selection.Count(); foreach (var resolution in r) { integrator.Remove(resolution); } //if (!all_goals_satisfied) //{ // Console.WriteLine("---- not all goal satisfied, returning score = 0"); // return 0; //} rootsSatisfied = rootGoalsSatisfied; //if (satRate > goal.RDS) { Console.WriteLine("---- returning score = " + (avg_sat_rate / goals.Count())); return(avg_sat_rate / goals.Count()); //} else { // return 0; //} }
public void TestBestCountermeasureSelection() { var input = @"declare goal [ root ] rsr .75 refinedby child1, child2 end declare goal [ child1 ] obstructedby o1 end declare goal [ child2 ] obstructedby o2 end declare obstacle [ o1 ] probability .2 resolvedby [substitution:root] cm1 end declare obstacle [ o2 ] refinedby so1 refinedby so2 end declare obstacle [ so1 ] probability .1 resolvedby [substitution:root] cm2 end declare obstacle [ so2 ] probability .3 resolvedby [substitution:root] cm3 end"; ModelBuilder parser = new ModelBuilder(); var model = parser.Parse(input); var optimizer = new NaiveCountermeasureSelectionOptimizer(model); var propagator = new BDDBasedPropagator(model); var root = model.Goal("root"); var sr = (DoubleSatisfactionRate)propagator.GetESR(root); Console.WriteLine("Satisfaction Rate without countermeasures: " + sr.SatisfactionRate); 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("* " + o); } } } }
public void TestCECountermeasureSelection2() { const string Path1 = "/Users/acailliau/Google Drive/PhD/Dissertation/case-studies/ambulance-dispatching-system/Models/runtime.kaos"; var input = File.ReadAllText(Path1); //input = @"declare goal [ root ] rsr .75 refinedby child1, child2 end //declare goal [ child1 ] obstructedby o1 end //declare goal [ child2 ] obstructedby o2 end //declare obstacle [ o1 ] // probability .2 // resolvedby [substitution:root] cm1 //end //declare obstacle [ o2 ] // refinedby so1 // refinedby so2 //end //declare obstacle [ so1 ] // probability .1 // resolvedby [substitution:root] cm2 //end //declare obstacle [ so2 ] // probability .3 // resolvedby [substitution:root] cm3 //end"; ModelBuilder parser = new ModelBuilder(); var model = parser.Parse(input, Path1); var optimizer = new MOCECountermeasureSelectionOptimizer(model); var propagator = new BDDBasedPropagator(model); var root = model.Goal("achieve_incident_resolved"); var sr = (DoubleSatisfactionRate)propagator.GetESR(root); Console.WriteLine("Satisfaction Rate without countermeasures: " + sr.SatisfactionRate); Console.WriteLine("Required Satisfaction Rate: " + root.RDS); //var minCost = optimizer.GetMinimalCost(root, propagator); //Console.WriteLine("Minimal cost: " + minCost); var optimalSelections = optimizer.GetOptimalSelections(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().OrderBy(x => x.Cost).ThenBy(x => x.SatisfactionRate)) { Console.WriteLine("* " + o); } } }
public void Update(MonitoredState ms, DateTime datetime) { logger.Info("Update. " + _roots.Count); // Create the new monitors and update the existing ones accordingly foreach (var m in obstacleMonitors.Values) { try { m.MonitorStep(ms, datetime); if (m.MonitoredSatisfactionRate != null) { // Update the satisfaction rate for the obstacle _model_running.satisfactionRateRepository.AddObstacleSatisfactionRate( m.Obstacle.Identifier, new DoubleSatisfactionRate(m.MonitoredSatisfactionRate.Mean)); } } catch (Exception e) { logger.Error($"Fail to update monitor for '{m.Obstacle.FriendlyName}' ({e.Message})"); } } // Compute the satisfaction rate for the root goals foreach (var root in _roots) { RootSatisfactionRates[root.Identifier] = (DoubleSatisfactionRate)_propagator.GetESR(root); logger.Info($"Satisfaction rate for {root.Identifier} = " + RootSatisfactionRates[root.Identifier]); } if (!string.IsNullOrEmpty(outputFilename)) { var now = DateTime.Now; string str = string.Format("{0:yyyy-MM-dd HH:mm:ss.fffffff},{1},{2}", now, string.Join(",", RootSatisfactionRates.OrderBy(x => x.Key).Select(x => x.Value.SatisfactionRate)), string.Join(",", obstacleMonitors.Select(x => (x.Value.MonitoredSatisfactionRate?.Mean ?? 0) + "," + (x.Value.MonitoredSatisfactionRate?.StdDev ?? 0))) ); using (StreamWriter sw = File.AppendText(outputFilename)) { sw.WriteLine(str); } } }
public void TestIntegrateSubstitutionProbabilityComputation() { var input = @"declare goal [ anchor ] refinedby child1, child2 end declare goal [ child1 ] obstructedby o end declare goal [ child2 ] end declare obstacle [ o ] probability .4 resolvedby [substitution:anchor] cm end declare goal [ cm ] obstructedby o2 end declare obstacle [ o2 ] probability .1 end"; ModelBuilder parser = new ModelBuilder(); var model = parser.Parse(input); var integrator = new ResolutionIntegrator(model); var r = model.Resolutions().Single(); integrator.Integrate(r); var propagator = new BDDBasedPropagator(model); var expected = ((DoubleSatisfactionRate)propagator.GetESR(model.Goal("anchor"))).SatisfactionRate; }
public void TestIntegrateObstaclePreventionProbabilityComputation() { var input = @"declare goal [ root ] refinedby anchor end declare goal [ anchor ] formalspec when Current() then sooner-or-later Target() refinedby child1, child2 end declare goal [ child1 ] formalspec when Current() then sooner-or-later Milestone() obstructedby o end declare goal [ child2 ] formalspec when Milestone() then sooner-or-later Target() end declare obstacle [ o ] probability .4 formalspec sooner-or-later (Current() and always not Milestone()) resolvedby [prevention:anchor] cm end declare goal [ cm ] obstructedby o2 end declare obstacle [ o2 ] probability .1 end"; ModelBuilder parser = new ModelBuilder(); var model = parser.Parse(input); var integrator = new ResolutionIntegrator(model); var r = model.Resolutions().Single(); integrator.Integrate(r); var obstruction = model.Obstructions(x => x.ObstacleIdentifier == "o").Single(); model.obstacleRepository.Remove(obstruction); var propagator = new BDDBasedPropagator(model); var expected = ((DoubleSatisfactionRate)propagator.GetESR(model.Goal("root"))).SatisfactionRate; }
public static void Main(string [] args) { Console.WriteLine("*** This is CMSelector 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); Init(args); var root = model.Goal(rootname); if (root == null) { PrintError("The goal '" + rootname + "' was not found"); } try { var optimizer = new NaiveCountermeasureSelectionOptimizer(model); var propagator = new BDDBasedPropagator(model); var sr = (DoubleSatisfactionRate)propagator.GetESR(root); Console.WriteLine("Satisfaction Rate without countermeasures: " + sr.SatisfactionRate); 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("* " + o); } } } 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("* " + o); } } 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 DoubleSatisfactionRate BDDBasedComputationPrebuilt() { return((DoubleSatisfactionRate)p3.GetESR(root)); }
public DoubleSatisfactionRate BDDBasedComputation() { var p2 = new BDDBasedPropagator(model); return((DoubleSatisfactionRate)p2.GetESR(root)); }
public void TestCECountermeasureSelection() { var input = @"declare goal [ root ] rsr .75 refinedby child1, child2 end declare goal [ child1 ] obstructedby o1 end declare goal [ child2 ] obstructedby o2 end declare obstacle [ o1 ] probability .2 refinedby so4 refinedby so5, so6 end declare obstacle [ so4 ] probability .1 end declare obstacle [ so5 ] probability .4 resolvedby [substitution:root] cm5 end declare obstacle [ so6 ] probability .4 resolvedby [substitution:root] cm4 end declare obstacle [ o2 ] refinedby so1 refinedby so2 end declare obstacle [ so1 ] probability .1 resolvedby [substitution:root] cm2 end declare obstacle [ so2 ] probability .3 resolvedby [substitution:root] cm3 end declare obstacle [ so3 ] probability .4 resolvedby [substitution:root] cm4 end "; //input = @"declare goal [ root ] rsr .75 refinedby child1, child2 end //declare goal [ child1 ] obstructedby o1 end //declare goal [ child2 ] obstructedby o2 end //declare obstacle [ o1 ] // probability .2 // resolvedby [substitution:root] cm1 //end //declare obstacle [ o2 ] // refinedby so1 // refinedby so2 //end //declare obstacle [ so1 ] // probability .1 // resolvedby [substitution:root] cm2 //end //declare obstacle [ so2 ] // probability .3 // resolvedby [substitution:root] cm3 //end"; ModelBuilder parser = new ModelBuilder(); var model = parser.Parse(input); var optimizer = new MOCECountermeasureSelectionOptimizer(model); var propagator = new BDDBasedPropagator(model); var root = model.Goal("root"); var sr = (DoubleSatisfactionRate)propagator.GetESR(root); Console.WriteLine("Satisfaction Rate without countermeasures: " + sr.SatisfactionRate); Console.WriteLine("Required Satisfaction Rate: " + root.RDS); //var minCost = optimizer.GetMinimalCost(root, propagator); //Console.WriteLine("Minimal cost: " + minCost); var optimalSelections = optimizer.GetOptimalSelections(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().OrderBy(x => x.Cost).ThenBy(x => x.SatisfactionRate)) { Console.WriteLine("* " + o); } } }