Skip to content

Commit

Permalink
refactor: adjusted namespaces
Browse files Browse the repository at this point in the history
refactor: deleted unused code
feat: added checker service
  • Loading branch information
SarcasticMoose committed Aug 15, 2024
1 parent 767771a commit 3bde13c
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 273 deletions.
2 changes: 1 addition & 1 deletion src/McdaToolkit/Extensions/EnumerableExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static class EnumerableExtentions
{
var sourceToArray = source.ToArray();
var rows = sourceToArray.Length;
var cols = sourceToArray.First().Count();
var cols = sourceToArray[0].Count();
var result = new T[rows, cols];
var i = 0;

Expand Down
9 changes: 0 additions & 9 deletions src/McdaToolkit/Mcda/Abstraction/IMcdaMethod.cs

This file was deleted.

90 changes: 0 additions & 90 deletions src/McdaToolkit/Mcda/Abstraction/McdaMethod.cs

This file was deleted.

8 changes: 0 additions & 8 deletions src/McdaToolkit/Mcda/Contracts/MatrixDto.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/McdaToolkit/Mcda/Errors/ArraySizesAreNotEqual.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using LightResults;

namespace McdaToolkit.Mcda.Helpers.Errors;

public class DecisionCriteriaHaveIncorrectValueError() : Error("Criteria decision types should be number ∈Z{-1;1}");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using LightResults;

namespace McdaToolkit.Mcda.Helpers.Errors;

public class MatrixColumnLengthNotEqualWeightsVectorLengthError() : Error("Columns length of data matrix should be equal length of weights and types arrays");
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using LightResults;

namespace McdaToolkit.Mcda.Errors;
namespace McdaToolkit.Mcda.Helpers.Errors;

public class WeightNotSumToOneError() : Error("Sum of weight have to equal 1");
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Collections;
using LightResults;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;

namespace McdaToolkit.Mcda.Abstraction;
namespace McdaToolkit.Mcda.Methods.Abstraction;

public interface ICalculation<TValue> where TValue : struct, IEquatable<TValue>, IFormattable
{
Expand Down
6 changes: 6 additions & 0 deletions src/McdaToolkit/Mcda/Methods/Abstraction/IMcdaMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace McdaToolkit.Mcda.Methods.Abstraction;

public interface IMcdaMethod : ICalculation<double>
{

}
45 changes: 45 additions & 0 deletions src/McdaToolkit/Mcda/Methods/Abstraction/McdaMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using LightResults;
using MathNet.Numerics.LinearAlgebra;
using McdaToolkit.Extensions;
using McdaToolkit.Mcda.Services;

namespace McdaToolkit.Mcda.Methods.Abstraction;

public abstract class McdaMethod : IMcdaMethod
{
private readonly MatrixCheckerService _matrixCheckerService = new();

protected abstract Result<Vector<double>> RunCalculation(double[,] matrix, double[] weights, int[] criteriaDirections);

/// <inheritdoc cref="ICalculation{TValue}.Calculate(System.Collections.Generic.IEnumerable{System.Collections.Generic.IEnumerable{TValue}},System.Collections.Generic.IEnumerable{TValue},System.Collections.Generic.IEnumerable{int})"/>
public Result<Vector<double>> Calculate(IEnumerable<IEnumerable<double>> matrix, IEnumerable<double> weights, IEnumerable<int> criteriaDirections)
{
var convertedMatrix = matrix.To2DArray();
var convertedWeights = weights.ToArray();
var convertedCriteriaDecision = criteriaDirections.ToArray();
var matrixChecked = _matrixCheckerService.ValidateData(convertedMatrix, convertedWeights, convertedCriteriaDecision);

if (matrixChecked.IsFailed)
{
return Result.Fail<Vector<double>>();
}
return RunCalculation(convertedMatrix, convertedWeights, convertedCriteriaDecision);
}

/// <summary>
/// Calculate provided data
/// </summary>
/// <param name="matrix">Data as set of alternatives and theirs attributes</param>
/// <param name="weights">Data determining relevance of each attribute</param>
/// <param name="criteriaDirections">Data that determines the columns is profit or cost</param>
/// <returns>Vector of processed data in descending order</returns>
public Result<Vector<double>> Calculate(double[,] matrix,double[] weights,int[] criteriaDirections)
{
var matrixChecked = _matrixCheckerService.ValidateData(matrix, weights, criteriaDirections);
if (matrixChecked.IsFailed)
{
return Result.Fail<Vector<double>>();
}
return RunCalculation(matrix, weights, criteriaDirections);
}
}
40 changes: 20 additions & 20 deletions src/McdaToolkit/Mcda/Methods/Topsis.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using LightResults;
using MathNet.Numerics.LinearAlgebra;
using McdaToolkit.Mcda.Abstraction;
using McdaToolkit.Mcda.Methods.Abstraction;
using McdaToolkit.Mcda.Options;
using McdaToolkit.Normalization.Service;
using McdaToolkit.Normalization.Service.Abstraction;
using McdaToolkit.Options;

namespace McdaToolkit.Mcda.Methods;

Expand All @@ -16,21 +16,6 @@ public Topsis(McdaMethodOptions options)
_normalizationServiceServiceService = new MatrixNormalizatorService(options.NormalizationMethod);
}

protected override Result<Vector<double>> RunCalculation(Matrix<double> matrix, Vector<double> weights, int[] criteriaDirections)
{
var normalizedMatrix = _normalizationServiceServiceService.NormalizeMatrix(matrix, criteriaDirections);
var weightedMatrix = WeightedMatrix(normalizedMatrix, weights);

var idealBest = IdealValues(weightedMatrix, true);
var idealWorst = IdealValues(weightedMatrix, false);

var distanceToBest = CalculateEuclideanDistance(weightedMatrix, idealBest);
var distanceToWorst = CalculateEuclideanDistance(weightedMatrix, idealWorst);
var topsisScores = CalculateTopsisScores(distanceToBest, distanceToWorst);

return Result.Ok(topsisScores);
}

private Matrix<double> WeightedMatrix(Matrix<double> matrix, Vector<double> weights)
{
for (int i = 0; i < matrix.RowCount; i++)
Expand All @@ -40,7 +25,6 @@ private Matrix<double> WeightedMatrix(Matrix<double> matrix, Vector<double> weig
matrix[i, j] *= weights[j];
}
}

return matrix;
}

Expand All @@ -54,8 +38,7 @@ private Vector<double> IdealValues(Matrix<double> matrix, bool pis)
});
}

private Vector<double> CalculateEuclideanDistance(Matrix<double> matrix,
Vector<double> ideal)
private Vector<double> CalculateEuclideanDistance(Matrix<double> matrix, Vector<double> ideal)
{
return Vector<double>.Build
.DenseOfArray(matrix
Expand All @@ -72,4 +55,21 @@ private Vector<double> CalculateTopsisScores(Vector<double> distanceToBest, Vect
{
return distanceToWorst.PointwiseDivide(distanceToBest.Add(distanceToWorst));
}

protected override Result<Vector<double>> RunCalculation(double[,] matrix,double[] weights,int[] criteriaDirections)
{
var matrixBuilded = Matrix<double>.Build.DenseOfArray(matrix);
var weightsBuilded = Vector<double>.Build.DenseOfArray(weights);
var normalizedMatrix = _normalizationServiceServiceService.NormalizeMatrix(matrixBuilded, criteriaDirections);
var weightedMatrix = WeightedMatrix(normalizedMatrix, weightsBuilded);

var idealBest = IdealValues(weightedMatrix, true);
var idealWorst = IdealValues(weightedMatrix, false);

var distanceToBest = CalculateEuclideanDistance(weightedMatrix, idealBest);
var distanceToWorst = CalculateEuclideanDistance(weightedMatrix, idealWorst);
var topsisScores = CalculateTopsisScores(distanceToBest, distanceToWorst);

return Result.Ok(topsisScores);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using McdaToolkit.Enums;

namespace McdaToolkit.Options;
namespace McdaToolkit.Mcda.Options;

/// <summary>
/// Configuration for Mcda methods
/// </summary>
public record McdaMethodOptions
{
/// <summary>
/// Current normalization method
/// </summary>
public NormalizationMethod NormalizationMethod { get; set; } = NormalizationMethod.MinMax;
}
30 changes: 30 additions & 0 deletions src/McdaToolkit/Mcda/Services/MatrixCheckerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using LightResults;
using McdaToolkit.Mcda.Helpers;
using McdaToolkit.Mcda.Helpers.Errors;

namespace McdaToolkit.Mcda.Services;

internal sealed class MatrixCheckerService
{
public Result ValidateData(double[,] matrix, double[] weights, int[] criteriaDirections)
{
var isWeightsCorrect = CheckDataHelper.IsWeightEqualOne(weights);
if (!isWeightsCorrect)
{
return Result.Fail(new WeightNotSumToOneError());
}

var isCriteriaDecisionCorrect = CheckDataHelper.IsCriteriaDesisionBetweenMinusOneAndOne(criteriaDirections);
if (!isCriteriaDecisionCorrect)
{
return Result.Fail(new DecisionCriteriaHaveIncorrectValueError());
}

var isSizesAreCorrect = CheckDataHelper.IsDataWeightsAndTypesHaveCorrectSizes(matrix, weights, criteriaDirections);
if (!isSizesAreCorrect)
{
return Result.Fail(new MatrixColumnLengthNotEqualWeightsVectorLengthError());
}
return Result.Ok();
}
}
4 changes: 4 additions & 0 deletions src/McdaToolkit/McdaToolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<None Include="..\..\Images\icon.png" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="McdaToolkit.UnitTests" />
</ItemGroup>

<ItemGroup Label="Dependencies">
<PackageReference Include="LightResults" Version="8.0.9" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
Expand Down
Loading

0 comments on commit 3bde13c

Please sign in to comment.