Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.api.Individual.java

Here you can find all the information about the file org.dgpf.search.api.Individual.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-03-30 13:08:24
 * Original Filename: org.dgpf.search.api.Individual.java
 * Version          : 2.0.2
 * Last modification: 2006-07-14
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" 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.
 */

 
package org.dgpf.search.api;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.sfc.collections.Arrays;
import org.sfc.meta.BoundedDoubleProperty;
import org.sfc.meta.MetaInformation;
import org.sfc.meta.Property;
import org.sfc.text.Text;
import org.sfc.utils.ICloneable;
import org.sfc.utils.Typesafe;

/**
 * The public individual-holder class containing data of each individual of
 * the population.
 *
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.
 *
 * @author Thomas Weise
 */

public class Individual<Genotype extends    Serializable>
                                 implements Serializable, ICloneable
  {
/**
 * The serial version uid.
 */

  private static final long serialVersionUID = 3;


/**
 * The property for the individual fitness functions.
 */

  public  static  final BoundedDoubleProperty INDIVIDUAL_FITNESS  ;
/**
 * The property for the individual data.
 */

  public  static  final Property<Serializable> INDIVIDUAL_DATA  ;
/**
 * The meta information for individuals.
 */

  public  static  final MetaInformation       INDIVIDUAL_METAINFO ;
  
  static
    {
    INDIVIDUAL_FITNESS =
             new BoundedDoubleProperty( "fitness",
                               SearchUtils.INDIVIDUAL_PROPERTIES,
                               0.0d, Double.MAX_VALUE);
    INDIVIDUAL_FITNESS.add_port(Individual.class, true, false, 
                                SearchUtils.FITNESS_FUNCTION_COUNT_FUNC);
    SearchData.FITNESS_FUNCTION_COUNT.add_port(
          Individual.class, true, false, null);
    
    INDIVIDUAL_DATA =
             new Property<Serializable>(
                               "data",
                               SearchUtils.INDIVIDUAL_PROPERTIES,
                               Serializable.class);
    INDIVIDUAL_DATA.add_port(Individual.class"get_individual", null, null);
    
    INDIVIDUAL_METAINFO = new MetaInformation();
    }
  
/**
 * The individual managed.
 */

  private transient Genotype    m_individual    ;
  
/**
 * The fitness values returned by the individual fitness functions.
 * @see #get_fitness(int)
 */

  private transient double[]    m_fitnesses   ;

/**
 * Create a new individual which can be used by a search involving
 * <code>p_fitness_count</code> fitness functions.
 * @param p_fitness_count The count of fitness functions that might be
 *                        applied to the individual.
 */

  public  Individual  (final int p_fitness_count)
    {
    super();
    this.m_fitnesses = new double[p_fitness_count];
    }
  
/**
 * The hidden, deserialization constructor.
 */

  Individual  ()
    {
    super();
    }


/**
 * Obtain the count of fitness values available for this individual.
 * @return  The count of fitness values available for this individual.
 */

  public  final int get_fitness_function_count ()
    {
    return this.m_fitnesses.length;
    }

/**
 * Obtain the fitness value of the fitness function at the specified index.
 * @param p_index The fitness function's index.
 * @return  The fitness value of the fitness function at the specified
 *          index.
 */

  public  final double  get_fitness (final int p_index)
    {
    return this.m_fitnesses[p_index];
    }


/**
 * Returns the individual stored in this information record.
 * @return The individual stored in this information record.
 */

  public  final Genotype  get_individual  ()
    {
    return this.m_individual;
    }

/**
 * Creates and returns a copy of this object.  The precise meaning
 * of "copy" may depend on the class of the object.
 *
 * @return     A clone of this instance.
 *
 * @see java.lang.Cloneable
 */

  @Override
  public  final Object  clone ()
    {
    Individual<Genotype>  l_i;
    try
      {
      l_i             = Typesafe.cast(super.clone());
      l_i.m_fitnesses = l_i.m_fitnesses.clone();
      return l_i;
      }
    catch(Throwable l_t)
      {
      return this//this will never happen.
      }
    }


/**
 * Set the fitness value of the fitness function at the specified index.
 * @param p_index   The fitness function's index.
 * @param p_fitness The fitness value of the fitness function at the
 *                  specified index.
 */

  protected final void set_fitness (final int    p_index,
                                    final double p_fitness)
    {
    this.m_fitnesses[p_index] = SearchUtils.format_positive(p_fitness);
    }


/**
 * Sets the individual to be stored in this information record.
 * @param p_individual The individual to be stored in this information
 *                     record.
 */

  protected final void  set_individual  (final Genotype p_individual)
    {
    this.m_individual = p_individual;
    }

/**
 * Assign this fitness record to the values of another one.
 * @param p_individual  The other fitness record.
 */

  protected final void  assign  (final Individual<Genotype> p_individual)
    {
    double[]  l_d2;
    int       l_i;

    l_d2 = p_individual.m_fitnesses;
    for(l_i = (l_d2.length-1); l_i >= 0; l_i--)
      {
      this.set_fitness(l_i, l_d2[l_i]);
      }

    this.set_individual(p_individual.m_individual);
    }




/**
 * Drop all data stored in this individual.
 */

  protected final void  clear ()
    {
    this.m_individual = null;
    if(this.m_fitnesses != null)
      {
      Arrays.fill(this.m_fitnesses, 0.0d);
      }
    }
  
/**
 * Clear an individual's fitness.
 */

  final void  clear_fitness()
    {
    if(this.m_fitnesses != null)
      {
      Arrays.fill(this.m_fitnesses, 0.0d);
      }
    }
  
/**
 * Attach a human readable representation of this individual to a string
 * builder.
 * @param p_sb    The string builder to write to.
 */

  public  final void  print (final StringBuilder p_sb)
    {
    final String        l_s;
          int           l_i;
    final double[]      l_dd;

    l_s = String.valueOf(this.m_individual);
    
    p_sb.append(l_s);
    
    if(l_s.indexOf('\n') >= 0) p_sb.append(Text.LINE_BREAK_CHARS);
    else                       p_sb.append(' ');
    
    p_sb.append('[');
    l_dd = this.m_fitnesses;
    
    for(l_i = 0; l_i < l_dd.length; l_i++)
      {      
      p_sb.append(Text.double_to_string(l_dd[l_i]));
      p_sb.append(' ');
      }
    p_sb.setCharAt(p_sb.length()-1, ']');
    }
  
/**
 * Obtain a string representation of this individual.
 * @return  The string representation of this individual.
 */

  @Override
  public  final String  toString  ()
    {
    StringBuilder l_sb;
    
    l_sb = new StringBuilder();
    this.print(l_sb);
    
    return l_sb.toString();
    }
  
/**
 * Write an individual's most important data to an object output stream.
 * @param p_oos The object output stream to write to.
 * @throws  IOException If io fails.
 */

  public  final void  transmit  (final ObjectOutputStream p_oos)
      throws IOException
    {
          int       l_i;
    final double[]  l_d;

//    p_oos.writeObject(this.m_individual);
    p_oos.writeUnshared(this.m_individual);
// This was removed to reduce traffic ... but it might be needed if the
// individuals use the same original object for transmission multiple
// times while changing its contents.

    l_d = this.m_fitnesses;
    l_i = l_d.length;
    p_oos.writeInt(l_i);
    for(--l_i; l_i >= 0; l_i--)
      {
      p_oos.writeDouble(l_d[l_i]);
      }
    }

/**
 * Read an individual's most important data from an object input stream.
 * @param p_ois The object input stream to read from.
 * @param p_ffc The count of fitness functions.
 * @return The newly read individual.
 * @throws  IOException             If io fails.
 * @throws  ClassNotFoundException  If the individuals genotype is
 *                                  invalid.
 */

  public static final Individual<?>  receive (final ObjectInputStream p_ois,
                                              final int               p_ffc)
        throws IOException, ClassNotFoundException
    {
    Individual<Serializable> l_iv;
    
    l_iv   = new Individual<Serializable>();    
    
    do_receive(l_iv, p_ois);
    
    return l_iv;
    }

  
/**
 * Read an individual's data from a stream.
 * @param p_individual    The individual to read.
 * @param p_ois           The object input stream to read from.
 * @throws  IOException             If io fails.
 * @throws  ClassNotFoundException  If the individuals genotype is
 *                                  invalid
 */

  protected static  final void do_receive
                          (final Individual<Serializable> p_individual,
                           final ObjectInputStream        p_ois)
        throws IOException, ClassNotFoundException
    { 
    int l_ffc;
    
    p_individual.clear();
    p_individual.set_individual((Serializable)(p_ois.readUnshared()));
        //p_ois.readObject()));
    
    l_ffc                    = p_ois.readInt();    
    if(p_individual.m_fitnesses == null)
      {
      p_individual.m_fitnesses = new double[l_ffc];
      }
    
//    (p_ois.readUnshared()));
// This was removed to reduce traffic ... but it might be needed if the
// individuals use the same original object for transmission multiple
// times while changing its contents.
    
    for(l_ffc = (p_individual.get_fitness_function_count()-1); l_ffc >= 0;
        l_ffc--)
      {
      p_individual.set_fitness(l_ffc, p_ois.readDouble());
      } 
    }
  
/**
 * Stores the <code>SfcEvent</code> into the stream.
 * @param p_s The output stream.
 * @throws  IOException If something io-like went wrong.
 */

  private final void writeObject(final ObjectOutputStream p_s)
        throws IOException
    {
    p_s.defaultWriteObject();    
    this.transmit(p_s);
    }
  


/**
 * Reconstitute the <code>SfcEvent</code> instance from a stream (that is,
 * deserialize it).
 * @param p_s The input stream.
 * @throws  IOException If something io-like went wrong.
 * @throws  ClassNotFoundException  If a needed class could not be found.
 */

  private final void readObject(final ObjectInputStream p_s)
        throws IOException, ClassNotFoundException
    {    
    Individual<Serializable> l_i;
    
    p_s.defaultReadObject();
    l_i = Typesafe.cast(this);
    do_receive(l_i, p_s);
    }
  }

File Information:

file name:Individual.java
package:org.dgpf.search.api
qualified name:org.dgpf.search.api.Individual.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/Individual.java
size:11.543 KB (11821 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-08-16 07:34:58 GMT+0000
last access: 2017-11-19 04:37:33 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-19 04:37:33 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo