Skip to content

Commit a9fa9bf

Browse files
maxsITMaksym Sitaylo
andauthored
Add Pareto optimization algorithm (#331)
Co-authored-by: Maksym Sitaylo <m.sitailo@sana-commerce.com>
1 parent dad43c3 commit a9fa9bf

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Algorithms.Other;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Algorithms.Tests.Other
10+
{
11+
public static class ParetoOptimizationTests
12+
{
13+
[Test]
14+
public static void Verify_Pareto_Optimization()
15+
{
16+
// Arrange
17+
var paretoOptimization = new ParetoOptimization();
18+
19+
var matrix = new List<List<decimal>>
20+
{
21+
new List<decimal> { 7, 6, 5, 8, 5, 6 },
22+
new List<decimal> { 4, 8, 4, 4, 5, 3 },
23+
new List<decimal> { 3, 8, 1, 4, 5, 2 },
24+
new List<decimal> { 5, 6, 3, 6, 4, 5 },
25+
new List<decimal> { 1, 4, 8, 6, 3, 6 },
26+
new List<decimal> { 5, 1, 8, 6, 5, 1 },
27+
new List<decimal> { 6, 8, 3, 6, 3, 5 }
28+
};
29+
30+
var expectedMatrix = new List<List<decimal>>
31+
{
32+
new List<decimal> { 7, 6, 5, 8, 5, 6 },
33+
new List<decimal> { 4, 8, 4, 4, 5, 3 },
34+
new List<decimal> { 1, 4, 8, 6, 3, 6 },
35+
new List<decimal> { 5, 1, 8, 6, 5, 1 },
36+
new List<decimal> { 6, 8, 3, 6, 3, 5 }
37+
};
38+
39+
// Act
40+
var optimizedMatrix = paretoOptimization.Optimize(matrix);
41+
42+
// Assert
43+
Assert.AreEqual(optimizedMatrix, expectedMatrix);
44+
}
45+
}
46+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Algorithms.Other
8+
{
9+
/// <summary>
10+
/// Almost all real complex decision-making task is described by more than one criterion.
11+
/// Therefore, the methods of multicriteria optimization are important. For a wide range
12+
/// of tasks multicriteria optimization, described by some axioms of "reasonable"
13+
/// behavior in the process of choosing from a set of possible solutions X, each set of
14+
/// selected solutions Sel X should be contained in a set optimal for Pareto.
15+
/// </summary>
16+
public class ParetoOptimization
17+
{
18+
/// <summary>
19+
/// Performs decision optimizations by using Paretor's optimization algorithm.
20+
/// </summary>
21+
/// <param name="matrix">Contains a collection of the criterias sets.</param>
22+
/// <returns>An optimized collection of the criterias sets.</returns>
23+
public List<List<decimal>> Optimize(List<List<decimal>> matrix)
24+
{
25+
var optimizedMatrix = new List<List<decimal>>(matrix.Select(i => i));
26+
int i = 0;
27+
while (i < optimizedMatrix.Count)
28+
{
29+
for (int j = i + 1; j < optimizedMatrix.Count; j++)
30+
{
31+
decimal directParwiseDifference = GetMinimalPairwiseDifference(optimizedMatrix[i], optimizedMatrix[j]);
32+
decimal indirectParwiseDifference = GetMinimalPairwiseDifference(optimizedMatrix[j], optimizedMatrix[i]);
33+
/*
34+
* in case all criteria of one set are larger that the criteria of another, this
35+
* decision is not optimal and it has to be removed
36+
*/
37+
if (directParwiseDifference >= 0 || indirectParwiseDifference >= 0)
38+
{
39+
optimizedMatrix.RemoveAt(directParwiseDifference >= 0 ? j : i);
40+
i--;
41+
break;
42+
}
43+
}
44+
45+
i++;
46+
}
47+
48+
return optimizedMatrix;
49+
}
50+
51+
/// <summary>
52+
/// Calculates the smallest difference between criteria of input decisions.
53+
/// </summary>
54+
/// <param name="arr1">Criterias of the first decision.</param>
55+
/// <param name="arr2">Criterias of the second decision.</param>
56+
/// <returns>Values that represent the smallest difference between criteria of input decisions.</returns>
57+
private decimal GetMinimalPairwiseDifference(List<decimal> arr1, List<decimal> arr2)
58+
{
59+
decimal min = decimal.MaxValue;
60+
if (arr1.Count == arr2.Count)
61+
{
62+
for (int i = 0; i < arr1.Count; i++)
63+
{
64+
decimal difference = arr1[i] - arr2[i];
65+
if (min > difference)
66+
{
67+
min = difference;
68+
}
69+
}
70+
}
71+
72+
return min;
73+
}
74+
}
75+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ find more than one implementation for the same objective but using different alg
168168
* [Koch Snowflake](./Algorithms/Other/KochSnowflake.cs)
169169
* [RGB-HSV Conversion](./Algorithms/Other/RGBHSVConversion.cs)
170170
* [Flood Fill](./Algorithms/Other/FloodFill.cs)
171+
* [Pareto Optimization](./Algorithms/Other/ParetoOptimization.cs)
171172
* [Problems](./Algorithms/Problems)
172173
* [Stable Marriage](./Algorithms/Problems/StableMarriage)
173174
* [Gale-Shapley](./Algorithms/Problems/StableMarriage/GaleShapley.cs)

0 commit comments

Comments
 (0)