Results 1 to 3 of 3
  1. #1
    player123 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Question passing variable in trees??

    hi there! i have these classes which i made to create and evaluate expression trees in the form of:

    (* (+ 2 4) (abs (cos 53)))

    the thing is it works perfectly.... BUT

    i want to pass in X and Y coordinates and evaluate the tree so..

    (* (+ 2 X) (abs (cos Y)))

    to evaluate the result i have to put the expression in a loop:

    Java Code:
    for (int y = 0; y < 150; y++) {
                for (int x = 0; x < 150; x++) {
    
                evaluate this:  (* (+ 2 X) (abs (cos Y)))
    
                }
    }
    the thing is i dont know to pass the X and Y variables, and then evaluate the tree within the loop..

    heres my code for the tree modelling:


    Java Code:
    public abstract class Node implements Cloneable{
        
        abstract double value(); 
        abstract String print(); 
        abstract void setLeft(Node left);
        abstract void setRight(Node right); 
        abstract int getSize(); 
        
        @Override
        public Node clone(){
            Node copy;
            try {
                copy = (Node) super.clone();
            } catch (CloneNotSupportedException unexpected) {
                throw new AssertionError(unexpected);
            }
     
            //In an actual implementation of this pattern you might now change references to
            //the expensive to produce parts from the copies that are held inside the prototype. 
            return copy;
        }
    }
    
    
    public class UnaryNode extends Node  {
    
       
        private UnaryFunction operator;
        private Node terminal;
        
        public UnaryNode(UnaryFunction op, Node terminal) {
            operator = op;
            this.terminal = terminal; 
        }
        
        public String print(){
            String r = "(" + operator.toString()+ " " + terminal.print() + ")";
            return r;
        }
        
       
      
        
        @Override
        double value() {
            double terminalVal = terminal.value();
            switch (operator) {
                case SQRT:
                    return Math.sqrt(terminalVal);
                case SIN:
                    return Math.sin(terminalVal);
                case COS:
                    return  Math.cos(terminalVal);
                case TAN:
                    return  Math.tan(terminalVal);
                case ABS:
                    return Math.abs(terminalVal);
                case EXP:
                    return  Math.exp(terminalVal);
                case SQRD:
                    return Math.pow(terminalVal, 2); 
                case CUBD:
                    return  Math.pow(terminalVal, 3);
                case LOG:
                    return Math.log(terminalVal); 
                default:
                    return Double.NaN;  // Bad operator.
            }
        }
    
        @Override
        void setLeft(Node left) {
            terminal = left; 
        }
    
        @Override
        void setRight(Node right) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        @Override
        int getSize() {
            return 1 + terminal.getSize(); 
        }
        
    
    }
    
    public class BinaryNode extends Node  {
    
        private BinaryFunction operator;
        private Node left;
        private Node right;
    
        public BinaryNode(BinaryFunction op, Node left, Node right) {
            operator = op;
            this.left = left;
            this.right = right;
        }
        
      
        
        public String print(){
            String r = "(" + operator.toString()+ " " + left.print() + " " + right.print()+")";
            return r;
        }
        
        public void setLeft(Node left){
            this.left = left; 
        }
        
        public void setRight(Node right){
            this.right = right; 
        }
    
        @Override
        double value() {
            double leftVal = left.value();
            double rightVal = right.value();
            switch (operator) {
                case ADD:
                    return leftVal + rightVal;
                case SUBTRACT:
                    return leftVal - rightVal;
                case MULTIPLY:
                    return leftVal * rightVal;
                case DIVIDE:
                    return leftVal / rightVal;
                case MAX:
                    return Math.max(leftVal, rightVal);
                case MIN:
                    return Math.min(leftVal, rightVal);
                case AVG:
                    return (leftVal + rightVal) / 2;
                case POW:
                    return (float) Math.pow(leftVal, rightVal);
                default:
                    return Double.NaN;  // Bad operator.
            }
        }
        
       
    
        @Override
        int getSize() {
             return 1 + left.getSize() + right.getSize(); 
        }
    }
    this is how i generate the trees:

    Java Code:
         
    
        public Node generateTree(int depth) {
            Node tree = null;
            if (depth > 0 && rnd.nextInt(20 - depth) == 0) {
                return getRandomTN(); 
            } else {        
                if(rnd.nextInt(2) == 0){
                    BinaryNode b = (BinaryNode) bList.get(rnd.nextInt(bList.size())).clone(); //
                    b.setRight(generateTree(depth + 1));
                    b.setLeft(generateTree(depth + 1));
                    tree = b;
                } else {
                    UnaryNode u = (UnaryNode) uList.get(rnd.nextInt(uList.size())).clone(); //
                    u.setLeft(generateTree(depth + 1));
                    tree = u;
                } 
                return tree;
            }
        }
    
        private Node getRandomTN() {
          
            Node t1 = new TerminalNode(rnd.nextInt(100)/100.0);
        
            return t1;
        }
    Thanks guys!

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    You can make a new type of VariableNode that stores a character or something, then have your eval() method take in parameters that get passed along such that when the VariableNode is hit, which then uses the right value for itself. Easy if you have unchangeable identifier names and a set amount of them, tricky otherwise. Or you can make static variables, though I'm not sure if that's proper usage.

  3. #3
    player123 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    You can make a new type of VariableNode that stores a character or something, then have your eval() method take in parameters that get passed along such that when the VariableNode is hit, which then uses the right value for itself. Easy if you have unchangeable identifier names and a set amount of them, tricky otherwise. Or you can make static variables, though I'm not sure if that's proper usage.
    i see thank you!

Similar Threads

  1. Game Trees
    By javaBoy1 in forum Advanced Java
    Replies: 0
    Last Post: 12-27-2008, 12:20 AM
  2. Help With Tournament Trees
    By wiggsfly in forum New To Java
    Replies: 2
    Last Post: 10-26-2008, 09:38 PM
  3. Passing Environment Variable in Eclipse Europa
    By ajaymenon.k in forum Eclipse
    Replies: 0
    Last Post: 11-19-2007, 10:08 AM
  4. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 08:09 PM
  5. Variable passing, Error: IOException
    By fernando in forum New To Java
    Replies: 3
    Last Post: 07-31-2007, 02:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •