MonteCarloSelector.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.0.0).
003  * Copyright (c) 2007-2017 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  */
020 package io.jenetics;
021 
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 
025 import java.util.Random;
026 
027 import io.jenetics.internal.util.Equality;
028 import io.jenetics.internal.util.Hash;
029 import io.jenetics.util.ISeq;
030 import io.jenetics.util.MSeq;
031 import io.jenetics.util.RandomRegistry;
032 import io.jenetics.util.Seq;
033 
034 /**
035  * The Monte Carlo selector selects the individuals from a given population
036  * randomly. This selector can be used to measure the performance of a other
037  * selectors. In general, the performance of a selector should be better than
038  * the selection performance of the Monte Carlo selector.
039  *
040  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
041  @since 1.0
042  @version 4.0
043  */
044 public final class MonteCarloSelector<
045     extends Gene<?, G>,
046     extends Comparable<? super C>
047 >
048     implements Selector<G, C>
049 {
050 
051     public MonteCarloSelector() {
052     }
053 
054     @Override
055     public ISeq<Phenotype<G, C>> select(
056         final Seq<Phenotype<G, C>> population,
057         final int count,
058         final Optimize opt
059     ) {
060         requireNonNull(population, "Population");
061         requireNonNull(opt, "Optimization");
062         if (count < 0) {
063             throw new IllegalArgumentException(format(
064                 "Selection count must be greater or equal then zero, but was %d.",
065                 count
066             ));
067         }
068 
069         final MSeq<Phenotype<G, C>> selection = MSeq
070             .ofLength(population.isEmpty() : count);
071 
072         if (count > && !population.isEmpty()) {
073             final Random random = RandomRegistry.getRandom();
074             final int size = population.size();
075 
076             for (int i = 0; i < count; ++i) {
077                 final int pos = random.nextInt(size);
078                 selection.set(i, population.get(pos));
079             }
080         }
081 
082         return selection.toISeq();
083     }
084 
085     @Override
086     public int hashCode() {
087         return Hash.of(getClass()).value();
088     }
089 
090     @Override
091     public boolean equals(final Object obj) {
092         return Equality.ofType(this, obj);
093     }
094 
095     @Override
096     public String toString() {
097         return format("%s", getClass().getSimpleName());
098     }
099 
100 }