[GameMaker Studio] Neural Network in Game Maker Studio – part 4

What makes the “learning” in machine learning is training. Now that we created a neural net, it is time to code the training part.

In order to train our neural network, we are going to use genetic algorithm. This is the easiest way to train our agents. Initially, we only have to create multiple instances of the arrow head object, our agent, with random weights. We’ll test it and select the one with the highest fitness (which output is the closest to what’s desired), copy it’s DNA – it’s weights and bias – to other instances, and then mutate every children.

Fitness

So, how are we going to choose which agent has the best DNA? That’s where fitness comes in.

Fitness is the determinant of which agent performs closest to the desired output. Agent with highest fitness will be cloned and mutated. This process will be repeated until an agent reach the most optimal performance our neural net can reach.

For every problem, there are different ways to compute the fitness. Fitness is an important factor in our neural net’s performance. It is necessary that the fitness calculation can determine which agent is performing the best out of the population.

User Event 0

Write the code below on the user event 0. This code will calculate the fitness of an agent based on its average distance to the target.

///Compute Fitness

agent_score += -distance_to_object(obj_target);
point_in_time += 1;

fitness = agent_score/point_in_time;
Alarm 0

The purpose of the alarm event is to create a delay between each check instead of checking every frame.

///Delay compute fitness
event_user(0);
alarm[0] = 5;
Create Event

Mutate Script

The mutate script will mutate the parameter ‘x’ by some certain rate and return a mutated value.

///mutate(x, rate)

//Init
    var _val = argument0;
    var _rate = argument1;
    var _chance = irandom(100);
    
//Randomize    
    if(_chance < 10*_rate){              //Flip sign
        _val *= -1;
    } else if(_chance < 20*_rate){       //Reroll
        _val = random_range(-1, 1); 
    } else if(_chance < 45*_rate){      //Increase(0% to 100%)
        _val = random_range(_val, _val*2); 
    } else if(_chance < 70*_rate){      //Decrease(0% to 100%)
        _val = random_range(0, _val); 
    }
    
//Return Val    
    return(_val);

Mutation

User Event 1

On the agent’s user event 1

///Mutate
//Mutate neuron weights
    for(var j=0; j<array_height_2d(weights) ; j++){
        for(var k=0; k<array_length_2d(weights, j); k++){
            weights[j,k] = mutate(weights[j,k], 0.4);
        }
    }
    
//Mutate output weights    
    for(var j=0; j<array_height_2d(output_weights) ; j++){
        for(var k=0; k<array_length_2d(output_weights, j); k++){            
            output_weights[j,k] = mutate(output_weights[j,k], 0.4);
        }
    }

When mutating, we either increase/decrease the value, flip the sign, or randomize it. But do take in mind that we won’t be changing all the weights and biases. There is only a small chances that a weight or a bias will be mutated.

Resetting the Agent

The remaining thing to do is to add a user event to reset the position and other variables of our agents . This will be executed after training; To test the neural net once again.

///Reset

//Back to start position
    x = xstart;
    y = ystart;

//Reset Vars
    fitness = 0;
    point_in_time = 0;
    agent_score = 0;
    alarm[0] = 5;

Training

Open our neural net main controller object obj_nn_main

User Event 0
///Train

//Search for fittest agent
    var _fittest = noone;   //Agent that has highest fitness score
    var _fitness = -99999;  //Fitness score of the most fit agent
    
    with(obj_arrowhead){
        //If current instance has higher fitness than remembered instance
        if(fitness > _fitness){
            _fittest = id;
            _fitness = fitness;
        }
    }
    
//Copy weights and biases
    var _weights;
    var _bias;
    var _output_weights;
    var _output_bias;
    
    //Save DNA for future use
    with(_fittest){
        _weights = weights;
        _bias = bias;        
        _output_weights = output_weights;
        _output_bias = output_bias;
    }

    with(obj_arrowhead){
        if(id != _fittest){
            //Copy DNA
            weights = _weights;
            bias = _bias;
            output_weights = _output_weights;
            output_bias = _output_bias;
            
            //Mutate
            event_user(1);
        }
        //Reset
        event_user(2);
    }
    
