本文整理汇总了Java中org.apache.commons.math3.genetics.UniformCrossover类的典型用法代码示例。如果您正苦于以下问题:Java UniformCrossover类的具体用法?Java UniformCrossover怎么用?Java UniformCrossover使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UniformCrossover类属于org.apache.commons.math3.genetics包,在下文中一共展示了UniformCrossover类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ga
import org.apache.commons.math3.genetics.UniformCrossover; //导入依赖的package包/类
/**
* Genetic algorithm global optimization.
*
* @param work
* Work unit to be optimized.
* @return Solution found.
*/
private static List<Task> ga(WorkUnit work) {
/*
* Generate initial population.
*/
List<Chromosome> list = new LinkedList<Chromosome>();
for (int i = 0; i < populationSize; i++) {
list.add(new TaskListChromosome(work.generateRandomValidSolution(), work));
}
Population initial = new ElitisticListPopulation(list, 2 * list.size(), elitismRate);
/*
* Initialize genetic algorithm.
*/
GeneticAlgorithm algorithm = new GeneticAlgorithm(new UniformCrossover<TaskListChromosome>(0.5), crossoverRate,
new RandomTaskMutation(lowMutationBoundary, highMutationBoundary), mutationRate,
new TournamentSelection(tournamentArity));
/*
* Run optimization.
*/
Population optimized = algorithm.evolve(initial, new FixedElapsedTime(optimizationTimeout));
/*
* Obtain result.
*/
return ((TaskListChromosome) optimized.getFittestChromosome()).getSolution();
}
示例2: Display
import org.apache.commons.math3.genetics.UniformCrossover; //导入依赖的package包/类
public Display() throws Exception {
setTitle("Commons-Math: Image Evolution Example");
setSize(600, 400);
setLayout(new FlowLayout());
Box bar = Box.createHorizontalBox();
ref = ImageIO.read(new File("resources/monalisa.png"));
//ref = ImageIO.read(new File("resources/feather-small.gif"));
referenceImage = resizeImage(ref, 50, 50, BufferedImage.TYPE_INT_ARGB);
testImage = new BufferedImage(referenceImage.getWidth(), referenceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
JLabel picLabel = new JLabel(new ImageIcon(ref));
bar.add(picLabel);
painter = new ImagePainter(ref.getWidth(), ref.getHeight());
bar.add(painter);
// set the images used for calculating the fitness function:
// refImage - the reference image
// testImage - the test image to draw the current chromosome
PolygonChromosome.setRefImage(referenceImage);
PolygonChromosome.setTestImage(testImage);
add(bar);
JButton startButton = new JButton("Start");
startButton.setActionCommand("start");
add(startButton);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isAlive()) {
stopRequest();
} else {
startEvolution();
}
}
});
// initialize a new genetic algorithm
ga = new GeneticAlgorithm(new UniformCrossover<Polygon>(0.5), 1.0,
new RandomPolygonMutation(MUTATION_RATE, MUTATION_CHANGE), 1.0,
new TournamentSelection(TOURNAMENT_ARITY));
// initial population
currentPopulation = getInitialPopulation();
bestFit = currentPopulation.getFittestChromosome();
}