示例#1
0
        protected void TestCase(double o1_esr, double o2_esr, double c1, double c2, double expected_root_esr)
        {
            Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory);
            const string filename = "./Examples/CaseModel.kaos";

            ModelBuilder parser = new ModelBuilder();
            string       input  = File.ReadAllText(filename);
            var          model  = parser.Parse(input, filename);

            var root = model.Goal("root");

            var refinement = model.GoalRefinements().Single();
            var p1         = (PrimitiveRefineeParameter <double>)refinement.SubGoalIdentifiers.Single(x => x.Identifier == "m1").Parameters;
            var p2         = (PrimitiveRefineeParameter <double>)refinement.SubGoalIdentifiers.Single(x => x.Identifier == "m2").Parameters;

            p1.Value = c1;
            p2.Value = c2;

            model.satisfactionRateRepository.AddObstacleSatisfactionRate("o1", new DoubleSatisfactionRate(o1_esr));
            model.satisfactionRateRepository.AddObstacleSatisfactionRate("o2", new DoubleSatisfactionRate(o2_esr));

            var _propagator       = GetPropagator(model);
            ISatisfactionRate esr = _propagator.GetESR(root);

            Assert.IsInstanceOf(typeof(DoubleSatisfactionRate), esr);
            DoubleSatisfactionRate esrd = (DoubleSatisfactionRate)esr;

            Assert.AreEqual(expected_root_esr, esrd.SatisfactionRate, 0.0001);
        }
 public void AddDomPropSatisfactionRate(string dompropId, ISatisfactionRate satRate)
 {
     if (!DomainPropertySatisfactionRates.ContainsKey(dompropId))
     {
         DomainPropertySatisfactionRates.Add(dompropId, new List <ISatisfactionRate> ());
     }
     DomainPropertySatisfactionRates [dompropId].Add(satRate);
 }
 public void AddObstacleSatisfactionRate(string obstacleId, ISatisfactionRate satRate)
 {
     if (!ObstacleSatisfactionRates.ContainsKey(obstacleId))
     {
         ObstacleSatisfactionRates.Add(obstacleId, new List <ISatisfactionRate> ());
     }
     ObstacleSatisfactionRates [obstacleId].Add(satRate);
 }
 public void AddCalibrationSatisfactionRate(string calibrationId, ISatisfactionRate satRate)
 {
     if (!CalibrationSatisfactionRates.ContainsKey(calibrationId))
     {
         CalibrationSatisfactionRates.Add(calibrationId, new List <ISatisfactionRate> ());
     }
     CalibrationSatisfactionRates [calibrationId].Add(satRate);
 }
 public void AddGoalSatisfactionRate(string goalId, ISatisfactionRate satRate)
 {
     if (!GoalSatisfactionRates.ContainsKey(goalId))
     {
         GoalSatisfactionRates.Add(goalId, new List <ISatisfactionRate> ());
     }
     GoalSatisfactionRates [goalId].Add(satRate);
 }
 public void AddDomHypothesisSatisfactionRate(string domhypId, ISatisfactionRate satRate)
 {
     if (!DomainHypothesisSatisfactionRates.ContainsKey(domhypId))
     {
         DomainHypothesisSatisfactionRates.Add(domhypId, new List <ISatisfactionRate> ());
     }
     DomainHypothesisSatisfactionRates [domhypId].Add(satRate);
 }
示例#7
0
 public ISatisfactionRate Sum(ISatisfactionRate x)
 {
     if (x is DoubleSatisfactionRate)
     {
         return(new DoubleSatisfactionRate(SatisfactionRate + ((DoubleSatisfactionRate)x).SatisfactionRate));
     }
     else
     {
         throw new NotSupportedException();
     }
 }
示例#8
0
 string ToString(ISatisfactionRate satRate)
 {
     if (satRate is DoubleSatisfactionRate d)
     {
         return(d.SatisfactionRate.ToString());
     }
     else
     {
         throw new NotImplementedException();
     }
 }
示例#9
0
        public static void Main(string [] args)
        {
            Console.WriteLine("*** This is Propagator 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);

            bool bdd = true;

            options.Add("bdd", "Uses the BDD-Based propagation (Default)", v => bdd = true);
            options.Add("pattern", "Uses the Pattern-Based propagation", v => bdd   = false);

            Init(args);

            var root = model.Goal(rootname);

            if (root == null)
            {
                PrintError("The goal '" + rootname + "' was not found");
            }

            try {
                IPropagator _propagator;
                if (bdd)
                {
                    _propagator = new BDDBasedPropagator(model);
                }
                else
                {
                    _propagator = new PatternBasedPropagator(model);
                }

                ISatisfactionRate      esr  = _propagator.GetESR(root);
                DoubleSatisfactionRate esrd = (DoubleSatisfactionRate)esr;

                Console.WriteLine(root.FriendlyName + ": {0:P}", esrd.SatisfactionRate);
            } 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");
            }
        }
示例#10
0
 DoubleSatisfactionRate ComputeRefineeSatisfactionRate(IEnumerable <GoalRefinee> refinees, Func <string, ISatisfactionRate> get)
 {
     return(refinees.Aggregate(
                DoubleSatisfactionRate.ZERO,
                (x, y) => {
         IRefineeParameter parameter = y.Parameters;
         if (!(parameter is PrimitiveRefineeParameter <double>))
         {
             throw new PropagationException(PropagationException.MISSING_PARAMETER);
         }
         var doubleParam = ((PrimitiveRefineeParameter <double>)parameter).Value;
         ISatisfactionRate sr = get(y.Identifier).Product(doubleParam);
         return (DoubleSatisfactionRate)x.Sum(sr);
     }
                ));
 }
