org.jenetics.Optimize Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.jenetics Show documentation
Show all versions of org.jenetics Show documentation
Jenetics - Java Genetic Algorithm Library
/*
* Java Genetic Algorithm Library (jenetics-3.4.0).
* Copyright (c) 2007-2016 Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter ([email protected])
*/
package org.jenetics;
import java.util.Comparator;
/**
* This {@code enum} determines whether the GA should maximize or minimize the
* fitness function.
*
* @author Franz Wilhelmstötter
* @since 1.0
* @version 3.0
*/
public enum Optimize {
/**
* GA minimization
*/
MINIMUM {
@Override
public >
int compare(final T a, final T b)
{
return b.compareTo(a);
}
},
/**
* GA maximization
*/
MAXIMUM {
@Override
public >
int compare(final T a, final T b)
{
return a.compareTo(b);
}
};
/**
* Compares two comparable objects. Returns a negative integer, zero, or a
* positive integer as the first argument is better than, equal to, or worse
* than the second.
*
* @param the comparable type
* @param a the first object to be compared.
* @param b the second object to be compared.
* @return a negative integer, zero, or a positive integer as the first
* argument is better than, equal to, or worse than the second.
* @throws NullPointerException if one of the arguments is {@code null}.
*/
public abstract >
int compare(final T a, final T b);
/**
* Create an appropriate comparator of the given optimization strategy. A
* collection of comparable objects with the returned comparator will be
* sorted in descending order, according to the given definition
* of better and worse.
*
* {@code
* final Population population = ...
* population.sort(Optimize.MINIMUM.descending());
* }
*
* The code example above will populationSort the population according it's fitness
* values in ascending order, since lower values are better in this
* case.
*
* @param the type of the objects to compare.
* @return a new {@link Comparator} for the type {@code T}.
*/
public > Comparator descending() {
return (a, b) -> compare(b, a);
}
/**
* Create an appropriate comparator of the given optimization strategy. A
* collection of comparable objects with the returned comparator will be
* sorted in ascending order, according to the given definition
* of better and worse.
*
* {@code
* final Population population = ...
* population.sort(Optimize.MINIMUM.ascending());
* }
*
* The code example above will populationSort the population according it's fitness
* values in descending order, since lower values are better in this
* case.
*
* @param the type of the objects to compare.
* @return a new {@link Comparator} for the type {@code T}.
*/
public > Comparator ascending() {
return this::compare;
}
/**
* Return the best value, according to this optimization direction.
*
* @param the fitness value type.
* @param a the first value.
* @param b the second value.
* @return the best value. If both values are equal the first one is returned.
*/
public > C best(final C a, final C b) {
return compare(b, a) > 0 ? b : a;
}
/**
* Return the worst value, according to this optimization direction.
*
* @param the fitness value type.
* @param a the first value.
* @param b the second value.
* @return the worst value. If both values are equal the first one is returned.
*/
public > C worst(final C a, final C b) {
return compare(b, a) < 0 ? b : a;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy