solution

The Solution interface

One of the first decisions that have to be taken when using metaheuristics is to define how to encode
or represent the tentative solutions of the problem to solve. Representation strongly depends on the
problem and determines the operations (e.g., recombination with other solutions, local search procedures,
etc.) that can be applied. Thus, selecting a specific representation has a great impact on the behavior
of metaheuristics and, hence, in the quality of the obtained results.
使用元启发式算法时必须做出的首要决定之一是定义如何编码
或表示要解决的问题的暂定解决方案。
代表性在很大程度上取决于
问题并确定操作(例如,与其他解决方案重新组合、本地搜索程序、
等),可以应用。
因此,选择特定的表示对行为有很大的影响
元启发式的,因此,在所获得的结果的质量。

The following figure depicts the basic components that are used for representing solutions in jMetal 5:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3aNQBl5B-1658305899020)(https://github.com/jMetal/jMetalDocumentation/blob/master/figures/jMetal5SolutionClassDiagram.png)]
where three representations are included: binary, real, and integer. By using this approach, many implementations can be provided for the same encoding, adding an extra degree of flexibility. The use of generics also allows that an attempt to incorrectly assign the value of a variable results in a compilation error, e.g., trying to assign to an int variable the variable value of a DoubleSolution.
下图描述了用于表示jMetal 5中解决方案的基本组件:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3a NQBl5 B-165830589020)(https://github.com/jMetal/jMetalDocumentation/blob/master/figures/jMetal5解决方案类图。巴布亚新几内亚)]
其中包括三种表示:二进制、实数和整数。通过使用这种方法,可以为相同的编码提供许多实现,增加了额外的灵活性。泛型的使用还允许错误分配变量值的尝试导致编译错误,例如,试图将“双解”的变量值分配给int变量。
重点词汇

The code of the Solution interface is shown next:

package org.uma.jmetal.solution;

public interface Solution<T> extends Serializable {
  public void setObjective(int index, double value) ;
  public double getObjective(int index) ;

  public T getVariableValue(int index) ;
  public void setVariableValue(int index, T value) ;
  public String getVariableValueString(int index) ;

  public int getNumberOfVariables() ;
  public int getNumberOfObjectives() ;

  public Solution<T> copy() ;

  public void setAttribute(Object id, Object value) ;
  public Object getAttribute(Object id) ;
}

The interface has methods for accessing both the variables and the objectives of a solution, a copy method, and to methods for accessing solution atributes.
该接口具有访问解决方案的变量和目标的方法、复制方法以及访问解决方案属性的方法。

Defining encodings

Defining a particular encoding implies implementing or extending the Solution interface. This way, the interfaces for solutions having a list of double and integer variables are defined as follows:
定义特定的编码意味着实现或扩展“解决方案”接口。
这样,具有双精度和整数变量列表的解决方案的接口定义如下:

package org.uma.jmetal.solution;

public interface DoubleSolution extends Solution<Double> {
  public Double getLowerBound(int index) ;
  public Double getUpperBound(int index) ;
}
package org.uma.jmetal.solution;

public interface IntegerSolution extends Solution<Integer> {
  public Integer getLowerBound(int index) ;
  public Integer getUpperBound(int index) ;
}

These interfaces provide for getting the lower and upper bounds of the double and integer variables. The way of setting those values are left to the implementation classes.

In the case of a binary solution, the interface is:
这些接口提供获取双精度和整数变量的下限和上限。
设置这些值的方式留给实现类。
在二进制解决方案的情况下,接口是:

import org.uma.jmetal.util.binarySet.BinarySet;

public interface BinarySolution extends Solution<BinarySet> {
  public int getNumberOfBits(int index) ;
  public int getTotalNumberOfBits() ;
}

assuming that we intend to represent a list of binary variables.

The adopted approach allows to define encodings having mixed variables. For example, this interface defines solutions composed of lists of double and integer values:
假设我们打算表示一个二进制变量列表。
采用的方法允许定义具有混合变量的编码。
例如,此接口定义了由双精度值和整数值列表组成的解决方案:

package org.uma.jmetal.solution;

public interface IntegerDoubleSolution extends Solution<Number> {
  public Number getLowerBound(int index) ;
  public Number getUpperBound(int index) ;
  public int getNumberOfIntegerVariables() ;
  public int getNumberOfDoubleVariables() ;
}

Implementing solutions

Once we have defined a set of interfaces for the different solutions, we provide default implementions to all of them. Our approach is to take as starting point an abstract class named AbstractGenericSolution:
一旦我们为不同的解决方案定义了一组接口,我们就会为它们提供默认实现。
我们的方法是以一个名为“Abstract Generic Solution”的抽象类为起点:

package org.uma.jmetal.solution.impl;

public abstract class AbstractGenericSolution<T, P extends Problem<?>> implements Solution<T> {
  private double[] objectives;
  private List<T> variables;
  protected P problem ;
  protected double overallConstraintViolationDegree ;
  protected int numberOfViolatedConstraints ;
  protected Map<Object, Object> attributes ;
  protected final JMetalRandom randomGenerator ;

which contains an implementation to all the methods in Solution. This class is extended by all the solution implementations in jMetal 5: DefaultBinarySolution, DefaultIntegerSolution, DefaultDoubleSolution, DefaultIntegerDoubleSolution, DefaultIntegerPermutationSolution, and DefaultDoubleBinarySolution.
其中包含“解决方案”中所有方法的实现。
此类由 jMetal 5 中的所有解决方案实现扩展:Default Binary SolutionDefault Integer SolutionDefault Double SolutionDefault Integer Double SolutionDefault Integer Permutation SolutionDefault Double Binary 解决方案

Where are the populations?

In jMetal 5 there is not any class to represent the concept of population. Instead, a Java List of Solution objects is used.
在 jMetal 5 中,没有任何类来表示人口的概念。
相反,使用了“解决方案”对象的 Java“列表”。
Some examples:

/* A population of double solutions */
List<DoubleSolution> doublePopulation ;

/* The same population using the generic interface */
List<Solution<Double>> doublePopulation ;

/* A population of binary solutions */
List<BinarySolucion> binaryPopulation ;

An utility class called SolutionListUtils offers a set of operations over solution lists, such as finding the best/worst solution, selecting solutions randomly, etc.
一个名为 [Solution List Utils] 的实用程序类(https://github.com/jMetal/jMetal/blob/master/jmetal-core/src/main/java/org/uma/jmetal/util/Solution List Utils。
java) 提供了一组对解决方案列表的操作,例如找到最佳/最差解决方案、随机选择解决方案等。

Solution attributes

The idea of incorporating attributes is to allow to add specific fields to solutions that are needed by some algorithms. For example, NSGA-II requires to rank the solutions and assign them the value of the crowding distance, while SPEA2 assigns a raw fitness to the solutions.

The attributes manipulated directly, but we include also this utility interface:
合并属性的想法是允许将特定字段添加到某些算法所需的解决方案中。
例如,NSGA-II 需要对解决方案进行排名并为其分配拥挤距离的值,而 SPEA2 为解决方案分配原始适应度。
直接操作的属性,但我们也包括这个实用程序接口:

package org.uma.jmetal.util.solutionattribute;
/**
 * Attributes allows to extend the {@link Solution} classes to incorporate data required by
 * operators or algorithms manipulating them.
 *
 * @author Antonio J. Nebro <antonio@lcc.uma.es>
 */
public interface SolutionAttribute <S extends Solution<?>, V> {
  public void setAttribute(S solution, V value) ;
  public V getAttribute(S solution) ;
  public Object getAttributeID() ;
}

and a default implementation:

package org.uma.jmetal.util.solutionattribute.impl;

public class GenericSolutionAttribute <S extends Solution<?>, V> implements SolutionAttribute<S, V>{

  @SuppressWarnings("unchecked")
  @Override
  public V getAttribute(S solution) {
    return (V)solution.getAttribute(getAttributeID());
  }

  @Override
  public void setAttribute(S solution, V value) {
     solution.setAttribute(getAttributeID(), value);
  }

  @Override
  public Object getAttributeID() {
    return this.getClass() ;
  }
}

Note that in the current implementation the getAttributedID() returns a class identifier. This means that we cannot have two different attributes of the same class.
请注意,在当前实现中,get Attributed ID() 返回一个类标识符。
这意味着我们不能有同一个类的两个不同属性。

Example of attribute: constraints

Optimization problems can have side constraints, and this implies that evaluating a solution requires to compute the objective functions and also the constraints to apply some kind of constraint handling mechanism. In jMetal 5 solution attributes are used to incorporate constraint information into the solutions.

The default constraint handling mechanism in jMetal is the overall constraing violation scheme defined in NSGA-II, so the following class is provided:
优化问题可能有侧面约束,这意味着评估解决方案需要计算目标函数以及应用某种约束处理机制的约束。
在 jMetal 5 中,解决方案属性用于将约束信息合并到解决方案中。
jMetal中默认的约束处理机制是NSGA-II中定义的整体约束违反方案,所以提供了如下类:

package org.uma.jmetal.util.solutionattribute.impl;
public class OverallConstraintViolation<S extends Solution<?>> extends GenericSolutionAttribute<S, Double> {
}

It is an empty class extending GenericSolutionAttribute specifying a double value for the attribute. Typically, the constraints are evaluted in the class defining the problem as is shown in this example:
它是一个扩展“通用解决方案属性”的空类,为该属性指定一个双精度值。
通常,约束在定义问题的类中进行评估,如下例所示:

package org.uma.jmetal.problem.multiobjective;

/** Class representing problem Binh2 */
public class Binh2 extends AbstractDoubleProblem implements ConstrainedProblem<DoubleSolution> {
  public OverallConstraintViolation<DoubleSolution> overallConstraintViolationDegree ;
  public NumberOfViolatedConstraints<DoubleSolution> numberOfViolatedConstraints ;
  /**
   * Constructor
   * Creates a default instance of the Binh2 problem
   */
  public Binh2() {
    ...
    overallConstraintViolationDegree = new OverallConstraintViolation<DoubleSolution>() ;
    numberOfViolatedConstraints = new NumberOfViolatedConstraints<DoubleSolution>() ;
  }

  /** Evaluate() method */
  @Override
  public void evaluate(DoubleSolution solution) {
     ...
  }

  /** EvaluateConstraints() method */
  @Override
  public void evaluateConstraints(DoubleSolution solution)  {
    double[] constraint = new double[this.getNumberOfConstraints()];

    double x0 = solution.getVariableValue(0) ;
    double x1 = solution.getVariableValue(1) ;

    constraint[0] = -1.0 * (x0 - 5) * (x0 - 5) - x1 * x1 + 25.0;
    constraint[1] = (x0 - 8) * (x0 - 8) + (x1 + 3) * (x1 + 3) - 7.7;

    double overallConstraintViolation = 0.0;
    int violatedConstraints = 0;
    for (int i = 0; i < this.getNumberOfConstraints(); i++) {
      if (constraint[i] < 0.0) {
        overallConstraintViolation += constraint[i];
        violatedConstraints++;
      }
    }

    overallConstraintViolationDegree.setAttribute(solution, overallConstraintViolation);
    numberOfViolatedConstraints.setAttribute(solution, violatedConstraints);
  }

This code includes also another attribute, called NumberOfViolatedConstraints that is used to set the number of violated constraints of a given solution.
此代码还包括另一个属性,称为“违反约束的数量”,用于设置给定解决方案的违反约束的数量。

Example of attribute: ranking

The use of solution attributes can be encapsulated. As an example, we have defined the following interface to assign a
rank to a solution (i.e, NSGA-II’s ranking):
可以封装解决方案属性的使用。
例如,我们定义了以下接口来分配一个
对解决方案的排名(即 NSGA-II 的排名):

package org.uma.jmetal.util.solutionattribute;

import java.util.List;

/**
 * Ranks a list of solutions according to the dominance relationship
 *
 * @author Antonio J. Nebro <antonio@lcc.uma.es>
 */
public interface Ranking<S extends Solution<?>> extends SolutionAttribute<S, Integer>{
  public Ranking<S> computeRanking(List<S> solutionList) ;
  public List<S> getSubfront(int rank) ;
  public int getNumberOfSubfronts() ;
}

so that a client class (e.g., the NSGAII class) can merely use:
因此客户端类(例如,NSGAII 类)只能使用:

Ranking ranking = computeRanking(jointPopulation);

This way, the solution attribute is managed internally by the class implementing the ranking and is hidden to the
metaheuristic.
这样,解决方案属性由实现排名的类在内部管理,并且对
启发式的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值