de.terministic.serein.core.genome.recombination.WholeArithmeticRecombination.java Source code

Java tutorial

Introduction

Here is the source code for de.terministic.serein.core.genome.recombination.WholeArithmeticRecombination.java

Source

/**
 * Copyright (C) 2015 Tobias Uhlig (tobias.uhlig@unibw.de)
 *
 * 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.
 */
package de.terministic.serein.core.genome.recombination;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.math3.exception.OutOfRangeException;

import de.terministic.serein.core.genome.DoubleGenome;

public class WholeArithmeticRecombination extends PairRecombination<DoubleGenome> {
    final double dominance;

    public WholeArithmeticRecombination(double dominance) {
        if (dominance < 0.0 || dominance > 1.0) {
            throw new OutOfRangeException(dominance, 0.0, 1.0);
        }
        this.dominance = dominance;
    }

    /**
     * Generates a Whole Arithmetic Recombination operator with dominance value
     * 0.5. This particular instance is also called Average Recombination.
     */
    public WholeArithmeticRecombination() {
        this(0.5);
    }

    @Override
    DoubleGenome recombine(DoubleGenome g1, DoubleGenome g2, Random random) {
        List<Double> genes = new ArrayList<>(g1.getGenes());
        for (int i = 0; i < g1.size(); i++) {
            double value = dominance * g1.getGenes().get(i) + (1 - dominance) * g2.getGenes().get(i);
            genes.set(i, value);
        }
        return g1.createInstance(genes);
    }

}