Skip to content

Commit

Permalink
Use getTruncatedInt
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Dec 1, 2024
1 parent c22f9b4 commit 6a8f57a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private Algorithm newAGEMOEA(TypedProperties properties, Problem problem) throws
.setMutationOperator(mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -295,7 +295,7 @@ private Algorithm newAGEMOEAII(TypedProperties properties, Problem problem) thro
.setMutationOperator(mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -305,14 +305,14 @@ private Algorithm newAbYSS(TypedProperties properties, Problem problem) throws J
MutationOperator<?> mutation = JMetalFactory.getInstance().createMutationOperator(adapter, properties);

Archive<DoubleSolution> archive = new CrowdingDistanceArchive<DoubleSolution>(
(int)properties.getDouble("archiveSize", 100));
properties.getTruncatedInt("archiveSize", 100));

ABYSSBuilder builder = new ABYSSBuilder(adapter, archive)
.setCrossoverOperator((CrossoverOperator<DoubleSolution>)crossover)
.setMutationOperator((MutationOperator<DoubleSolution>)mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -323,7 +323,7 @@ private Algorithm newCDG(TypedProperties properties, Problem problem) throws JMe
CDGBuilder builder = new CDGBuilder(adapter).setCrossover(crossover);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -333,7 +333,7 @@ private Algorithm newDMOPSO(TypedProperties properties, Problem problem) throws
DMOPSOBuilder builder = new DMOPSOBuilder(adapter);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -345,21 +345,21 @@ private Algorithm newESPEA(TypedProperties properties, Problem problem) throws J
ESPEABuilder builder = new ESPEABuilder(adapter, crossover, mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private Algorithm newFAME(TypedProperties properties, Problem problem) throws JMetalException {
DoubleProblemAdapter adapter = createDoubleProblemAdapter(problem);

FAME algorithm = new FAME(adapter,
(int)properties.getDouble("populationSize", 100),
(int)properties.getDouble("archiveSize", 100),
(int)properties.getDouble("maxEvaluations", 25000),
properties.getTruncatedInt("populationSize", 100),
properties.getTruncatedInt("archiveSize", 100),
properties.getTruncatedInt("maxEvaluations", 25000),
new SpatialSpreadDeviationSelection<>(properties.getInt("numberOfTournaments", 5)),
new SequentialSolutionListEvaluator<>());

return new JMetalAlgorithmAdapter(algorithm, properties, adapter);
return new JMetalAlgorithmAdapter(algorithm, adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -370,7 +370,7 @@ private Algorithm newGDE3(TypedProperties properties, Problem problem) throws JM
GDE3Builder builder = new GDE3Builder(adapter).setCrossover(crossover);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -381,15 +381,15 @@ private Algorithm newGWASFGA(TypedProperties properties, Problem problem) throws
SelectionOperator selection = new BinaryTournamentSelection(new RankingAndCrowdingDistanceComparator());

GWASFGA algorithm = new GWASFGA(adapter,
(int)properties.getDouble("populationSize", 100),
properties.getTruncatedInt("populationSize", 100),
DefaultAlgorithms.getMaxIterations(properties),
crossover,
mutation,
selection,
new SequentialSolutionListEvaluator<DoubleSolution>(),
properties.getDouble("epsilon", 0.01));

return new JMetalAlgorithmAdapter(algorithm, properties, adapter);
return new JMetalAlgorithmAdapter(algorithm, adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -400,14 +400,14 @@ private Algorithm newIBEA(TypedProperties properties, Problem problem) throws JM
SelectionOperator selection = new BinaryTournamentSelection();

IBEA algorithm = new IBEA(adapter,
(int)properties.getDouble("populationSize", 100),
(int)properties.getDouble("archiveSize", 100),
(int)properties.getDouble("maxEvaluations", 25000),
properties.getTruncatedInt("populationSize", 100),
properties.getTruncatedInt("archiveSize", 100),
properties.getTruncatedInt("maxEvaluations", 25000),
selection,
crossover,
mutation);

return new JMetalAlgorithmAdapter(algorithm, properties, adapter);
return new JMetalAlgorithmAdapter(algorithm, adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -420,12 +420,12 @@ private Algorithm newMOCell(TypedProperties properties, Problem problem) throws

CrossoverOperator<?> crossover = JMetalFactory.getInstance().createCrossoverOperator(adapter, properties);
MutationOperator<?> mutation = JMetalFactory.getInstance().createMutationOperator(adapter, properties);
BoundedArchive archive = new CrowdingDistanceArchive((int)properties.getDouble("archiveSize", 100));
BoundedArchive archive = new CrowdingDistanceArchive(properties.getTruncatedInt("archiveSize", 100));

MOCellBuilder builder = new MOCellBuilder(adapter, crossover, mutation).setArchive(archive);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -437,7 +437,7 @@ private Algorithm newMOCHC(TypedProperties properties, Problem problem) throws J

SelectionOperator parentSelection = new RandomSelection<BinarySolution>();
SelectionOperator newGenerationSelection = new RankingAndCrowdingSelection<BinarySolution>(
(int)properties.getDouble("populationSize", 100));
properties.getTruncatedInt("populationSize", 100));

MOCHCBuilder builder = new MOCHCBuilder((BinaryProblemAdapter)adapter)
.setCrossover(crossover)
Expand All @@ -446,7 +446,7 @@ private Algorithm newMOCHC(TypedProperties properties, Problem problem) throws J
.setParentSelection(parentSelection);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -460,7 +460,7 @@ private Algorithm newMOEAD(TypedProperties properties, Problem problem) throws J
MOEADBuilder builder = new MOEADBuilder(adapter, variant).setCrossover(crossover).setMutation(mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -482,7 +482,7 @@ private Algorithm newMOMBI(TypedProperties properties, Problem problem) throws J
new SequentialSolutionListEvaluator<DoubleSolution>(),
properties.getString("pathWeights", null));

return new JMetalAlgorithmAdapter(algorithm, properties, adapter);
return new JMetalAlgorithmAdapter(algorithm, adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -504,7 +504,7 @@ private Algorithm newMOMBI2(TypedProperties properties, Problem problem) throws
new SequentialSolutionListEvaluator<DoubleSolution>(),
properties.getString("pathWeights", null));

return new JMetalAlgorithmAdapter(algorithm, properties, adapter);
return new JMetalAlgorithmAdapter(algorithm, adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -521,13 +521,13 @@ private Algorithm newMOSA(TypedProperties properties, Problem problem) throws JM

MOSA algorithm = new MOSA(initialSolution,
adapter,
(int)properties.getDouble("maxEvaluations", 25000),
properties.getTruncatedInt("maxEvaluations", 25000),
archive,
mutation,
1.0,
new Exponential(0.95));

return new JMetalAlgorithmAdapter(algorithm, properties, adapter);
return new JMetalAlgorithmAdapter(algorithm, adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -537,10 +537,10 @@ private Algorithm newNSGAII(TypedProperties properties, Problem problem) throws
MutationOperator<?> mutation = JMetalFactory.getInstance().createMutationOperator(adapter, properties);

NSGAIIBuilder builder = new NSGAIIBuilder(adapter, crossover, mutation,
(int)properties.getDouble("populationSize", 100));
properties.getTruncatedInt("populationSize", 100));
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -557,7 +557,7 @@ private Algorithm newNSGAIII(TypedProperties properties, Problem problem) throws
.setSelectionOperator(selection);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand Down Expand Up @@ -590,7 +590,7 @@ private Algorithm newOMOPSO(TypedProperties properties, Problem problem) throws
.setEta(properties.getDouble("epsilon", epsilons.get(0)));
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -599,12 +599,12 @@ private Algorithm newPAES(TypedProperties properties, Problem problem) throws JM
MutationOperator<?> mutation = JMetalFactory.getInstance().createMutationOperator(adapter, properties);

PAES paes = new PAES(adapter,
(int)properties.getDouble("maxEvaluations", 25000),
(int)properties.getDouble("archiveSize", 100),
(int)properties.getDouble("bisections", 8),
properties.getTruncatedInt("maxEvaluations", 25000),
properties.getTruncatedInt("archiveSize", 100),
properties.getTruncatedInt("bisections", 8),
mutation);

return new JMetalAlgorithmAdapter(paes, properties, adapter);
return new JMetalAlgorithmAdapter(paes, adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -616,7 +616,7 @@ private Algorithm newPESA2(TypedProperties properties, Problem problem) throws J
PESA2Builder builder = new PESA2Builder(adapter, crossover, mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -632,7 +632,7 @@ private Algorithm newRNSGAII(TypedProperties properties, Problem problem) throws
properties.getDouble("epsilon", 0.01));
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -641,12 +641,12 @@ private Algorithm newSMPSO(TypedProperties properties, Problem problem) throws J
MutationOperator<DoubleSolution> mutation = (MutationOperator<DoubleSolution>)
JMetalFactory.getInstance().createMutationOperator(adapter, properties);

BoundedArchive archive = new CrowdingDistanceArchive((int)properties.getDouble("archiveSize", 100));
BoundedArchive archive = new CrowdingDistanceArchive(properties.getTruncatedInt("archiveSize", 100));

SMPSOBuilder builder = new SMPSOBuilder(adapter, archive).setMutation(mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -658,7 +658,7 @@ private Algorithm newSPEA2(TypedProperties properties, Problem problem) throws J
SPEA2Builder builder = new SPEA2Builder(adapter, crossover, mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -670,7 +670,7 @@ private Algorithm newSMSEMOA(TypedProperties properties, Problem problem) throws
SMSEMOABuilder builder = new SMSEMOABuilder(adapter, crossover, mutation);
loadProperties(properties, builder);

return new JMetalAlgorithmAdapter(builder.build(), properties, adapter);
return new JMetalAlgorithmAdapter(builder.build(), adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -683,7 +683,7 @@ private Algorithm newWASFGA(TypedProperties properties, Problem problem) throws
double[] referencePoint = properties.getDoubleArray("referencePoint", new double[problem.getNumberOfObjectives()]);

WASFGA algorithm = new WASFGA(adapter,
(int)properties.getDouble("populationSize", 100),
properties.getTruncatedInt("populationSize", 100),
DefaultAlgorithms.getMaxIterations(properties),
crossover,
mutation,
Expand All @@ -693,7 +693,7 @@ private Algorithm newWASFGA(TypedProperties properties, Problem problem) throws
DoubleStream.of(referencePoint).boxed().toList(),
properties.getString("weightVectorsFile", ""));

return new JMetalAlgorithmAdapter(algorithm, properties, adapter);
return new JMetalAlgorithmAdapter(algorithm, adapter, properties.getTruncatedInt("maxEvaluations", 25000));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.moeaframework.algorithm.extension.Extension;
import org.moeaframework.algorithm.extension.Extensions;
import org.moeaframework.core.Solution;
import org.moeaframework.core.TypedProperties;
import org.moeaframework.core.population.NondominatedPopulation;
import org.moeaframework.problem.Problem;

Expand All @@ -47,7 +46,7 @@ public class JMetalAlgorithmAdapter<T extends org.uma.jmetal.solution.Solution<?
private final ProblemAdapter<T> problem;

/**
* The max evaluations the algorithm is run.
* The maximum number of function evaluations the algorithm is run.
*/
private final int maxEvaluations;

Expand All @@ -62,17 +61,17 @@ public class JMetalAlgorithmAdapter<T extends org.uma.jmetal.solution.Solution<?
* Constructs an adapter for the specified JMetal algorithm.
*
* @param algorithm the JMetal algorithm
* @param properties the properties used to configure this algorithm
* @param problem the problem adapter
* @param maxEvaluations the maximum number of function evaluations the algorithm is run
*/
public JMetalAlgorithmAdapter(
org.uma.jmetal.algorithm.Algorithm<List<T>> algorithm,
TypedProperties properties,
ProblemAdapter<T> problem) {
ProblemAdapter<T> problem,
int maxEvaluations) {
super();
this.algorithm = algorithm;
this.maxEvaluations = properties.getTruncatedInt("maxEvaluations");
this.problem = problem;
this.maxEvaluations = maxEvaluations;
this.extensions = new JMetalExtensions(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package org.moeaframework.algorithm.jmetal.adapters;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.moeaframework.algorithm.jmetal.mocks.MockPermutationProblem;
Expand Down Expand Up @@ -46,13 +44,13 @@ public void testConvert() {
PermutationSolution<Integer> theirSolution = adapter.createSolution();
Solution mySolution = adapter.convert(theirSolution);

List<Integer> theirPermutation = theirSolution.variables();
int[] theirPermutation = theirSolution.variables().stream().mapToInt(x -> x).toArray();
int[] myPermutation = Permutation.getPermutation(mySolution.getVariable(0));

Assert.assertEquals(theirPermutation.size(), myPermutation.length);
Assert.assertEquals(theirPermutation.length, myPermutation.length);

for (int i = 0; i < theirPermutation.size(); i++) {
Assert.assertEquals((int)theirPermutation.get(i), myPermutation[i]);
for (int i = 0; i < theirPermutation.length; i++) {
Assert.assertEquals(theirPermutation[i], myPermutation[i]);
}
}

Expand Down

0 comments on commit 6a8f57a

Please sign in to comment.