Smart Technology - Codemao

Form 3 - ECA

2024-2025

Floor 4 - Computer Room

Mr. Peter

Outline

Outline

1

Ex11_1 - Bird and hamburger

2

Ex11_2 - Cat and hamburger

Codemao Platform

Change language

Change to English

Ex11_1 - Bird and hamburger

Tasks:

1.

Collect control data from playing using List variable

Train a Neural Network to play the Game

2.

This video shows that the AI I trained is playing this game

Setup the scene

1.

Play the game to understand the game rules

Import the bird_and_hamburger_template.bcm4 to the codemao

2.

Ex11_1 - Bird and hamburger

Bird

Forever to change the action of the bird

Press button to move it up or down

Ex11_1 - Bird and hamburger

Once you finished the bird programming part, please just run the program to check whether it is ok.

Ex11_1 - Bird and hamburger

Hamburger

Randomly generated

Ex11_1 - Bird and hamburger

Try to test your game

Ex11_1 - Bird and hamburger

What is the AI need to understand?

1.

If the Y axis distance between the hamburger and the bird is too large, the AI need to move.

2.

If the bird is lower than the hamburger, the bird need to fly up

3.

If the bird is higher than the hamburger, the bird need to fly down

Y axis distance between bird and hamburger:

Y position of the bird - y position of hamburger

4.

According to the formula above, we need to AI understand that the bird need to fly up if the formula is negative and the bird need to down if it is positive.

Ex11_1 - Bird and hamburger

Gather data from your gameplay to enhance model training

1.

Create a List variable called "training_data"

2.

Update the default code, add a string that join "up", "_" and the Y axis distance between bird and hamburger into training data List, if mouse is pressed.

-

Y axis distance between bird and hamburger:

Y position of the bird - y position of hamburger

3.

Update the default code, add a string that join "down", "_" and the Y axis distance between bird and hamburger into training data List, if mouse is not pressed.

Ex11_1 - Bird and hamburger

Modify your bird programming to be the following

This allow us to collect data from our operations to control the bird

Ex11_1 - Bird and hamburger

Collect data of your playing controls

At least eat 10 hamburgers and then click the download button to download the data

Ex11_1 - Bird and hamburger

1. Open the downloaded data, and copy the data in the txt file

2. Open this link and paste the content to download the csv file

Ex11_1 - Bird and hamburger

1. Import AI 2.0 Extension

Ex11_1 - Bird and hamburger

2. Create AI model and Import Data

Select the downloaded file

Ex11_1 - Bird and hamburger

What is artificial neural network?

- Artificial Neural network is simulate our human brain

Y position of the bird - y position of hamburger

Fly up or Fly down

We input the distance number into the neural network

Then, the neural network will output a result based on our input

Ex11_1 - Bird and hamburger

How to train it?

- To train the neural network, we need to continuously input the distance and update neural network if the neural network output a wrong output.

100

Fly up

During training

Wrong output, so that the neural network need to update based on an algorithm called "backpropagation".

Positive, so that the bird need to fly down

Ex11_1 - Bird and hamburger

3. Define the column

Classification

Feature1

This is the expected output if the number is input into the neural network

This is the data input into the neural network during training

Ex11_1 - Bird and hamburger

4. Prepare data for the AI training

Rename the model

Ex11_1 - Bird and hamburger

4. Adjust parameters for AI training

Enable to change the neural network

- Epoch:

How many times the model need to learn the data

- Learning Rate

The speed that the model learn, if the number is too big, it may not able to find the most accurate parameters.

- Neural network

Design by your self. If so many neuron, it will be overfitting (remember the data).

But it will not provide high accuracy if neuron is not enough.

Ex11_1 - Bird and hamburger

4. Adjust parameters for AI training

Ex11_1 - Bird and hamburger

1. Set 500 ~ 1000

2. Set 0.001

3. Start your AI model training

4. Best accuracy should be greater than 58% accuracy

4. Adjust parameters for AI training

You can input some distance number to check whether the AI model is trained well or not.

- Testing the model:

Ex11_1 - Bird and hamburger

5. Let the AI plays the game

Use the AI model and pass the distance number into the model. If the AI say press, that means we need to move the bird up and if the model say release, that means we need to move the bird down.

If you trained the AI is not good enough, you can use the perfect training data to train your AI model

Ex11_1 - Bird and hamburger

Download your program and name it as 01_YourName_Ex11_1.bcm4

Ex11_1 - Bird and hamburger