001 /*
002 * Java Genetic Algorithm Library (jenetics-1.5.0).
003 * Copyright (c) 2007-2013 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@gmx.at)
019 */
020 package org.jenetics;
021
022 import static org.jenetics.util.object.checkProbability;
023 import static org.jenetics.util.object.eq;
024 import static org.jenetics.util.object.hashCodeOf;
025
026 /**
027 * Abstract implementation of the alterer interface.
028 *
029 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
030 * @since 1.0
031 * @version 1.0 — <em>$Date: 2013-12-02 $</em>
032 */
033 public abstract class AbstractAlterer<G extends Gene<?, G>>
034 implements Alterer<G>
035 {
036
037 /**
038 * Return an alterer which does nothing.
039 *
040 * @return an alterer which does nothing.
041 */
042 public static <G extends Gene<?, G>> Alterer<G> Null() {
043 return new Alterer<G>() {
044 @Override
045 public <C extends Comparable<? super C>> int alter(
046 final Population<G, C> population,
047 final int generation
048 ) {
049 return 0;
050 }
051
052 @Override
053 public int hashCode() {
054 return hashCodeOf(getClass()).value();
055 }
056
057 @Override
058 public boolean equals(final Object obj) {
059 return obj == this ||
060 obj != null && obj.getClass() == getClass();
061 }
062
063 @Override
064 public String toString() {
065 return "Alterer.Null";
066 }
067 };
068 }
069
070 public static final double DEFAULT_ALTER_PROBABILITY = 0.2;
071
072 /**
073 * The altering probability.
074 */
075 protected final double _probability;
076
077 /**
078 * Constructs an alterer with a given recombination probability.
079 *
080 * @param probability The recombination probability.
081 * @throws IllegalArgumentException if the {@code probability} is not in the
082 * valid range of {@code [0, 1]}.
083 */
084 protected AbstractAlterer(final double probability) {
085 _probability = checkProbability(probability);
086 }
087
088 /**
089 * Return the recombination/alter probability for this alterer.
090 *
091 * @return The recombination probability.
092 */
093 public double getProbability() {
094 return _probability;
095 }
096
097 @Override
098 public int hashCode() {
099 return hashCodeOf(getClass()).and(_probability).value();
100 }
101
102 @Override
103 public boolean equals(final Object obj) {
104 if (obj == this) {
105 return true;
106 }
107 if (!(obj instanceof AbstractAlterer<?>)) {
108 return false;
109 }
110
111 final AbstractAlterer<?> alterer = (AbstractAlterer<?>)obj;
112 return eq(_probability, alterer._probability);
113 }
114 }
115
116
117
|