2016-07-05 2 views
1

나는 Jenetics 라이브러리를 실험하기 시작했다. 그러나 매우 쉬운 "custom"유전자/염색체 세트를 만들기 위해 몇 가지 문제가있다. 내가 시도한 것은 내부에 다른 (임의의) 개수의 맞춤 유전자가 포함 된 맞춤 염색체를 만드는 것이 었습니다. 단순성을 위해서 유전자는 단순히 정수 값을 포함합니다. 똑같은 단순성을 위해서 유전자의 내용은 0에서 9까지의 숫자 일 수 있고 유전자는 숫자 9를 포함하지 않는 경우에만 유효하다고 간주됩니다 (다시 말하면 지체없이 간단하지만 그냥 맞춤화하려고했습니다)Jenetics 커스텀 유전자/염색체

CustomGene :

public class CustomGene implements Gene<Integer, CustomGene> { 

    private Integer value; 

    private CustomGene(Integer value) { 
     this.value = value; 
    } 

    public static CustomGene of(Integer value) { 
     return new CustomGene(value); 
    } 

    public static ISeq<CustomGene> seq(Integer min, Integer max, int length) { 
     Random r = RandomRegistry.getRandom(); 
     return MSeq.<CustomGene>ofLength(length).fill(() -> 
       new CustomGene(random.nextInt(r, min, max)) 
     ).toISeq(); 
    } 

    @Override 
    public Integer getAllele() { 
     return value; 
    } 

    @Override 
    public CustomGene newInstance() { 
     final Random random = RandomRegistry.getRandom(); 
     return new CustomGene(Math.abs(random.nextInt(9))); 
    } 

    @Override 
    public CustomGene newInstance(Integer integer) { 
     return new CustomGene(integer); 
    } 

    @Override 
    public boolean isValid() { 
     return value != 9; 
    } 
} 

CustomChromosome :

import org.jenetics.Chromosome; 
import org.jenetics.util.ISeq; 
import org.jenetics.util.RandomRegistry; 

import java.util.Iterator; 
import java.util.Random; 

public class CustomChromosome implements Chromosome<CustomGene> { 

    private ISeq<CustomGene> iSeq; 
    private final int length; 

    public CustomChromosome(ISeq<CustomGene> genes) { 
     this.iSeq = genes; 
     this.length = iSeq.length(); 
    } 

    public static CustomChromosome of(ISeq<CustomGene> genes) { 
     return new CustomChromosome(genes); 
    } 

    @Override 
    public Chromosome<CustomGene> newInstance(ISeq<CustomGene> iSeq) { 
     this.iSeq = iSeq; 
     return this; 
    } 

    @Override 
    public CustomGene getGene(int i) { 
     return iSeq.get(i); 
    } 

    @Override 
    public int length() { 
     return iSeq.length(); 
    } 

    @Override 
    public ISeq<CustomGene> toSeq() { 
     return iSeq; 
    } 

    @Override 
    public Chromosome<CustomGene> newInstance() { 
     final Random random = RandomRegistry.getRandom(); 
     ISeq<CustomGene> genes = ISeq.empty(); 
     for (int i = 0; i < length; i++) { 
      genes = genes.append(CustomGene.of(Math.abs(random.nextInt(9)))); 
     } 
     return new CustomChromosome(genes); 
    } 

    @Override 
    public Iterator<CustomGene> iterator() { 
     return iSeq.iterator(); 
    } 

    @Override 
    public boolean isValid() { 
     return iSeq.stream().allMatch(CustomGene::isValid); 
    } 
} 

홈페이지 :

여기

내 코드입니다

import org.jenetics.Genotype; 
import org.jenetics.Optimize; 
import org.jenetics.engine.Engine; 
import org.jenetics.engine.EvolutionResult; 
import org.jenetics.util.Factory; 
import org.jenetics.util.RandomRegistry; 

import java.util.Random; 

public class Main { 

    private static int maxSum = - 100; 

    private static Integer eval(Genotype<CustomGene> gt) { 
     final int sum = gt.getChromosome().stream().mapToInt(CustomGene::getAllele).sum(); 
     if(sum > maxSum) 
      maxSum = sum; 
     return sum; 

    } 

    public static void main(String[] args) { 
     final Random random = RandomRegistry.getRandom(); 

     Factory<Genotype<CustomGene>> g = 
       Genotype.of(CustomChromosome.of(CustomGene.seq(0, 9, Math.abs(random.nextInt(9)) + 1))); 

     Engine<CustomGene, Integer> engine = Engine 
       .builder(Main::eval, g) 
       .optimize(Optimize.MAXIMUM) 
       .populationSize(100) 
       .build(); 

     Genotype<CustomGene> result = engine.stream().limit(10000) 
       .collect(EvolutionResult.toBestGenotype()); 

     System.out.println(eval(result)); 
     result.getChromosome().stream().forEach(i -> { 
      System.out.print(i.getAllele() + " "); 
     }); 

     System.out.println(); 

     System.out.println(maxSum); 
    } 
} 

내가이 출력 얻을 왜 이해가 안 : 우리는 분명 가장 큰 피트니스 기능과 선택한 유전자형을 가지고 유전자형의 차이를 볼 수 있습니다

13 (evaluated result) 
1 8 0 4 (all the alleles form the genes of the chosen chromosome) 
32 (the actual maximum fitness found) 

합니다. 왜? 나는 내가 뭔가 잘못하고 있고 어리석은 실수라고 알고 있지만, 내가 잘못하고있는 것을 정말로 이해하지 못하는 것 같습니다. 너 나 좀 도와 줄래?

감사합니다. 라이브러리 here의 창조자에 의해 게시 된

답변

0

대답했다 :

당신은 Chromosome.newInstance (ISEQ) 방식의 계약을 위반했습니다. 이 메소드는 새로운 염색체 인스턴스를 반환해야합니다. 수정 후

@Override 
public Chromosome<CustomGene> newInstance(ISeq<CustomGene> iSeq) { 
    return new CustomChromosome(iSeq); 
}