private void CreateRandomVariables(Func <bool> hazard, Dictionary <string, Func <bool> > states, IList <Fault> faults)
        {
            var usedFaults = faults ?? _model.Faults;
            var usedStates = states ?? new Dictionary <string, Func <bool> >();

            var randomVariableCreator = new RandomVariableFactory(_model);

            _hazardVar = randomVariableCreator.FromState(hazard, "H");
            _states    = usedStates.Select(state => randomVariableCreator.FromState(state.Value, state.Key)).ToList();
            _faultVars = randomVariableCreator.FromFaults(usedFaults);
            _mcsVars   = randomVariableCreator.FromDccaLimitedByFaults(hazard, _faultVars);
        }
        public string GenerateSimulationData(ICollection <FaultRandomVariable> faults, ICollection <McsRandomVariable> mcs,
                                             ICollection <BooleanRandomVariable> states, BooleanRandomVariable hazard, int numberOfSimulations)
        {
            _faults = faults;
            _mcs    = mcs;
            _hazard = hazard;
            _states = states;
            _numberOfSimulations = numberOfSimulations;

            Console.Out.WriteLine("Create simulation data...");

            var allVariables = AllRandomVariables();
            var allFormulas  = CreateAllFormulas();

            SafetySharpProbabilisticSimulator.Configuration.UseOptionProbabilitiesInSimulation = _config.UseRealProbabilitiesForSimulation;
            var simulator = new SafetySharpProbabilisticSimulator(_model, allFormulas.Values.ToArray());

            using (var w = new StreamWriter(DataPath))
            {
                w.WriteLine(string.Join(",", allVariables.Select(randomVariable => randomVariable.Name)));
                for (var currentStep = 0; currentStep < _numberOfSimulations; currentStep++)
                {
                    if (_numberOfSimulations > 100 && currentStep % (_numberOfSimulations / 100) == 0)
                    {
                        Console.Out.WriteLine($"{(double)currentStep / _numberOfSimulations:P0} done.");
                    }

                    simulator.SimulateSteps(_stepBounds);
                    var results = new bool[allVariables.Count];
                    for (var varIndex = 0; varIndex < allVariables.Count; varIndex++)
                    {
                        var currentVariable = allVariables[varIndex];
                        // cut sets cannot be checked for a given state, so check the occurence of its faults
                        if (currentVariable is McsRandomVariable)
                        {
                            var cutSet = (McsRandomVariable)currentVariable;
                            results[varIndex] =
                                cutSet.FaultVariables.All(fault => simulator.GetCountOfSatisfiedOnTrace(allFormulas[fault]) > 0);
                        }
                        // check the occurence of the random variable
                        else
                        {
                            results[varIndex] = simulator.GetCountOfSatisfiedOnTrace(allFormulas[currentVariable]) > 0;
                        }
                    }
                    w.WriteLine(string.Join(",", results.Select(res => res ? 'T' : 'F')));
                }
                w.Flush();
            }
            return(DataPath);
        }
        /// <summary>
        /// Learn the best fitting DAG structure and probability distributions by using simulation data
        /// </summary>
        public BayesianNetwork LearnBayesianNetwork(ICollection <FaultRandomVariable> faults, ICollection <McsRandomVariable> mcs,
                                                    ICollection <BooleanRandomVariable> states, BooleanRandomVariable hazard)
        {
            _faults = faults;
            _mcs    = mcs;
            _hazard = hazard;
            _states = states;

            Console.Out.WriteLine("Generate data for reducing model domain...");
            CreateWhiteAndBlacklist();
            Console.Out.WriteLine("Call R script for learning the network...");
            var jsonResult = CallRLearningScript();
            var result     = JsonConvert.DeserializeObject <BayesianNetworkResult>(jsonResult);

            return(ToBayesianNetwork(result));
        }
Beispiel #4
0
        /// <summary>
        /// Identifies as many conditional independencies in the model as possible.
        /// </summary>
        public DualKeyDictionary <RandomVariable, ICollection <ISet <RandomVariable> > > FindIndependencies(IList <FaultRandomVariable> faults, ICollection <McsRandomVariable> minimalCriticalSets, IList <BooleanRandomVariable> states, BooleanRandomVariable hazard)
        {
            _hazard = hazard;
            _faults = faults;
            _minimalCriticalSets = new HashSet <McsRandomVariable>(minimalCriticalSets);
            _states = states;
            _conditionalIndependencies = new DualKeyDictionary <RandomVariable, ICollection <ISet <RandomVariable> > >();

            var variablesForCalculation = GetVariablesForCalculation();

            CalculatePairIndependencies(variablesForCalculation);

            if (_config.UseDccaResultsForLearning && _minimalCriticalSets.Count > 0)
            {
                GenerateDccaIndependencies();
                CalculateFurtherDccaIndependencies();
            }

            return(_conditionalIndependencies);
        }
Beispiel #5
0
        /// <summary>
        /// Use the structure of the DCCA to improve the missing orientations in the current DAG pattern.
        /// </summary>
        public DagPattern <RandomVariable> UseDccaForOrientations(ICollection <McsRandomVariable> minimalCriticalSets, ICollection <FaultRandomVariable> faults, BooleanRandomVariable hazard)
        {
            foreach (var minimalCriticalSet in minimalCriticalSets)
            {
                _dag.OrientUndirectedEdge(minimalCriticalSet, hazard);
                foreach (var fault in minimalCriticalSet.FaultVariables)
                {
                    _dag.OrientUndirectedEdge(fault, minimalCriticalSet);
                }
            }
            Console.Out.WriteLine("After using DCCA to orient edges:");
            _dag.ExportToGraphviz();
            OrientRemainingEdges(false);
            Console.Out.WriteLine("After orienting remaining edges after DCCA:");
            _dag.ExportToGraphviz();

            return(_dag);
        }