001 /*
002 * Java Genetic Algorithm Library (jenetics-1.6.0).
003 * Copyright (c) 2007-2014 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 java.lang.String.format;
023 import static org.jenetics.internal.util.object.eq;
024
025 import java.io.Serializable;
026
027 import javolution.lang.Immutable;
028
029 import org.jscience.mathematics.number.Float64;
030
031 import org.jenetics.internal.util.HashBuilder;
032
033 import org.jenetics.util.Function;
034
035 /**
036 * Implements an exponential fitness scaling, whereby all fitness values are
037 * modified the following way.
038 * <p><img src="doc-files/exponential-scaler.gif"
039 * alt="f_s=\left(a\cdot f+b \rigth)^c"
040 * >.</p>
041 *
042 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
043 * @since 1.0
044 * @version 1.0 — <em>$Date: 2014-03-05 $</em>
045 *
046 * @deprecated Will be removed in next major version, respectively replaced with
047 * a variant which will be parametrized with {@code Double}s.
048 */
049 @Deprecated
050 public final class ExponentialScaler
051 implements
052 Function<Float64, Float64>,
053 Serializable,
054 Immutable
055 {
056 private static final long serialVersionUID = 1L;
057
058 public static final ExponentialScaler SQR_SCALER = new ExponentialScaler(2);
059 public static final ExponentialScaler SQRT_SCALER = new ExponentialScaler(0.5);
060
061 private final double _a;
062 private final double _b;
063 private final double _c;
064
065 /**
066 * Create a new FitnessScaler.
067 *
068 * @param a <pre>fitness = (<strong>a</strong> * fitness + b) ^ c</pre>
069 * @param b <pre>fitness = (a * fitness + <strong>b</strong>) ^ c</pre>
070 * @param c <pre>fitness = (a * fitness + b) ^ <strong>c</strong></pre>
071 */
072 public ExponentialScaler(final double a, final double b, final double c) {
073 _a = a;
074 _b = b;
075 _c = c;
076 }
077
078 /**
079 * Create a new FitnessScaler.
080 *
081 * @param b <pre>fitness = (1 * fitness + <strong>b</strong>) ^ c</pre>
082 * @param c <pre>fitness = (1 * fitness + b) ^ <strong>c</strong></pre>
083 */
084 public ExponentialScaler(final double b, final double c) {
085 this(1.0, b, c);
086 }
087
088 /**
089 * Create a new FitnessScaler.
090 *
091 * @param c <pre>fitness = (1 * fitness + 0) ^ <strong>c</strong></pre>
092 */
093 public ExponentialScaler(final double c) {
094 this(0.0, c);
095 }
096
097
098 @Override
099 public Float64 apply(final Float64 value) {
100 return Float64.valueOf(Math.pow((_a*value.doubleValue() + _b), _c));
101 }
102
103 @Override
104 public int hashCode() {
105 return HashBuilder.of(getClass()).and(_a).and(_b).and(_c).value();
106 }
107
108 @Override
109 public boolean equals(final Object obj) {
110 if (obj == this) {
111 return true;
112 }
113 if (obj == null || obj.getClass() != getClass()) {
114 return false;
115 }
116
117 final ExponentialScaler selector = (ExponentialScaler)obj;
118 return eq(_a, selector._a) && eq(_b, selector._b) && eq(_c, selector._c);
119 }
120
121 @Override
122 public String toString() {
123 return format(
124 "%s[a=%f, b=%f, c=%f]",
125 getClass().getSimpleName(), _a, _b, _c
126 );
127 }
128 }
|