-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplate.py
More file actions
47 lines (38 loc) · 2.11 KB
/
Template.py
File metadata and controls
47 lines (38 loc) · 2.11 KB
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
# This is the template file for the Flappy Bird project. You can use this file to implement your own solution.
import random
import torch
import torch.nn as nn
MUTATION_RATE = 0.4 # Probability of each weight/bias being mutated
MUTATION_STRENGTH = 0.1
N = 5 # Number of birds to select for next round
""" Each bird contains a neural network model that decides when to jump (variable name is 'model'). Each bird also contains a 'deathTime' that records how long the bird survived for.
This should be used to determine which birds are the best. """
# Neural network model for each bird
class Model(nn.Module):
def __init__(self, in_channels, out_channels):
""" Initializes the neural network model with correct input and output sizes. """
super(Model, self).__init__()
# TODO: Define the layers of the neural network here
def forward(self, input):
"""
Forward pass of the neural network.
Input is [closest_pipe_x, closest_pipe_y, y_position, velocity]. All values are normalized between 0 and 1.
Output is a single value between 0 and 1. If output < 0.5, the bird should jump.
"""
# TODO: Define the forward pass of the neural network here
return 0 # Placeholder return value
def mutate_bird(bird):
"""
Mutates the bird's neural network model based on the mutation rate and strength (constants above).
"""
new_bird = bird.clone()
# TODO: Implement mutation of the bird's model here (loop through new_bird.model.parameters() and mutate each parameter). bird.clone() is used to copy the bird without modifying the original.
return new_bird
def select_next_round(birds):
"""
Selects the N best birds to be used for the next round.
"""
best_birds = []
# TODO: Sort the birds based on their deathTime and return the top performers OR use a probability distribution based on deathTime to select birds (so that even lower performing birds have a chance of being selected).
best_birds = birds[:N] # Placeholder selection (Remove this line)
return best_birds