[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/

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
45 [게임 일반] 선택 기반 게임의 표준 패턴 : Standard Patterns in Choice-Based Games 졸리운_곰 2025.01.13 236
44 [게임 일반] SharpMoku a Gomoku/Five in a Row Written in C# : SharpMoku a Gomoku/Five in a Row C#로 작성됨 : 오목게임 개발 file 졸리운_곰 2024.10.31 311
43 [게임 마켓플레이스] 스팀에 ‘게임’을 출시하고 싶다면? file 졸리운_곰 2024.09.07 316
42 [게임 일반] 카드 덱 이미지 (트럼프 카드) 졸리운_곰 2024.05.27 224
41 [게임 개발 일반] 콘솔은 어려울수록, 모바일은 쉬울수록 '돈' 쓴다 file 졸리운_곰 2023.10.30 264
40 [게임 개발 일반] 게임 초기 기획서 작성 방법 졸리운_곰 2023.07.16 250
39 [게임 일반] 하이퍼 캐주얼 모바일 게임 퍼블리셔가 말하는 개발자의 필수 역량은? file 졸리운_곰 2023.02.08 245
38 [게임 일반] 외신, 2D 게임 개발에 적합한 엔진 10종 선정 file 졸리운_곰 2023.01.15 206
37 [게임 일반] 성공적인 하이퍼 캐주얼 게임 제작을 위해 알아야 할 모든 것 졸리운_곰 2023.01.11 192
36 [게임 일반] 게임의 정의와 요소 file 졸리운_곰 2023.01.02 185
35 [게임 일반] 게임(Game), 일상에서 만드는 놀이의 즐거움 file 졸리운_곰 2023.01.02 205
34 [게임 일반] 우리는 왜 게임에 빠지는가 - 게임의 요소와 게임 변천의 역사 졸리운_곰 2023.01.02 250
33 [게임 일반] 재미있는 게임이란? 10부. 게임의 6단계 file 졸리운_곰 2023.01.02 194
32 [게임 일반] 재미있는 게임이란? 9부 -스타2의 패턴전략- file 졸리운_곰 2023.01.02 214
31 [게임 일반] 재미있는 게임이란? 8부 -블리자드의 패턴전략- file 졸리운_곰 2023.01.02 265
30 [게임 일반] 재미있는 게임이란? 7부 -Wow의 패턴전략- file 졸리운_곰 2023.01.02 162
29 [게임 일반] 재미있는 게임이란? 6부 -패턴과 리소스- file 졸리운_곰 2023.01.02 228
28 [게임 일반] 재미있는 게임이란? 5부 -카오스 시스템- file 졸리운_곰 2023.01.02 266
27 [게임 일반] 재미있는 게임이란? 4부 -패턴과 상호작용-' file 졸리운_곰 2023.01.02 186
26 [게임 일반] 재미있는 게임이란? 3부 '게임이 나아가야 할 방향' file 졸리운_곰 2023.01.02 162
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED