Genetic Programming。GP算法在方程逼近求解上的应用。

源代码在线查看: function.java

软件大小: 59 K
上传用户: cjf0426
关键词: Programming Genetic 算法 方程
下载地址: 免注册下载 普通下载 VIP

相关代码

												//base class for function				abstract class Function extends TreeNode {								        public TreeNode[] Subtree;								        public String toString(int level) {				        					                boolean allArgsAreTerminals = true;				                for (int a = 0; a < Subtree.length; a++) {				                        allArgsAreTerminals = allArgsAreTerminals && (Subtree[a] instanceof Terminal);				                }				                String s = new String();				                if (!allArgsAreTerminals) {				                        s = indent(level) + getName() + "(\n";				                        int i = 0;				                        while (i < Subtree.length-1) {				                                s = s + Subtree[i].toString(level+1) + ",\n";				                                i++;				                        }				                        if (i < Subtree.length) {				                                s = s + Subtree[i].toString(level+1) + "\n";				                        }				                        s = s + indent(level) + ")";				                } else {				                        s = indent(level) + getName() + "(";				                        int i = 0;				                        while (i < Subtree.length-1) {				                                s = s + Subtree[i].toString(0) + ",";				                                i++;				                        }				                        if (i < Subtree.length) {				                                s = s + Subtree[i].toString(0);				                        }				                        s = s + ")";				                }				                return s;				        }								        protected Object clone() {				                Function temp = null;				                try {				                        temp = (Function)getClass().newInstance();				                        for (int i = 0; i < Subtree.length; i++) {				                                temp.Subtree[i] = (TreeNode)Subtree[i].clone();				                        }				                } catch (Exception e) {				                }				                return temp;				        }								        int countNodes() {				                int count = 1;				                for (int a = 0; a < Subtree.length; a++) {				                        count = count + Subtree[a].countNodes();				                }				                return count;				        }								        int countNodes(Condition cond) {				                int count = (cond.test(this) ? 1 : 0);				                for (int a = 0; a < Subtree.length; a++) {				                        count = count + Subtree[a].countNodes(cond);				                }				                return count;				        }								}												class Addition extends Function {								        Addition() {				                Subtree = new TreeNode[2];				        }								        String getName() {				                return "add";				        }								        double eval(double x) {				                return Subtree[0].eval(x) + Subtree[1].eval(x);				        }								}												class Subtraction extends Function {								        Subtraction() {				                Subtree = new TreeNode[2];				        }								        String getName() {				                return "sub";				        }								        double eval(double x) {				                return Subtree[0].eval(x) - Subtree[1].eval(x);				        }								}												class Multiplication extends Function {								        Multiplication() {				                Subtree = new TreeNode[2];				        }								        String getName() {				                return "mul";				        }								        double eval(double x) {				                return Subtree[0].eval(x) * Subtree[1].eval(x);				        }								}												class Division extends Function {								        Division() {				                Subtree = new TreeNode[2];				        }								        String getName() {				                return "div";				        }								        double eval(double x) {				                double divisor = Subtree[1].eval(x);				                return (divisor == 0 ? 1 : Subtree[0].eval(x) / divisor);				        }								}												class Sine extends Function {								        Sine() {				                Subtree = new TreeNode[1];				        }								        String getName() {				                return "sin";				        }								        double eval(double x) {				                return Math.sin(Subtree[0].eval(x));				        }								}												class Cosine extends Function {								        Cosine() {				                Subtree = new TreeNode[1];				        }								        String getName() {				                return "cos";				        }								        double eval(double x) {				                return Math.cos(Subtree[0].eval(x));				        }								}												class Exp extends Function {								        Exp() {				                Subtree = new TreeNode[1];				        }								        String getName() {				                return "exp";				        }								        double eval(double x) {				                double v = Subtree[0].eval(x);				                if (v > 100) {				                        v = 100;				                } else if (v < -100) {				                        v = -100;				                }				                return Math.exp(v);				        }								}												class First extends Function {								        First() {				                Subtree = new TreeNode[2];				        }								        String getName() {				                return "first";				        }								        double eval(double x) {				                return Subtree[0].eval(x);				        }								}												class Second extends Function {								        Second() {				                Subtree = new TreeNode[2];				        }								        String getName() {				                return "second";				        }								        double eval(double x) {				                return Subtree[1].eval(x);				        }								}							

相关资源