Train on keypress

Aside on using key press to train our agents, we can also use an alarm to train our agents after every delay. But for simplicity, we will train our them on key press.

Keyboard press event for <Spacebar>
///Train
event_user(0);
Show Fitness

This part is optional, but this can help you to see which of the agent is performing well, and can make debugging easier.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

///Draw fitness
draw_set_colour(c_lime);
with(obj_arrowhead){
    draw_text(x, y, fitness);
}

We are done

Run the game and see how your neural net performs.

If your neural net behaves like the example below. You successfully created your first neural net. It is now time to use the neural net for more complex task.

After quite some time building a machine learning algorithm

 

 

 

 

 

 

 

 

, it is the time to make a use out of it. This time, ideas and information will come from you. Share your creation! Feel free to comment a link or a snapshot about how you used this machine learning algorithm.

Want to learn more about this topic? Give me a nudge or write a comment about what topic you want to tackle next. Until next post. Cheers!

 

[출처] https://highleapstudios.wordpress.com/2019/10/12/neural-network-in-game-maker-studio-4/

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
151 [flutter (플루터 앱 개발)] flutter pocketbase 사용법 2탄(회원가입) file 졸리운_곰 2024.06.10 272
150 [flutter (플루터 앱 개발)] flutter pocketbase 사용방법 1탄(초기설정) file 졸리운_곰 2024.06.10 242
149 [게임 일반] 카드 덱 이미지 (트럼프 카드) 졸리운_곰 2024.05.27 224
148 [flutter (플루터 앱 개발)]Flutter - InAppWebview Communication (인앱웹뷰로 웹과 앱 양방향 통신하기, 자바스크립트 커뮤니케이션) file 졸리운_곰 2024.04.13 274
147 [flutter (플루터 앱 개발)] flutter - fvm 적용하기 (Futter Version Management) file 졸리운_곰 2024.04.11 189
146 [flutter (플루터 앱 개발)]Using Flutter FVM in Windows & MacOS file 졸리운_곰 2024.04.10 294
145 [flutter (플루터 앱 개발)] Flutter version management in Visual Studio Code with FVM 졸리운_곰 2024.04.10 231
144 [flutter (플루터 앱 개발)] pHp backend with flutter file 졸리운_곰 2024.03.18 350
143 [flutter (플루터 앱 개발)] [Flutter] fvm으로 Flutter 버전 관리 졸리운_곰 2024.03.17 203
142 [firebase v9] 파이어베이스 버전9 시작하기 - firebase v9 file 졸리운_곰 2023.12.24 309
141 [ Firebase ] v9부터 크게 달라진 사용법들 모아보기 file 졸리운_곰 2023.12.24 268
140 [게임 개발 일반] 콘솔은 어려울수록, 모바일은 쉬울수록 '돈' 쓴다 file 졸리운_곰 2023.10.30 264
139 [Unity] How to Create a Metaverse dApp with Unity : Unity로 Metaverse dApp을 만드는 방법 file 졸리운_곰 2023.09.13 397
138 [게임 개발 일반] 게임 초기 기획서 작성 방법 졸리운_곰 2023.07.16 250
» [GameMaker Studio] Neural Network in Game Maker Studio – part 4 file 졸리운_곰 2023.07.07 462
136 [GameMaker Studio] Neural Network in Game Maker Studio – part 3 file 졸리운_곰 2023.07.07 413
135 [GameMaker Studio] Neural Network in Game Maker Studio – part 2 file 졸리운_곰 2023.07.07 506
134 [GameMaker Studio] Neural Network in Game Maker Studio – part 1 file 졸리운_곰 2023.07.07 378
133 [construct2] HOW TO REMOVE THE SCREEN POWERED BY CONSTRUCT 2? file 졸리운_곰 2023.03.24 266
132 [flutter (플루터 앱 개발)] How to use Flutter’s Rest API file 졸리운_곰 2023.03.21 349
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED