Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGITH authored Sep 13, 2019
1 parent 0c4735b commit 06a3786
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Flappy_NN/Bar.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Bar
{
float x, y;
Bar()
{
x = width;
y = random(90, height - 90);
}

void drawBar()
{
line(x, y - 110, x, y + 110);
x--;
}

}
99 changes: 99 additions & 0 deletions Flappy_NN/Flappy_NN.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

ArrayList<Bar> barList = new ArrayList<Bar>();

void setup()
{
size(1280,1080);
}

float barSpawnTimer = 400;

float accelerationX, accelerationY, velocityX, velocityY;
float posX = 320, posY = 360;

float gravity = 0.3, jumpforce = 8.5;

float heightDifference, lengthDifference;

boolean enablePlayer;

void draw()
{
background(102, 255, 255);

barSpawnTimer++;
if(barSpawnTimer > 200)
{
barList.add(new Bar());
barSpawnTimer = 0;
}
for(Bar bar : barList)
{
bar.drawBar();
}
if(barList.get(0).x < 320)
{
barList.remove(0);
}
ellipse(barList.get(0).x, barList.get(0).y, 10, 10);

if(barList.get(0).x < 400){
enablePlayer = true;
}
if(enablePlayer)
{
accelerationX += 0;
accelerationY += gravity;
velocityX += accelerationX;
velocityY += accelerationY;
posX += velocityX;
posY += velocityY;
accelerationX *= 0;
accelerationY *= 0;
if(posY < 0)
{
velocityY = 0;
}
stroke(10);
ellipse(posX,posY,25,25);
fill(250);

heightDifference = barList.get(0).y - posY;
lengthDifference = barList.get(0).x - posX;

if(barSpawnTimer > 10)
{
giveInput();
}
}
}

boolean released = true;
float REMEMBER_HEIGHT_DIF, REMEMBER_LENGTH_DIF;

void keyPressed()
{
if(key == ' ')
{
velocityY = 0;
accelerationY -= jumpforce;
released = false;
//print("HD: " + heightDifference + "\n");
//print("LD: " + lengthDifference + "\n");
//print("\n");
REMEMBER_HEIGHT_DIF = heightDifference;
REMEMBER_LENGTH_DIF = lengthDifference;

if(lengthDifference <= 56 && (heightDifference <= 15 && heightDifference >= -220))
{
//print("HD: " + heightDifference + "\n");
//print("LD: " + lengthDifference + "\n");
//print("\n");
}
}
}

void keyReleased()
{
released = true;
}
99 changes: 99 additions & 0 deletions Flappy_NN/NN.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

float target(float HD, float LD)
{
//medium height = calculate ideal height-for-(ideal length-for-HD)
//difference_from_medium = HD - medium height
//if(difference_from_medium < range)
//return 1
//else
//return 0

//Tip: de 1 & 0 moeten misschien andersom zijn
if(LD >= 56 && (HD >= -75))
{
//print(1);
//print("\n");
return 1;
}
else
{
//print(0);
//print("\n");
return 0;
}

}

float sigmoid(float x)
{
return 1/(1+pow(2.71828182846, -x));
}

float learning_rate = 0.2;

float w1 = random(1)*.2-.1;
float w2 = random(1)*.2-.1;
float b = random(1)*.2-.1;

ArrayList<float[]> dataList = new ArrayList<float[]>();

void giveInput(){
float[] point = {heightDifference, lengthDifference, target(heightDifference, lengthDifference)};
dataList.add(point);
float output = train();
print(output + "\n");
print("\n");
jump_or_not(output);
}

float train(){

for(int i = 0; i < 5000; i++){
//Geef input
float[] point = dataList.get(round(random(0,dataList.size() - 1)));
float target = point[2];
//Maak (training) voorspelling
float z = w1 * point[0] + w2 * point[1] + b;
float pred = sigmoid(z);

//Bereken cost & back-propagate
float cost = pow((pred - target),2);

//Derivatives berekeningen:
float dcost_dpred = 2 * (pred - target);
float dpred_dz = sigmoid(z) * (1-sigmoid(z));

float dz_dw1 = point[0];
float dz_dw2 = point[1];
float dz_db = 1;

float dcost_dw1 = dcost_dpred * dpred_dz * dz_dw1;
float dcost_dw2 = dcost_dpred * dpred_dz * dz_dw2;
float dcost_db = dcost_dpred * dpred_dz * dz_db;

w1 -= learning_rate * dcost_dw1;
w2 -= learning_rate * dcost_dw2;
b -= learning_rate * dcost_db;

}

float z = w1 * heightDifference + w2 * lengthDifference + b;
float pred = sigmoid(z);
return pred;
}


void jump_or_not(float output)
{
if(output > 0.5)
{

}
else
{
velocityY = 0;
accelerationY -= jumpforce;
}
}

//Geef input (& target)

0 comments on commit 06a3786

Please sign in to comment.