JMetal环境搭建和多目标优化问题的求解

文章目录

      • JMetal 下载
      • 导入项目,生成jar包
        • 创建项目
        • 求解问题

JMetal 下载

最近的课程需要用到了JMetal框架,之前未接触过,所以在这里记录下。
JMetal项目是由Maven构建,这里直接从GitHub下载,为了方便使用我将项目打成jar包,后续使用直接引入jar包即可。
GitHub 链接:https://github.com/jMetal/jMetal
总共有以下四个模块:
Algorithm 具体实现的算法
core 核心包
exec 算法配置
problem 经典优化问题
JMetal环境搭建和多目标优化问题的求解_第1张图片

导入项目,生成jar包

将下载的项目进行解压,打开Eclipse,导入项目打包
Step:Package Explorer -> 右键 -> Import -> Maven ->Existing Maven Project 选择刚才解压的项目JMetal环境搭建和多目标优化问题的求解_第2张图片
等待项目构建完成,可以看到有以下结构
JMetal环境搭建和多目标优化问题的求解_第3张图片
打成jar包:
Step:Package Explorer -> 右键 -> Export -> Java ->JAR file 勾选 保存即可
有一些警告直接忽略即可
JMetal环境搭建和多目标优化问题的求解_第4张图片
为了在使用中查看源码,可以再打包一个源码包:
同样的
Step:Package Explorer -> 右键 -> Export -> Java ->JAR file
注意勾选Export Java Resource files and resources保存即可
JMetal环境搭建和多目标优化问题的求解_第5张图片
至此,JMetal 已打包完成,下一步我将介绍简单的使用。

创建项目

采用Booth’s Function 单目标优化测试函数:

f ( x 1 , x 2 ) = ( x 1 + 2 x 2 − 7 ) 2 + ( 2 x 1 + x 2 − 5 ) 2 f(x_1,x_2) = (x_1+2x_2-7)^2+(2x_1+x_2-5)^2 f(x1,x2)=(x1+2x27)2+(2x1+x25)2
m i n f ( x 1 , x 2 ) = f ( 1 , 3 ) = 0 min f(x_1,x_2)=f(1,3)=0 minf(x1,x2)=f(1,3)=0
s e a r c h − d o m a n : − 10 ≤ x 1 , x 2 ≤ 10 search-doman:-10\leq{x_1,x_2}\leq10 searchdoman:10x1,x210

新建SingleObjectOptimization项目
创建lib文件夹引入上文打包好的jmetal.jar,jmetal-src.jar 再add to buildpath添加到项目中
JMetal环境搭建和多目标优化问题的求解_第6张图片

求解问题

定义求解问题:

import java.util.ArrayList;
import java.util.List;

import org.uma.jmetal.problem.impl.AbstractDoubleProblem;
import org.uma.jmetal.solution.DoubleSolution;

/**
 * Booth 优化问题
 * @author shup
 *
 */
public class BoothProblem extends AbstractDoubleProblem{
	
	public BoothProblem() {
		// 变量个数
		setNumberOfVariables(2);
		// 目标函数个数
		setNumberOfObjectives(1);
		setName("BoothProblem");
		
		// 定义域范围
		List<Double> lowerLimit = new ArrayList<>(getNumberOfVariables());
		List<Double> upperLimit = new ArrayList<>(getNumberOfVariables());
		for (int i=0; i<getNumberOfVariables(); i++){
			lowerLimit.add(-10.0);
			upperLimit.add(10.0);
		}
		setLowerLimit(lowerLimit);
		setUpperLimit(upperLimit);
	}

	@Override
	public void evaluate(DoubleSolution solution) {
		// 变量值
		Double[] X = solution.getVariables().toArray(new Double[getNumberOfVariables()]);
		
		//
		double f = Math.pow(X[0]+2*X[1]-7, 2)+Math.pow(2*X[0]+X[1]-5, 2);
		
		solution.setObjective(0, f);
	}

}

调用NSGAII 算法

import java.util.List;

import org.uma.jmetal.algorithm.Algorithm;
import org.uma.jmetal.algorithm.multiobjective.nsgaii.NSGAIIBuilder;
import org.uma.jmetal.operator.CrossoverOperator;
import org.uma.jmetal.operator.MutationOperator;
import org.uma.jmetal.operator.SelectionOperator;
import org.uma.jmetal.operator.impl.crossover.SBXCrossover;
import org.uma.jmetal.operator.impl.mutation.PolynomialMutation;
import org.uma.jmetal.operator.impl.selection.BinaryTournamentSelection;
import org.uma.jmetal.problem.Problem;
import org.uma.jmetal.solution.DoubleSolution;
import org.uma.jmetal.util.AlgorithmRunner;
import org.uma.jmetal.util.comparator.RankingAndCrowdingDistanceComparator;

/**
 * NSGAII 算法求解
 * @author shup
 *
 */
public class NSGAIIRunner {
	public static void main(String[] args) {
		Problem<DoubleSolution> problem;
		Algorithm<List<DoubleSolution>> algorithm;
		CrossoverOperator<DoubleSolution> crossover;
		MutationOperator<DoubleSolution> mutation;
		SelectionOperator<List<DoubleSolution>, DoubleSolution> selection;
 
		// 定义优化问题
		problem = new BoothProblem();
		// 种群规模
		int popSize = 80;
		// SBX交叉算子
		double crossoverProbability = 0.9;
		double crossoverDistributionIndex = 20.0;
		crossover = new SBXCrossover(crossoverProbability, crossoverDistributionIndex);
		// 变异算子
		double mutationProbability = 1.0 / problem.getNumberOfVariables();
		double mutationDistributionIndex = 20.0;
		mutation = new PolynomialMutation(mutationProbability, mutationDistributionIndex);
		// 选择算子
		selection = new BinaryTournamentSelection<DoubleSolution>(
				new RankingAndCrowdingDistanceComparator<DoubleSolution>());
		// 注册
		algorithm = new NSGAIIBuilder<DoubleSolution>(problem, crossover, mutation, popSize)
				.setSelectionOperator(selection).setMaxEvaluations(30000).build();
		// 运行算法
		new AlgorithmRunner.Executor((Algorithm<?>) algorithm).execute();
 
		// 结果集输出
		List<DoubleSolution> population = algorithm.getResult();
		for (DoubleSolution p : population) {
			System.out.println(p);
		}
	}
}

运行结果:
JMetal环境搭建和多目标优化问题的求解_第7张图片
以上就是JMetal的简单用法,下一篇我将介绍多目标问题如何定义以及使用JMetal进行求解

你可能感兴趣的:(最优化)