Package eas.users.lukas.neuroCEGPM.selection

Source Code of eas.users.lukas.neuroCEGPM.selection.SelectionTournament

/*
* File name:        SelectionTournament.java (package eas.users.lukas.neuroCEGPM.selection)
* Author(s):        Lukas König
* Java version:     7.0
* Generation date:  04.03.2014 (09:26:09)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.users.lukas.neuroCEGPM.selection;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import eas.miscellaneous.StaticMethods;
import eas.users.lukas.neuroCEGPM.RobNeural;

/**
* @author Lukas König
*/
public class SelectionTournament implements Selection {

    private static final long serialVersionUID = 1462365765040422087L;
    private int tournamentSize = 3;
    private Random rand;
   
    public SelectionTournament(Random rand) {
        this.rand = rand;
    }
   
    @Override
    public void select(List<RobNeural> population) {
        HashSet<RobNeural> visited = new HashSet<RobNeural>();
        LinkedList<RobNeural> tournament;
       
        while (visited.size() <= population.size() - tournamentSize) {
            tournament = new LinkedList<RobNeural>();
            RobNeural bestRob = null;
            double bestFit = Double.NEGATIVE_INFINITY;
           
            while (tournament.size() < tournamentSize) {
                RobNeural rob = population.get(rand.nextInt(population.size()));
               
                if (!visited.contains(rob)) {
                    tournament.add(rob);
                    visited.add(rob);
                   
                    if (rob.getFitness() > bestFit) {
                        bestFit = rob.getFitness();
                        bestRob = rob;
                    }
                }
            }
           
            LinkedList<Thread> threads = new LinkedList<Thread>();
            for (RobNeural rob : tournament) {
                LinkedList<BigDecimal> genomeTrans = null;
                if (bestRob.getGenomeTrans() != null) {
                    genomeTrans = new LinkedList<>(bestRob.getGenomeTrans());
                }
               
                threads.add(rob.setController(
                        new LinkedList<BigDecimal>(bestRob.getGenome()),
                        genomeTrans,
                        bestRob.getRand(),
                        true));
            }
           
            StaticMethods.joinThreads(threads);

            for (RobNeural rob : tournament) {
                rob.setFitness(bestRob.getFitness());
            }
        }
    }
}
TOP

Related Classes of eas.users.lukas.neuroCEGPM.selection.SelectionTournament

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.