public double GetMinimalCost(Goal goal, IPropagator propagator) { var countermeasure_goals = _model.Resolutions().Select(x => x.ResolvingGoalIdentifier).Distinct(); // Initialize vector var mapping = Enumerable.Zip(Enumerable.Range(0, countermeasure_goals.Count()), countermeasure_goals, (arg1, arg2) => new { arg1, arg2 }) .ToDictionary(item => item.arg1, item => item.arg2); double[] sampling = GetInitialSampling(countermeasure_goals, mapping); var random = new Random(); var rho = .1; int i = 0; while (Iterations < StoppingCriterion & i < MaxIterations) { var gammaT = IterateCost(goal, propagator, sampling, mapping, random, rho); if (Math.Abs(gammaT - lastGammaT) <= EPSILON) { Iterations++; } else { Iterations = 0; } lastGammaT = gammaT; Console.WriteLine($"{gammaT:0.00} | " + string.Join(" ", sampling.Select(x => $"{x:0.00}"))); i++; } return(lastGammaT); }
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 Render(KAOSModel model) { //var goalsInRefinements = model.GoalRefinements ().SelectMany (x => x.SubGoals ().Union (new[] { // x.ParentGoal () //})).Distinct (); foreach (var g in model.Goals()) { Render(g); } foreach (var d in model.GoalRefinements().SelectMany(x => x.DomainProperties()).Distinct()) { Render(d); } foreach (var r in model.GoalRefinements()) { Render(r); } foreach (var o in model.Obstacles()) { Render(o); } foreach (var o in model.ObstacleRefinements()) { Render(o); } foreach (var o in model.Obstructions()) { Render(o); } foreach (var o in model.Resolutions()) { Render(o); } foreach (var r in model.GoalAgentAssignments()) { Render(r, true); } foreach (var o in model.Resolutions()) { RenderAnchorArrow(o); } }
public IEnumerable <Resolution> Optimize() { logger.Info("Optimizer called"); if (optimizationInProgress) { logger.Info("Optimization in progress"); return(null); } if (!monitor.Ready) { return(Enumerable.Empty <Resolution>()); } optimizationInProgress = true; var start = Stopwatch.StartNew(); var resolutions = model.Resolutions(); minCost = int.MaxValue; ComputeMinCost(resolutions.First(), resolutions.Skip(1), Enumerable.Empty <Resolution>()); logger.Info("Min cost: " + minCost); bestScore = 0; bestSelection = Enumerable.Empty <Resolution>(); ComputeScore(resolutions.First(), resolutions.Skip(1), Enumerable.Empty <Resolution>()); logger.Info("Best Selection: {0} for {{{1}}}", bestScore, string.Join(",", bestSelection.Select(x => x.ResolvingGoal().Identifier))); start.Stop(); logger.Info("Time for optimization: {0} ms ", start.ElapsedMilliseconds); optimizationInProgress = false; return(bestSelection); }
/// <summary> /// Returns the minimal cost ensuring the goals respective RDSs. Returns -1 if no /// selection satisfy the RDS. /// </summary> /// <param name="goals">The goals</param> /// <returns>The minimal cost.</returns> public double GetMinimalCost(HashSet <Goal> goals, IPropagator propagator) { var timer = Stopwatch.StartNew(); int minCost = -1; var sr = new Dictionary <string, DoubleSatisfactionRate>(); bool all_goals_satisfied = true; foreach (var goal in goals) { sr.Add(goal.Identifier, (DoubleSatisfactionRate)propagator.GetESR(goal)); all_goals_satisfied &= (sr[goal.Identifier].SatisfactionRate > goal.RDS); } if (all_goals_satisfied) { 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 = new Dictionary <string, DoubleSatisfactionRate>(); all_goals_satisfied = true; foreach (var goal in goals) { sr.Add(goal.Identifier, (DoubleSatisfactionRate)propagator.GetESR(goal)); all_goals_satisfied &= (sr[goal.Identifier].SatisfactionRate > goal.RDS); } if (all_goals_satisfied) { safe_count++; stats.MaxSafeCost = Math.Max(stats.MaxSafeCost, cost); if (minCost == -1 || cost < minCost) { minCost = cost; } } Console.WriteLine("Selection: " + string.Join(",", r.Select(x => x.ResolvingGoalIdentifier)) + " cost: " + cost + " " + string.Join(" ", goals.Select(x => x.Identifier + ": " + sr[x.Identifier].SatisfactionRate))); foreach (var resolution in r) { integrator.Remove(resolution); } } timer.Stop(); stats.TimeToComputeMinimalCost = timer.Elapsed; stats.NbResolvingGoals = countermeasure_goals.Count(); stats.NbSelections = count; stats.NbTestedSelections = tested_count; stats.NbSafeSelections = safe_count; return(minCost); }
/// <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); }