Exemplo n.º 1
0
        public async Task <ActionResult <FileBayesianNetwork> > Inferencia([FromBody] InferViewModel data)
        {
            var arquivoRedeBayesiana = await _context.ArquivosRedeBayesiana.FindAsync(data.IdRede);

            if (arquivoRedeBayesiana == null)
            {
                return(NotFound());
            }
            ServiceRedeBayesiana serviceRede   = new ServiceRedeBayesiana();
            FileBayesianNetwork  redeBayesiana = JsonConvert.DeserializeObject <FileBayesianNetwork>(arquivoRedeBayesiana.ArquivoJson);

            redeBayesiana = serviceRede.LoadRedeBayesiana(redeBayesiana);
            BayesInfer bayesInfer = new BayesInfer(redeBayesiana);

            redeBayesiana = bayesInfer.InferModel(data.ListBelief);


            return(redeBayesiana);
        }
Exemplo n.º 2
0
        public async Task <JsonResult> InferBelief()
        {
            var arquivoRedeBayesiana = await _context.ArquivosRedeBayesiana.FindAsync(_nodeBeliefService.IdRede);


            ServiceRedeBayesiana serviceRede   = new ServiceRedeBayesiana();
            FileBayesianNetwork  redeBayesiana = JsonConvert.DeserializeObject <FileBayesianNetwork>(arquivoRedeBayesiana.ArquivoJson);

            redeBayesiana = serviceRede.LoadRedeBayesiana(redeBayesiana);
            BayesInfer bayesInfer = new BayesInfer(redeBayesiana);

            List <Belief> beliefs = new List <Belief>();

            foreach (var item in _nodeBeliefService.NodesBelief)
            {
                beliefs.Add(new Belief
                {
                    NodeName    = item.Id,
                    BeliefValue = item.BeliefType == 2 ? null : (int?)item.BeliefType
                });
                _logger.LogInformation("Message displayed: {Message}", "Node name add: " + item.Id);
            }

            try
            {
                redeBayesiana = bayesInfer.InferModel(beliefs);
                _logger.LogInformation("Message displayed: {Message}", "Inferencia realizada em " + beliefs.Count + " nodos");
            }
            catch (Exception e)
            {
                _logger.LogError("Message displayed: {Message}", "Erro " + e.Message);
                //throw;
            }


            NodeBeliefResult res;

            List <NodeBeliefResult> lstNodeBeliefResult = new List <NodeBeliefResult>();

            try
            {
                foreach (var item in redeBayesiana.Nodes)
                {
                    res          = new NodeBeliefResult();
                    res.NodeName = item.Id;
                    if (item.InferPrimary.IsObserved)
                    {
                        if (item.InferPrimary.ObservedValue[0] == 0)
                        {
                            res.ResultAusente  = 1;
                            res.ResultPresente = 0;
                        }
                        else
                        {
                            res.ResultAusente  = 0;
                            res.ResultPresente = 1;
                        }
                    }
                    else if (item.Parents.Count() == 0)
                    {
                        res.ResultAusente  = item.InferProbPrior.ObservedValue.GetMean()[0];
                        res.ResultPresente = item.InferProbPrior.ObservedValue.GetMean()[1];
                    }
                    else
                    {
                        res.ResultAusente  = item.InferModelResult[0].GetProbs()[1];
                        res.ResultPresente = item.InferModelResult[0].GetProbs()[0];
                    }
                    lstNodeBeliefResult.Add(res);
                }
            }
            catch (Exception e)
            {
                _logger.LogError("Message displayed: {Message}", "Erro " + e.Message);
            }

            return(Json(lstNodeBeliefResult));
            //return Json(lstNodeBeliefResult);
        }