我使用背包问题实现了一个杰尼提斯变体,如下所示:
@Value
public class Knapsack {
public static void main( final String[] args ) {
final var knapsackEngine = Engine.builder( Knapsack::fitness, Knapsack.codec() )
.constraint( Knapsack.constraint() )
.build();
final var bestPhenotype = knapsackEngine.stream()
.limit( 1000L )
.collect( EvolutionResult.toBestPhenotype() );
final var knapsack = bestPhenotype.getGenotype().getGene().getAllele();
final var profit = bestPhenotype.getFitness();
final var weight = knapsack.getWeight();
System.out.println( "Valid: " + bestPhenotype.isValid() );
System.out.println( String.format( "Solution: profit %d | weight %d", profit, weight ) );
System.out.println( String.format( "Optimum: profit %d | weight %d", Problem.OPTIMAL_PROFIT, Problem.OPTIMAL_WEIGHT ) );
}
List<Item> items;
public int getProfit() {
return items.stream()
.mapToInt( Item::getProfit )
.sum();
}
public int getWeight() {
return items.stream()
.mapToInt( Item::getWeight )
.sum();
}
private static Codec<Knapsack, AnyGene<Knapsack>> codec() {
return Codec.of(
Genotype.of( AnyChromosome.of( Knapsack::create ) ),
genotype -> genotype.getGene().getAllele() );
}
private static Knapsack create() {
final Random rand = RandomRegistry.getRandom();
final List<Item> items = Problem.ITEMS.stream()
.filter( item -> rand.nextBoolean() )
.collect( Collectors.toList() );
return new Knapsack( items );
}
private static int fitness( final Knapsack knapsack ) {
return knapsack.getProfit();
}
private static Constraint<AnyGene<Knapsack>, Integer> constraint() {
return Constraint.of( phenotype -> {
final Knapsack knapsack = phenotype.getGenotype().getGene().getAllele();
final int weight = knapsack.getItems().stream()
.mapToInt( Item::getWeight )
.sum();
return weight <= Problem.MAX_CAPACITY;
} );
}
}@Value是隆布克的一部分,并生成一些代码,如构造函数、getter等。Problem类为特定的背包问题(从01.html生成的P07)定义了一些常量:
public class Problem {
public static final int MAX_CAPACITY = 750;
public static final BitChromosome OPTIMAL_SOLUTION = BitChromosome.of( "101010111000011" );
public static final int OPTIMAL_PROFIT = 1458;
public static final int OPTIMAL_WEIGHT = 749;
private static final List<Integer> profits = List.of(
135, 139, 149, 150, 156,
163, 173, 184, 192, 201,
210, 214, 221, 229, 240 );
private static final List<Integer> weights = List.of(
70, 73, 77, 80, 82,
87, 90, 94, 98, 106,
110, 113, 115, 118, 120 );
public static final List<Item> ITEMS = IntStream.range( 0, profits.size() )
.mapToObj( i -> new Item( profits.get( i ), weights.get( i ) ) )
.collect( Collectors.toList() );
}尽管Jenetics 用户指南说(参见第2.5节):
给定的问题通常应该以一种方式编码,即进化
Engine不可能创建无效的个人(Genotypes)。
我想知道为什么引擎不断创造的解决方案的重量超过背包的最大容量。因此,尽管根据给定的Constraint,这些解决方案是无效的,但Phenotype#isValid()返回true。
我可以通过将健身功能更改为:
private static int fitness( final Knapsack knapsack ) {
final int profit = knapsack.getProfit();
final int weight = knapsack.getWeight();
return weight <= Problem.MAX_CAPACITY ? profit : 0;
}或者确保编解码器只能创建有效的解决方案:
private static Knapsack create() {
final Random rand = RandomRegistry.getRandom();
final List<Item> items = Problem.ITEMS.stream()
.filter( item -> rand.nextBoolean() )
.collect( Collectors.toList() );
final Knapsack knapsack = new Knapsack( items );
return knapsack.getWeight() <= Problem.MAX_CAPACITY ? knapsack : create();
}但是如果Constraint没有效果的话,它的目的是什么呢?
发布于 2019-07-13 05:50:52
我在最新版本的Jenetics中介绍了Constraint接口。它的意思是作为最后的防线,当涉及到检查一个人的有效性。在您的示例中,您使用了Constraint接口的工厂方法,它只接受有效性谓词。Constraint的第二个重要方法是repair方法。此方法试图修复给定的个人。在不定义此方法的情况下,只创建了一个新的随机表型。由于这个接口是新的,所以我似乎还没有很好地解释Constraint接口的预期用途。这是我议程上的#541。在第二个例子中,#540给出了一个可能的使用示例。
void constrainedVersion() {
final Codec<double[], DoubleGene> codec = Codecs
.ofVector(DoubleRange.of(0, 1), 4);
final Constraint<DoubleGene, Double> constraint = Constraint.of(
pt -> isValid(codec.decode(pt.getGenotype())),
(pt, g) -> {
final double[] r = normalize(codec.decode(pt.getGenotype()));
return newPT(r, g);
}
);
}
private static Phenotype<DoubleGene, Double> newPT(final double[] r, final long gen) {
final Genotype<DoubleGene> gt = Genotype.of(
DoubleChromosome.of(
DoubleStream.of(r).boxed()
.map(v -> DoubleGene.of(v, DoubleRange.of(0, 1)))
.collect(ISeq.toISeq())
)
);
return Phenotype.of(gt, gen);
}
private static boolean isValid(final double[] x) {
return x[0] + x[1] + x[2] == 1 && x[3] > 0.8;
}
private static double[] normalize(final double[] x) {
double[] r = x;
final double sum = r[0] + r[1] + r[2];
if (sum != 1) {
r[0] /= sum;
r[1] /= sum;
r[2] /= sum;
}
if (r[3] > 0.8) {
r[3] = 0.8;
}
return r;
}Phenotype::isValid方法返回true,因为它是一个局部有效性检查,它只检查个体的所有染色体和基因是否有效或在有效范围内。
我希望我能回答你的问题,一个更好的描述一个(或更多)的例子正在进行中。另一方面:如果您对Constraint接口的良好使用示例有想法,请告诉我。
https://stackoverflow.com/questions/57010094
复制相似问题