1. 程式人生 > >jMetal使用教程(2)

jMetal使用教程(2)

上一節介紹了jMetal的大致框架結構,本節貼一個完整的例子。

優化問題

假定我們需要對這樣的問題進行優化:

也就是多目標優化測試集ZDT1函式。

這個優化問題有30個決策變數,2個優化目標函式(最小化)。決策變數的定義域相同,都是[0,1]的實數。目標函式也是實數型別。

我們決定採用NSGA-II演算法對這個優化問題進行建模求解,選擇運算元採用二元錦標賽選擇法,交叉運算元用SBXcrossover,變異運算元用SimpleRandomMutation(隨機變異)。

配置演算法

定義Problem

既然定義域和目標函式都是實數域的,那我們只要對AbstractDoubleProblem類進行實現就行了。

  1. public class ZDT1 extends AbstractDoubleProblem {
  2. /** Constructor. Creates default instance of problem ZDT1 (30 decision variables) */
  3. public ZDT1() {
  4. this(30);
  5. }
  6. /**
  7. * Creates a new instance of problem ZDT1.
  8. *
  9. * @param numberOfVariables Number of variables.
  10. */
  11. public ZDT1(Integer numberOfVariables)
    {
  12. setNumberOfVariables(numberOfVariables);//設定決策變數個數,30
  13. setNumberOfObjectives(2);//設定優化目標函式個數,2
  14. setName("ZDT1");//給這個問題起名,ZDT1.
  15. List<Double> lowerLimit = new ArrayList<>(getNumberOfVariables()) ;
  16. List<Double> upperLimit = new ArrayList<>(getNumberOfVariables()) ;
  17. //設定定義域
  18. for (int i = 0; i < getNumberOfVariables(); i++) {
  19. lowerLimit.add(0.0);
  20. upperLimit.add(1.0);
  21. }
  22. setLowerLimit(lowerLimit);
  23. setUpperLimit(upperLimit);
  24. }
  25. //這裡就是優化目標函式的實現過程,Algorithm.evlataionPopulation()會呼叫這個方法
  26. /** Evaluate() method */
  27. public void evaluate(DoubleSolution solution) {
  28. double[] f = new double[getNumberOfObjectives()];
  29. f[0] = solution.getVariableValue(0);
  30. double g = this.evalG(solution);
  31. double h = this.evalH(f[0], g);
  32. f[1] = h * g;
  33. solution.setObjective(0, f[0]);
  34. solution.setObjective(1, f[1]);
  35. }
  36. /**
  37. * Returns the value of the ZDT1 function G.
  38. *
  39. * @param solution Solution
  40. */
  41. private double evalG(DoubleSolution solution) {
  42. double g = 0.0;
  43. for (int i = 1; i < solution.getNumberOfVariables(); i++) {
  44. g += solution.getVariableValue(i);
  45. }
  46. double constant = 9.0 / (solution.getNumberOfVariables() - 1);
  47. g = constant * g;
  48. g = g + 1.0;
  49. return g;
  50. }
  51. /**
  52. * Returns the value of the ZDT1 function H.
  53. *
  54. * @param f First argument of the function H.
  55. * @param g Second argument of the function H.
  56. */
  57. public double evalH(double f, double g) {
  58. double h ;
  59. h = 1.0 - Math.sqrt(f / g);
  60. return h;
  61. }
  62. }

上面是包裡面的ZDT1原始碼。

定義Solution

