// Catalano Genetic Library
// The Catalano Framework
//
// Copyright © Diego Catalano, 2012-2018
// diego.catalano at live.com
//
//
//    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 2.1 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, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
package Catalano.Evolutionary.Metaheuristics.Monoobjective;

import Catalano.Core.DoubleRange;
import Catalano.Math.Matrix;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Represents individual in the population.
 * @author Diego Catalano
 */
public class Individual implements Comparable, Cloneable {
    
    private double[] location;
    private double fitness;
    
    public static List CreatePopulation(int populationSize, List boundConstraints, IObjectiveFunction function){
        
        List population = new ArrayList<>(populationSize);
        for (int i = 0; i < populationSize; i++) {
            double[] location = Matrix.UniformRandom(boundConstraints);
            double fitness = function.Compute(location);
            population.add(new Individual(location, fitness));
        }
        
        return population;
        
    }
    
    public static List CreatePopulation(double[][] population, double[] fitness){
        
        List pop = new ArrayList<>(population.length);
        for (int i = 0; i < population.length; i++) {
            pop.add(new Individual(population[i], fitness[i]));
        }
        
        return pop;
        
    }

    /**
     * Get location in the space.
     * @return Location.
     */
    public double[] getLocation() {
        return location;
    }
    
    /**
     * Get location in the space.
     * @param index Index.
     * @return Value.
     */
    public double getLocation(int index){
        return location[index];
    }

    /**
     * Set location in the space.
     * @param location Location.
     */
    public void setLocation(double[] location) {
        this.location = location;
    }
    
    /**
     * Set location in the space.
     * @param index Index.
     * @param location Location.
     */
    public void setLocation(int index, double location){
        this.location[index] = location;
    }

    /**
     * Get fitness.
     * @return Fitness.
     */
    public double getFitness() {
        return fitness;
    }

    /**
     * Set fitness.
     * @param fitness Fitness.
     */
    public void setFitness(double fitness) {
        this.fitness = fitness;
    }
    
    /**
     * Initialize a new instance of the Individual class.
     * @param location Location.
     */
    public Individual(double[] location){
        this(location, Double.NaN);
    }

    /**
     * Initialize a new instance of the Individual class.
     * @param location Location.
     * @param fitness Fitness.
     */
    public Individual(double[] location, double fitness) {
        this.location = location;
        this.fitness = fitness;
    }

    @Override
    public int compareTo(Individual o) {
        return Double.compare(fitness, o.getFitness());
    }
    
    public Individual getClone(){
        return new Individual(Arrays.copyOf(location, location.length), fitness);
    }
    
}
 

Ads help maintain this website.