Skip to content

Commit bb6bf65

Browse files
committed
Resolving code smells reported by Sonar Cloud
1 parent e2418ac commit bb6bf65

File tree

11 files changed

+130
-152
lines changed

11 files changed

+130
-152
lines changed

Diff for: src/GeneticSharp.Domain/Crossovers/AlternatingPositionCrossover.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected override IList<IChromosome> PerformCross(IList<IChromosome> parents)
5252
return new List<IChromosome> { child1, child2 };
5353
}
5454

55-
private IChromosome CreateChild(IChromosome firstParent, IChromosome secondParent)
55+
private static IChromosome CreateChild(IChromosome firstParent, IChromosome secondParent)
5656
{
5757
var child = firstParent.CreateNew();
5858
var childGenes = new Gene[firstParent.Length];

Diff for: src/GeneticSharp.Domain/Crossovers/OnePointCrossover.cs

+5-11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ namespace GeneticSharp
3333
[DisplayName("One-Point")]
3434
public class OnePointCrossover : CrossoverBase
3535
{
36-
#region Constructors
3736
/// <summary>
3837
/// Initializes a new instance of the <see cref="GeneticSharp.Domain.Crossovers.OnePointCrossover"/> class.
3938
/// </summary>
@@ -48,18 +47,14 @@ public OnePointCrossover(int swapPointIndex) : base(2, 2)
4847
/// </summary>
4948
public OnePointCrossover() : this(0)
5049
{
51-
}
52-
#endregion
53-
54-
#region Properties
50+
}
51+
5552
/// <summary>
5653
/// Gets or sets the index of the swap point.
5754
/// </summary>
5855
/// <value>The index of the swap point.</value>
5956
public int SwapPointIndex { get; set; }
60-
#endregion
61-
62-
#region Methods
57+
6358
/// <summary>
6459
/// Performs the cross with specified parents generating the children.
6560
/// </summary>
@@ -77,7 +72,7 @@ protected override IList<IChromosome> PerformCross(IList<IChromosome> parents)
7772
if (SwapPointIndex >= swapPointsLength)
7873
{
7974
throw new ArgumentOutOfRangeException(
80-
"parents",
75+
nameof(parents),
8176
"The swap point index is {0}, but there is only {1} genes. The swap should result at least one gene to each side.".With(SwapPointIndex, firstParent.Length));
8277
}
8378

@@ -112,7 +107,6 @@ protected virtual IChromosome CreateChild(IChromosome leftParent, IChromosome ri
112107
child.ReplaceGenes(cutGenesCount, rightParent.GetGenes().Skip(cutGenesCount).ToArray());
113108

114109
return child;
115-
}
116-
#endregion
110+
}
117111
}
118112
}

