Skip to content

Commit 56d97df

Browse files
committed
PMD fixes
1 parent aa43fe9 commit 56d97df

File tree

12 files changed

+64
-47
lines changed

12 files changed

+64
-47
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Gradle test
1+
name: MAL test
22

33
on:
44
push:
@@ -15,15 +15,15 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v3
19-
- name: Set up JDK 17
20-
uses: actions/setup-java@v3
18+
- uses: actions/checkout@v4
19+
- name: Set up JDK 21
20+
uses: actions/setup-java@v4
2121
with:
22-
java-version: '17'
22+
java-version: '21'
2323
distribution: 'temurin'
2424

2525
- name: Setup Gradle
26-
uses: gradle/gradle-build-action@v2
26+
uses: gradle/actions/setup-gradle@v4
2727

2828
- name: gradle test
2929
run: ./gradlew --no-daemon --console=plain test

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ allprojects {
5555

5656
javadoc {
5757
failOnError = true
58-
title "emu-v${version}-doc"
58+
title "mal-v${version}-doc"
5959
options.encoding = 'UTF-8'
6060
options.addBooleanOption('Werror', true)
6161
}

examples/src/main/java/com/ledmington/mal/examples/Diet.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public Diet() {
234234
final Set<Solution> allSolutions = new HashSet<>();
235235

236236
for (int it = 0; it < 10; it++) {
237-
System.out.printf("Run n.%,d\n", it);
237+
System.out.printf("Run n.%,d%n", it);
238238
ga.setState(state.get().firstGeneration(g).build());
239239
ga.run();
240240

@@ -248,7 +248,7 @@ public Diet() {
248248
for (int i = 0; i < Food.values().length; i++) {
249249
p += e.getKey().get(i) * Food.values()[i].avgPricePerKilo;
250250
}
251-
System.out.printf("%s -> %f (%.2f$)\n", e.getKey(), e.getValue(), p);
251+
System.out.printf("%s -> %f (%.2f$)%n", e.getKey(), e.getValue(), p);
252252
});
253253
g = scores.entrySet().stream()
254254
.sorted(Map.Entry.comparingByValue())
@@ -260,8 +260,8 @@ public Diet() {
260260

261261
final long end = System.nanoTime();
262262

263-
System.out.printf("\n%,d solutions evaluated\n", allSolutions.size());
264-
System.out.printf("Total search time: %.3f seconds\n", (double) (end - beginning) / 1_000_000_000.0);
263+
System.out.printf("%n%,d solutions evaluated%n", allSolutions.size());
264+
System.out.printf("Total search time: %.3f seconds%n", (double) (end - beginning) / 1_000_000_000.0);
265265

266266
if (!ex.isShutdown()) {
267267
ex.shutdown();

examples/src/main/java/com/ledmington/mal/examples/GeneticTsp.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public boolean equals(final Object other) {
103103

104104
public String toString() {
105105
final StringBuilder sb = new StringBuilder();
106-
sb.append("[");
106+
sb.append('[');
107107
boolean firstElement = true;
108108
for (final int j : array) {
109109
if (firstElement) {
@@ -113,7 +113,7 @@ public String toString() {
113113
}
114114
sb.append(String.format("%2d", j));
115115
}
116-
sb.append("]");
116+
sb.append(']');
117117
return sb.toString();
118118
}
119119
}
@@ -136,17 +136,17 @@ public GeneticTsp() {
136136
final double[][] coordinates = new double[2][nCities];
137137

138138
System.out.println("Traveling Salesman Problem's data:");
139-
System.out.printf("Number of cities : %,d\n", nCities);
139+
System.out.printf("Number of cities : %,d%n", nCities);
140140
System.out.printf(
141-
"Number of unique paths : %.3e\n",
141+
"Number of unique paths : %.3e%n",
142142
new BigDecimal(factorial(BigInteger.valueOf(nCities - 1)).divide(BigInteger.TWO)));
143143

144144
System.out.println();
145145
System.out.println("Cities coordinates:");
146146
for (int i = 0; i < nCities; i++) {
147147
coordinates[0][i] = rng.nextDouble(-10.0, 10.0);
148148
coordinates[1][i] = rng.nextDouble(-10.0, 10.0);
149-
System.out.printf("%2d: (%+.3f; %+.3f)\n", i, coordinates[0][i], coordinates[1][i]);
149+
System.out.printf("%2d: (%+.3f; %+.3f)%n", i, coordinates[0][i], coordinates[1][i]);
150150
}
151151
System.out.println();
152152

@@ -220,7 +220,7 @@ public GeneticTsp() {
220220
final Set<Solution> allSolutions = new HashSet<>();
221221

222222
for (int it = 0; it < 10; it++) {
223-
System.out.printf("Run n.%,d\n", it);
223+
System.out.printf("Run n.%,d%n", it);
224224
ga.setState(state.get().firstGeneration(g).build());
225225
ga.run();
226226

@@ -229,7 +229,7 @@ public GeneticTsp() {
229229
scores.entrySet().stream()
230230
.sorted(Map.Entry.comparingByValue())
231231
.limit(10)
232-
.forEach(e -> System.out.printf("%s -> %f\n", e.getKey(), e.getValue()));
232+
.forEach(e -> System.out.printf("%s -> %f%n", e.getKey(), e.getValue()));
233233
g = scores.entrySet().stream()
234234
.sorted(Map.Entry.comparingByValue())
235235
.limit(10)
@@ -240,8 +240,8 @@ public GeneticTsp() {
240240

241241
final long end = System.nanoTime();
242242

243-
System.out.printf("\n%,d solutions evaluated\n", allSolutions.size());
244-
System.out.printf("Total search time: %.3f seconds\n", (double) (end - beginning) / 1_000_000_000.0);
243+
System.out.printf("%n%,d solutions evaluated%n", allSolutions.size());
244+
System.out.printf("Total search time: %.3f seconds%n", (double) (end - beginning) / 1_000_000_000.0);
245245

246246
if (!ex.isShutdown()) {
247247
ex.shutdown();

examples/src/main/java/com/ledmington/mal/examples/Knapsack.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public boolean equals(final Object other) {
8585

8686
public String toString() {
8787
final StringBuilder sb = new StringBuilder();
88-
sb.append("[");
88+
sb.append('[');
8989
boolean firstElement = true;
9090
for (int i = 0; i < array.length; i++) {
9191
if (array[i]) {
@@ -97,7 +97,7 @@ public String toString() {
9797
sb.append(String.format("%2d", i));
9898
}
9999
}
100-
sb.append("]");
100+
sb.append(']');
101101
return sb.toString();
102102
}
103103
}
@@ -111,16 +111,16 @@ public Knapsack() {
111111
final double capacity = 20.0;
112112

113113
System.out.println("Knapsack data:");
114-
System.out.printf("Knapsack's capacity : %.3f\n", capacity);
115-
System.out.printf("Number of items : %,d\n", nItems);
116-
System.out.printf("Total possible solutions : %.3e\n", new BigDecimal(BigInteger.ONE.shiftLeft(nItems)));
114+
System.out.printf("Knapsack's capacity : %.3f%n", capacity);
115+
System.out.printf("Number of items : %,d%n", nItems);
116+
System.out.printf("Total possible solutions : %.3e%n", new BigDecimal(BigInteger.ONE.shiftLeft(nItems)));
117117
System.out.println();
118118

119119
System.out.println("Items data:");
120120
for (int i = 0; i < nItems; i++) {
121121
weights[i] = rng.nextDouble(0.1, 6.0);
122122
values[i] = rng.nextDouble(0.1, 6.0);
123-
System.out.printf("%3d: (w: %.3f; v: %.3f)\n", i, weights[i], values[i]);
123+
System.out.printf("%3d: (w: %.3f; v: %.3f)%n", i, weights[i], values[i]);
124124
}
125125
System.out.println();
126126

@@ -182,7 +182,7 @@ public Knapsack() {
182182
final Set<Solution> allSolutions = new HashSet<>();
183183

184184
for (int it = 0; it < 10; it++) {
185-
System.out.printf("Run n.%,d\n", it);
185+
System.out.printf("Run n.%,d%n", it);
186186
ga.setState(state.get().firstGeneration(g).build());
187187
ga.run();
188188

@@ -199,7 +199,7 @@ public Knapsack() {
199199
}
200200
}
201201
System.out.printf(
202-
"%s -> (total-weight: %.3f; total-value: %.3f)\n",
202+
"%s -> (total-weight: %.3f; total-value: %.3f)%n",
203203
e.getKey(), totalWeight, e.getValue());
204204
});
205205
g = scores.entrySet().stream()
@@ -212,8 +212,8 @@ public Knapsack() {
212212

213213
final long end = System.nanoTime();
214214

215-
System.out.printf("\n%,d solutions evaluated\n", allSolutions.size());
216-
System.out.printf("Total search time: %.3f seconds\n", (double) (end - beginning) / 1_000_000_000.0);
215+
System.out.printf("%n%,d solutions evaluated%n", allSolutions.size());
216+
System.out.printf("Total search time: %.3f seconds%n", (double) (end - beginning) / 1_000_000_000.0);
217217

218218
if (!ex.isShutdown()) {
219219
ex.shutdown();

examples/src/main/java/com/ledmington/mal/examples/NeuralNetwork.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ public NeuralNetwork() {
236236
.minimize(x -> {
237237
// minimize RMSE
238238
double score = 0.0;
239+
final float[] inputs = new float[2];
240+
float[] outputs;
239241
for (int i = 0; i < nPoints; i++) {
240-
final float[] outputs = x.predict(new float[] {dataset[i].x, dataset[i].y});
242+
inputs[0] = dataset[i].x;
243+
inputs[1] = dataset[i].y;
244+
outputs = x.predict(inputs);
241245
for (int j = 0; j < outputVariables; j++) {
242246
score += (outputs[j] - solutions[i][j]) * (outputs[j] - solutions[i][j]);
243247
}

examples/src/main/java/com/ledmington/mal/examples/RandomStrings.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ enum Choice {
112112
final Set<String> allSolutions = new HashSet<>();
113113

114114
for (int it = 0; it < 100; it++) {
115-
System.out.printf("Run n.%,d\n", it);
115+
System.out.printf("Run n.%,d%n", it);
116116
ga.setState(state.get().firstGeneration(g).build());
117117
ga.run();
118118

@@ -121,7 +121,7 @@ enum Choice {
121121
scores.entrySet().stream()
122122
.sorted(Map.Entry.comparingByValue())
123123
.limit(10)
124-
.forEach(e -> System.out.printf("%s -> %f\n", e.getKey(), e.getValue()));
124+
.forEach(e -> System.out.printf("%s -> %f%n", e.getKey(), e.getValue()));
125125
g = scores.entrySet().stream()
126126
.sorted(Map.Entry.comparingByValue())
127127
.limit(10)
@@ -132,7 +132,7 @@ enum Choice {
132132

133133
final long end = System.nanoTime();
134134

135-
System.out.printf("\n%,d solutions evaluated\n", allSolutions.size());
136-
System.out.printf("Total search time: %.3f seconds\n", (double) (end - beginning) / 1_000_000_000.0);
135+
System.out.printf("%n%,d solutions evaluated%n", allSolutions.size());
136+
System.out.printf("Total search time: %.3f seconds%n", (double) (end - beginning) / 1_000_000_000.0);
137137
}
138138
}

lib/src/main/java/com/ledmington/mal/GeneticAlgorithmConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public GeneticAlgorithmConfigBuilder<X> mutation(final Function<X, X> mutation)
165165
public GeneticAlgorithmConfigBuilder<X> maximize(final Function<X, Double> fitness) {
166166
Objects.requireNonNull(fitness, "The fitness function cannot be null");
167167
fitnessFunction = fitness;
168-
scoreComparator = (a, b) -> -Double.compare(a, b);
168+
scoreComparator = (a, b) -> Double.compare(b, a);
169169
return this;
170170
}
171171

lib/src/main/java/com/ledmington/mal/ParallelGeneticAlgorithm.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public void setState(final GeneticAlgorithmConfig<X> config) {
9292
cachedScores = new ConcurrentHashMap<>(config.populationSize(), 1.0f);
9393
survivingPopulation = (int) ((double) config.populationSize() * config.survivalRate());
9494
bestOfAllTime = new LinkedHashSet<>(survivingPopulation * 2, 1.0f);
95-
startTime = System.currentTimeMillis();
9695
cachedScoresComparator = (a, b) -> config.scoreComparator().compare(cachedScores.get(a), cachedScores.get(b));
9796
tasks = new ArrayList<>(config.populationSize());
9897

lib/src/main/java/com/ledmington/mal/SerialGeneticAlgorithm.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public class SerialGeneticAlgorithm<X> implements GeneticAlgorithm<X> {
3434

3535
protected final RandomGenerator rng;
3636
protected GeneticAlgorithmConfig<X> config = null;
37-
protected long startTime;
3837
private int generation = 0;
3938
protected List<X> population;
4039
protected List<X> nextGeneration;
@@ -75,7 +74,6 @@ public void setState(final GeneticAlgorithmConfig<X> config) {
7574
cachedScores = new HashMap<>(config.populationSize(), 1.0f);
7675
survivingPopulation = (int) ((double) config.populationSize() * config.survivalRate());
7776
bestOfAllTime = new LinkedHashSet<>(survivingPopulation * 2, 1.0f);
78-
startTime = System.currentTimeMillis();
7977
cachedScoresComparator = (a, b) -> config.scoreComparator().compare(cachedScores.get(a), cachedScores.get(b));
8078
}
8179

0 commit comments

Comments
 (0)