Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.netautomaton.base.NetUtilities.java

Here you can find all the information about the file org.dgpf.gp.netautomaton.base.NetUtilities.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-01-02 17:03:44
 * Original Filename: org.dgpf.netautomaton.base.NetUtilities.java
 * Version          : 2.2.1
 * Last modification: 2006-05-08
 * Last modified 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.gp.netautomaton.base;

import org.dgpf.gp.automaton.base.Expression;
import org.dgpf.gp.automaton.base.Instruction;
import org.dgpf.gp.automaton.expressions.Constant;
import org.dgpf.gp.automaton.expressions.UnaryExpression;
import org.dgpf.gp.automaton.instructions.IfJump;
import org.dgpf.gp.automaton.instructions.Write;
import org.dgpf.gp.automaton.search.AutomatonUtils;
import org.dgpf.gp.automaton.search.ExpressionHandler;
import org.dgpf.gp.automaton.search.InstructionHandler;
import org.dgpf.gp.automaton.search.ProgramContext;
import org.dgpf.gp.automaton.search.SequenceFactory;
import org.dgpf.gp.netautomaton.base.NetSupport.Link;
import org.dgpf.gp.netautomaton.expressions.CanSendMessage;
import org.dgpf.gp.netautomaton.expressions.HasWord;
import org.dgpf.gp.netautomaton.expressions.ReceiveWord;
import org.dgpf.gp.netautomaton.instructions.SendMessage;
import org.dgpf.gp.netautomaton.instructions.SendWord;
import org.dgpf.search.api.utils.WeightedSet;
import org.dgpf.search.api.utils.WeightedSetBuilder;
import org.sfc.collections.Arrays;
import org.sfc.math.stochastic.Randomizer;
import org.sfc.utils.Typesafe;

/**
 * A class containing some network utilities.
 *
 * @author Thomas Weise
 */

