package gp;
/**
* @author Maximus
*
*/
public class RegrIndividual extends GPTree implements InterfaceIndividual {
static String term[] = { "1", "x", "pi" };
static String unary[] = { "exp", "sin", "cos" };
static String binary[] = { "+", "-", "*" };
static int maxHeight = 5;
static int initHeight = 3;
static int modeCreate = MODE_GROW;
static int modeMutate = MODE_GROW;
public RegrIndividual(GPTree parent, int h, int mode) {
super(parent, h, mode);
}
public RegrIndividual(RegrIndividual y) {
super(y);
}
public RegrIndividual(int h, int mode) {
super(null, h, mode);
}
@Override
public RegrIndividual clone() {
return new RegrIndividual(this);
}
@Override
public double evaluate() {
double sum = 0;
for (int i = -100; i < 101; i++) {
Math.pow(2, getY(i) - getF(i));
}
return sum;
}
@Override
public void crossover(InterfaceIndividual y) {
super.crossover((GPTree) y, this.height);
}
@Override
public void mutate(double pMut) {
super.mutate(pMut, this.height != 0 ? ran.nextInt(this.height) + 1 : 0,
modeMutate);
}
@Override
public String getStringRepresentation() {
return super.toString();
}
@Override
public void defaultInit() {
initRandomTree(null, initHeight, modeCreate);
}
@Override
public GPTree makeRandomSubtree(int h, int mode) {
// System.out.println();
if (h > 0) { // letzter Knoten hat höhe 0
if ((mode == 0)) {
this.arity = ran.nextInt(2) + 1;
} else {
this.arity = ran.nextInt(3);
}
} else {
this.arity = 0;
}
switch (arity) {
case 0:
type = ran.nextInt(3);
nodeCount = 1;
height = 0;
left = null;
right = null;
break;
case 1:
type = ran.nextInt(3);
left = new RegrIndividual(this, h - 1, mode);
left.up = this;
right = null;
nodeCount = left.nodeCount + 1;
height = left.height + 1;
break;
case 2:
type = ran.nextInt(3);
left = new RegrIndividual(this, h - 1, mode);
left.up = this;
right = new RegrIndividual(this, h - 1, mode);
right.up = this;
nodeCount = right.nodeCount + left.nodeCount + 1;
height = left.height > right.height ? left.height + 1
: right.height + 1;
break;
}
return this;
}
@Override
public GPTree copyTree(GPTree tree) {
return new RegrIndividual((RegrIndividual) tree);
}
@Override
public double evalTreeAt(Object o) {
RegrIndividual a = (RegrIndividual) o;
double sum = 0;
for (int i = -100; i < 101; i++) {
Math.pow(2, a.getY(i) - a.getF(i));
}
return sum;
}
@Override
public int getTermCount() {
return term.length;
}
@Override
public int getUnaryCount() {
return unary.length;
}
@Override
public int getBinaryCount() {
return binary.length;
}
@Override
public String getTermSymbol(int i) {
return term[i];
}
@Override
public String getUnarySymbol(int i) {
return unary[i];
}
@Override
public String getBinarySymbol(int i) {
return binary[i];
}
private double getY(double x) {
return x + Math.pow(2, x) + Math.pow(3, x) + Math.pow(4, x);
}
private double getF(double x) {
RegrIndividual l = (RegrIndividual) left;
RegrIndividual r = (RegrIndividual) right;
// System.out.println(this);
switch (arity) {
case 0:
switch (getTermSymbol(type)) {
case "1":
return 1;
case "x":
return x;
case "pi":
return Math.PI;
}
break;
case 1:
switch (getUnarySymbol(type)) {
case "exp":
return Math.exp(l.getF(x));
case "sin":
return Math.sin(l.getF(x));
case "cos":
return Math.cos(l.getF(x));
}
break;
case 2:
switch (getBinarySymbol(type)) {
case "+":
return l.getF(x) + r.getF(x);
case "-":
return l.getF(x) - r.getF(x);
case "*":
return l.getF(x) * r.getF(x);
}
break;
}
throw new RuntimeException("wrong arity" + arity);
}
}