示例#11
0
        protected void TestDivideAndConquer(double o1_esr, double o2_esr, double expected_root_esr)
        {
            Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory);
            const string filename = "./Examples/DivideAndConquerModel.kaos";

            ModelBuilder parser = new ModelBuilder();
            string       input  = File.ReadAllText(filename);
            var          model  = parser.Parse(input, filename);

            var root = model.Goal("root");

            model.satisfactionRateRepository.AddObstacleSatisfactionRate("o1", new DoubleSatisfactionRate(o1_esr));
            model.satisfactionRateRepository.AddObstacleSatisfactionRate("o2", new DoubleSatisfactionRate(o2_esr));

            var _propagator       = GetPropagator(model);
            ISatisfactionRate esr = _propagator.GetESR(root);

            Assert.IsInstanceOf(typeof(DoubleSatisfactionRate), esr);
            DoubleSatisfactionRate esrd = (DoubleSatisfactionRate)esr;

            Assert.AreEqual(expected_root_esr, esrd.SatisfactionRate, 0.0001);
        }
示例#12
0
        public ISatisfactionRate GetESR(Goal g)
        {
            if (goalCache.ContainsKey(g))
            {
                return(goalCache[g]);
            }

            ISatisfactionRate cps = null;
            var refinements       = _model.GoalRefinements(r => r.ParentGoalIdentifier == g.Identifier);

            if (refinements.Count() > 1)
            {
                throw new PropagationException(PropagationException.MULTIPLE_REFINEMENTS);
            }

            if (refinements.Count() == 1)
            {
                cps = GetESR(refinements.Single());
            }

            if (refinements.Count() == 0)
            {
                var obstructions = _model.Obstructions(r => r.ObstructedGoalIdentifier == g.Identifier);
                if (obstructions.Count() == 0)
                {
                    cps = DoubleSatisfactionRate.ONE;
                }
                else
                {
                    cps = obstructions.Select(x => GetESR(x).OneMinus()).Aggregate(DoubleSatisfactionRate.ONE, (x, y) => (DoubleSatisfactionRate)x.Product(y));
                }
            }

            _model.satisfactionRateRepository.AddGoalSatisfactionRate(g.Identifier, cps);
            goalCache.Add(g, cps);
            return(cps);
        }
 public ISatisfactionRate Sum(ISatisfactionRate x)
 {
     throw new NotImplementedException();
 }
        public override void Handle(KAOSCoreElement element, ParsedUDistribution attribute, KAOSModel model)
        {
            ISatisfactionRate satRate = null;

            if (attribute is ParsedUniformDistribution)
            {
                var ud = ((ParsedUniformDistribution)attribute);
                satRate = new UniformSatisfactionRate(ud.LowerBound, ud.UpperBound);
            }
            else if (attribute is ParsedBetaDistribution)
            {
                var ud = ((ParsedBetaDistribution)attribute);
                satRate = new BetaSatisfactionRate(ud.Alpha, ud.Beta);
            }
            else if (attribute is ParsedTriangularDistribution)
            {
                var ud = ((ParsedTriangularDistribution)attribute);
                satRate = new TriangularSatisfactionRate(ud.Min, ud.Mode, ud.Max);
            }
            else if (attribute is ParsedPertDistribution)
            {
                var ud = ((ParsedPertDistribution)attribute);
                satRate = new PERTSatisfactionRate(ud.Min, ud.Mode, ud.Max);
            }
            else if (attribute is ParsedQuantileDistribution pqd)
            {
                if (!model.Parameters.ContainsKey("experts.quantiles"))
                {
                    throw new InvalidProgramException("Please specify the quantiles.");
                }

                // Get the string and removes first and last characters (parenthesis)
                string quantile_string = model.Parameters["experts.quantiles"];
                quantile_string = quantile_string.Remove(quantile_string.Length - 1).Substring(1);

                // Build the list of quantiles
                var quantiles = quantile_string.Split(',').Select(x => {
                    var y = x.Trim();
                    if (y.EndsWith("%", StringComparison.Ordinal))
                    {
                        return(double.Parse(y.Remove(y.Length - 1)));
                    }
                    else
                    {
                        return(double.Parse(y));
                    }
                }).ToArray();

                satRate = new QuantileList(pqd.Quantiles);
            }
            else
            {
                throw new NotImplementedException();
            }

            // Fill the expert if provided
            if (attribute.ExpertIdentifier != null)
            {
                if (model.modelMetadataRepository.ExpertExists(attribute.ExpertIdentifier))
                {
                    satRate.ExpertIdentifier = attribute.ExpertIdentifier;
                }
                else
                {
                    throw new BuilderException("Expert '" + attribute.ExpertIdentifier + "' is not defined.", attribute);
                }
            }

            // Adds to the correct collection
            if (element is Obstacle)
            {
                model.satisfactionRateRepository.AddObstacleSatisfactionRate(element.Identifier, satRate);
            }
            else if (element is DomainProperty)
            {
                model.satisfactionRateRepository.AddDomPropSatisfactionRate(element.Identifier, satRate);
            }
            else if (element is Calibration)
            {
                model.satisfactionRateRepository.AddCalibrationSatisfactionRate(element.Identifier, satRate);
            }
            else
            {
                throw new NotImplementedException();
            }
        }