決策變數不復雜,完全可以使用開源包提供的DefaultBinarySolution。

  1. public class DefaultDoubleSolution
  2. extends AbstractGenericSolution<Double, DoubleProblem>
  3. implements DoubleSolution {
  4. /** Constructor */
  5. public DefaultDoubleSolution(DoubleProblem problem) {
  6. super(problem) ;//會在父類AbstractGenericSolution裡設定決策變數和優化目標函式空間,這裡便是30和2.
  7. initializeDoubleVariables();//隨機初始化決策變數
  8. initializeObjectiveValues();//優化目標函式設為0,0
  9. }
  10. /** Copy constructor */
  11. public DefaultDoubleSolution(DefaultDoubleSolution solution) {
  12. super(solution.problem) ;
  13. for (int i = 0; i < problem.getNumberOfVariables(); i++) {
  14. setVariableValue(i, solution.getVariableValue(i));
  15. }
  16. for (int i = 0; i < problem.getNumberOfObjectives(); i++) {
  17. setObjective(i, solution.getObjective(i)) ;
  18. }
  19. attributes = new HashMap<Object, Object>(solution.attributes) ;
  20. }
  21. @Override
  22. public Double getUpperBound(int index) {
  23. return problem.getUpperBound(index);
  24. }
  25. @Override
  26. public Double getLowerBound(int index) {
  27. return problem.getLowerBound(index) ;
  28. }
  29. @Override
  30. public DefaultDoubleSolution copy() {
  31. return new DefaultDoubleSolution(this);
  32. }
  33. @Override
  34. public String getVariableValueString(int index) {
  35. return getVariableValue(index).toString() ;
  36. }
  37. private void initializeDoubleVariables() {
  38. for (int i = 0 ; i < problem.getNumberOfVariables(); i++) {
  39. Double value = randomGenerator.nextDouble(getLowerBound(i), getUpperBound(i)) ;
  40. setVariableValue(i, value) ;
  41. }
  42. }
  43. }

定義Operator

開源包的常用的操作運算元都有,我們只要把需要的提供給Algorithm就可以了。

配置Algorithm

  1. import org.uma.jmetal.algorithm.Algorithm;
  2. import org.uma.jmetal.algorithm.multiobjective.nsgaii.NSGAIIBuilder;
  3. import org.uma.jmetal.operator.CrossoverOperator;
  4. import org.uma.jmetal.operator.MutationOperator;
  5. import org.uma.jmetal.operator.SelectionOperator;
  6. import org.uma.jmetal.operator.impl.crossover.SBXCrossover;
  7. import org.uma.jmetal.operator.impl.mutation.SimpleRandomMutation;
  8. import org.uma.jmetal.operator.impl.selection.BinaryTournamentSelection;
  9. import org.uma.jmetal.problem.Problem;
  10. import org.uma.jmetal.problem.multiobjective.zdt.ZDT1;
  11. import org.uma.jmetal.solution.DoubleSolution;
  12. import org.uma.jmetal.util.AlgorithmRunner;
  13. import org.uma.jmetal.util.JMetalLogger;
  14. import org.uma.jmetal.util.comparator.RankingAndCrowdingDistanceComparator;
  15. import java.io.FileNotFoundException;
  16. import java.util.List;
  17. import static org.uma.jmetal.runner.AbstractAlgorithmRunner.printFinalSolutionSet;
  18. import static org.uma.jmetal.runner.AbstractAlgorithmRunner.printQualityIndicators;
  19. /**
  20. * Solve ZDT1 with NSGA-II on jMetal
  21. */
  22. public class ZDT1_jMetal {
  23. public static void main(String[] args) throws FileNotFoundException {
  24. Problem<DoubleSolution> problem;
  25. Algorithm<List<DoubleSolution>> algorithm;
  26. CrossoverOperator<DoubleSolution> crossover;
  27. MutationOperator<DoubleSolution> mutation;
  28. SelectionOperator<List<DoubleSolution>, DoubleSolution> selection;
  29. String referenceParetoFront = "";
  30. //定義優化問題
  31. problem = new ZDT1();
  32. //配置SBX交叉運算元
  33. double crossoverProbability = 0.9;
  34. double crossoverDistributionIndex = 20.0;
  35. crossover = new SBXCrossover(crossoverProbability, crossoverDistributionIndex);
  36. //配置變異運算元
  37. double mutationProbability = 1.0 / problem.getNumberOfVariables();
  38. // double mutationDistributionIndex = 20.0 ;
  39. mutation = new SimpleRandomMutation(mutationProbability);
  40. //配置選擇運算元
  41. selection = new BinaryTournamentSelection<DoubleSolution>(
  42. new RankingAndCrowdingDistanceComparator<DoubleSolution>());
  43. //將元件註冊到algorithm
  44. algorithm = new NSGAIIBuilder<DoubleSolution>(problem, crossover, mutation)
  45. .setSelectionOperator(selection)
  46. .setMaxEvaluations(25000)
  47. .setPopulationSize(100)
  48. .build();
  49. /* 或者用這樣的方法註冊一個演算法
  50. evaluator = new SequentialSolutionListEvaluator<DoubleSolution>();
  51. algorithm = new NSGAII<DoubleSolution>(problem, 25000, 100, crossover,
  52. mutation, selection, evaluator);
  53. */
  54. //用AlgorithmRunner執行演算法
  55. AlgorithmRunner algorithmRunner = new AlgorithmRunner.Executor(algorithm).execute();
  56. //獲取結果集
  57. List<DoubleSolution> population = algorithm.getResult();
  58. long computingTime = algorithmRunner.getComputingTime();
  59. JMetalLogger.logger.info("Total execution time: " + computingTime + "ms");
  60. //將全部種群列印到檔案中
  61. printFinalSolutionSet(population);
  62. if (!referenceParetoFront.equals("")) printQualityIndicators(population, referenceParetoFront);
  63. }
  64. }

