Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ed7037f
Add extra neuron for heartbeat, add heartbeat
Timfa2112 Jul 14, 2017
2bada42
Create README.md
alamaz Jul 22, 2017
6773a88
Update README.md
alamaz Jul 22, 2017
4848443
Added radioactive mode
jbenzakoun Jul 23, 2017
b9a05fc
Merge remote-tracking branch 'origin/master'
jbenzakoun Jul 23, 2017
9f99587
Fresh blood
jbenzakoun Jul 23, 2017
890299c
Simulation timer
jbenzakoun Jul 23, 2017
0668a8c
Creature number
jbenzakoun Jul 23, 2017
893809d
Processing 3
jbenzakoun Jul 23, 2017
cd34246
Moved global variables
jbenzakoun Jul 23, 2017
9276dfb
Added timer
jbenzakoun Jul 23, 2017
156e3a6
Multithread
jbenzakoun Jul 23, 2017
dd909fa
Other technique to remove jumpers
jbenzakoun Jul 23, 2017
d3dbd6e
Mass extinction
jbenzakoun Jul 23, 2017
0868161
Adding gitattributes for EOL
jbenzakoun Jul 23, 2017
8f7c73e
Multithread 2
jbenzakoun Jul 24, 2017
45ab38b
Savable
jbenzakoun Jul 24, 2017
6619429
Save screen
jbenzakoun Jul 24, 2017
50add5c
Interface glitch
jbenzakoun Jul 24, 2017
8684a6f
Autosave
jbenzakoun Jul 24, 2017
e627564
Autosave 2
jbenzakoun Jul 24, 2017
35c39b8
Autosave timecoded
jbenzakoun Jul 24, 2017
4a1320d
AutoPause
jbenzakoun Jul 24, 2017
4385614
Saving 2
jbenzakoun Jul 25, 2017
e0de5fc
Optimisation
jbenzakoun Jul 26, 2017
085a505
Expanded brain
jbenzakoun Jul 26, 2017
d162539
Ignore all gz files
jbenzakoun Jul 27, 2017
de5d440
Light save
jbenzakoun Jul 27, 2017
d543575
Gift function
jbenzakoun Jul 27, 2017
2f8d5b6
Update mutation comportement
jbenzakoun Jul 27, 2017
553b8fd
Changed default parameters
jbenzakoun Jul 27, 2017
a6b662d
Update readme file
jbenzakoun Jul 27, 2017
31b9401
Readme update for install
jbenzakoun Jul 27, 2017
4ed0941
added commands for reducing and increasing gift
jbenzakoun Jul 27, 2017
5b3f5bc
added "k" in readme
jbenzakoun Jul 27, 2017
0875f06
Modified mutation properties
jbenzakoun Jul 27, 2017
7be6c0e
Mass extinction percentage
jbenzakoun Jul 27, 2017
5369506
Brain improvement
jbenzakoun Jul 28, 2017
98976a3
Chomp limitation
jbenzakoun Jul 28, 2017
6a2a577
Save v4
jbenzakoun Jul 28, 2017
3eaf1bb
Brainiac mode
jbenzakoun Jul 28, 2017
f2c7eb7
Variable friction
jbenzakoun Jul 28, 2017
595cd3c
Save v4b
jbenzakoun Jul 28, 2017
ead6466
Updated readme for Brainiac
jbenzakoun Jul 28, 2017
ff17500
Bugfix
jbenzakoun Jul 28, 2017
5f5557c
maxChomp
jbenzakoun Jul 29, 2017
75b6fce
Jumper truncature
jbenzakoun Jul 29, 2017
952a02c
PowerDouble
jbenzakoun Jul 29, 2017
42c8f9e
Heartbeat neuron
jbenzakoun Jul 29, 2017
b39b514
Merge remote-tracking branch 'Master2112/master'
jbenzakoun Jul 29, 2017
9005f12
Linear transform for brain
jbenzakoun Jul 30, 2017
606bf96
Complex fitness improvement
jbenzakoun Jul 31, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+*.pde text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

