要处理的问题是   钉子装盒问题 
某企业生产6种钉子,其尺寸、重量、成本和售价如下表 
钉子尺寸 重量 成本 售价 
4        1   4    8 
3.5     0.85 3.5  6 
3      0.7   3    5 
2.5    0.5   2.5  4 
2     0.25   2    3 
1.5     1   1.5   2 
假设每种钉子都有无限多个,现需要将这些钉子混装为一盒销售,装盒时的限制条件如下: 
 1.每盒要装3种钉子 
 2.每盒钉子的总重不能超过20盎司 
 3.每盒钉子的个数不能超过30 
 4.若某种钉子被装盒,那么在该盒中这种钉子的数量必须在5~10之间 目标是找到一种装盒方案,使得到的利润最高。请设计一个遗传算法方案(无需求解)处理该问题。 
package ElectricCommerce; import org.jgap.Chromosome; 
import org.jgap.Configuration; 
import org.jgap.Gene; 
import org.jgap.Genotype; 
import org.jgap.IChromosome; 
import org.jgap.impl.DefaultConfiguration; 
import org.jgap.impl.IntegerGene; public class nailManagement { public static void main(String[] args) throws Exception { Configuration conf = new DefaultConfiguration(); 
conf.setFitnessFunction(new myFitnessFunction()); 
conf.setPopulationSize(4); 
conf.setPreservFittestIndividual(true); 
conf.setKeepPopulationSizeConstant(false); 
Gene[] sampleGenes = new Gene[6]; 
sampleGenes[0] = new IntegerGene(conf, 0, 10); 
sampleGenes[1] = new IntegerGene(conf, 0, 10); 
sampleGenes[2] = new IntegerGene(conf, 0, 10); 
sampleGenes[3] = new IntegerGene(conf, 0, 10); 
sampleGenes[4] = new IntegerGene(conf, 0, 10); 
sampleGenes[5] = new IntegerGene(conf, 0, 10); Chromosome sampleChromosome = new Chromosome(conf, sampleGenes); 
conf.setSampleChromosome(sampleChromosome); Genotype population = Genotype.randomInitialGenotype(conf); 
for (int i = 0; i < 1000; i++) { 
population.evolve(); 
IChromosome fittest = population.getFittestChromosome(); 
double fitness = fittest.getFitnessValue(); System.out.println("Currently fittest Chromosome has fitness " 
+ fitness); } 
IChromosome bestSolutionSoFar = population.getFittestChromosome(); 
System.out.println("The best solution has a fitness value of " 
+ bestSolutionSoFar.getFitnessValue()); 
Integer aVal = (Integer) bestSolutionSoFar.getGene(0).getAllele(); 
Integer bVal = (Integer) bestSolutionSoFar.getGene(1).getAllele(); 
Integer cVal = (Integer) bestSolutionSoFar.getGene(2).getAllele(); 
Integer dVal = (Integer) bestSolutionSoFar.getGene(3).getAllele(); 
Integer eVal = (Integer) bestSolutionSoFar.getGene(4).getAllele(); 
Integer fVal = (Integer) bestSolutionSoFar.getGene(5).getAllele(); System.out.println("a = " + aVal.intValue()); 
System.out.println("b = " + bVal.intValue()); 
System.out.println("c = " + cVal.intValue()); 
System.out.println("d = " + dVal.intValue()); 
System.out.println("e = " + eVal.intValue()); 
System.out.println("f = " + fVal.intValue()); } 

package ElectricCommerce; import org.jgap.*; public class myFitnessFunction extends FitnessFunction { 
private static final long serialVersionUID = 1L; public static int nail1, nail2, nail3, nail4, nail5, nail6; 
public static int nailAmountOfVariety; 
public static int nailAmount; 
public static double totalValue; 
public static double totalWeight; public double evaluate(IChromosome a_subject) { myFitnessFunction.nailAtgene(a_subject); nailAmount = amountOfNail(); 
totalValue = getTotalValueOfNail(); 
totalWeight = getTotalWeightOfNail(); if (nailAmountOfVariety == 3) if (0 < nailAmount && nailAmount <= 30) if (0 < totalWeight && totalWeight <= 20) 
return totalValue; 
else 
return 0; else 
return 0; 
else 
return 0; } 
public static void nailAtgene(IChromosome a_potentialSolution) { nail1 = getNumberOfNailAtGene(a_potentialSolution, 0); 
if (5 < nail1 && nail1 < 10) 
nailAmountOfVariety++; nail2 = getNumberOfNailAtGene(a_potentialSolution, 1); 
if (5 < nail2 && nail1 < 10) 
nailAmountOfVariety++; nail3 = getNumberOfNailAtGene(a_potentialSolution, 2); 
if (5 < nail3 && nail1 < 10) 
nailAmountOfVariety++; nail4 = getNumberOfNailAtGene(a_potentialSolution, 3); 
if (5 < nail4 && nail1 < 10) 
nailAmountOfVariety++; nail5 = getNumberOfNailAtGene(a_potentialSolution, 4); 
if (5 < nail5 && nail1 < 10) 
nailAmountOfVariety++; nail6 = getNumberOfNailAtGene(a_potentialSolution, 5); 
if (5 < nail6 && nail1 < 10) 
nailAmountOfVariety++; } 
public static int getNumberOfNailAtGene(IChromosome a_potentialSolution, 
int a_position) { 
Integer numNail = (Integer) a_potentialSolution.getGene(a_position) 
.getAllele(); 
return numNail.intValue(); 
} public static int amountOfNail() { 
return nail1 + nail2 + nail3 + nail4 + nail5 + nail6; 
} public static double getTotalValueOfNail() { 
double totalValue = 0; 
totalValue = 4 * nail1 + 2.5 * nail2 + 2 * nail3 + 1.5 * nail4 + nail5 
+ 0.5 * nail6; 
return totalValue; 
} public static double getTotalWeightOfNail() { 
double totalWeight = 0; 
totalWeight = nail1 + 0.85 * nail2 + 0.7 * nail3 + 0.5 * nail4 + 0.25 
* nail5 + nail6; 
return totalWeight;