I am trying to find best subset of set. Imagine that we need to find subset of objects. We have got some fitness function for this subset. So at the beginning we should make a population of subsets and then using GA we should try to find the best subset.
I would like to use Jenetics.io but I do not know how to use it in this case. Problem for me is that chromosomes are much different data structure than subset.
I would like to have a function( population, fitness function) which makes all needed job.
I tried to understand how Jenetics exactly works. Maybe I am wrong but I think there is no way to make it works the way I want.
Please give me advice , maybe there is option to use Jenetics in this case?
There is a sub-set example in the Jenetics library. Essentially, it has the following form:
class SubsetExample
implements Problem<ISeq<MyObject>, EnumGene<MyObject>, Double>
{
// Define your basic set here.
private final ISeq<MyObject> basicSet = ISeq.empty();
private final int subSetSize = 5;
@Override
public Function<ISeq<MyObject>, Double> fitness() {
return subSet -> {
assert(subset.size() == subSetSize);
double fitness = 0;
for (MyObject obj : subSet) {
// Do some fitness calculation
}
return fitness;
};
}
@Override
public Codec<ISeq<MyObject>, EnumGene<MyObject>> codec() {
return codecs.ofSubSet(basicSet, subSetSize);
}
public static void main(final String[] args) {
final SubsetExample problem = new SubsetExample()
final Engine<EnumGene<MyObject>, Double> engine = Engine.builder(problem)
.minimizing()
.maximalPhenotypeAge(5)
.alterers(
new PartiallyMatchedCrossover<>(0.4),
new Mutator<>(0.3))
.build();
final Phenotype<EnumGene<MyObject>, Double> result = engine.stream()
.limit(limit.bySteadyFitness(55))
.collect(EvolutionResult.toBestPhenotype());
System.out.print(result);
}
}