/********************************************************************** # 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 weightLimit) return 0d; } return value; } /** * Gets the Allele Set for the Problem * @return GeneticAlleleSet: The Allele Set */ public GeneticAlleleSet getAlleleSet() { GeneticAlleleSet gas = new GeneticAlleleSet(); String[] values = new String[] {"Y", "N"}; gas.addCategoricalAllele("Binary (Y/N)", values, 50); return gas; } /** * Gets the Objective Type * @return int: The Objective Type */ public int getObjectiveType() {return OBJECTIVE_TYPE_MAX;} /** * Main for Running the Problem * @param args Not Used */ public static final void main(String[] args) { GeneticEngine ge = new GeneticEngine(); GeneticRunTask grt = ge.createGeneticTask(new SingleKnapsack()); grt.getConfig().setShowGenerationResults(true); grt.getConfig().setShowFinalReport(true); grt.run(); int wt = 0; double value = 0d; for (int i=0; i