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