data/autosave\.gz
data/*\.gz

error\.log

data/tempfile\.gz
31 changes: 28 additions & 3 deletions Axon.pde
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Axon{
class Axon {
final double MUTABILITY_MUTABILITY = 0.7;
final int mutatePower = 9;
final double MUTATE_MULTI;
Expand All @@ -8,9 +8,12 @@ class Axon{
public Axon(double w, double m){
weight = w;
mutability = m;
MUTATE_MULTI = Math.pow(0.5,mutatePower);
MUTATE_MULTI = Math.pow(0.8,mutatePower);
}

public Axon copyAxon(){
return new Axon(this.weight,this.mutability);
}
public Axon mutateAxon(){
double mutabilityMutate = Math.pow(0.5,pmRan()*MUTABILITY_MUTABILITY);
return new Axon(weight+r()*mutability/MUTATE_MULTI,mutability*mutabilityMutate);
Expand All @@ -21,4 +24,26 @@ class Axon{
public double pmRan(){
return (Math.random()*2-1);
}
}

public void saveToJson(JsonGenerator g){
try {
g.writeNumberField("weight", weight);
g.writeNumberField("mutability", mutability);
} catch(Exception e){
writeToErrorLog(e);
}
}

public void loadFromJson(JsonParser p){
try{
while(p.nextToken() != JsonToken.END_OBJECT){
String fieldName = p.getCurrentName();
p.nextToken();
if(fieldName.equals("weight")){ this.weight = p.getDoubleValue(); }
else if(fieldName.equals("mutability")){ this.mutability = p.getDoubleValue(); }
}
} catch(Exception e){
writeToErrorLog(e);
}
}
}
194 changes: 170 additions & 24 deletions Brain.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
class Brain {
float heartbeatInterval = (float)(Math.PI/12);
int timesUsed;
float[][] neurons;
Axon[][][] axons;
int BRAIN_WIDTH = 0;
int BRAIN_HEIGHT = 0;
Brain(int bw, int bh, Axon[][][] templateAxons, Boolean haveNeurons, Boolean mutate){ //This is to copy a brain EXACTLY.
setUpBasics(bw,bh,haveNeurons);
axons = new Axon[BRAIN_WIDTH-1][BRAIN_HEIGHT][BRAIN_HEIGHT-1];
axons = new Axon[BRAIN_WIDTH-1][BRAIN_HEIGHT][BRAIN_HEIGHT];
if(mutate){
for(int x = 0; x < BRAIN_WIDTH-1; x++){
for(int y = 0; y < BRAIN_HEIGHT; y++){
for(int z = 0; z < BRAIN_HEIGHT-1; z++){
for(int z = 0; z < BRAIN_HEIGHT; z++){
axons[x][y][z] = templateAxons[x][y][z].mutateAxon();
}
}
}
}else{
for(int x = 0; x < BRAIN_WIDTH-1; x++){
for(int y = 0; y < BRAIN_HEIGHT; y++){
for(int z = 0; z < BRAIN_HEIGHT-1; z++){
axons[x][y][z] = new Axon(templateAxons[x][y][z].weight,templateAxons[x][y][z].mutability);
for(int z = 0; z < BRAIN_HEIGHT; z++){
axons[x][y][z] = templateAxons[x][y][z].copyAxon();
}
}
}
}
}
Brain(int bw, int bh){
setUpBasics(bw,bh,false);
axons = new Axon[BRAIN_WIDTH-1][BRAIN_HEIGHT][BRAIN_HEIGHT-1];
axons = new Axon[BRAIN_WIDTH-1][BRAIN_HEIGHT][BRAIN_HEIGHT];
for(int x = 0; x < BRAIN_WIDTH-1; x++){
for(int y = 0; y < BRAIN_HEIGHT; y++){
for(int z = 0; z < BRAIN_HEIGHT-1; z++){
for(int z = 0; z < BRAIN_HEIGHT; z++){
double startingWeight = 0;
if(y == BRAIN_HEIGHT-1){
if(y == BRAIN_HEIGHT - 1){
startingWeight = (Math.random()*2-1)*STARTING_AXON_VARIABILITY;
}
axons[x][y][z] = new Axon(startingWeight,AXON_START_MUTABILITY);
Expand All @@ -42,10 +44,10 @@ class Brain {
void changeBrainStructure(int bw, int bh, int rowInsertionIndex, int rowRemovalIndex){
setUpBasics(bw,bh,false);
Axon[][][] oldAxons = axons;
axons = new Axon[BRAIN_WIDTH-1][BRAIN_HEIGHT][BRAIN_HEIGHT-1];
axons = new Axon[BRAIN_WIDTH-1][BRAIN_HEIGHT][BRAIN_HEIGHT];
for(int x = 0; x < BRAIN_WIDTH-1; x++){
for(int y = 0; y < BRAIN_HEIGHT; y++){
for(int z = 0; z < BRAIN_HEIGHT-1; z++){
for(int z = 0; z < BRAIN_HEIGHT; z++){
if(y == rowInsertionIndex || z == rowInsertionIndex){
double startingWeight = 0;
if(y == BRAIN_HEIGHT-1 || true){
Expand All @@ -66,6 +68,7 @@ class Brain {
}
}
void setUpBasics(int bw, int bh, Boolean haveNeurons){
timesUsed = 0;
BRAIN_WIDTH = bw;
BRAIN_HEIGHT = bh;
if(haveNeurons){
Expand All @@ -80,50 +83,106 @@ class Brain {
}
}
}else{
neurons = null;
neurons = new float[BRAIN_WIDTH][BRAIN_HEIGHT];//null;
}
}
public void useBrain(Creature owner){
ArrayList<Node> n = owner.n;
ArrayList<Muscle> m = owner.m;
final ArrayList<Node> n = owner.n;
final ArrayList<Muscle> m = owner.m;
for(int i = 0; i < n.size(); i++){
Node ni = n.get(i);
neurons[0][i] = dist(ni.x, ni.y, ni.z, foodX, foodY, foodZ);
neurons[0][i] = dist(ni.x, ni.y, ni.z, owner.foodX, owner.foodY, owner.foodZ);
}

for(int i = 0; i < m.size(); i++){
Muscle am = m.get(i);
Node ni1 = n.get(am.c1);
Node ni2 = n.get(am.c2);
neurons[0][n.size()+i] = dist(ni1.x, ni1.y, ni1.z, ni2.x, ni2.y, ni2.z)/am.len;
}

neurons[0][n.size()+m.size()] = sin(heartbeatInterval*timesUsed);
neurons[0][n.size()+m.size()+1] = cos(2*heartbeatInterval*timesUsed);
timesUsed += 1;

for(int x = 1; x < BRAIN_WIDTH; x++){
for(int y = 0; y < BRAIN_HEIGHT-1; y++){
for(int y = 0; y < BRAIN_HEIGHT; y++){
float total = 0;
for(int input = 0; input < BRAIN_HEIGHT; input++){
total += neurons[x-1][input]*axons[x-1][input][y].weight;
}
if(x == BRAIN_WIDTH-1){
neurons[x][y] = total;
}else{
neurons[x][y] = sigmoid(total);
if(x == BRAIN_WIDTH - 1){
neurons[x][y] = sigmoidapprox(total);
} else {
neurons[x][y] = linear(total);
}
}
}
for(int i = 0; i < n.size(); i++){
n.get(i).brainOutput = neurons[BRAIN_WIDTH-1][i];
}
for(int i = 0; i < m.size(); i++){
m.get(i).brainOutput = neurons[BRAIN_WIDTH-1][n.size()+i];
}
}
public float linear(float input){
if(input >= -5 || input <= 5){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this line in windows x64 I get the error: "processing.app.SketchException: Not expecting symbol 0xA0, which is NO-BREAK SPACE."

The offending character appears to be the whitespace between the or (||) and the second use of the input variable.

Deleting it and hitting the spacebar appears to resolve this issue.

return input/5;
}
else if(input < -5){
return -1;
}
else{
return 1;
}
}
public float sigmoid(float input){
return 1.0/(1.0+pow(2.71828182846,-input));
return -1.0+(2.0/(1.0+pow(2.71828182846,-input)));
// Sigmoid centered on 0 and giving response between -1 and +1
}
public float sigmoidapprox(float input){
if(input >= -1.1779 && input <= 1.1779){
return 0.5*input;
} else if(input >= -2.7537 && input < -1.1779){
return 0.210*input-0.3416;
} else if(input > 1.1779 && input <= 2.7537){
return 0.210*input+0.3416;
} else if(input >= -5 && input < -2.7537){
return 0.0354*input-0.8224;
} else if(input > 3 && input <= 5){
return 0.0354*input+0.8224;
} else if(input < -5){
return 0;
} else {
return 1;
}
}
Brain getUsableCopyOfBrain(){
return new Brain(BRAIN_WIDTH,BRAIN_HEIGHT,axons,true,false);
return new Brain(BRAIN_WIDTH,BRAIN_HEIGHT,axons.clone(),true,false);
}
Brain copyBrain(){
return new Brain(BRAIN_WIDTH,BRAIN_HEIGHT,axons,false,false);
return new Brain(BRAIN_WIDTH,BRAIN_HEIGHT,axons.clone(),false,false);
}
Brain copyMutatedBrain(){
return new Brain(BRAIN_WIDTH,BRAIN_HEIGHT,axons,false,true);
return new Brain(BRAIN_WIDTH,BRAIN_HEIGHT,axons.clone(),false,true);
}
Brain copyExpandedBrain(){
Axon[][][] extaxons = new Axon[BRAIN_WIDTH][BRAIN_HEIGHT][BRAIN_HEIGHT];
for(int x = 0; x < BRAIN_WIDTH; x++){
for(int y = 0; y < BRAIN_HEIGHT; y++){
for(int z = 0; z < BRAIN_HEIGHT; z++){
if(x == 0){
if(y == z){
extaxons[x][y][z] = new Axon(5.0,AXON_START_MUTABILITY);
} else {
extaxons[x][y][z] = new Axon(0.0,AXON_START_MUTABILITY);
}
}
else{ extaxons[x][y][z] = axons[x-1][y][z].copyAxon(); }
}
}
}
return new Brain(BRAIN_WIDTH+1,BRAIN_HEIGHT,extaxons.clone(),false,false);
}
public void drawBrain(float scaleUp, Creature owner){
ArrayList<Node> n = owner.n;
Expand Down Expand Up @@ -153,7 +212,7 @@ class Brain {
}
for(int x = 0; x < BRAIN_WIDTH-1; x++){
for(int y = 0; y < BRAIN_HEIGHT; y++){
for(int z = 0; z < BRAIN_HEIGHT-1; z++){
for(int z = 0; z < BRAIN_HEIGHT; z++){
drawAxon(x,y,x+1,z,scaleUp);
}
}
Expand All @@ -177,4 +236,91 @@ class Brain {
return color(255,255,255);
}
}
}

public void saveToJson(JsonGenerator g){
try{
g.writeNumberField("width", BRAIN_WIDTH);
g.writeNumberField("height", BRAIN_HEIGHT);
if(axons != null){
g.writeArrayFieldStart("axons");
for (int i = 0; i < axons.length; i++){
g.writeStartArray();
for(int j = 0; j < axons[i].length; j++){
g.writeStartArray();
for(int k = 0; k < axons[i][j].length; k++){
g.writeStartObject(); axons[i][j][k].saveToJson(g); g.writeEndObject();
}
g.writeEndArray();
}
g.writeEndArray();
}
g.writeEndArray();
}
if(neurons != null){
g.writeArrayFieldStart("neurons");
for(int i = 0; i < neurons.length; i++){
g.writeStartArray();
for(int j = 0; j < neurons[i].length; j++){
g.writeNumber(neurons[i][j]);
}
g.writeEndArray();
}
g.writeEndArray();
}
} catch(Exception e){
writeToErrorLog(e);
}
}

public void loadFromJson(JsonParser p){
try{
while(p.nextToken() != JsonToken.END_OBJECT){
String fieldName = p.getCurrentName();
JsonToken token = p.nextToken();
if(fieldName.equals("width")){ this.BRAIN_WIDTH = p.getIntValue(); }
else if(fieldName.equals("height")){ this.BRAIN_HEIGHT = p.getIntValue(); }
else if(fieldName.equals("axons")){
if (token != JsonToken.START_ARRAY) { throw new IOException("Expected Array"); }
int i = 0;
axons = new Axon[BRAIN_WIDTH-1][BRAIN_HEIGHT][BRAIN_HEIGHT];
while((token = p.nextToken()) != JsonToken.END_ARRAY){
if (token == JsonToken.START_ARRAY){
int j = 0;
while((token = p.nextToken()) != JsonToken.END_ARRAY){
if (token == JsonToken.START_ARRAY){
int k = 0;
while((token = p.nextToken()) != JsonToken.END_ARRAY){
if (token != JsonToken.START_OBJECT) { throw new Exception("Expected Object"); }
axons[i][j][k] = new Axon(0,0);
axons[i][j][k].loadFromJson(p);
k += 1;
}
}
j += 1;
}
}
i += 1;
}
}
else if(fieldName.equals("neurons")){
if (token != JsonToken.START_ARRAY) { throw new IOException("Expected Array"); }
int i = 0;
neurons = new float[BRAIN_WIDTH][BRAIN_HEIGHT];
while((token = p.nextToken()) != JsonToken.END_ARRAY){
if (token == JsonToken.START_ARRAY){
int j = 0;
while(p.nextToken() != JsonToken.END_ARRAY){
neurons[i][j] = p.getFloatValue();
j += 1;
}
}
i += 1;
}
}
}
} catch(Exception e){
writeToErrorLog(e);
}
}

}
Loading