11package 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 ;
113import 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 ;
164import 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 ;
225import org .moeaframework .core .spi .RegisteredAlgorithmProvider ;
23- import org .moeaframework .core .variable .RealVariable ;
246import org .moeaframework .util .TypedProperties ;
25- import org .moeaframework .util .io .CommentedLineReader ;
267import org .moeaframework .util .weights .NormalBoundaryDivisions ;
27- import org .moeaframework .util .weights .NormalBoundaryIntersectionGenerator ;
28- import org .moeaframework .util .weights .WeightGenerator ;
298
309public 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