Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 731a9ee

Browse files
committed
Add GDMOEAD class
1 parent f2943ca commit 731a9ee

File tree

5 files changed

+92
-119
lines changed

5 files changed

+92
-119
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Add the following dependency to your `pom.xml`:
2424
<dependency>
2525
<groupId>org.moeaframework</groupId>
2626
<artifactId>gd</artifactId>
27-
<version>1.1</version>
27+
<version>1.2</version>
2828
</dependency>
2929
```
3030

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>org.moeaframework</groupId>
44
<artifactId>gd</artifactId>
5-
<version>1.1</version>
5+
<version>1.2</version>
66
<packaging>jar</packaging>
77

88
<name>Generalized Decomposition</name>

src/main/java/org/moeaframework/gd/Example.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void main(String[] args) {
2828
.withProperty("targets", "pf/DTLZ2.3D.pf")
2929
.runSeeds(50));
3030

31-
analyzer.printAnalysis();
31+
analyzer.display();
3232
}
3333

3434
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.moeaframework.gd;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import org.moeaframework.algorithm.MOEAD;
11+
import org.moeaframework.core.FrameworkException;
12+
import org.moeaframework.core.Population;
13+
import org.moeaframework.core.PopulationIO;
14+
import org.moeaframework.core.Problem;
15+
import org.moeaframework.core.Solution;
16+
import org.moeaframework.core.operator.RandomInitialization;
17+
import org.moeaframework.core.spi.OperatorFactory;
18+
import org.moeaframework.core.variable.RealVariable;
19+
import org.moeaframework.util.io.CommentedLineReader;
20+
import org.moeaframework.util.weights.NormalBoundaryDivisions;
21+
import org.moeaframework.util.weights.NormalBoundaryIntersectionGenerator;
22+
import org.moeaframework.util.weights.WeightGenerator;
23+
24+
public class GDMOEAD extends MOEAD {
25+
26+
public GDMOEAD(Problem problem) {
27+
this(problem, NormalBoundaryDivisions.forProblem(problem));
28+
}
29+
30+
public GDMOEAD(Problem problem, String targets) {
31+
this(problem, new GeneralizedDecomposition(loadTargets(targets)));
32+
}
33+
34+
public GDMOEAD(Problem problem, NormalBoundaryDivisions divisions) {
35+
this(problem,
36+
new GeneralizedDecomposition(new NormalBoundaryIntersectionGenerator(
37+
problem.getNumberOfObjectives(), divisions)));
38+
}
39+
40+
public GDMOEAD(Problem problem, WeightGenerator weightGenerator) {
41+
super(problem,
42+
weightGenerator.size(),
43+
20, //neighborhoodSize
44+
weightGenerator,
45+
new RandomInitialization(problem),
46+
OperatorFactory.getInstance().getVariation(problem.isType(RealVariable.class)? "de+pm": null, problem),
47+
0.9, //delta
48+
2, //eta
49+
-1); //updateUtility
50+
}
51+
52+
private static List<double[]> loadTargets(String resource) {
53+
Population population = null;
54+
55+
try {
56+
File file = new File(resource);
57+
58+
if (file.exists()) {
59+
population = PopulationIO.readObjectives(file);
60+
} else {
61+
try (InputStream input = GDMOEAD.class.getResourceAsStream("/" + resource)) {
62+
if (input != null) {
63+
population = PopulationIO.readObjectives(new CommentedLineReader(new InputStreamReader(input)));
64+
}
65+
}
66+
}
67+
} catch (IOException e) {
68+
throw new FrameworkException("failed to load " + resource, e);
69+
}
70+
71+
if (population == null) {
72+
throw new FrameworkException("could not find " + resource);
73+
}
74+
75+
List<double[]> targets = new ArrayList<double[]>();
76+
77+
for (Solution solution : population) {
78+
targets.add(solution.getObjectives());
79+
}
80+
81+
return targets;
82+
}
83+
84+
}
Lines changed: 5 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
11
package org.moeaframework.gd;
22

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.io.InputStreamReader;
7-
import java.util.ArrayList;
8-
import java.util.List;
9-
10-
import org.moeaframework.algorithm.MOEAD;
113
import org.moeaframework.core.Algorithm;
12-
import org.moeaframework.core.FrameworkException;
13-
import org.moeaframework.core.Initialization;
14-
import org.moeaframework.core.Population;
15-
import org.moeaframework.core.PopulationIO;
164
import org.moeaframework.core.Problem;
17-
import org.moeaframework.core.Solution;
18-
import org.moeaframework.core.Variable;
19-
import org.moeaframework.core.Variation;
20-
import org.moeaframework.core.operator.RandomInitialization;
21-
import org.moeaframework.core.spi.OperatorFactory;
225
import org.moeaframework.core.spi.RegisteredAlgorithmProvider;
23-
import org.moeaframework.core.variable.RealVariable;
246
import org.moeaframework.util.TypedProperties;
25-
import org.moeaframework.util.io.CommentedLineReader;
267
import org.moeaframework.util.weights.NormalBoundaryDivisions;
27-
import org.moeaframework.util.weights.NormalBoundaryIntersectionGenerator;
28-
import org.moeaframework.util.weights.WeightGenerator;
298

309
public class GDMOEADProvider extends RegisteredAlgorithmProvider {
3110

@@ -36,106 +15,16 @@ public GDMOEADProvider() {
3615
}
3716

3817
private Algorithm newGDMOEAD(TypedProperties properties, Problem problem) {
39-
//provide weights
40-
WeightGenerator weightGenerator = null;
18+
GDMOEAD algorithm;
4119

4220
if (properties.contains("targets")) {
43-
weightGenerator = new GeneralizedDecomposition(loadTargets(properties.getString("targets", null)));
21+
algorithm = new GDMOEAD(problem, properties.getString("targets"));
4422
} else {
45-
NormalBoundaryDivisions divisions = NormalBoundaryDivisions.fromProperties(properties, problem);
46-
47-
weightGenerator = new GeneralizedDecomposition(new NormalBoundaryIntersectionGenerator(
48-
problem.getNumberOfObjectives(), divisions));
49-
}
50-
51-
int populationSize = weightGenerator.size();
52-
53-
//enforce population size lower bound
54-
if (populationSize < problem.getNumberOfObjectives()) {
55-
System.err.println("increasing MOEA/D population size");
56-
populationSize = problem.getNumberOfObjectives();
57-
}
58-
59-
Initialization initialization = new RandomInitialization(problem);
60-
61-
//default to de+pm for real-encodings
62-
String operator = properties.getString("operator", null);
63-
64-
if ((operator == null) && checkType(RealVariable.class, problem)) {
65-
operator = "de+pm";
66-
}
67-
68-
Variation variation = OperatorFactory.getInstance().getVariation(operator, properties, problem);
69-
70-
int neighborhoodSize = 20;
71-
int eta = 2;
72-
73-
if (properties.contains("neighborhoodSize")) {
74-
neighborhoodSize = Math.max(2,
75-
(int)(properties.getDouble("neighborhoodSize", 0.1) * populationSize));
76-
}
77-
78-
if (neighborhoodSize > populationSize) {
79-
neighborhoodSize = populationSize;
80-
}
81-
82-
if (properties.contains("eta")) {
83-
eta = Math.max(2, (int)(properties.getDouble("eta", 0.01) * populationSize));
84-
}
85-
86-
return new MOEAD(
87-
problem,
88-
populationSize,
89-
neighborhoodSize,
90-
weightGenerator,
91-
initialization,
92-
variation,
93-
properties.getDouble("delta", 0.9),
94-
eta,
95-
(int)properties.getDouble("updateUtility", -1));
96-
}
97-
98-
private List<double[]> loadTargets(String resource) {
99-
File file = new File(resource);
100-
Population population = null;
101-
102-
try {
103-
if (file.exists()) {
104-
population = PopulationIO.readObjectives(file);
105-
} else {
106-
try (InputStream input = getClass().getResourceAsStream("/" + resource)) {
107-
if (input != null) {
108-
population = PopulationIO.readObjectives(new CommentedLineReader(new InputStreamReader(input)));
109-
}
110-
}
111-
}
112-
} catch (IOException e) {
113-
throw new FrameworkException("failed to load " + resource, e);
114-
}
115-
116-
if (population == null) {
117-
throw new FrameworkException("could not find " + resource);
118-
}
119-
120-
List<double[]> targets = new ArrayList<double[]>();
121-
122-
for (Solution solution : population) {
123-
targets.add(solution.getObjectives());
124-
}
125-
126-
return targets;
127-
}
128-
129-
private boolean checkType(Class<? extends Variable> type, Problem problem) {
130-
Solution solution = problem.newSolution();
131-
132-
for (int i=0; i<solution.getNumberOfVariables(); i++) {
133-
if (!type.isInstance(solution.getVariable(i))) {
134-
return false;
135-
}
23+
algorithm = new GDMOEAD(problem, NormalBoundaryDivisions.fromProperties(properties, problem));
13624
}
13725

138-
return true;
26+
algorithm.applyConfiguration(properties);
27+
return algorithm;
13928
}
14029

14130
}

0 commit comments

Comments
 (0)