Example #1
0
 public NaiveCountermeasureSelectionOptimizerWithUncertainty(KAOSModel model, double threshold)
 {
     _model         = model;
     stats          = new OptimizationStatistics();
     integrator     = new SoftResolutionIntegrator(model);
     this.threshold = threshold;
 }
        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;
            //}
        }
Example #3
0
        public static void Main(string[] args)
        {
            Console.WriteLine("*** This is ModelChecker 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("");

            Init(args);

            Console.WriteLine("Goals: " + model.Goals().Count());
            Console.WriteLine("Root goals: " + model.RootGoals().Count());
            Console.WriteLine("Leaf goals: " + model.LeafGoals().Count());
            Console.WriteLine("Goal refinements: " + model.GoalRefinements().Count());
            Console.WriteLine();
            Console.WriteLine("Obstacles: " + model.Obstacles().Count());
            Console.WriteLine("Root obstacles: " + model.RootObstacles().Count());
            Console.WriteLine("Leaf obstacles: " + model.LeafObstacles().Count());
            Console.WriteLine("Obstacle refinements: " + model.ObstacleRefinements().Count());
            Console.WriteLine();
            Console.WriteLine("Resolutions: " + model.Resolutions().Count());
            Console.WriteLine();
            Console.WriteLine("Contexts: " + model.Contexts().Count());

            var integrator = new SoftResolutionIntegrator(model);

            foreach (var resolution in model.Resolutions())
            {
                integrator.Integrate(resolution);
            }

            Console.WriteLine();
            Console.WriteLine("Generated goal exceptions: " + model.Exceptions().Count());
            Console.WriteLine(" (distributed over " + model.Exceptions().Select(x => x.AnchorGoalIdentifier).Distinct().Count() + " goals)");
            Console.WriteLine("Generated provided assumption: " + model.ObstacleAssumptions().Count());
            Console.WriteLine(" (distributed over " + model.ObstacleAssumptions().Select(x => x.AnchorGoalIdentifier).Distinct().Count() + " goals)");
        }
 public CECountermeasureSelectionOptimizer(KAOSModel model)
 {
     _model     = model;
     stats      = new OptimizationStatistics();
     integrator = new SoftResolutionIntegrator(model);
 }
        public static void Main(string [] args)
        {
            Console.WriteLine("*** This is CMIntegrator 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("");

            Init(args);

            bool stop = false;

            while (!stop)
            {
                try {
                    Console.Write("> ");
                    var input = Console.ReadLine().Trim();
                    if (input.Equals("quit") | input.Equals("exit"))
                    {
                        stop = true;
                        continue;
                    }

                    bool  success = false;
                    Regex regex   = new Regex(@"resolve ([a-zA-Z][a-zA-Z0-9_-]*)");
                    Match match   = regex.Match(input);
                    if (match.Success)
                    {
                        success = true;
                        var o_identifier = match.Groups [1].Value;
                        if (o_identifier.Equals("all"))
                        {
                            var integrator = new SoftResolutionIntegrator(model);
                            foreach (var resolution in model.Resolutions())
                            {
                                integrator.Integrate(resolution);
                            }
                            continue;
                        }


                        Obstacle o;
                        if ((o = model.Obstacle(o_identifier)) != null)
                        {
                            var resolutions = o.Resolutions().ToArray();
                            for (int i = 0; i < resolutions.Length; i++)
                            {
                                Console.WriteLine($"[{i}] " + resolutions [i].ResolvingGoal().FriendlyName);
                            }
                            Console.Write("Select the countermeasure goal to integrate: ");
                            int index = -1;
                            do
                            {
                                var input_index = Console.ReadLine();
                                if (int.TryParse(input_index, out index))
                                {
                                    if (index < resolutions.Length)
                                    {
                                        var integrator = new SoftResolutionIntegrator(model);
                                        integrator.Integrate(resolutions [index]);
                                    }
                                    else
                                    {
                                        index = -1;
                                    }
                                }
                            } while (index < 0);
                            continue;
                        }
                        else
                        {
                            Console.WriteLine($"Obstacle '{o_identifier}' not found");
                        }
                    }

                    regex = new Regex(@"export ([a-zA-Z0-9_\.-]+)");
                    match = regex.Match(input);
                    if (match.Success)
                    {
                        success = true;
                        if (File.Exists(match.Groups [1].Value))
                        {
                            Console.Write($"Do you want to overwrite '{match.Groups [1].Value}' (yes/no)? ");
                            var input_resp = Console.ReadLine().Trim();
                            if (input_resp.Equals("yes"))
                            {
                                File.Delete(match.Groups [1].Value);
                            }
                            else
                            {
                                continue;
                            }
                        }

                        var e = new KAOSFileExporter(model);
                        File.WriteAllText(match.Groups [1].Value, e.Export());
                        Console.WriteLine("Model exported to " + match.Groups [1].Value);
                        continue;
                    }

                    regex = new Regex(@"export_diagram ([a-zA-Z0-9_\.-]+)");
                    match = regex.Match(input);
                    if (match.Success)
                    {
                        success = true;
                        if (File.Exists(match.Groups [1].Value))
                        {
                            Console.Write($"Do you want to overwrite '{match.Groups [1].Value}' (yes/no)? ");
                            var input_resp = Console.ReadLine().Trim();
                            if (input_resp.Equals("yes"))
                            {
                                File.Delete(match.Groups [1].Value);
                            }
                            else
                            {
                                continue;
                            }
                        }

                        Document document = OmnigraffleMainClass.ExportModel(model);
                        OmniGraffleGenerator.Export(document, match.Groups [1].Value);

                        Console.WriteLine("Model exported to " + match.Groups [1].Value);
                        continue;
                    }

                    if (!success)
                    {
                        Console.WriteLine("Command not recognized.");
                    }
                } catch (Exception e) {
                    PrintError(e.Message);
                    Console.Write("Print more? (yes/no)");
                    var input_resp = Console.ReadLine().Trim();
                    if (!input_resp.Equals("yes"))
                    {
                        continue;
                    }
                    else
                    {
                        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");
                    }
                }
            }
        }
Example #6
0
        public static void Main(string[] args)
        {
            Console.WriteLine("*** This is Monitor 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("roots=", "Specify the roots goal for which to compute the satisfaction rate. (Default: root)", v => rootname = v);

            string outfile = null;

            options.Add("outfile=", "Specify the output file for the satisfaction rates", v => outfile = v);

            Init(args);

            optimization_model = BuildModel();

            roots = rootname.Split(',').Select(x => model.Goal(x)).ToHashSet();
            if (roots.Count() == 0 || roots.Any(x => x == null))
            {
                PrintError("A root goal among '" + rootname + "' was not found");
            }

            try {
                modelMonitor = new ModelMonitor(model, roots);
                modelMonitor.SetOutputFile(outfile);

                _active_resolutions = new HashSet <Resolution>();
                _integrator_model   = new SoftResolutionIntegrator(model);

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

                var commands = new List <ICommand>();
                commands.Add(new GetSatisfactionRateCommand(model, roots, modelMonitor));
                commands.Add(new ExportModelCommand(model));
                commands.Add(new PrintDebugCommand(model, roots, modelMonitor));

                stop = false;

                Listen();
                Optimize();

                while (!stop)
                {
                    Console.Write("> ");
                    var input = Console.ReadLine();
                    if (input.Equals("quit") | input.Equals("exit"))
                    {
                        stop = true;
                        continue;
                    }

                    foreach (var command in commands)
                    {
                        command.Execute(input);
                    }
                }

                Console.WriteLine("Exiting...");
                modelMonitor.Stop();
            } 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");
            }
        }