Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify MOEA for continious optimization #695

Closed
jenetics opened this issue May 7, 2020 · 1 comment
Closed

Simplify MOEA for continious optimization #695

jenetics opened this issue May 7, 2020 · 1 comment
Assignees
Milestone

Comments

@jenetics
Copy link
Owner

jenetics commented May 7, 2020

The current classes for MOEA are optimized for a one-time optimization.

final var result = engine.stream()
	.limit(100)
	.collect(MOEA.toParetoSet(IntRange.of(20, 25)))

If you want to have intermediate results, the effort to do so is a little bit to much for an average library user.

public void evolve(final Subscriber<Result> subscriber) {
	final ParetoFront<Phenotype<DoubleGene, Vec<double[]>>> best =
		new ParetoFront<>(this::dominance, this::equals);

	final var stream = _engine.stream()
		.limit(r -> Thread.currentThread().isInterrupted())
		.peek(r -> add(best, IntRange.of(30, 50), r))
		.map(r -> new Result(r, List.copyOf(best)));

	final var publisher = new StreamPublisher<Result>();
	publisher.subscribe(subscriber);
	publisher.attach(stream);
}

private void add(
	final ParetoFront<Phenotype<DoubleGene, Vec<double[]>>> best,
	final IntRange size,
	final EvolutionResult<DoubleGene, Vec<double[]>> result
) {
	final ISeq<Phenotype<DoubleGene, Vec<double[]>>> front = front(
		result.population(),
		this::dominance
	);
	best.addAll(front.asList());
	trim(best, size);
}

private int dominance(
	final Phenotype<DoubleGene, Vec<double[]>> a,
	final Phenotype<DoubleGene, Vec<double[]>> b
) {
	return Pareto.dominance(a.fitness().data(), b.fitness().data());
}

private boolean equals(final Phenotype<?, ?> a, final Phenotype<?, ?> b) {
	return Objects.equals(a.genotype(), b.genotype());
}

private void trim(
	final ParetoFront<Phenotype<DoubleGene, Vec<double[]>>> best,
	final IntRange size
) {
	if (best.size() > size.max() - 1) {
		best.trim(
			size.min(),
			this::compare,
			this::distance,
			v -> v.fitness().length()
		);
	}
}

private int compare(
	final Phenotype<DoubleGene, Vec<double[]>> a,
	final Phenotype<DoubleGene, Vec<double[]>> b,
	final int i
) {
	return a.fitness().compare(b.fitness(), i);
}

private double distance(
	final Phenotype<DoubleGene, Vec<double[]>> a,
	final Phenotype<DoubleGene, Vec<double[]>> b,
	final int i
) {
	return a.fitness().distance(b.fitness(), i);
}
@jenetics jenetics added this to the v6.1.0 milestone May 7, 2020
@jenetics jenetics self-assigned this May 7, 2020
jenetics added a commit that referenced this issue May 8, 2020
jenetics added a commit that referenced this issue May 10, 2020
jenetics added a commit that referenced this issue May 10, 2020
jenetics added a commit that referenced this issue May 11, 2020
jenetics added a commit that referenced this issue May 11, 2020
jenetics added a commit that referenced this issue May 11, 2020
jenetics added a commit that referenced this issue May 11, 2020
@jenetics
Copy link
Owner Author

Merged into r6.1.0 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant