001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics;
021
022import static java.lang.String.format;
023import static java.util.Objects.requireNonNull;
024
025import io.jenetics.util.ISeq;
026import io.jenetics.util.MSeq;
027import io.jenetics.util.RandomRegistry;
028import io.jenetics.util.Seq;
029
030/**
031 * The Monte Carlo selector selects the individuals from a given population
032 * randomly. This selector can be used to measure the performance of another
033 * selector. In general, the performance of a selector should be better than
034 * the selection performance of the Monte Carlo selector.
035 *
036 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
037 * @since 1.0
038 * @version 5.0
039 */
040public final class MonteCarloSelector<
041        G extends Gene<?, G>,
042        C extends Comparable<? super C>
043>
044        implements Selector<G, C>
045{
046
047        public MonteCarloSelector() {
048        }
049
050        @Override
051        public ISeq<Phenotype<G, C>> select(
052                final Seq<Phenotype<G, C>> population,
053                final int count,
054                final Optimize opt
055        ) {
056                requireNonNull(population, "Population");
057                requireNonNull(opt, "Optimization");
058                if (count < 0) {
059                        throw new IllegalArgumentException(format(
060                                "Selection count must be greater or equal then zero, but was %d.",
061                                count
062                        ));
063                }
064
065                final MSeq<Phenotype<G, C>> selection;
066                if (count > 0 && !population.isEmpty()) {
067                        selection = MSeq.ofLength(count);
068                        final var random = RandomRegistry.random();
069                        final int size = population.size();
070
071                        for (int i = 0; i < count; ++i) {
072                                final int pos = random.nextInt(size);
073                                selection.set(i, population.get(pos));
074                        }
075                } else {
076                        selection = MSeq.empty();
077                }
078
079                return selection.toISeq();
080        }
081
082        @Override
083        public String toString() {
084                return format("%s", getClass().getSimpleName());
085        }
086
087}