public async Task <List <IAlternativeComparisonModel> > AddAsync(List <IAlternativeComparisonModel> comparisons) { foreach (IAlternativeComparisonModel comparison in comparisons) { comparison.DateCreated = DateTime.Now; comparison.DateUpdated = DateTime.Now; comparison.AlternativeRatio = 0; } comparisons = _altCompRepo.AddRange(comparisons); await _altCompRepo.SaveAsync(); return(comparisons); }
public async Task <bool> CalculateAlternativeWeights(Guid choiceId, double[] choiceWeights, List <ICriterionModel> criteria) { var alternatives = await _alternativeRepository.GetByChoiceIDAsync(choiceId); var hash = new Dictionary <Guid, int>(); int i = 0; foreach (var alternative in alternatives) { hash[alternative.AlternativeID] = i; i++; } var weights = new List <double[]>(); foreach (var criterion in criteria) { double[,] matrix = new double[alternatives.Count, alternatives.Count]; foreach (var alternative in alternatives) { matrix[hash[alternative.AlternativeID], hash[alternative.AlternativeID]] = 1; var comparisons = await _alternativeComparisonRepo.GetByCriteriaAndFirstAlternativeIDAsync(criterion.CriteriaID, alternative.AlternativeID); foreach (var comparison in comparisons) { matrix[hash[comparison.AlternativeID1], hash[comparison.AlternativeID2]] = comparison.AlternativeRatio; matrix[hash[comparison.AlternativeID2], hash[comparison.AlternativeID1]] = 1 / comparison.AlternativeRatio; } } var weight = _AHPService.CalculatePriortyVector(matrix); var consistency = _AHPService.CalculateConsistency(matrix, weight); if (consistency > 0.1) { foreach (var alternative in alternatives) { var comparisons = await _alternativeComparisonRepo.GetByCriteriaAndFirstAlternativeIDAsync(criterion.CriteriaID, alternative.AlternativeID); foreach (var comparison in comparisons) { comparison.AlternativeRatio = 0; await _alternativeComparisonRepo.UpdateAsync(comparison); } } await _alternativeComparisonRepo.SaveAsync(); return(false); } weights.Add(weight); } double[,] alternativeWeightMatrix = new double[alternatives.Count, criteria.Count]; i = 0; foreach (var weight in weights) { for (int j = 0; j < alternatives.Count; j++) { alternativeWeightMatrix[j, i] = weight[j]; } i++; } var alternativeScores = _AHPService.FinalCalculate(choiceWeights, alternativeWeightMatrix); //sprema dobivene scoreove using (var uof = _unitOfWorkFactory.Create()) { i = 0; foreach (var alternative in alternatives) { alternative.AlternativeScore = alternativeScores[i]; await _alternativeRepository.UpdateAsync(alternative); i++; } await _alternativeRepository.SaveAsync(); uof.Commit(); } return(true); }