在 Jenetics 中定义约束
Defining constraints in Jenetics
我以前用过MOEA Framework, which knows the concept of constraints. That is, a solution to a problem might have a good fitness, but is infeasible. For instance, when working with the knapsack problem,一个特定的物品组合可能会带来高利润,但它们的重量超过了背包的容量。相应的适应度函数将包括如下行:
// Set the fitness (=> profit) of the solution (=> knapsack).
solution.setObjective(0, profit)
// Set the constrain (=> 0.0 if OK, , otherwise the distance to the boundary).
solution.setConstraint(0, (weight <= capacity) ? 0.0 : weight - capacity)
多objective背包问题的另一个例子是不允许背包使用已经在另一个背包中使用的物品的约束。
有Jenetics类似的东西吗?或者我如何将约束编码为适应度函数(或其他地方)的一部分?
Jenetics doesn't support constraints directly. You can set a phenotypeValidator
in the Engine.Builder
。这将拒绝 Phenotypes
并重新创建 无效的 个人。不完全是约束,而是一种。第二种可能是return所有目标的最小适应度值。
截至 Jenetic v5.0.0, both phenotypeValidator
and genotypeValidator
have been removed, but it is now possible to define a Constraint
(see also user guide,第 2.5 节):
Engine.builder(/* ... */)
.constraint(Constraint.of(phenotype -> /* test validity */)
.build();
也可以实施 Constraint
的 repair
方法来尝试修复给定的个体。
请注意(参见):
It [the Constraint
interface] is meant as the last line of defense, when it comes to check the validity of an individual. […] The second important method [besides test
] of the Constraint
is the repair
method. This method tries to fix the given individual. Without defining this method, only a new, random phenotype is created.
还请考虑在适应度函数本身内验证您的约束,并且 return 值与您要优化的值相反。这可能会降低整体性能,但允许您在更高级别上验证结果。
例如如果您最大化双精度值只是 return 作为适应度函数的结果的最低负双精度,如果优化的解决方案没有通过约束。
我以前用过MOEA Framework, which knows the concept of constraints. That is, a solution to a problem might have a good fitness, but is infeasible. For instance, when working with the knapsack problem,一个特定的物品组合可能会带来高利润,但它们的重量超过了背包的容量。相应的适应度函数将包括如下行:
// Set the fitness (=> profit) of the solution (=> knapsack).
solution.setObjective(0, profit)
// Set the constrain (=> 0.0 if OK, , otherwise the distance to the boundary).
solution.setConstraint(0, (weight <= capacity) ? 0.0 : weight - capacity)
多objective背包问题的另一个例子是不允许背包使用已经在另一个背包中使用的物品的约束。
有Jenetics类似的东西吗?或者我如何将约束编码为适应度函数(或其他地方)的一部分?
Jenetics doesn't support constraints directly. You can set a phenotypeValidator
in the Engine.Builder
。这将拒绝 Phenotypes
并重新创建 无效的 个人。不完全是约束,而是一种。第二种可能是return所有目标的最小适应度值。
截至 Jenetic v5.0.0, both phenotypeValidator
and genotypeValidator
have been removed, but it is now possible to define a Constraint
(see also user guide,第 2.5 节):
Engine.builder(/* ... */)
.constraint(Constraint.of(phenotype -> /* test validity */)
.build();
也可以实施 Constraint
的 repair
方法来尝试修复给定的个体。
请注意(参见
It [the
Constraint
interface] is meant as the last line of defense, when it comes to check the validity of an individual. […] The second important method [besidestest
] of theConstraint
is therepair
method. This method tries to fix the given individual. Without defining this method, only a new, random phenotype is created.
还请考虑在适应度函数本身内验证您的约束,并且 return 值与您要优化的值相反。这可能会降低整体性能,但允许您在更高级别上验证结果。
例如如果您最大化双精度值只是 return 作为适应度函数的结果的最低负双精度,如果优化的解决方案没有通过约束。