Diff for: src/GeneticSharp.Domain/OperatorsStrategy/OperatorsStrategyBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public abstract class OperatorsStrategyBase : IOperatorsStrategy
3636
/// <param name="parents">The parents.</param>
3737
/// <param name="firstParentIndex">the index of the first parent selected for a crossover</param>
3838
/// <returns>children for the current crossover if it was performed, null otherwise</returns>
39-
protected IList<IChromosome> SelectParentsAndCross(IPopulation population, ICrossover crossover,
39+
protected static IList<IChromosome> SelectParentsAndCross(IPopulation population, ICrossover crossover,
4040
float crossoverProbability, IList<IChromosome> parents, int firstParentIndex)
4141
{
4242
var selectedParents = parents.Skip(firstParentIndex).Take(crossover.ParentsNumber).ToList();

Diff for: src/GeneticSharp.Domain/Populations/Population.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public Population(int minSize, int maxSize, IChromosome adamChromosome)
2323
{
2424
if (minSize < 2)
2525
{
26-
throw new ArgumentOutOfRangeException("minSize", "The minimum size for a population is 2 chromosomes.");
26+
throw new ArgumentOutOfRangeException(nameof(minSize), "The minimum size for a population is 2 chromosomes.");
2727
}
2828

2929
if (maxSize < minSize)
3030
{
31-
throw new ArgumentOutOfRangeException("maxSize", "The maximum size for a population should be equal or greater than minimum size.");
31+
throw new ArgumentOutOfRangeException(nameof(maxSize), "The maximum size for a population should be equal or greater than minimum size.");
3232
}
3333

34-
ExceptionHelper.ThrowIfNull("adamChromosome", adamChromosome);
34+
ExceptionHelper.ThrowIfNull(nameof(adamChromosome), adamChromosome);
3535

3636
CreationDate = DateTime.Now;
3737
MinSize = minSize;

Diff for: src/GeneticSharp.Domain/Selections/EliteSelection.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace GeneticSharp
1414
[DisplayName("Elite")]
1515
public sealed class EliteSelection : SelectionBase
1616
{
17-
int _previousGenerationChromosomesNumber;
17+
readonly int _previousGenerationChromosomesNumber;
1818
List<IChromosome> _previousGenerationChromosomes;
1919

2020
/// <summary>

Diff for: src/GeneticSharp.Extensions/Mathematic/EquationChromosome.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace GeneticSharp.Extensions
77
/// </summary>
88
public sealed class EquationChromosome : ChromosomeBase
99
{
10-
#region Constructors
1110
/// <summary>
1211
/// Initializes a new instance of the <see cref="EquationChromosome"/> class.
1312
/// </summary>
@@ -17,7 +16,7 @@ public EquationChromosome(int expectedResult, int variablesNumber) : base(variab
1716
{
1817
if (expectedResult >= int.MaxValue / 2)
1918
{
20-
throw new ArgumentOutOfRangeException("expectedResult", expectedResult, "EquationChromosome expected value must be lower");
19+
throw new ArgumentOutOfRangeException(nameof(expectedResult), expectedResult, "EquationChromosome expected value must be lower");
2120
}
2221

2322
ResultRange = expectedResult * 2;
@@ -26,18 +25,14 @@ public EquationChromosome(int expectedResult, int variablesNumber) : base(variab
2625
{
2726
ReplaceGene(i, GenerateGene(i));
2827
}
29-
}
30-
#endregion
31-
32-
#region Properties
28+
}
29+
3330
/// <summary>
3431
/// Gets the result range.
3532
/// </summary>
3633
/// <value>The result range.</value>
3734
public int ResultRange { get; private set; }
38-
#endregion
3935

40-
#region Methods
4136
/// <summary>
4237
/// Creates the new.
4338
/// </summary>
@@ -55,7 +50,6 @@ public override IChromosome CreateNew()
5550
public override Gene GenerateGene(int geneIndex)
5651
{
5752
return new Gene(RandomizationProvider.Current.GetInt(ResultRange * -1, ResultRange + 1));
58-
}
59-
#endregion
53+
}
6054
}
6155
}

Diff for: src/GeneticSharp.Extensions/Sudoku/ISudokuChromosome.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32

43
namespace GeneticSharp.Extensions
54
{

Diff for: src/GeneticSharp.Extensions/Sudoku/SudokuChromosomeBase.cs

+54-51
Original file line numberDiff line numberDiff line change
@@ -46,71 +46,74 @@ public Dictionary<int, List<int>> ExtendedMask
4646
get
4747
{
4848
if (_extendedMask == null)
49+
BuildExtenedMask();
50+
51+
return _extendedMask;
52+
}
53+
}
54+
55+
private void BuildExtenedMask()
56+
{
57+
// We generate 1 to 9 figures for convenience
58+
var indices = Enumerable.Range(1, 9).ToList();
59+
var extendedMask = new Dictionary<int, List<int>>(81);
60+
if (_targetSudokuBoard != null)
61+
{
62+
//If target sudoku mask is provided, we generate an inverted mask with forbidden values by propagating rows, columns and boxes constraints
63+
var forbiddenMask = new Dictionary<int, List<int>>();
64+
List<int> targetList = null;
65+
for (var index = 0; index < _targetSudokuBoard.Cells.Count; index++)
4966
{
50-
// We generate 1 to 9 figures for convenience
51-
var indices = Enumerable.Range(1, 9).ToList();
52-
var extendedMask = new Dictionary<int, List<int>>(81);
53-
if (_targetSudokuBoard!=null)
67+
var targetCell = _targetSudokuBoard.Cells[index];
68+
if (targetCell != 0)
5469
{
55-
//If target sudoku mask is provided, we generate an inverted mask with forbidden values by propagating rows, columns and boxes constraints
56-
var forbiddenMask = new Dictionary<int, List<int>>();
57-
List<int> targetList = null;
58-
for (var index = 0; index < _targetSudokuBoard.Cells.Count; index++)
70+
//We parallelize going through all 3 constraint neighborhoods
71+
var row = index / 9;
72+
var col = index % 9;
73+
var boxStartIdx = (index / 27 * 27) + (index % 9 / 3 * 3);
74+
75+
for (int i = 0; i < 9; i++)
5976
{
60-
var targetCell = _targetSudokuBoard.Cells[index];
61-
if (targetCell != 0)
77+
//We go through all 9 cells in the 3 neighborhoods
78+
var boxtargetIdx = boxStartIdx + (i % 3) + ((i / 3) * 9);
79+
var targetIndices = new[] { (row * 9) + i, i * 9 + col, boxtargetIdx };
80+
foreach (var targetIndex in targetIndices)
6281
{
63-
//We parallelize going through all 3 constraint neighborhoods
64-
var row = index / 9;
65-
var col = index % 9;
66-
var boxStartIdx = (index / 27 * 27) + (index % 9 / 3 * 3);
67-
68-
for (int i = 0; i < 9; i++)
82+
if (targetIndex != index)
6983
{
70-
//We go through all 9 cells in the 3 neighborhoods
71-
var boxtargetIdx = boxStartIdx + (i % 3) + ((i / 3) * 9);
72-
var targetIndices = new[] { (row * 9) + i, i * 9 + col, boxtargetIdx };
73-
foreach (var targetIndex in targetIndices)
84+
if (!forbiddenMask.TryGetValue(targetIndex, out targetList))
85+
{
86+
//If the current neighbor cell does not have a forbidden values list, we create it
87+
targetList = new List<int>();
88+
forbiddenMask[targetIndex] = targetList;
89+
}
90+
if (!targetList.Contains(targetCell))
7491
{
75-
if (targetIndex != index)
76-
{
77-
if (!forbiddenMask.TryGetValue(targetIndex, out targetList))
78-
{
79-
//If the current neighbor cell does not have a forbidden values list, we create it
80-
targetList = new List<int>();
81-
forbiddenMask[targetIndex] = targetList;
82-
}
83-
if (!targetList.Contains(targetCell))
84-
{
85-
// We add current cell value to the neighbor cell forbidden values
86-
targetList.Add(targetCell);
87-
}
88-
}
92+
// We add current cell value to the neighbor cell forbidden values
93+
targetList.Add(targetCell);
8994
}
9095
}
9196
}
9297
}
93-
94-
// We invert the forbidden values mask to obtain the cell permitted values domains
95-
for (var index = 0; index < _targetSudokuBoard.Cells.Count; index++)
96-
{
97-
extendedMask[index] = indices.Where(i => !forbiddenMask[index].Contains(i)).ToList();
98-
}
99-
10098
}
101-
else
102-
{
103-
//If we have no sudoku mask, 1-9 numbers are allowed for all cells
104-
for (int i = 0; i < 81; i++)
105-
{
106-
extendedMask.Add(i, indices);
107-
}
108-
}
109-
_extendedMask = extendedMask;
99+
}
110100

101+
// We invert the forbidden values mask to obtain the cell permitted values domains
102+
for (var index = 0; index < _targetSudokuBoard.Cells.Count; index++)
103+
{
104+
extendedMask[index] = indices.Where(i => !forbiddenMask[index].Contains(i)).ToList();
105+
}
106+
107+
}
108+
else
109+
{
110+
//If we have no sudoku mask, 1-9 numbers are allowed for all cells
111+
for (int i = 0; i < 81; i++)
112+
{
113+
extendedMask.Add(i, indices);
111114
}
112-
return _extendedMask;
113115
}
116+
_extendedMask = extendedMask;
114117
}
115118

116119
public abstract IList<SudokuBoard> GetSudokus();

Diff for: src/GeneticSharp.Extensions/Tsp/TspFitness.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace GeneticSharp.Extensions
1616
/// </summary>
1717
public class TspFitness : IFitness
1818
{
19-
#region Constructors
2019
/// <summary>
2120
/// Initializes a new instance of the <see cref="GeneticSharp.Extensions.Tsp.TspFitness"/> class.
2221
/// </summary>
@@ -35,12 +34,12 @@ public TspFitness(int numberOfCities, int minX, int maxX, int minY, int maxY)
3534

3635
if (maxX >= int.MaxValue)
3736
{
38-
throw new ArgumentOutOfRangeException("maxX");
37+
throw new ArgumentOutOfRangeException(nameof(maxX));
3938
}
4039

4140
if (maxY >= int.MaxValue)
4241
{
43-
throw new ArgumentOutOfRangeException("maxY");
42+
throw new ArgumentOutOfRangeException(nameof(maxY));
4443
}
4544

4645
for (int i = 0; i < numberOfCities; i++)
@@ -49,9 +48,7 @@ public TspFitness(int numberOfCities, int minX, int maxX, int minY, int maxY)
4948
Cities.Add(city);
5049
}
5150
}
52-
#endregion
53-
54-
#region Properties
51+
5552
/// <summary>
5653
/// Gets the cities.
5754
/// </summary>
@@ -80,10 +77,8 @@ public TspFitness(int numberOfCities, int minX, int maxX, int minY, int maxY)
8077
/// Gets the max y.
8178
/// </summary>
8279
/// <value>The max y.</value>
83-
public int MaxY { get; private set; }
84-
#endregion
85-
86-
#region IFitness implementation
80+
public int MaxY { get; private set; }
81+
8782
/// <summary>
8883
/// Performs the evaluation against the specified chromosome.
8984
/// </summary>
@@ -139,7 +134,6 @@ public double Evaluate(IChromosome chromosome)
139134
private static double CalcDistanceTwoCities(TspCity one, TspCity two)
140135
{
141136
return Math.Sqrt(Math.Pow(two.X - one.X, 2) + Math.Pow(two.Y - one.Y, 2));
142-
}
143-
#endregion
137+
}
144138
}
145139
}

0 commit comments

Comments
 (0)