Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 16th, 2012  |  syntax: None  |  size: 3.42 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Genetic Programming Stackoverflow error
  2. import java.util.*;
  3.  
  4. public class IPG {
  5.  
  6.     class Node {
  7.  
  8.         private int arity;
  9.         private String label;
  10.         private int value;
  11.         private Node[] children;
  12.         protected int visit = 0;
  13.         protected boolean isVisited = false;
  14.  
  15.         public Node(int arity, String label) {
  16.             this.arity = arity;
  17.             this.label = label;
  18.             this.children = new Node[3];
  19.         }
  20.  
  21.         public Node(Node another) {
  22.             this.label = another.label;
  23.             this.arity = another.arity;
  24.         }
  25.  
  26.         @Override
  27.         public String toString() {
  28.             return label;
  29.         }
  30.  
  31.         String getLabel() {
  32.             return label;
  33.         }
  34.  
  35.         void insertChild(int pos, Node n) {
  36.             if (pos < arity) {
  37.                 children[pos] = n;
  38.             }
  39.         }
  40.  
  41.         Node getChildAt(int i) {
  42.             return children[i];
  43.         }
  44.  
  45.         void setValue(int value) {
  46.             this.value = value;
  47.         }
  48.  
  49.         int getArity() {
  50.             return arity;
  51.         }
  52.  
  53.         void replace(Node another) {
  54.             this.arity = another.arity;
  55.             this.children = another.children;
  56.             this.label = another.label;
  57.         }
  58.  
  59.     }
  60.  
  61.     private Node[] functions = { new Node(2, "AND"), new Node(2, "OR"),
  62.             new Node(1, "NOT"), new Node(3, "IF") };
  63.  
  64.     private Node[] terminals = { new Node(0, "A0"), new Node(0, "D1"),
  65.             new Node(0, "D0"), new Node(0, "A1"), new Node(0, "D2"),
  66.             new Node(0, "D3"), new Node(0, "A2"), new Node(0, "D4"),
  67.             new Node(0, "D5"), new Node(0, "D6"), new Node(0, "D7") };
  68.  
  69.     private Random random = new Random();
  70.  
  71.     private int multiplexerType = 3;
  72.  
  73.     public Node getTerminal() {
  74.         return terminals[random.nextInt(multiplexerType)];
  75.     }
  76.  
  77.     public Node getFunction() {
  78.  
  79.         return functions[random.nextInt(3)];
  80.     }
  81.  
  82.     public Node getAnyNode() {
  83.         return random.nextInt(2) == 1 ? getFunction() : getTerminal();
  84.     }
  85.  
  86.     public Node generateGrow(int depth) {
  87.         Node root;
  88.  
  89.         if (depth > 1)
  90.             root = getAnyNode();
  91.  
  92.         else
  93.             root = getTerminal();
  94.  
  95.         for (int i = 0; i < root.getArity(); i++)
  96.             root.insertChild(i, generateGrow(depth - 1));
  97.  
  98.         return root;
  99.     }
  100.  
  101.     public Node generateFull(int depth) {
  102.         Node root;
  103.  
  104.         if (depth > 1)
  105.             root = getFunction();
  106.  
  107.         else
  108.             root = getTerminal();
  109.  
  110.         for (int i = 0; i < root.getArity(); i++)
  111.             root.insertChild(i, generateFull(depth - 1));
  112.  
  113.         return root;
  114.     }
  115.  
  116.     public void printPostOrder() {
  117.         Node root = generateFull(3);
  118.         printPostOrder(root);
  119.     }
  120.  
  121.     private void printPostOrder(Node n) {
  122.         if (n == null) {
  123.             return;
  124.         } else {
  125.             System.out.println(n + " ");
  126.  
  127.             printPostOrder(n.children[0]);
  128.             printPostOrder(n.children[1]);
  129.             printPostOrder(n.children[2]);
  130.         }
  131.     }
  132.  
  133.     public static void main(String[] args) {
  134.         new IPG().printPostOrder();
  135.     }
  136. }
  137.        
  138. private String [] functionNames = {"AND", "OR", "NOT", "IF"};
  139. private int [] functionArities = {2,2,1,3};
  140.  
  141. public Node getFunction() {
  142.     int index = random.nextInt(3); // If you want to use "IF" nodes too this
  143.                                    // needs to be nextInt(4)
  144.     return new Node(functionArities[index],functionNodes[index]);
  145. }