<commit_before>/**
 * Computational Intelligence Library (CIlib)
 * Copyright (C) 2003 - 2010
 * Computational Intelligence Research Group (CIRG@UP)
 * Department of Computer Science
 * University of Pretoria
 * South Africa
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
package net.sourceforge.cilib.functions.discrete;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.cilib.functions.ContinuousFunction;
import net.sourceforge.cilib.type.types.container.Vector;


/**
 * Implementation of Hollands Royal Road function.
 * Intended for bit strings of length 240
 * <p>
 * References:
 * </p>
 * <ul><li>
 * <NAME>;, "A Description of Holland's Royal Road Function" (1994).
 * Sante Fe Institute
 * </li></ul>
 */
public class RoyalRoad implements ContinuousFunction {

    private int k;
    private int b;
    private int g;
    private int mstar;
    private double v;
    private double ustar;
    private double u;

    public RoyalRoad() {
        // set up default parameters as described by Holland
        k = 4;
        b = 8;
        g = 7;
        mstar = 4;
        v = 0.02;
        ustar = 1.0;
        u = 0.3;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Double apply(Vector input) {
        Preconditions.checkArgument(input.size() == Math.pow(2,k) * b * g,
            "Royal Road function must be used with vectors of length " + Math.pow(2,k) * b * g + " for the given configuration.");

        return part(input) + bonus(input);
    }

    private Double part(Vector input) {
        double partFitness = 0.0;

        List<Vector> blocks = getBlocks(input);
        for (Vector block : blocks) {
            int numOnes = 0;
            for(int i = 0; i < block.size(); i++) {
                if (block.booleanValueOf(i)) {
                    numOnes++;
                }
            }

            if (numOnes != b) {
                if (numOnes <= mstar) {
                    partFitness += numOnes * v;
                } else {
                    partFitness += (numOnes - mstar) * (-v);
                }
            }
        }

        return partFitness;
    }

    private Double bonus(Vector input) {<commit_msg>Fixed a mistake in a precondition.<commit_after>/**
 * Computational Intelligence Library (CIlib)
 * Copyright (C) 2003 - 2010
 * Computational Intelligence Research Group (CIRG@UP)
 * Department of Computer Science
 * University of Pretoria
 * South Africa
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
package net.sourceforge.cilib.functions.discrete;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.cilib.functions.ContinuousFunction;
import net.sourceforge.cilib.type.types.container.Vector;


/**
 * Implementation of Hollands Royal Road function.
 * Intended for bit strings of length 240
 * <p>
 * References:
 * </p>
 * <ul><li>
 * <NAME>;, "A Description of Holland's Royal Road Function" (1994).
 * Sante Fe Institute
 * </li></ul>
 */
public class RoyalRoad implements ContinuousFunction {

    private int k;
    private int b;
    private int g;
    private int mstar;
    private double v;
    private double ustar;
    private double u;

    public RoyalRoad() {
        // set up default parameters as described by Holland
        k = 4;
        b = 8;
        g = 7;
        mstar = 4;
        v = 0.02;
        ustar = 1.0;
        u = 0.3;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Double apply(Vector input) {
        Preconditions.checkArgument(input.size() == Math.pow(2,k) * (b + g),
            "Royal Road function must be used with vectors of length " + Math.pow(2,k) * (b + g) + " for the given configuration.");

        return part(input) + bonus(input);
    }

    private Double part(Vector input) {
        double partFitness = 0.0;

        List<Vector> blocks = getBlocks(input);
        for (Vector block : blocks) {
            int numOnes = 0;
            for(int i = 0; i < block.size(); i++) {
                if (block.booleanValueOf(i)) {
                    numOnes++;
                }
            }

            if (numOnes != b) {
                if (numOnes <= mstar) {
                    partFitness += numOnes * v;
                } else {
                    partFitness += (numOnes - mstar) * (-v);
                }
            }
        }

        return partFitness;
    }

    private Double bonus(Vector input) {
{'fraction_non_alphanumeric': 0.09876757507377192, 'fraction_numerical': 0.011456344384655441, 'mean_word_length': 2.2388982574480045, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '682001', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1567, 'n_words': 591}