Beispiel #1
0
        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);
                }
            }
        }
Beispiel #2
0
        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);
                }
            }
        }
Beispiel #3
0
        static void OptimizeLoop()
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };
            var currentSelection = new HashSet <string> ();

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: kaos_cm_selection_queue_name,
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var _propagator_optimization = new BDDBasedPropagator(model);
                    while (!stop)
                    {
                        Thread.Sleep(TimeSpan.FromMinutes(1));

                        bool optimization_required = false;
                        foreach (var root in roots)
                        {
                            var doubleSatisfactionRate = modelMonitor.RootSatisfactionRates[root.Identifier];
                            if (doubleSatisfactionRate == null)
                            {
                                logger.Info($"Ignoring '{root.Identifier}', no satisfaction rate monitored.");
                                continue;
                            }
                            logger.Info($"RSR = " + root.RDS);
                            if (doubleSatisfactionRate.SatisfactionRate >= root.RDS)
                            {
                                logger.Info("Current configuration is above RSR for " + root.FriendlyName + ".");
                            }
                            else
                            {
                                logger.Info("Current configuration is below RSR for " + root.FriendlyName + ".");
                                optimization_required = true;
                            }
                        }

                        if (!optimization_required)
                        {
                            continue;
                        }

                        //var minimalcost = optimizer.GetMinimalCost(roots, _propagator_optimization);
                        //var optimalSelections = optimizer.GetOptimalSelections(minimalcost, roots, _propagator_optimization).FirstOrDefault();

                        var optimizer = new MOCECountermeasureSelectionOptimizer(optimization_model);
                        optimization_model.satisfactionRateRepository = modelMonitor._model_running.satisfactionRateRepository;

                        Console.WriteLine("Computing optimization");


                        var propagator        = new BDDBasedPropagator(optimization_model);
                        var enumerable        = new HashSet <Goal> (optimization_model.Goals(x => roots.Select(y => y.Identifier).Contains(x.Identifier)));
                        var optimalSelections = optimizer.GetOptimalSelections(enumerable, 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);
                            }
                        }

                        if (optimalSelections.Count() > 0)
                        {
                            var optimalSelection = optimalSelections
                                                   .Where(x => x.SatRoots.SetEquals(roots.Select(y => y.Identifier)))
                                                   .OrderBy(x => x.Cost)
                                                   .ThenByDescending(x => x.SatisfactionRate)
                                                   .FirstOrDefault();

                            if (optimalSelection == null)
                            {
                                optimalSelection = optimalSelections
                                                   .OrderByDescending(x => x.SatRoots.Count)
                                                   .ThenBy(x => x.Cost)
                                                   .ThenByDescending(x => x.SatisfactionRate)
                                                   .FirstOrDefault();
                            }

                            if (optimalSelection == null)
                            {
                                logger.Info("No optimal selection found!");
                                return;
                            }

                            // Update the model
                            var deployment_methods = new List <string>();
                            foreach (var resolution in optimalSelection.Resolutions.Except(_active_resolutions))
                            {
                                _integrator_model.Integrate(resolution);
                                deployment_methods.Add(resolution.ResolvingGoal().CustomData["ondeploy"]);
                            }
                            foreach (var resolution in optimalSelection.Resolutions.Intersect(_active_resolutions))
                            {
                                _integrator_model.Remove(resolution);
                                deployment_methods.Add(resolution.ResolvingGoal().CustomData["onwithold"]);
                            }
                            modelMonitor.ModelChanged();

                            _active_resolutions = optimalSelection.Resolutions.ToHashSet();

                            // Deploy the countermeasures in the running system
                            var json = new JavaScriptSerializer().Serialize(deployment_methods);
                            logger.Info(json);

                            var body = Encoding.UTF8.GetBytes(json);

                            channel.BasicPublish(exchange: "",
                                                 routingKey: kaos_cm_selection_queue_name,
                                                 basicProperties: null,
                                                 body: body);
                        }
                        else
                        {
                            logger.Info("No selection found!");
                        }
                    }
                }
        }
Beispiel #4
0
        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);
                }
            }
        }