使用apache-math的遗传算法例子

主要实现Chromosome接口的fitness方法

 

package genetictest;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math3.genetics.AbstractListChromosome;
import org.apache.commons.math3.genetics.BinaryChromosome;
import org.apache.commons.math3.genetics.BinaryMutation;
import org.apache.commons.math3.genetics.Chromosome;
import org.apache.commons.math3.genetics.ElitisticListPopulation;
import org.apache.commons.math3.genetics.FixedGenerationCount;
import org.apache.commons.math3.genetics.GeneticAlgorithm;
import org.apache.commons.math3.genetics.InvalidRepresentationException;
import org.apache.commons.math3.genetics.OnePointCrossover;
import org.apache.commons.math3.genetics.Population;
import org.apache.commons.math3.genetics.StoppingCondition;
import org.apache.commons.math3.genetics.TournamentSelection;

public class GeneticAlgorithmTest {
	public static final int NUM_GENERATIONS = 100;
	
	public static void main(String[] args) {
		// initialize a new genetic algorithm
		GeneticAlgorithm ga = new GeneticAlgorithm(
		    new OnePointCrossover<Integer>(),
		    1,
		    new BinaryMutation(),
		    0.10,
		    new TournamentSelection(21)
		);
		        
		// initial population
		Population initial = getInitialPopulation();
		        
		// stopping condition
		StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS);
		        
		// run the algorithm
		Population finalPopulation = ga.evolve(initial, stopCond);
		        
		// best chromosome from the final population
		Chromosome bestFinal = finalPopulation.getFittestChromosome();
		System.out.println(bestFinal);
	}

	private static Population getInitialPopulation() {
		List<Chromosome> chromosomes = new ArrayList<>();
		int populationLimit = 1000;
		double elitismRate = 0.1;
		int chromosomeCount = BagBinaryChromosome.WEIGHTS.length; 
		
		for(int i = 0; i < populationLimit; i++){
			List<Integer> temp = new ArrayList<>(chromosomeCount);
			for(int j = 0; j < chromosomeCount; j++){
				temp.add(Math.random() < 0.5? 1:0);
			}
			chromosomes.add(new BagBinaryChromosome(temp));
		}
		
		Population ret = new ElitisticListPopulation(chromosomes, populationLimit, elitismRate);
		return ret;
	}
}

class MyBinaryChromosome extends BinaryChromosome{
	
	public MyBinaryChromosome(Integer[] representation) throws InvalidRepresentationException {
		super(representation);
	}
	
	public MyBinaryChromosome(List<Integer> representation) throws InvalidRepresentationException {
		super(representation);
	}
	
	@Override
	public double fitness() {
		double ret = 0.0;
		for(Integer val : this.getRepresentation()){
			ret += Math.pow(val, 3);
		}
		return ret;
	}
	
	@Override
	public AbstractListChromosome<Integer> newFixedLengthChromosome(List<Integer> chromosomeRepresentation) {
		return new MyBinaryChromosome(chromosomeRepresentation);
	}
	
}

class BagBinaryChromosome extends BinaryChromosome{
	
	public static final int[] WEIGHTS = {2,2,6,5,4,2,2,6,5,4};
	public static final int[] VALUE = {6,3,5,4,6,6,3,5,4,6};
	public static final int MAX_WEIGHT = 20;
	
	public BagBinaryChromosome(List<Integer> representation) throws InvalidRepresentationException {
		super(representation);
	}

	@Override
	public double fitness() {
		double ret = 0.0;
		int weight = 0;
		int index = 0;
		for(Integer val : this.getRepresentation()){
			if(val.equals(1)){
				ret += VALUE[index];
				weight += WEIGHTS[index];
			}
			index++;
		}
		if(weight > MAX_WEIGHT){
			ret = 0.0;
		}
		return ret;
	}
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder(super.toString());
		
		int value = 0;
		int weight = 0;
		int index = 0;
		for(Integer val : this.getRepresentation()){
			if(val.equals(1)){
				value += VALUE[index];
				weight += WEIGHTS[index];
			}
			index++;
		}
		sb.append(", WEIGHT: ").append(weight).append(", value: ").append(value);
		
		return sb.toString();
	}

	@Override
	public AbstractListChromosome<Integer> newFixedLengthChromosome(List<Integer> chromosomeRepresentation) {
		return new BagBinaryChromosome(chromosomeRepresentation);
	}
	
}

转载于:https://my.oschina.net/u/1778261/blog/833192

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值