可能有人會問DefaultDoubleSolution是在哪裡註冊進演算法的,答案是在類AbstractDoubleProblem裡,因為ZDT1 extends AbstractDoubleProblem,而AbstractDoubleProblem override了createSolution()方法。

  1. @Override
  2. public DoubleSolution createSolution() {
  3. return new DefaultDoubleSolution(this) ;
  4. }

這樣便把DefaultDoubleSolution註冊進行了演算法。

整個演算法的全部元件也就註冊完畢,可以用AlgorithmRunner跑演算法了。

執行結束後,資料夾下有兩個檔案:FUN.tsv以及VAR.tsv,分別是種群中每個個體的目標函式值以及變數取值。

FUN.tsv:

  1. 0.9991565401450556 0.033138122686883334
  2. 2.1067790428441063E-8 1.0595872968618374
  3. 0.9991565401450556 0.033138122686883334
  4. 0.0030874146922019036 1.0077962135879674
  5. 0.37208796983749715 0.4376249588422198
  6. 0.012235830807169515 0.9449966538390078
  7. 0.904673999160248 0.08738808177979915
  8. 0.8782179346960335 0.09748359378144718
  9. 0.5431994204942634 0.30592147506205464
  10. 0.9169218831064787 0.07802500987158069
  11. 9.468202994405428E-4 1.0379086786786225
  12. ...

