Genetic Algorithm을 정리한 내용입니다~^^*
Genetic Algorithm
SETUP
Step 1: Initialize: Create a population of N elements, each with randomly generated DNA.
DRAW
Step 2: Selection: Evaluate the fitness of each element of the population and build a mating pool.
Step 3: Reproduction: Repeat N times:
a. Pick two parents with probability accoridng to relative fitness
b. Crossover - create a "child" by combining the DNA of these two parents
c. Mutation - mutate the child;s DNA based on a given probability
d. Add the new child to a new population
Step 4: Replace the old population with the new populatio and return to Step 2.
이 알고리즘에 따라 “To be or not to be”라는 문장을 생성하는 프로그램을 테스트 해보실까요~^^*
Setup() 안에 있는 popmax와 mutationRate를 수정하여 플레이 해 보셔도 재미있을 것 같아요.
수정 후 플레이하고 프로그램을 종료하면, 수정된 내용은 저장이 되지 않고 원래의 코드로 복귀되니 자유롭게 수정하여 테스트 해 볼 수 있어요~^^*
popmax와 mutationRate가 크면, 군집의 규모가 커지고 군집 내 다양성이 높아져 목표하는 문장을 생성하기 쉬울 것 같습니다.
물론, 프로그램의 효율성과 군집 정체성의 안정성을 위해
popmax =1000;
mutationRate 0.01;
이 효율적인 것 같기는 하구요~^^*
let target;
let popmax;
let mutationRate;
let population;
let bestPhrase;
let allPhrases;
let stats;
function setup() {
bestPhrase = createP("Best phrase:");
//bestPhrase.position(10,10);
bestPhrase.class("best");
allPhrases = createP("All phrases:");
allPhrases.position(600, 10);
allPhrases.class("all");
stats = createP("Stats");
//stats.position(10,200);
stats.class("stats");
//createCanvas(640, 360);
target = "To be or not to be.";
popmax = 200;
mutationRate = 0.01;
// Create a population with a target phrase, mutation rate, and population max
population = new Population(target, mutationRate, popmax);
}
function draw() {
// Generate mating pool
population.naturalSelection();
//Create next generation
population.generate();
// Calculate fitness
population.calcFitness();
population.evaluate();
// If we found the target phrase, stop
if (population.isFinished()) {
//println(millis()/1000.0);
noLoop();
}
displayInfo();
}
function displayInfo() {
// Display current status of population
let answer = population.getBest();
bestPhrase.html("Best phrase:<br>" + answer);
let statstext =
"total generations: " + population.getGenerations() + "<br>";
statstext +=
"average fitness: " + nf(population.getAverageFitness()) + "<br>";
statstext += "total population: " + popmax + "<br>";
statstext += "mutation rate: " + floor(mutationRate * 100) + "%";
stats.html(statstext);
allPhrases.html("All phrases:<br>" + population.allPhrases());
}
오늘 저와 함께 Genetic Algorithm을 바탕으로 하는 “To be or not to be” 문구 생성하기 프로그램을 군집크기와 돌연변이률을 조정하여 다양하게 테스트 해 주셔서 감사합니다~^^*
우리 내일 또 Genetic Algorithm을 좀 더 깊게 공부해 볼까요~^^*
오늘도
멋진 아침! 보내시고요!
맛있는 점심 드시고요!
멋진 하루 보내시고요!
내일 또 만나서 코딩 공부 함께 할까요~~^^*
네~~^^* 꿈은 이루어 집니다~~^^*
댓글 남기기