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