Пример #1
0
        public async Task <bool> CalculateCriteriaWeights(Guid choiceId)
        {
            var criteria = await _criterionRepository.GetByChoiceIDAsync(choiceId);

            var hash = new Dictionary <Guid, int>();
            int i    = 0;

            foreach (var criterion in criteria)
            {
                hash[criterion.CriteriaID] = i;
                i++;
            }
            double[,] matrix = new double[criteria.Count, criteria.Count];
            foreach (var criterion in criteria)
            {
                matrix[hash[criterion.CriteriaID], hash[criterion.CriteriaID]] = 1;
                var comparisons = await _criteriaComparisonRepo.GetByFirstCriterionIDAsync(criterion.CriteriaID);

                foreach (var comparison in comparisons)
                {
                    matrix[hash[comparison.CriteriaID1], hash[comparison.CriteriaID2]] = comparison.CriteriaRatio;
                    matrix[hash[comparison.CriteriaID2], hash[comparison.CriteriaID1]] = 1 / comparison.CriteriaRatio;
                }
            }
            var weights = _AHPService.CalculatePriortyVector(matrix);

            double consistency = _AHPService.CalculateConsistency(matrix, weights);

            if (consistency > 0.1)
            {
                //using (var uof = _unitOfWorkFactory.Create())
                //{

                foreach (var criterion in criteria)
                {
                    var comparisons = await _criteriaComparisonRepo.GetByFirstCriterionIDAsync(criterion.CriteriaID);

                    foreach (var comparison in comparisons)
                    {
                        comparison.CriteriaRatio = 0;
                        await _criteriaComparisonRepo.UpdateAsync(comparison);
                    }
                }
                await _criteriaComparisonRepo.SaveAsync();

                //uof.Commit();
                return(false);

                // }
            }
            return(await CalculateAlternativeWeights(choiceId, weights, criteria));
        }
        public async Task <List <ICriteriaComparisonModel> > UpdateAsync(List <ICriteriaComparisonModel> comparisons)
        {
            using (var uof = _unitOfWorkFactory.Create())
            {
                foreach (var comparison in comparisons)
                {
                    var baseComparison = await _criteriaComparisonRepository.GetByIDAsync(comparison.CriteriaID1, comparison.CriteriaID2);

                    baseComparison.DateUpdated   = DateTime.Now;
                    baseComparison.CriteriaRatio = comparison.CriteriaRatio;
                    await _criteriaComparisonRepository.UpdateAsync(baseComparison);
                }

                await _criteriaComparisonRepository.SaveAsync();

                uof.Commit();
                return(comparisons);
            }
        }