ExponentialScaler.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.3.0).
003  * Copyright (c) 2007-2018 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 io.jenetics.internal.util.Hashes.hash;
024 
025 import java.io.Serializable;
026 import java.util.Objects;
027 import java.util.function.Function;
028 
029 /**
030  * Implements an exponential fitness scaling, whereby all fitness values are
031  * modified the following way.
032  <p><img src="doc-files/exponential-scaler.gif"
033  *          alt="f_s=\left(a\cdot f+b \rigth)^c"
034  *     >.</p>
035  *
036  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
037  @since 1.0
038  @version 2.0
039  */
040 public final class ExponentialScaler
041     implements
042         Function<Double, Double>,
043         Serializable
044 {
045     private static final long serialVersionUID = 2L;
046 
047     public static final ExponentialScaler SQR_SCALER = new ExponentialScaler(2);
048     public static final ExponentialScaler SQRT_SCALER = new ExponentialScaler(0.5);
049 
050     private final double _a;
051     private final double _b;
052     private final double _c;
053 
054     /**
055      * Create a new FitnessScaler.
056      *
057      @param <pre>fitness = (<strong>a</strong> * fitness + b) ^ c</pre>
058      @param <pre>fitness = (a * fitness + <strong>b</strong>) ^ c</pre>
059      @param <pre>fitness = (a * fitness + b) ^ <strong>c</strong></pre>
060      */
061     public ExponentialScaler(final double a, final double b, final double c) {
062         _a = a;
063         _b = b;
064         _c = c;
065     }
066 
067     /**
068      * Create a new FitnessScaler.
069      *
070      @param <pre>fitness = (1 * fitness + <strong>b</strong>) ^ c</pre>
071      @param <pre>fitness = (1 * fitness + b) ^ <strong>c</strong></pre>
072      */
073     public ExponentialScaler(final double b, final double c) {
074         this(1.0, b, c);
075     }
076 
077     /**
078      * Create a new FitnessScaler.
079      *
080      @param <pre>fitness = (1 * fitness + 0) ^ <strong>c</strong></pre>
081      */
082     public ExponentialScaler(final double c) {
083         this(1.00.0, c);
084     }
085 
086 
087     @Override
088     public Double apply(final Double value) {
089         return Math.pow(_a*value + _b, _c);
090     }
091 
092     @Override
093     public int hashCode() {
094         return hash(_a, hash(_b, hash(_c)));
095     }
096 
097     @Override
098     public boolean equals(final Object obj) {
099         return obj == this ||
100             obj instanceof ExponentialScaler &&
101             Objects.equals(((ExponentialScalerobj)._a, _a&&
102             Objects.equals(((ExponentialScaler)obj)._b, _b&&
103             Objects.equals(((ExponentialScaler)obj)._c, _c);
104     }
105 
106     @Override
107     public String toString() {
108         return format(
109             "%s[a=%f, b=%f, c=%f]",
110             getClass().getSimpleName(), _a, _b, _c
111         );
112     }
113 }