public  final  class NetUtilities
  {
  
/**
 * The default factory used to create receive code.
 */

  public  static  final SequenceFactory RECEIVE_FACTORY
    = new ReceiveFactory();
  
/**
 * The default factory used to create send code.
 */

  public  static  final SequenceFactory SEND_FACTORY
    = new SendFactory();
  
/**
 * The basic instruction set.
 */

  public  static  final WeightedSet<InstructionHandler>
      BASIC_INSTRUCTION_SET;
  
/**
 * The basic expression set.
 */

  public  static  final WeightedSet<ExpressionHandler>
      BASIC_EXPRESSION_SET;
       
/**
 * The basic sequence handlers.
 */

  public  static  final WeightedSet<SequenceFactory>
      BASIC_SEQUENCE_FACTORIES;
  
  
  static
    {
    WeightedSetBuilder<InstructionHandler>          l_wsb;
    WeightedSetBuilder<ExpressionHandler>           l_wse;
    WeightedSetBuilder<SequenceFactory>             l_wst;
    
    l_wsb = new WeightedSetBuilder<InstructionHandler>(
                                          InstructionHandler.class);
    l_wsb.add(SendMessage.HANDLER, 1.0d);
    l_wsb.add(SendWord.HANDLER, 1.1d);    
    l_wsb.add(AutomatonUtils.BASIC_INSTRUCTION_SET);
    BASIC_INSTRUCTION_SET = new WeightedSet<InstructionHandler>(l_wsb);
    
    l_wse = new WeightedSetBuilder<ExpressionHandler>(
                                          ExpressionHandler.class);    
    l_wse.add(AutomatonUtils.BASIC_EXPRESSION_SET);
    l_wse.add(CanSendMessage.HANDLER, 1.0d);
    l_wse.add(HasWord.HANDLER, 1.1d);
    l_wse.add(ReceiveWord.HANDLER, 1.0);
    BASIC_EXPRESSION_SET = new WeightedSet<ExpressionHandler>(l_wse);
    
    l_wst = new WeightedSetBuilder<SequenceFactory>(SequenceFactory.class);
    l_wst.add(AutomatonUtils.BASIC_SEQUENCE_FACTORIES);
    l_wst.add(SEND_FACTORY,  1.0d);
    l_wst.add(RECEIVE_FACTORY, 1.1d);    
    BASIC_SEQUENCE_FACTORIES = new WeightedSet<SequenceFactory>(l_wst);
    }
  
    
/**
 * The hidden, disabled constructor.
 */

  private NetUtilities()
    {
    Typesafe.do_not_call();
    }
  
  
/**
 * Create a set of links between the automatons.
 * @param p_links   The link array to be filled.
 * @param p_random  The randomizer.
 */

  static  final void  create_links(final Link[][]         p_links,
                                   final Randomizer       p_random)
    {
    final int         l_count      ; 
    final boolean[][] l_b, l_b2    ;
          int         l_i, l_x, l_y, l_z, l_j, l_v, l_incr;
          
    l_count = p_links.length;       
    l_b     = new boolean[l_count][l_count];
    l_b2    = new boolean[l_count][l_count];
        
    for(l_i = (l_count-1); l_i >= 0; l_i--)
      {
      l_b[l_i][l_i] = true;
      }
    
    l_i    = (2*l_count);
    l_incr = 4;
    while( ((l_i--) > 0) || check_reach(l_b, l_b2))
      {      
      for(l_v = 0;; l_v++)
        {
        l_y = p_random.nextInt(l_count);
        l_x = p_random.nextInt(l_count);
        
        if(l_b[l_y][l_x]) continue;
        
        l_z = 0;
        for(l_j = (l_count-1); l_j >= 0; l_j--)
          {
          if( (l_j != l_y) && l_b[l_y][l_j] ) l_z++;
          }
        if(l_z >= l_incr)
          {
          if(l_v > (l_count*l_count))
            {
            l_incr++;
            l_v = 0;
            }
          continue;
          }
        break;
        }
      
      l_b[l_y][l_x] = true;
      l_b[l_x][l_y] = true;
      }
    
    
    for(l_i = (l_count-1); l_i >= 0; l_i--)
      {
      l_y = 0;
      for(l_x = (l_count-1); l_x >= 0; l_x--)
        {
        if(l_b[l_i][l_x]) l_y++;        
        }
      
      p_links[l_i] = new Link[l_y-1];
      l_y          = 0;
      
      for(l_x = (l_count-1); l_x >= 0; l_x--)
        {
        if(l_b[l_i][l_x] && (l_i != l_x))
          {          
          if(l_x < l_i)
            {
            p_links[l_i][l_y] = new Link(l_i, l_x);
            }
          else
            {
            for(l_z = (p_links[l_x].length-1); l_z >= 0; l_z--)
              {
              if( p_links[l_x][l_z].m_end_2 == l_i)
                {
                p_links[l_i][l_y] = p_links[l_x][l_z];
                }
              }
            }
          l_y++;
          }
        }
      }
    
    
    }
  
/**
 * Check if all nodes can reach each other.
 * @param p_b   The connection matrix.
 * @param p_b2  The trial matrix.
 * @return  <code>true</code> if and only if all nodes can reach each
 *          other.
 */

  private static  final boolean check_reach(final boolean[][] p_b,
                                            final boolean[][] p_b2)
    {
    final int     l_count;
          int     l_i, l_j, l_k;
          boolean l_b;
          
    l_count = p_b.length-1;      
    Arrays.array_data_copy(p_b, p_b2);
    
    l_b = true;
    while(l_b)
      {
      l_b = false;
      for(l_i = l_count; l_i >= 0; l_i--)
        {
        for(l_j = l_count; l_j >= 0; l_j--)
          {
          if(p_b2[l_i][l_j])
            {
            for(l_k = l_count; l_k >= 0; l_k--)
              {
              if(p_b2[l_i][l_k])
                {
                if(!(p_b2[l_j][l_k]))
                  {
                  l_b            = true;
                  p_b2[l_j][l_k] = true;
                  }
                }
              }
            }
          }
        }
      }
    
    for(l_i = (l_count-2); l_i >= 0; l_i--)
      {
      if(!(p_b2[l_count-1][l_i])) return true;
      }
    
    return false;
    }  
  

/**
 * A factory for send-data sequences.
 *
 * @author Thomas Weise
 */

  private static  final class SendFactory extends SequenceFactory
    {
/**
 * Create a fixed sequence of instructions.
 * @param p_context       The program context to be used.
 * @param p_instruction_count The count of instructions of the program
 *                            the new sequence will be part of.
 * @param p_memory_size   The memory size currently available.
 * @param p_position      The position where the new sequence will be
 *                        placed. Use this to align your
 *                        <code>IfJump</code>-instructions.
 * @param p_at_end        <code>true</code> if and only if the code
 *                        sequence is to be inserted at the end.
 * @return  The new, random instruction.
 */

    @Override
    public final  Instruction[] create(
                              final ProgramContext  p_context,
                              final int             p_instruction_count,
                              final int             p_memory_size,
                              final int             p_position,
                              final boolean         p_at_end)
      {
      Instruction[] l_ins;

      l_ins     = new Instruction[2];
      l_ins[0]  = SendWord.HANDLER.create(p_context, 0, p_memory_size);
      l_ins[1]  = SendMessage.INSTANCE;

      return l_ins;
      }
    }
/**
 * A factory for receive-data sequences.
 *
 * @author Thomas Weise
 */

  private static  final class ReceiveFactory extends SequenceFactory
    {
/**
 * The internal unary expression.
 */

    private static  final Expression  UE =
        new UnaryExpression(UnaryExpression.LNOT, HasWord.INSTANCE);

/**
 * Create a fixed sequence of instructions.
 * @param p_context       The program context to be used.
 * @param p_instruction_count The count of instructions of the program
 *                            the new sequence will be part of.
 * @param p_memory_size   The memory size currently available.
 * @param p_position      The position where the new sequence will be
 *                        placed. Use this to align your
 *                        <code>IfJump</code>-instructions.
 * @param p_at_end        <code>true</code> if and only if the code
 *                        sequence is to be inserted at the end.
 * @return  The new, random instruction.
 */

    @Override
    public final  Instruction[] create(
                      final ProgramContext  p_context,
                      final int             p_instruction_count,
                      final int             p_memory_size,
                      final int             p_position,
                      final boolean         p_at_end)
      {
      Instruction[] l_ins;

      l_ins    = new Instruction[3];
      l_ins[0] = new IfJump(UE, p_at_end ? 0 : (p_position+3));
      l_ins[1] = Write.wrap(ReceiveWord.INSTANCE, p_context.get_randomizer(),
                            p_memory_size);
      l_ins[2] = new IfJump(Constant.CTRUE, p_position);

      return l_ins;
      }
    }
  }

File Information:

file name:NetUtilities.java
package:org.dgpf.gp.netautomaton.base
qualified name:org.dgpf.gp.netautomaton.base.NetUtilities.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/netautomaton/base/NetUtilities.java
size:10.977 KB (11241 B)
uploaded: 2015-07-22 04:10:55 GMT+0000
last update: 2006-06-08 13:24:42 GMT+0000
last access: 2017-11-22 09:17:15 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-22 09:17:15 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo