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