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