All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.ga.operators.selection.SelectionFunction Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2010-2018 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite 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.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite 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 Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see .
 */
package org.evosuite.ga.operators.selection;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.evosuite.ga.Chromosome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Abstract base class of selection functions
 * 
 * @author Gordon Fraser
 */
public abstract class SelectionFunction implements Serializable {

	private static final long serialVersionUID = -2514933149542277609L;

	/** Constant logger */
	protected static final Logger logger = LoggerFactory.getLogger(SelectionFunction.class);

	/**
	 * Do we want to minimize or maximize fitness?
	 */
	protected boolean maximize = true;

	/**
	 * Return index of next offspring
	 * 
	 * @param population
	 *            a {@link java.util.List} object.
	 * @return a int.
	 */
	public abstract int getIndex(List population);

	/**
	 * Return two parents
	 * 
	 * @param population
	 *            a {@link java.util.List} object.
	 * @return a {@link org.evosuite.ga.Chromosome} object.
	 */
	public T select(List population) {
		return select(population, 1).get(0);
	}

	/**
	 * Return n parents
	 * 
	 * @param population
	 *            a {@link java.util.List} object.
	 * @param number
	 *            n
	 * @return a {@link java.util.List} object.
	 */
	public List select(List population, int number) {
		List offspring = new ArrayList();
		for (int i = 0; i < number; i++) {
			offspring.add(population.get(getIndex(population)));
		}
		return offspring;
	}

	/**
	 * Are we maximizing or minimizing fitness?
	 * 
	 * @param max
	 *            a boolean.
	 */
	public void setMaximize(boolean max) {
		maximize = max;
	}

	/**
	 * 

* isMaximize *

* * @return true is we have to maximize */ public boolean isMaximize() { return maximize; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy