Silverback9

#야생으로

Creative Coding 독학 제249일 2024년12월01일(일)

오늘 matingPool[ ]과 calcFitness( )에 대해 공부해 보기로 했었지요~^^*

class Population {
  constructor(p, m, num) {
    this.population; // Array to hold the current population
    this.matingPool; // ArrayList which we will use for our "mating pool"
    this.generations = 0; // Number of generations
    this.finished = false; // Are we finished evolving?
    this.target = p; // Target phrase
    this.mutationRate = m; // Mutation rate
    this.perfectScore = 1;

    this.best = "";

    this.population = [];
    for (let i = 0; i < num; i++) {
      this.population[i] = new DNA(this.target.length);
    }
    this.matingPool = [];
    this.calcFitness();
  }
   .
   .
   .
} 

함수 calcFitness( )를 통해 적합성을 평가하여 짝지기 대상자에 적합한 요소를 matingPool[ ]에 모으는 것 같아요~^^*

DRAW

  Step 2: Selection: Evaluate the fitness of each element of the population and build a mating pool.

 선별: 군집의 각 요소들의 적합성을 평가하여 mating pool를 만든다. 

오늘은 calcFitness( )에 대해서만 공부해 보기로 할게요~^^*

matingPool[ ]은 Step 3에서 구체적으로 다루어 지는 것 같아서요.

오늘 우리 Step 2에 집중해 볼까요~^^*

class Population {
 constructor(p, m, num) {
     .
     .
     .
    this.matingPool = [];
    this.calcFitness();
  }  
  
 calcFitness() {
    for (let i = 0; i < this.population.length; i++) {
      this.population[i].calcFitness(target);
    }
  }
 .
 .
 .
}   

네…

클래스 Population의 calcFitness( )은 population[ ]의 모든 요소들의 내장 함수 calcFiness( )를 호출하며 목표 문구를 argument로 전달하네요…

그렇다면!

네~^^* 클래스 DNA의 calcFitness( )를 살펴 보아야겠어요~^^*

class DNA {
  constructor(num) {
    this.genes = [];
    this.fitness = 0;
    for (let i = 0; i < num; i++) {
      this.genes[i] = newChar();
    }
  }
  .
  .
  .
  
  calcFitness(target) {
    let score = 0;
    for (let i = 0; i < this.genes.length; i++) {
      if (this.genes[i] == target.charAt(i)) {
        score++;
      }
    }
    this.fitness = score / target.length;
  }
  .
  .
  .
}

목표 문구의 한 글자 한 글자와 클래스 DNA의 배열 genes[ ] 한 요소 한 요소를 차례대로 대조해 보며, 같은 글자가 있는 경우 점수를 1 증가시킵니다.

즉, 같은 자리에 같은 글자가 있는 경우가 몇 번인가가 점수가 되겠네요.

모든 자리의 글자를 대조해 보는 것이 마무리되면,

그 점수를 목표 문구의 길이로 나눈 값이 이 DNA의 fitness값이 되는 것 같아요.

예를 들어, 목표 “unicorn” DNA “ptikoln” 의 경우,

target .length는 7이고, score는 3이고, fitness는 3/7이 될 것 같아요~~^^*

네~^^*

클래스 Population의 내장 함수 calcFitness()는 클래스 DNA의 내장함수 calcFitness를 호출하여, 목표 문구에 대한 배열 genes[]의 의 적합도 fitness를 계산하도록 안내하는 역할을 하는 것 같아요.

이 적합도 fitness가 matingpoop[] 만들기에 어떻게 쓰여질 지~~^^* 궁금하시죠~~^^*

그건 우리 내일 편안하게 음악 듣고 화요일에 알아 볼까요~~^^*

오늘 저와 함께 클래스 Population의 calcFitness()와 클래스 DNA의 calcFitness()에 대해 살펴 봐 주셔서 감사합니다~^^* Step 2 완료~~^^*

클래스 Population와 클래스 DNA가 서로 도와가며 아주 신기한 멋진 일을 해내어 갈 것 같은 좋은 예감~^^*

좋은 예감을 가지고~^^*

점심 맛있게 드시고요~^^*

오늘도

기쁨 나누는 만남 멋지게 나누시고요~^^*

멋진 하루와 따뜻한 밤 보내시기 바래요~~^^*

우리 내일 같이 음악 들어요~~^^*

네~~^^* 꿈은 이루어 집니다~~^^*

댓글 남기기