Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.vm.optimization.O1.java

Here you can find all the information about the file org.dgpf.gp.vm.optimization.O1.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-07-04 17:45:24
 * Original Filename: org.dgpf.gp.vm.optimization.O1.java
 * Version          : 1.0.0
 * Last modification: 2006-07-04
 *                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.vm.optimization;

import org.dgpf.gp.vm.base.Instruction;
import org.dgpf.gp.vm.base.VMContext;
import org.dgpf.gp.vm.instructions.ctrl.Call;
import org.dgpf.gp.vm.mutation.CodeEditor;
import org.sfc.utils.Typesafe;

/**
 * This optimizer removes all procedures never called.
 *
 * @author Thomas Weise
 */

final class O1
  {
/**
 * The empty program.
 */

  static  final Instruction[][] EMPTY = new Instruction[0][0];
  
/**
 * Optimize a program by removing unnecessary procedures.
 * @param p_code      The program code.
 * @param p_context   The virtual machine context to be used.
 * @return  The optimized program code.
 */

  static  final Instruction[][] optimize(      Instruction[][] p_code,
                                         final VMContext       p_context)
    {
    int             l_l, l_i, l_j, l_d, l_c;
    byte[]          l_b;
    boolean         l_z;
    Instruction[]   l_in;
    Instruction     l_x;
    Instruction[][] l_n;
    
    l_l = p_code.length;
    l_b = p_context.get_buffer().get_bytes(l_l, (byte)0);
    l_d = Math.min(l_l, p_context.get_min_proc_count());
    for(l_i = (l_d-1); l_i >= 0; l_i--)
      {
      if(p_code[l_i].length > 0) l_b[l_i] = -1;
      else                       l_d--;
      }
    
    do
      {
      l_z = false;
      
      for(l_i = (l_l-1); l_i >= 0; l_i--)
        {
        if(l_b[l_i] == -1)
          {
          l_in = p_code[l_i];
          for(l_j = (l_in.length-1); l_j >= 0; l_j--)
            {
            l_x = l_in[l_j];
            if( l_x instanceof Call )
              {              
              l_c = ((Call)l_x).get_index();
              if((l_c >= 0) && (l_c < p_code.length) && (l_b[l_c] == 0) &&
                 (p_code[l_c].length > 0))
                {
                l_z = true;
                l_b[l_c] = -1;
                l_d++;
                }
              }
            }
          
          l_b[l_i] = -2;
          }
        }     
      } while(l_z);
    
    if(l_d >= l_l) return p_code;
    if(l_d <= 0)   return EMPTY; 
        
    for(l_i = (l_l-1); l_i >= 0; l_i--)
      {
      if(l_b[l_i] == 0)//TODO: optimize
        {
        CodeEditor.delete_proc(p_code, l_i, 1, l_l--, p_code);
        }
      }
    
    l_n = new Instruction[l_d][];
    System.arraycopy(p_code, 0, l_n, 0, l_d);
    
//    l_j = 0;
//    for(l_i = 0; l_i < l_l; l_i++)
//      {
//      if(l_b[l_i] != 0)
//        {
//        l_n[l_j++] = p_code[l_i];
//        }
//      }
    
    return l_n;
    }
  
/**
 * No instances of this class, hun.
 */

  private O1()
    {
    Typesafe.do_not_call();
    }
  }

File Information:

file name:O1.java
package:org.dgpf.gp.vm.optimization
qualified name:org.dgpf.gp.vm.optimization.O1.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/vm/optimization/O1.java
size:3.762 KB (3853 B)
uploaded: 2015-07-22 04:10:58 GMT+0000
last update: 2006-08-01 10:41:13 GMT+0000
last access: 2017-11-19 04:56:55 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:56:55 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo