Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Cropping a zoomed image in VB.NetExploring Reactive Extensions - Testing »
    comments

    Integrating jNEAT into a Custom Simulation

    by Rob Earl on Dec 18, 2010 in categories Java

    jNEAT is a Java implementation of the NEAT method. NEAT is a technique for evolving neural networks, which uses genetic algorithms to learn network structures aswell as weights. For more information there are several papers at the links above.

    Building jNEAT

    Download the latest version: http://nn.cs.utexas.edu/?neat-java
    Download and install Apache Ant: http://ant.apache.org/ (`apt-get install ant` on Ubuntu/Debian)

    It's fairly straightforward to build a jNEAT library using Ant. Extract the archive somewhere and place the following code into build.xml in the root directory.

    1. <project name="jneat" default="jar" basedir=".">
    2.   <property name="src" value="."/>
    3.  
    4.   <target name="compile">
    5.     <javac srcdir="${src}"/>
    6.   </target>
    7.  
    8.   <target name="compile-debug">
    9.     <javac debug="on" srcdir="${src}"/>
    10.   </target>
    11.  
    12.   <target name="jar" depends="compile">
    13.     <jar jarfile="jneat.jar" basedir="${src}">
    14.        <include name="gui/*.class"/>
    15.        <include name="jneat/*.class"/>
    16.        <include name="jNeatCommon/*.class"/>
    17.        <include name="log/*.class"/>
    18.     </jar>
    19.   </target>
    20. </project>

    Running `ant` from a command line in the jNEAT root directory will produce jneat.jar which can be added to your development environment such as:

    NetBeans: Right click Libraries->Add JAR/Folder
    Eclipse: Right click project->Build Path->Add External Archives

    Create a jNEAT Population

    Import the jNEAT library.

    1. import jneat.*;

    Initialise a jNEAT population:

    1. neatPop = new Population(30 /* population size */, 9 /* network inputs */ , 2 /* network outputs */, 5 /* max index of nodes */, true /* recurrent */, 0.5 /* probability of connecting two nodes */ );

    The only parameters we need to worry about are the population size, network inputs and network outputs. The value of these depend on the simulation. Associate each member of this population with one member of your simulation.

    Evaluation

    Evaluate the population by running your simulation for a set period of time, query each organism's network for actions every update.

    1. // Within main simulation loop
    2. //
    3.  
    4. Vector neatOrgs = neatPop.getOrganisms();
    5.  
    6. for(int i=0;i<neatOrgs.size();i++)
    7. {
    8.   // Extract the neural network from the jNEAT organism.
    9.   Network brain = ((Organism)neatOrgs.get(i)).getNet();
    10.  
    11.   double inputs[] = new double[numInputs+1];
    12.   inputs[numInputs] = -1.0; // Bias
    13.  
    14.   // Populate the rest of "inputs" from this organism's status in the simulation.
    15.   //
    16.   //
    17.  
    18.   // Load these inputs into the neural network.
    19.   brain.load_sensors(inputs);
    20.  
    21.   int net_depth = brain.max_depth();
    22.   // first activate from sensor to next layer....
    23.   brain.activate();
    24.  
    25.   // next activate each layer until the last level is reached
    26.   for (int relax = 0; relax <= net_depth; relax++)
    27.   {
    28.       brain.activate();
    29.   }
    30.        
    31.   // Retrieve outputs from the final layer.
    32.   double output1 = ((NNode) brain.getOutputs().elementAt(0)).getActivation();
    33.   double output2 = ((NNode) brain.getOutputs().elementAt(1)).getActivation();
    34.  
    35.   // Use the outputs to modify the associated member of the population.
    36.   //
    37.   //
    38.  
    39. }

    Evolution

    After this evaluation process has run for enough iterations, assign each member a fitness and move to the next generation by instructing jNEAT to evolve the population:

    1. Vector neatOrgs = neatPop.getOrganisms();
    2.  
    3. for(int i=0;i<neatOrgs.size();i++)
    4. {
    5.   // Assign each organism a "fitness". A measure of how well the organism performed since the last evolution.
    6.   ((Organism)neatOrgs.get(i)).setFitness(fitness);
    7. }
    8.  
    9. neatPop.epoch(generation++); // Evolve the population and increment the generation.

    Examining the Population

    There are a couple of methods which can be used to see if the population is changing at all:

    neatPop.viewtext();

    This will print a breakdown of the population and can help diagnose problems if you aren't seeing any improvements.

    jNEAT also comes with a handy GUI which can display a visual representation of each organism's neural network. Before doing this the population needs to be saved to a file:

    neatPop.print_to_file_by_species("SavedPopulation.txt");

    To run the GUI, open a command line and run the following:

    1. cd /path/to/jNEAT/gui/
    2. ./run.sh gui.MainGui

    Select the View Graph tab, click Load Pop and locate the saved population. Selecting an organism from the list on the left will display its network structure.

    1434 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)