VAR.tsv:

  1. 0.9991565401450556 0.00794252537669871 0.005488981392981174 0.005967005140972121 0.01162606289274464 0.002201447846900849 9.94537425543528E-5 0.029018625966103245 6.246349761912435E-4 0.012411458377153944 6.13041394387238E-4 0.006672893568571068 0.004683245361043199 0.0060270800004386255 0.02092406367804804 0.006027912828149411 0.008141486757360285 0.005252329949473475 0.013588708050315333 4.75250834980327E-4 0.004877702130228007 0.00865616423126847 0.007134221657092997 5.82125403093642E-4 0.00218258306150891 0.002637239173984704 0.00470701459937204 0.003447998437515465 0.025117498398781753 3.858761644549365E-4
  2. 2.1067790428441063E-8 0.0013449663939222802 0.005567610317640469 0.005954896771763153 0.011208137141151507 0.0022871789133387587 2.5533956835596477E-4 0.0289730300686882 0.0011161183476621267 0.012088625407047743 4.853248844326898E-4 0.005576530486173516 0.0044146222306917 0.005591849962373359 0.023510812594118094 0.00559698303998508 0.008526548202102548 0.004978541310143319 0.013456059721873422 2.1599275436113581E-4 6.464262956128893E-4 0.008659442671210112 0.007455323262755572 5.330734255766625E-4 0.0022947851159189434 0.0030778464130739574 0.004314818022123099 0.001781848589289457 0.021977664906484087 5.945794636948833E-4
  3. 0.9991565401450556 0.00794252537669871 0.005488981392981174 0.005967005140972121 0.01162606289274464 0.002201447846900849 9.94537425543528E-5 0.029018625966103245 6.246349761912435E-4 0.012411458377153944 6.13041394387238E-4 0.006672893568571068 0.004683245361043199 0.0060270800004386255 0.02092406367804804 0.006027912828149411 0.008141486757360285 0.005252329949473475 0.013588708050315333 4.75250834980327E-4 0.004877702130228007 0.00865616423126847 0.007134221657092997 5.82125403093642E-4 0.00218258306150891 0.002637239173984704 0.00470701459937204 0.003447998437515465 0.025117498398781753 3.858761644549365E-4
  4. 0.0030874146922019036 0.0013365539852478229 0.005182909391226278 0.005924058411554409 0.01118229152772865 0.0023366965932723107 6.324521846327583E-4 0.02902028721867136 0.0011174873826591525 0.01607317788065011 6.077775832455531E-4 0.006609063533908074 0.00472212763605761 0.005586115559330742 0.022093069920163133 0.006032975289233088 0.014177472883379724 0.0052542905516060915 0.013637172738984167 2.734608072910316E-4 0.0029789664575808625 0.008723311209515585 0.006044781487938784 0.0013550275939853108 0.002301784894338042 0.003094401868258896 0.00424763759778414 0.006162891640480703 0.02260147162700153 5.921644622272565E-4
  5. 0.37208796983749715 0.007932549826772503 0.005733838000832022 0.0061354083927397255 0.011604533262840262 0.002057314894585316 0.0010022101119812942 0.02989733667311966 6.08282024185579E-4 0.012397126776630542 5.998275335958403E-4 0.01506087655460484 0.00464291351178865 0.0062300516654778156 0.022091445462216315 0.006034719138346714 0.008774192194392884 0.005141814397898589 0.013523660901499517 2.4368083649250132E-4 0.005086534568764959 0.00876978780529207 0.0061817493730077135 3.773946149720617E-4 0.0022826316241897696 0.0026309829327736903 0.0043080146273272845 0.004863767090151326 0.022041285378979106 0.002919760394030697
  6. 0.012235830807169515 8.125951402240999E-4 0.0050749586887912515 0.005970026173914942 0.011124075536426553 0.002255533872477719 2.3804311416268848E-4 0.02896806187268283 0.001178737141893214 0.012889284788012045 0.001146718928935113 0.006684571308397864 0.004701577104002668 0.006145095155351619 0.009724710443257225 0.005958323414437711 0.008857307647993846 0.005005266186932944 0.013775188791515004 4.986577741944896E-4 0.0012484367533888785 0.008655578390633521 0.006356590677832672 0.002476110520203009 0.0037830608116732195 0.0013633927705415851 0.004291824785684132 0.004911922026652875 0.025117218984693157 3.150747401875623E-4
  7. 0.904673999160248 0.00795295101934479 0.004668491628057082 0.005965422404070663 0.010626120447424963 0.0018483651646261116 1.0211366264457851E-4 0.029031421890728742 0.001213671378936406 0.012512256675432692 6.128517111181102E-4 0.015087440116505575 0.004740608112462564 0.0055412868961014165 0.02094905360517965 0.006027669409611225 0.008384542334256154 0.005253642628464492 0.013586813701144918 0.004404204800487962 0.004886217079598717 0.011257629400441088 0.007164707950615741 4.574943191636639E-4 0.0021761859274254847 0.014838541276046113 0.004684293032704989 0.0036265152774918536 0.025032963402199793 4.252820662178495E-4
  8. 0.8782179346960335 0.0016041708744499602 0.006899999702285518 0.00596999474056966 0.011088167521215015 0.002414424964104206 0.0013525153329467644 0.03008160193028079 0.001191286365730821 0.013034967872517633 0.0011680907731348967 0.00663808869634212 0.004778598593081433 0.006288306113962981 0.02093409398141728 0.006034864244001367 0.008904099721092313 0.005508775770161228 0.01364664361798086 0.0014072719221244947 0.0018549389516085008 0.00866606529745734 0.00632902496958632 4.3252550983526896E-4 0.003591273575747169 0.0038914114011958235 0.00467832727817725 0.0035085180247624317 0.02227928299366481 0.0028692334726116804
  9. 0.5431994204942634 0.00812864056228683 0.0054381362608871 0.00498397304793199 0.01163767694734597 0.002406401968957903 9.361500782183961E-4 0.028992472660711033 4.280811187147705E-4 0.012521274567758417 4.8304214654408366E-4 0.007111109599035777 0.0044435470772817365 0.006034656565301625 0.02210510168861987 0.0060382383279277965 0.014059265672495021 0.005262244892028347 0.0145231048082589 2.3815913505616292E-4 0.004018610517012443 0.008982714988567425 0.007162729211694236 4.6775328230258333E-4 0.0038205971791198216 0.0028941264828776193 0.004715670709902365 0.004113310760342626 0.022015405766954235 0.003089015596881602
  10. 0.9169218831064787 0.006873197457623489 0.00447774875980394 0.005954202128148086 0.011576360276168318 0.006896467448871528 9.206732369738643E-4 0.02897264139541762 5.598003243183494E-4 0.01237036281596434 4.6634548218446635E-4 0.015021961148446691 0.004757679638463736 0.005585230975314136 0.020916036471700053 0.005834251076688314 0.009084933278510208 0.005272191124850147 0.0134324998468881 6.047881665027912E-4 0.002957451465274495 0.008499051927838732 0.007123894173680034 0.002112191438272757 0.002769627956314701 0.002471389201318404 0.0042890598339392245 0.003901969367837922 0.022247904479982455 8.015950795629669E-4
  11. 9.468202994405428E-4 0.008108147862655369 0.005583740318491689 0.005946454839843732 0.011164232647664014 0.002286774670967257 1.1149365314788023E-4 0.028973009754609456 0.0011721179276656145 0.012385508343827754 3.9589275921711405E-4 0.014830479357325774 0.004406450685528179 0.0049941145573371105 0.02177121944593872 0.005609038391525347 0.00844751082128077 0.0049769935425854304 0.013462266883788203 2.3996073800178056E-4 6.415175339798465E-4 0.008758940923228804 0.006348655136129133 0.0014859936779251556 0.002253622341351854 0.019780768066960823 0.004260788287923102 0.001446761508704708 0.021921147218730886 0.0029345747646658715
  12. 0.01881114602498743 0.007950642093694135 0.005488981392981174 0.005966960159766708 0.011167300795306003 0.002181038745644766 7.410690428433743E-5 0.029012592469730494 0.003946689261948498 0.012325150399621565 3.71494052081239E-4 0.014776692391525034 0.00475905650450145 0.005997136771136709 0.022168669550171185 0.005664564121655441 0.00936513900739629 0.004982238230485714 0.013584475258313973 1.6149304723474316E-4 8.903025893192045E-4 0.00865639451948499 0.0071707157124095595 6.292935575611989E-4 0.0022541207125643956 0.002637239173984704 0.004714748023095962 0.003468929720545867 0.02505763572340817 0.0029598391320594573
  13. 0.23298499893079222 0.0016257135964861228 0.005152513731803023 0.005971223451951964 0.013022620080819674 0.0022741786008502523 2.54873713356231E-4 0.029021059392691468 6.457452430218996E-4 0.01198914807877857 4.714804591001599E-4 0.005437365608112113 0.004408468955113647 0.00558455308391179 0.022058037793348712 0.006037399830304305 0.0085623704760998 0.005273431769422143 0.013741960971672398 0.004770808818953412 6.23699456592703E-4 0.008743518199645634 0.007113945292034585 0.002483368688865986 0.002730915065464896 0.002197458924763655 0.0042855016145770305 0.0034872616532188806 0.022200447971782188 0.0029203348635731853
  14. 0.33795832387903835 0.0013878670101924624 0.0055012503483938355 0.005958291972440601 0.012914031689822733 0.007138961596017977 3.84525222857473E-4 0.029840209969010784 0.0011032732823058758 0.01283281375895352 4.8202285623741715E-4 0.014957296596785033 0.004408204202206455 0.005001675240016523 0.02206280627496202 0.006022449348261073 0.01870705955357888 0.005124585663793871 0.01357482811478876 0.004016850242940491 0.0028816735759766903 0.008982900408717218 0.006108748845230449 4.8987491496662E-4 0.0023523795052291237 0.0027156586238228945 0.004296059457185933 0.0033134144697818816 0.022108790733905877 0.002949604185074081
  15. 0.4945505347724345 0.008110055817903682 0.00542073529197596 0.004921465130614271 0.005196867991197382 0.0023591958295319845 9.297511701872295E-4 0.029021046828295295 4.580314575888718E-4 0.012336993098472863 3.3578423302317443E-4 0.006431969059674306 0.004726765961830731 0.0055315518363454885 0.022076739525782676 0.005591527738462926 0.013957200637348632 0.005506438608579756 0.013181024528799675 2.673496983465554E-4 0.004012632628060874 0.008799703034390628 0.0071225117567952 0.002247042705390879 0.003755134298478031 0.0026688968989695035 0.004324543168471341 0.004157400247394643 0.022152291218177262 5.158377860154952E-4
  16. 0.5616446249215836 0.001332180905821624 0.005655272194585227 0.005968019488366946 0.011571283469224354 0.006891705356197903 2.245200277602782E-4 0.028974273057526836 0.0011294908731750352 0.011696370199975221 0.0010875099129613733 0.006583585298019992 0.004412862909391745 0.006027765055281159 0.021038488585787704 0.00626320650333361 0.013641898210769634 0.004829241691886474 0.013574885614491496 4.908691193604029E-4 0.0021466268588485483 0.008986294882455328 0.005986254921579244 0.002148578218728449 0.003633975416714848 0.015335827444375675 0.004276883749172991 0.0036772290963547824 0.022124858462572203 0.0027092539305345028
  17. 0.1357838304237608 0.0012656456037606007 0.0069005060777872235 0.005954655120669166 0.011208558910543785 0.0024160739252773065 2.0281725708336566E-4 0.02898747166913942 0.001132392022993191 0.012517098440548102 0.0010141382926396824 0.013765693996134802 0.004713277852764419 0.006383591227011038 0.022099275370331437 0.005597872579773948 0.014521025136041603 0.004832002896453246 0.01357872488190142 4.899588499504312E-4 2.3574371555969763E-4 0.00897270251174062 0.007089457465681361 5.174000474507623E-4 0.0037066112913601047 0.02031417090275161 0.004682521407157776 0.006244701161205608 0.025124929406302836 5.501911428790443E-4
  18. 0.5171866527435598 0.008122573167751591 0.004739832497325629 0.004992906296453595 0.005196867991197382 0.002029563171327076 9.361500782183961E-4