-
Notifications
You must be signed in to change notification settings - Fork 0
/
Value.java
122 lines (108 loc) · 3.1 KB
/
Value.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* A subclass of Node that represents either a variable or a constant
*/
import java.util.Random;
public class Value extends Node{
//////////
//Fields//
//////////
private double value;
private boolean isInput;
public static Random rand = new Random();
private static final int NUM_CHILDREN=0; //How many children th enode should have
private static final int MEAN_VALUE=10;
private static final int STANDARD_DEVIATION=5;
private static final double INPUT_FREQUENCY=0.1; //Amount of times a random node should be input
private static final int STD_OF_DELTA=5;//Std dev of mutatiuon
////////////////
//Constructors//
////////////////
/** Constructor given parent, value, and isInput*/
public Value(Node parent, double value, boolean isInput){
super(parent, NUM_CHILDREN);
this.isInput=isInput;
this.value=value;
}
/** Constructor given parent*/
public Value(Node parent){
this(parent,Value.randomValue(),false);
}
/** Constructor given value and parent*/
public Value(Node parent, double value){
this(parent,value,false);
}
/** Constructor parent and isInput*/
public Value(Node parent, boolean isInput){
this(parent,0,isInput);
}
/** Constructor given isInput*/
public Value(boolean isInput){
this(null,0,isInput);
}
/** Constructor given value*/
public Value(double value){
this(null,value,false);
}
/** Constructor given nothing - Random value and random isInput*/
public Value(){
this(null,Value.randomValue(),Value.randomBoolean());
}
/** Deep copy */
public Value(Value v){
this(null,v.value,v.isInput);
}
///////////
//Methods//
///////////
/** If is input */
public boolean isInput(){
return isInput;
}
/** Perform the deep copy */
public Value deepCopy(){
return new Value(this);
}
/** Get a random boolean value*/
public static boolean randomBoolean(){
if (rand.nextDouble()>INPUT_FREQUENCY){
return false;
}else{
return true;
}
}
/** This method returns a random value with Gaussian distribution and mean of MEAN_VALUE*/
public static double randomValue(){
return rand.nextGaussian()*STANDARD_DEVIATION+MEAN_VALUE;
}
/** Mutate node */
public void mutate(){
//mutate value
double delta = rand.nextGaussian()*STD_OF_DELTA;
value+=value;
//Maybe turn into input node
if(Value.randomBoolean()){
isInput=!isInput;
}
}
/** Converts it to a string */
public String getSymbol(){
if (isInput){
return "X";
}else{
return Double.toString(value);
}
}
/** Returns the value of this node as an int*/
public double evaluate(int x){
if(isInput){
//System.out.println("X: "+x);
return x;
}
else{
return value;
}
}
@Override
public String toString(){
return getSymbol();
}
}