To run an algorithm in jMetal you have two choices: using an IDE or using the command line. We explain both methods in this section. We comment first how to configure a metaheuristic algorithm to solve a problem.
要在 jMetal 中运行算法,您有两种选择:使用 IDE 或使用命令行。
我们将在本节中解释这两种方法。
我们首先评论如何配置元启发式算法来解决问题。
In jMetal 5, to configure and run an algorithm we need to write a class for that purpose; we refer to such a class as runner. Configuring an algorithm from an external configuration file is still a missing feature.
We provide at least a runner class for every algorithm included in jMetal. They can be found in the jmetal-exec
module, in the folder https://github.com/jMetal/jMetal/tree/master/jmetal-exec/src/main/java/org/uma/jmetal/runner/multiobjective.
在 jMetal 5 中,为了配置和运行算法,我们需要为此编写一个类;
我们将这样的类称为 runner。
从外部配置文件配置算法仍然是一个缺失的功能。
我们为 jMetal 中包含的每个算法至少提供了一个运行器类。
它们可以在文件夹 https://github.com/jMetal/jMetal/tree/master/jmetal-exec/src/main/java/org/uma/jmetal/runner/ 的 jmetal-exec
模块中找到
多目标。
As explanatory examples, we include different runners for the NSGA-II algorithm, showing different ways of configuring and using it:
作为示例,我们使用 NSGA-II 算法的不同运行器,展示了配置和使用它的不同方式:
NSGAIIRunner
: configuration of the standard NSGA-II to solve continuous problems.配置标准 NSGA-II 以解决连续性问题。NSGAIIIntegerRunner
: configuration to solve integer problems.解决整数问题的配置NSGAIIBinaryRunner
: configuration to solve binary problems.解决二进制问题的配置NSGAIIMeasuresRunner
: similar to NSGAIIRunner
, but it includes examples of how to use measures.类似于NSGAIIRunner
,但它包含如何使用度量的示例。NSGAIIMeasuresWithChartsRunner
: similar to NSGAIIMeasuresRunner
, but plotting a graph showing the evolution of the front during the execution of the algorithm.类似于NSGAIIMeasures Runner
,但在算法执行过程中绘制了一个曲线图,显示前沿的演变。NSGAIIStoppingByTimeRunner
: example showing how to configure NSGA-II to use a stopping condition based on a predefined time instead of a given number of evaluations.示例显示如何配置 NSGA-II 以使用基于预定义时间而不是给定评估次数的停止条件。ParallelNSGAIIRunner
: as NSGAIIRunner
but configured to use threads to evaluate the populations in parallel.作为“NSGAIIRunner”,但配置为使用线程并行评估种群。We describe next the NSGAIIRunner
class. The Javadoc comment indicates the program parameters: the first one is the class of the problem to solve; the second one, is an optional parameter, indicating the path to a file containing a reference front. This front is an approximation to the optimal Pareto front of the problem to be solved, and in case of being provided, it will be used to compute all the quality indicators available:
我们接下来描述 NSGAIIRunner
类。
Javadoc 注释指示程序参数:第一个是要解决的问题的类;
第二个,是一个可选参数,指示包含引用前面的文件的路径。
该前沿是待解决问题的最优帕累托前沿的近似值,如果提供,它将用于计算所有可用的质量指标:
public class NSGAIIRunner extends AbstractAlgorithmRunner {
/**
* @param args Command line arguments.
* @throws JMetalException
* @throws FileNotFoundException
* Invoking command: java org.uma.jmetal.runner.multiobjetive.NSGAIIRunner problemName [referenceFront]
*/
public static void main(String[] args) throws JMetalException, FileNotFoundException {
The first part of the main
method declares the type of the problem to solve (a problem dealing with DoubleSolution
individuals in this example) and the operators. The referenceParetoFront
is used to indicate the name of the optional reference front:
main
方法的第一部分声明了要解决的问题的类型(在本例中是处理 Double Solution
个体的问题)和运算符。
reference Pareto Front
用于表示可选的参考前线的名称:
Problem<DoubleSolution> problem;
Algorithm<List<DoubleSolution>> algorithm;
CrossoverOperator<DoubleSolution> crossover;
MutationOperator<DoubleSolution> mutation;
SelectionOperator<List<DoubleSolution>, DoubleSolution> selection;
String referenceParetoFront = "" ;
The next group of sentences parse the program arguments. A benchmark problem (ZDT1 in the example) is solved by default when no arguments are indicated:
下一组句子解析程序参数。
当没有指定参数时,默认解决基准问题(示例中的 ZDT1):
String problemName ;
if (args.length == 1) {
problemName = args[0];
} else if (args.length == 2) {
problemName = args[0] ;
referenceParetoFront = args[1] ;
} else {
problemName = "org.uma.jmetal.problem.multiobjective.zdt.ZDT1";
referenceParetoFront = "jmetal-problem/src/test/resources/pareto_fronts/ZDT1.pf" ;
}
Next, the problem is loaded using its class name:
接下来,使用其类名加载问题:
problem = ProblemUtils.<DoubleSolution> loadProblem(problemName);
Then, the operators and the algorithm are configured:
然后,配置算子和算法:
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)
.setSelectionOperator(selection)
.setMaxEvaluations(25000)
.setPopulationSize(100)
.build() ;
The last step is to run the algorithm and to write the obtained solutions into two files: one for the variable values and one for the objective values; optionally, if a reference front has been provided it also prints the values of all the available quality indicators for the computed results:
最后一步是运行算法并将获得的解决方案写入两个文件:一个用于变量值,一个用于目标值;
可选地,如果提供了参考前沿,它还会打印计算结果的所有可用质量指标的值:
AlgorithmRunner algorithmRunner = new AlgorithmRunner.Executor(algorithm)
.execute() ;
List<DoubleSolution> population = algorithm.getResult() ;
long computingTime = algorithmRunner.getComputingTime() ;
JMetalLogger.logger.info("Total execution time: " + computingTime + "ms");
printFinalSolutionSet(population);
if (!referenceParetoFront.equals("")) {
printQualityIndicators(population, referenceParetoFront) ;
}
}
Once you have configured your algorithm, you can use your favorite IDE to execute them. For example, in the case of IntellJ Idea you can select the runner class name and select the option “Run ‘NSGAIIRunner.main()’” by clicking with the left mouse button if you intend to run NSGA-II:
一旦你配置了你的算法,你就可以使用你最喜欢的 IDE 来执行它们。
例如,在 Intell J Idea 的情况下,如果您打算运行 NSGA-II,您可以通过单击鼠标左键选择运行器类名称并选择选项“Run ‘NSGAIIRunner.main()’”:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BUl5udtG-1658305135759)(https://github.com/jMetal/jMetalDocumentation/blob/master/figures/runningNSGAIIRunnerInIntelliJIdea.png)]
As a result of the ex作为执行的结果,以下消息将打印到输出控制台中:ecution, the following messages are printed into the output console:
jul 27, 2015 4:21:59 PM org.uma.jmetal.runner.multiobjective.NSGAIIRunner main
INFORMACIÓN: Total execution time: 1147ms
jul 27, 2015 4:21:59 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Random seed: 1438006918503
jul 27, 2015 4:21:59 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Objectives values have been written to file FUN.tsv
jul 27, 2015 4:21:59 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Variables values have been written to file VAR.tsv
jul 27, 2015 4:22:00 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printQualityIndicators
INFORMACIÓN:
Hypervolume (N) : 0.6594334269577787
Hypervolume : 0.6594334269577787
Epsilon (N) : 0.012122558511198256
Epsilon : 0.012122558511198256
GD (N) : 2.054388435747992E-4
GD : 2.054388435747992E-4
IGD (N) : 1.8304524180524584E-4
IGD : 1.8304524180524584E-4
IGD+ (N) : 0.003808931172199927
IGD+ : 0.003808931172199927
Spread (N) : 0.34070732976112383
Spread : 0.34070732976112383
R2 (N) : 0.13179198315493879
R2 : 0.13179198315493879
Error ratio : 1.0
The results tagged with (N)
indicate that the fronts are normalized before computing the quality indicator.
标有“(N)”的结果表明在计算质量指标之前对前沿进行了归一化。
If you plan to run a jMetal algorithm from the command line, you have to take into account the following requirements:
mvn package
. This will create, for each subproject (i.e, jmetal-core
, jmetal-problem
, jmetal-algorithm
, and jmetal-exec
), a jar file with all the dependences.CLASSPATH
environment variable:mvn package
构建项目。CLASSPATH
环境变量:export CLASSPATH=jmetal-core/target/jmetal-core-5.6-jar-with-dependencies.jar:jmetal-problem/target/jmetal-problem-5.6-jar-with-dependencies.jar:jmetal-exec/target/jmetal-exec-5.6-jar-with-dependencies.jar:jmetal-problem/target/jmetal-problem-5.6-jar-with-dependencies.jar
Then you can execute an algorithm this way (we are going to execute NSGA-II):
然后你可以这样执行一个算法(我们将执行 NSGA-II):
java org.uma.jmetal.runner.multiobjective.NSGAIIRunner
-cp
or -classpath
options of the java
command:java
命令的 -cp
或 -classpath
选项来指示这些 jar 文件的位置:java -cp jmetal-exec/target/jmetal-exec-5.0-SNAPSHOT-jar-with-dependencies.jar:jmetal-core/target/jmetal-core-5.0-SNAPSHOT-jar-with-dependencies.jar:jmetal-problem/target/jmetal-problem-5.0-SNAPSHOT-jar-with-dependencies.jar:jmetal-algorithm/target/jmetal-algorithm-5.0-Beta-35-jar-with-dependencies.jar org.uma.jmetal.runner.multiobjective.NSGAIIRunner
This example executes NSGA-II with the default parameters. If you want to solve a given problem its class name must be provided as an argument. For example, to solve the benchmark problem ZDT4
the command would be:
此示例使用默认参数执行 NSGA-II。
如果你想解决一个给定的问题,它的类名必须作为参数提供。
例如,要解决基准问题“ZDT4”,命令将是:
java org.uma.jmetal.runner.multiobjective.NSGAIIRunner org.uma.jmetal.problem.multiobjective.zdt.ZDT4
and the output will be similar to this:
jul 27, 2015 6:48:27 PM org.uma.jmetal.runner.multiobjective.NSGAIIRunner main
INFORMACIÓN: Total execution time: 683ms
jul 27, 2015 6:48:27 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Random seed: 1438015706581
jul 27, 2015 6:48:27 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Objectives values have been written to file FUN.tsv
jul 27, 2015 6:48:27 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Variables values have been written to file VAR.tsv
In the case of problems having a known Pareto front (or a Pareto front approximation), adding the file containing it allows to apply the available quality indicators to the obtained front. This way, the command to solve ZDT4 would be:
在具有已知 Pareto 前沿(或 Pareto 前沿近似)的问题的情况下,添加包含它的文件允许将可用的质量指标应用于获得的前沿。
这样,解决 ZDT4 的命令将是:
java org.uma.jmetal.runner.multiobjective.NSGAIIRunner org.uma.jmetal.problem.multiobjective.zdt.ZDT4 jmetal-problem/src/test/resources/pareto_fronts/ZDT4.pf
and this would be output:
jul 27, 2015 6:49:21 PM org.uma.jmetal.runner.multiobjective.NSGAIIRunner main
INFORMACIÓN: Total execution time: 598ms
jul 27, 2015 6:49:21 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Random seed: 1438015760471
jul 27, 2015 6:49:21 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Objectives values have been written to file FUN.tsv
jul 27, 2015 6:49:21 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printFinalSolutionSet
INFORMACIÓN: Variables values have been written to file VAR.tsv
jul 27, 2015 6:49:21 PM org.uma.jmetal.runner.AbstractAlgorithmRunner printQualityIndicators
INFORMACIÓN:
Hypervolume (N) : 0.6584874391103687
Hypervolume : 0.658491021119803
Epsilon (N) : 0.014508161683056214
Epsilon : 0.014508161681605389
GD (N) : 1.7281971372005978E-4
GD : 1.7281858245371445E-4
IGD (N) : 1.9833943989483466E-4
IGD : 1.9833851420211548E-4
IGD+ (N) : 0.00425088535021156
IGD+ : 0.004250860866635309
Spread (N) : 0.4449171015114183
Spread : 0.44491700055639544
R2 (N) : 0.13208551920620412
R2 : 0.13208472309027727
Error ratio : 1.0