All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.chen0040.gp.lgp.gp.PopulationInitialization Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package com.github.chen0040.gp.lgp.gp;


import com.github.chen0040.gp.lgp.LGP;
import com.github.chen0040.gp.lgp.enums.LGPInitializationStrategy;
import com.github.chen0040.gp.lgp.helpers.InstructionHelper;
import com.github.chen0040.gp.lgp.program.*;
import com.github.chen0040.gp.services.RandEngine;
import com.github.chen0040.data.exceptions.NotImplementedException;

import java.util.List;


/**
 * Created by xschen on 7/5/2017.
 */
public class PopulationInitialization {
   public static void apply(List programs, LGP manager) {
      RandEngine randEngine = manager.getRandEngine();
      if(manager.getProgramInitializationStrategy() == LGPInitializationStrategy.ConstantLength){
         initializeWithConstantLength(programs, manager, randEngine);
      } else if(manager.getProgramInitializationStrategy() == LGPInitializationStrategy.VariableLength) {
         initializeWithVariableLength(programs, manager, randEngine);
      } else {
         throw new NotImplementedException();
      }
   }

   // Xianshun says:
   // specified here is a variable length initialization that selects initial program
   // lengths from a uniform distribution within a specified range of m_iInitialMinProgLength - m_iIinitialMaxProgLength
   // the method is recorded in chapter 7 section 7.6 page 164 of Linear Genetic Programming 2004
   // Xianshun says:
   // the program generated in this way will have program length as small as
   // iMinProgLength and as large as iMaxProgLength
   // the program length is distributed uniformly between iMinProgLength and iMaxProgLength
   private static void initializeWithVariableLength(List programs, LGP manager, RandEngine randEngine) {
      int popSize = manager.getPopulationSize();

      for(int i=0; i < popSize; ++i) {
         Program lgp= new Program();
         initialize(lgp, manager, randEngine, randEngine.nextInt(manager.getPopInitMinProgramLength(), manager.getPopInitMaxProgramLength()));
         programs.add(lgp);
      }
   }

   // Xianshun says:
   // the program generated in this way will have program length as small as
   // iMinProgLength and as large as iMaxProgLength
   // the program length is distributed uniformly between iMinProgLength and iMaxProgLength
   private static void initializeWithConstantLength(List programs, LGP manager, RandEngine randEngine) {
      int popSize = manager.getPopulationSize();

      for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy