/********************************************************************** # Copyright (c) EWA Systems Inc. 1998 - 2005 All Rights Reserved # No part of this program may be photocopied, reproduced, # or translated to another programming language without # the prior written consent of EWA Systems. **********************************************************************/ package com.ewasystems.opt.ga.test; import com.ewasystems.opt.ga.GeneticAlleleSet; import com.ewasystems.opt.ga.GeneticEngine; import com.ewasystems.opt.ga.GeneticGene; import com.ewasystems.opt.ga.GeneticProblem; import com.ewasystems.opt.ga.GeneticRunTask; /** *
Genetic Algorithm Test Case: Single Knapsack
*Description: Maximize Value in Limited Weight Backpack
*Copyright: EWASystems Copyright (c) 1998 - 2005
*Company: EWASystems
* @author Lincoln Evans-Beauchamp * @version 1.0 */ public class SingleKnapsack extends GeneticProblem { /** Backpack Weight Limit */ protected int weightLimit = 625; /** Object Weights */ protected int[] objectWeights = new int[50]; /** Object Values */ protected double[] objectValues = new double[50]; /** * Problem Constructor */ public SingleKnapsack() { super("1 Knapsack 50 Objects", "Minimize f(x) = 1/4000*sum(x(i)-100)^2 - prod((x(i)-100)/sqrt(i)) + 1 where X = [-600, 600] by .02"); for (int i=0; i<50; i++) { objectWeights[i] = (int)(10d + 10d*Math.random()); objectValues[i] = (100d*Math.random()); } } /** * The Objective Function * @param gene The Genetic Gene to Test * @return double: The Genetic Gene's Fitness */ public double f(GeneticGene gene) { double wt = 0d, value = 0d; for (int i=0; i