Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Add GDMOEAD class
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Dec 9, 2022
1 parent f2943ca commit 731a9ee
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 119 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Add the following dependency to your `pom.xml`:
<dependency>
<groupId>org.moeaframework</groupId>
<artifactId>gd</artifactId>
<version>1.1</version>
<version>1.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.moeaframework</groupId>
<artifactId>gd</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>

<name>Generalized Decomposition</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/moeaframework/gd/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void main(String[] args) {
.withProperty("targets", "pf/DTLZ2.3D.pf")
.runSeeds(50));

analyzer.printAnalysis();
analyzer.display();
}

}
84 changes: 84 additions & 0 deletions src/main/java/org/moeaframework/gd/GDMOEAD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.moeaframework.gd;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.moeaframework.algorithm.MOEAD;
import org.moeaframework.core.FrameworkException;
import org.moeaframework.core.Population;
import org.moeaframework.core.PopulationIO;
import org.moeaframework.core.Problem;
import org.moeaframework.core.Solution;
import org.moeaframework.core.operator.RandomInitialization;
import org.moeaframework.core.spi.OperatorFactory;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.util.io.CommentedLineReader;
import org.moeaframework.util.weights.NormalBoundaryDivisions;
import org.moeaframework.util.weights.NormalBoundaryIntersectionGenerator;
import org.moeaframework.util.weights.WeightGenerator;

public class GDMOEAD extends MOEAD {

public GDMOEAD(Problem problem) {
this(problem, NormalBoundaryDivisions.forProblem(problem));
}

public GDMOEAD(Problem problem, String targets) {
this(problem, new GeneralizedDecomposition(loadTargets(targets)));
}

public GDMOEAD(Problem problem, NormalBoundaryDivisions divisions) {
this(problem,
new GeneralizedDecomposition(new NormalBoundaryIntersectionGenerator(
problem.getNumberOfObjectives(), divisions)));
}

public GDMOEAD(Problem problem, WeightGenerator weightGenerator) {
super(problem,
weightGenerator.size(),
20, //neighborhoodSize
weightGenerator,
new RandomInitialization(problem),
OperatorFactory.getInstance().getVariation(problem.isType(RealVariable.class)? "de+pm": null, problem),
0.9, //delta
2, //eta
-1); //updateUtility
}

private static List<double[]> loadTargets(String resource) {
Population population = null;

try {
File file = new File(resource);

if (file.exists()) {
population = PopulationIO.readObjectives(file);
} else {
try (InputStream input = GDMOEAD.class.getResourceAsStream("/" + resource)) {
if (input != null) {
population = PopulationIO.readObjectives(new CommentedLineReader(new InputStreamReader(input)));
}
}
}
} catch (IOException e) {
throw new FrameworkException("failed to load " + resource, e);
}

if (population == null) {
throw new FrameworkException("could not find " + resource);
}

List<double[]> targets = new ArrayList<double[]>();

for (Solution solution : population) {
targets.add(solution.getObjectives());
}

return targets;
}

}
121 changes: 5 additions & 116 deletions src/main/java/org/moeaframework/gd/GDMOEADProvider.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
package org.moeaframework.gd;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.moeaframework.algorithm.MOEAD;
import org.moeaframework.core.Algorithm;
import org.moeaframework.core.FrameworkException;
import org.moeaframework.core.Initialization;
import org.moeaframework.core.Population;
import org.moeaframework.core.PopulationIO;
import org.moeaframework.core.Problem;
import org.moeaframework.core.Solution;
import org.moeaframework.core.Variable;
import org.moeaframework.core.Variation;
import org.moeaframework.core.operator.RandomInitialization;
import org.moeaframework.core.spi.OperatorFactory;
import org.moeaframework.core.spi.RegisteredAlgorithmProvider;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.util.TypedProperties;
import org.moeaframework.util.io.CommentedLineReader;
import org.moeaframework.util.weights.NormalBoundaryDivisions;
import org.moeaframework.util.weights.NormalBoundaryIntersectionGenerator;
import org.moeaframework.util.weights.WeightGenerator;

public class GDMOEADProvider extends RegisteredAlgorithmProvider {

Expand All @@ -36,106 +15,16 @@ public GDMOEADProvider() {
}

private Algorithm newGDMOEAD(TypedProperties properties, Problem problem) {
//provide weights
WeightGenerator weightGenerator = null;
GDMOEAD algorithm;

if (properties.contains("targets")) {
weightGenerator = new GeneralizedDecomposition(loadTargets(properties.getString("targets", null)));
algorithm = new GDMOEAD(problem, properties.getString("targets"));
} else {
NormalBoundaryDivisions divisions = NormalBoundaryDivisions.fromProperties(properties, problem);

weightGenerator = new GeneralizedDecomposition(new NormalBoundaryIntersectionGenerator(
problem.getNumberOfObjectives(), divisions));
}

int populationSize = weightGenerator.size();

//enforce population size lower bound
if (populationSize < problem.getNumberOfObjectives()) {
System.err.println("increasing MOEA/D population size");
populationSize = problem.getNumberOfObjectives();
}

Initialization initialization = new RandomInitialization(problem);

//default to de+pm for real-encodings
String operator = properties.getString("operator", null);

if ((operator == null) && checkType(RealVariable.class, problem)) {
operator = "de+pm";
}

Variation variation = OperatorFactory.getInstance().getVariation(operator, properties, problem);

int neighborhoodSize = 20;
int eta = 2;

if (properties.contains("neighborhoodSize")) {
neighborhoodSize = Math.max(2,
(int)(properties.getDouble("neighborhoodSize", 0.1) * populationSize));
}

if (neighborhoodSize > populationSize) {
neighborhoodSize = populationSize;
}

if (properties.contains("eta")) {
eta = Math.max(2, (int)(properties.getDouble("eta", 0.01) * populationSize));
}

return new MOEAD(
problem,
populationSize,
neighborhoodSize,
weightGenerator,
initialization,
variation,
properties.getDouble("delta", 0.9),
eta,
(int)properties.getDouble("updateUtility", -1));
}

private List<double[]> loadTargets(String resource) {
File file = new File(resource);
Population population = null;

try {
if (file.exists()) {
population = PopulationIO.readObjectives(file);
} else {
try (InputStream input = getClass().getResourceAsStream("/" + resource)) {
if (input != null) {
population = PopulationIO.readObjectives(new CommentedLineReader(new InputStreamReader(input)));
}
}
}
} catch (IOException e) {
throw new FrameworkException("failed to load " + resource, e);
}

if (population == null) {
throw new FrameworkException("could not find " + resource);
}

List<double[]> targets = new ArrayList<double[]>();

for (Solution solution : population) {
targets.add(solution.getObjectives());
}

return targets;
}

private boolean checkType(Class<? extends Variable> type, Problem problem) {
Solution solution = problem.newSolution();

for (int i=0; i<solution.getNumberOfVariables(); i++) {
if (!type.isInstance(solution.getVariable(i))) {
return false;
}
algorithm = new GDMOEAD(problem, NormalBoundaryDivisions.fromProperties(properties, problem));
}

return true;
algorithm.applyConfiguration(properties);
return algorithm;
}

}

0 comments on commit 731a9ee

Please sign in to comment.