@@ -466,20 +466,19 @@ private class ClusterSolution implements Cloneable {
466
466
* @param n The number of nodes (must be positive).
467
467
* @param k The number of clusters (must be positive).
468
468
* @param memberships The membership vector (length must equal n, values must be in the range [0, k - 1]).
469
- * @throws IllegalArgumentException If any parameter is invalid.
470
469
*/
471
470
ClusterSolution (int n , int k , int [] memberships ) {
472
471
if (n <= 0 ) {
473
- throw new IllegalArgumentException ("N must be positive." );
474
- }
475
- if (k <= 1 ) {
476
- throw new IllegalArgumentException ("K must be larger than 1." );
477
- }
478
- if (n <= k ) {
479
- throw new IllegalArgumentException ("N must be larger than K." );
472
+ n = memberships .length ;
473
+ LogEvent log = new LogEvent (Logger .WARNING , "Invalid number of nodes." ,
474
+ "Number of nodes (N) must be positive. Using the length of the membership vector (" + n + ") instead." );
475
+ Dna .logger .log (log );
480
476
}
477
+ validateArguments (n , k );
481
478
if (memberships == null || memberships .length != n ) {
482
- throw new IllegalArgumentException ("Memberships must have length equal to N." );
479
+ LogEvent log = new LogEvent (Logger .ERROR , "Invalid membership vector." ,
480
+ "Memberships must have length equal to N." );
481
+ Dna .logger .log (log );
483
482
}
484
483
validateMemberships (memberships , k );
485
484
this .N = n ;
@@ -493,18 +492,9 @@ private class ClusterSolution implements Cloneable {
493
492
* @param n The number of nodes (must be positive).
494
493
* @param k The number of clusters (must be positive).
495
494
* @param rng The random number generator.
496
- * @throws IllegalArgumentException If any parameter is invalid.
497
495
*/
498
496
ClusterSolution (int n , int k , Random rng ) {
499
- if (n <= 0 ) {
500
- throw new IllegalArgumentException ("N must be positive." );
501
- }
502
- if (k <= 1 ) {
503
- throw new IllegalArgumentException ("K must be larger than 1." );
504
- }
505
- if (n <= k ) {
506
- throw new IllegalArgumentException ("N must be larger than K." );
507
- }
497
+ validateArguments (n , k );
508
498
this .N = n ;
509
499
this .K = k ;
510
500
this .memberships = createRandomMemberships (n , k , rng );
@@ -529,13 +519,37 @@ protected ClusterSolution clone() throws CloneNotSupportedException {
529
519
return new ClusterSolution (this .N , this .K , this .memberships .clone ());
530
520
}
531
521
522
+ /**
523
+ * Validates the input arguments for the ClusterSolution constructor.
524
+ *
525
+ * @param n The number of nodes.
526
+ * @param k The number of clusters.
527
+ */
528
+ private void validateArguments (int n , int k ) {
529
+ if (k <= 1 ) {
530
+ k = 2 ;
531
+ LogEvent log = new LogEvent (Logger .WARNING , "Invalid number of clusters." ,
532
+ "Number of clusters (K) must be greater than 1. Using 2 clusters instead." );
533
+ Dna .logger .log (log );
534
+ }
535
+ if (n <= k ) {
536
+ LogEvent log = new LogEvent (Logger .ERROR , "Invalid number of nodes and clusters." ,
537
+ "Number of nodes (N) must be greater than the number of clusters (K)." );
538
+ Dna .logger .log (log );
539
+ }
540
+ }
532
541
/**
533
542
* Validates that all memberships are within the range [0, K - 1].
543
+ *
544
+ * @param memberships The membership vector to validate.
545
+ * @param k The number of clusters.
534
546
*/
535
547
private void validateMemberships (int [] memberships , int k ) {
536
548
for (int membership : memberships ) {
537
549
if (membership < 0 || membership >= k ) {
538
- throw new IllegalArgumentException ("Membership values must be in the range [0, K - 1]." );
550
+ LogEvent log = new LogEvent (Logger .ERROR , "Invalid membership value." ,
551
+ "Membership values must be in the range [0, K - 1]." );
552
+ Dna .logger .log (log );
539
553
}
540
554
}
541
555
}
@@ -581,12 +595,12 @@ private int[] createRandomMemberships(int N, int K, Random rng) {
581
595
*
582
596
* @param foreignMemberships A membership vector of a foreign cluster solution.
583
597
* @param rng The random number generator to use.
584
- * @throws IllegalArgumentException If the input vector is invalid or incompatible.
585
598
*/
586
- int [] crossover (int [] foreignMemberships , Random rng ) throws IllegalArgumentException {
587
- // Validate input
599
+ int [] crossover (int [] foreignMemberships , Random rng ) {
588
600
if (foreignMemberships == null || foreignMemberships .length != this .memberships .length ) {
589
- throw new IllegalArgumentException ("Incompatible membership vector lengths." );
601
+ LogEvent log = new LogEvent (Logger .ERROR , "Invalid membership vector." ,
602
+ "Membership vector must have the same length as the current solution." );
603
+ Dna .logger .log (log );
590
604
}
591
605
validateMemberships (foreignMemberships , K );
592
606
@@ -804,7 +818,6 @@ private double[] evaluateQuality(double[][] congruenceNetwork, double[][] confli
804
818
* @param q The array of quality values for the parent generation (their modularity or EI scores transformed to [0, 1] where 1 is high fitness).
805
819
* @param numElites The number of elite solutions to retain for the children generation.
806
820
* @return A list of children containing the cloned elite solutions from the parent generation.
807
- * @throws IllegalArgumentException If the elite percentage is outside the valid range [0, 1].
808
821
*/
809
822
private ArrayList <ClusterSolution > eliteRetentionStep (ArrayList <ClusterSolution > clusterSolutions , double [] q , int numElites ) {
810
823
int [] qRanks = calculateRanks (q ); // Rank the quality values in descending order
@@ -815,7 +828,6 @@ private ArrayList<ClusterSolution> eliteRetentionStep (ArrayList<ClusterSolution
815
828
try {
816
829
children .add ((ClusterSolution ) clusterSolutions .get (i ).clone ());
817
830
} catch (CloneNotSupportedException e ) {
818
-
819
831
LogEvent log = new LogEvent (Logger .ERROR , "Elite solution at index " + i + " could not be cloned." , "Elite solutions are not copied to the children generation." );
820
832
Dna .logger .log (log );
821
833
}
@@ -925,7 +937,9 @@ private ArrayList<ClusterSolution> mutationStep(ArrayList<ClusterSolution> child
925
937
return children ; // No mutations to perform
926
938
}
927
939
if (numMutations < 0 ) {
928
- throw new IllegalArgumentException ("Number of mutations must be non-negative." );
940
+ LogEvent log = new LogEvent (Logger .ERROR , "Invalid number of mutations." ,
941
+ "Number of mutations must be non-negative." );
942
+ Dna .logger .log (log );
929
943
}
930
944
931
945
for (int i = numElites ; i < numParents ; i ++) {
@@ -1125,8 +1139,9 @@ private PolarizationResult geneticTimeStep(int t, long seed) {
1125
1139
*
1126
1140
* @param matrix The matrix for which to calculate the norm.
1127
1141
* @return The entrywise 1-norm (= sum of all absolute cell values) of the matrix.
1142
+ * @throws IllegalArgumentException if the matrix is null.
1128
1143
*/
1129
- private double calculateMatrixNorm (double [][] matrix ) {
1144
+ private double calculateMatrixNorm (double [][] matrix ) throws IllegalArgumentException {
1130
1145
if (matrix == null ) {
1131
1146
LogEvent log = new LogEvent (Logger .ERROR , "Matrix is null." , "Error when trying to calculate the matrix norm in the genetic algorithm. Matrix cannot be null." );
1132
1147
Dna .logger .log (log );
@@ -1487,7 +1502,15 @@ private PolarizationResult greedyTimeStep(Matrix congruence, Matrix conflict, bo
1487
1502
double [][] congruenceMatrix = congruence .getMatrix ();
1488
1503
double [][] conflictMatrix = conflict .getMatrix ();
1489
1504
ArrayList <Double > maxQArray = new ArrayList <Double >();
1490
- double combinedNorm = calculateMatrixNorm (congruenceMatrix ) + calculateMatrixNorm (congruenceMatrix );
1505
+ double combinedNorm = 0.0 ;
1506
+ try {
1507
+ combinedNorm = calculateMatrixNorm (congruenceMatrix ) + calculateMatrixNorm (congruenceMatrix );
1508
+ } catch (Exception e ) {
1509
+ LogEvent log = new LogEvent (Logger .ERROR ,
1510
+ "Error when calculating the matrix norm." ,
1511
+ "Error when calculating the matrix norm in the greedy algorithm." );
1512
+ Dna .logger .log (log );
1513
+ }
1491
1514
1492
1515
if (congruenceMatrix .length > 0 || combinedNorm == 0.0 ) { // if the network has no nodes or edges, skip this step and return 0 directly
1493
1516
0 commit comments