上一节介绍了jMetal的大致框架结构,本节贴一个完整的例子。
优化问题
假定我们需要对这样的问题进行优化:
也就是多目标优化测试集ZDT1函数。
这个优化问题有30个决策变量,2个优化目标函数(最小化)。决策变量的定义域相同,都是[0,1]的实数。目标函数也是实数类型。
我们决定采用NSGA-II算法对这个优化问题进行建模求解,选择算子采用二元锦标赛选择法,交叉算子用SBXcrossover,变异算子用SimpleRandomMutation(随机变异)。
配置算法
定义Problem
既然定义域和目标函数都是实数域的,那我们只要对AbstractDoubleProblem类进行实现就行了。
public class ZDT1 extends AbstractDoubleProblem { /** Constructor. Creates default instance of problem ZDT1 (30 decision variables) */ public ZDT1() { this(30); } /** * Creates a new instance of problem ZDT1. * * @param numberOfVariables Number of variables. */ public ZDT1(Integer numberOfVariables) { setNumberOfVariables(numberOfVariables);//设定决策变量个数,30 setNumberOfObjectives(2);//设定优化目标函数个数,2 setName("ZDT1");//给这个问题起名,ZDT1. List<Double> lowerLimit = new ArrayList<>(getNumberOfVariables()) ; List<Double> upperLimit = new ArrayList<>(getNumberOfVariables()) ; //设置定义域 for (int i = 0; i < getNumberOfVariables(); i++) { lowerLimit.add(0.0); upperLimit.add(1.0); } setLowerLimit(lowerLimit); setUpperLimit(upperLimit); } //这里就是优化目标函数的实现过程,Algorithm.evlataionPopulation()会调用这个方法 /** Evaluate() method */ public void evaluate(DoubleSolution solution) { double[] f = new double[getNumberOfObjectives()]; f[0] = solution.getVariableValue(0); double g = this.evalG(solution); double h = this.evalH(f[0], g); f[1] = h * g; solution.setObjective(0, f[0]); solution.setObjective(1, f[1]); } /** * Returns the value of the ZDT1 function G. * * @param solution Solution */ private double evalG(DoubleSolution solution) { double g = 0.0; for (int i = 1; i < solution.getNumberOfVariables(); i++) { g += solution.getVariableValue(i); } double constant = 9.0 / (solution.getNumberOfVariables() - 1); g = constant * g; g = g + 1.0; return g; } /** * Returns the value of the ZDT1 function H. * * @param f First argument of the function H. * @param g Second argument of the function H. */ public double evalH(double f, double g) { double h ; h = 1.0 - Math.sqrt(f / g); return h; } }
上面是包里面的ZDT1源码。
定义Solution
决策变量不复杂,完全可以使用开源包提供的DefaultBinarySolution。
public class DefaultDoubleSolution extends AbstractGenericSolution<Double, DoubleProblem> implements DoubleSolution { /** Constructor */ public DefaultDoubleSolution(DoubleProblem problem) { super(problem) ;//会在父类AbstractGenericSolution里设置决策变量和优化目标函数空间,这里便是30和2. initializeDoubleVariables();//随机初始化决策变量 initializeObjectiveValues();//优化目标函数设为0,0 } /** Copy constructor */ public DefaultDoubleSolution(DefaultDoubleSolution solution) { super(solution.problem) ; for (int i = 0; i < problem.getNumberOfVariables(); i++) { setVariableValue(i, solution.getVariableValue(i)); } for (int i = 0; i < problem.getNumberOfObjectives(); i++) { setObjective(i, solution.getObjective(i)) ; } attributes = new HashMap<Object, Object>(solution.attributes) ; } @Override public Double getUpperBound(int index) { return problem.getUpperBound(index); } @Override public Double getLowerBound(int index) { return problem.getLowerBound(index) ; } @Override public DefaultDoubleSolution copy() { return new DefaultDoubleSolution(this); } @Override public String getVariableValueString(int index) { return getVariableValue(index).toString() ; } private void initializeDoubleVariables() { for (int i = 0 ; i &