#38 examples.multidimension.Knapsack throws exception

JGAP 3.2
closed-fixed
Klaus
7
2007-03-02
2007-03-01
No

This example takes two parameters, volume and colors. The 2nd parameter uncovers a boundary problem. This happens for me in JGAP3.1, and JGAP3.2rc1.

In examples.multidimension.Knapsack.java, lines188-192:
[snip]
int colors = Integer.parseInt(args[1]);
if (colors < 1 || colors > KnapsackMain.COLORS.length) {
System.out.println("The <number of colors> argument must be between" + " 1 and " + KnapsackMain.COLORS.length);
[snip]

The example works correctly, unless colors = 1. However, if colors = 1, then the example throws the following exception:

java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at org.jgap.impl.IntegerGene.mapValueToWithinBounds(IntegerGene.java:308)
at org.jgap.impl.NumberGene.setAllele(NumberGene.java:147)
at org.jgap.impl.CrossoverOperator.doCrossover(CrossoverOperator.java:219)
at org.jgap.impl.CrossoverOperator.operate(CrossoverOperator.java:179)
at org.jgap.Genotype.applyGeneticOperator(Genotype.java:649)
at org.jgap.Genotype.applyGeneticOperators(Genotype.java:561)
at org.jgap.Genotype.evolve(Genotype.java:246)
at examples.multidimension.KnapsackMain.findItemsForVolume(KnapsackMain.java:122)
at examples.multidimension.KnapsackMain.main(KnapsackMain.java:196)

Is this a problem with the sample (should be validating <= 1)?

Or is this a problem with the underlying library? (should I be able to specify that I want only one color in my knapsack)?

Discussion

  • Klaus

    Klaus - 2007-03-02
    • priority: 5 --> 7
    • assigned_to: nobody --> klausikm
     
  • Klaus

    Klaus - 2007-03-02

    Logged In: YES
    user_id=722855
    Originator: NO

    Thanx for your detailed report!
    I cannot check the code right now, but it must be a problem with the library itself (presumably in method IntegerGene.mapValueToWithinBounds. I'll check is asap and fix the bug.

     
  • Klaus

    Klaus - 2007-03-02

    Logged In: YES
    user_id=722855
    Originator: NO

    My guess was correct. It was a bug in IntegerGene. I added some exposing test cases and fixed the bug.

     
  • Klaus

    Klaus - 2007-03-02
    • status: open --> closed-fixed
     
  • John O'Duinn

    John O'Duinn - 2007-03-10

    Logged In: YES
    user_id=789790
    Originator: YES

    hi;

    Just a quick note to confirm that your fix did the trick.

    I grabbed the latest (v1.43) /jgap/src/org/jgap/impl/IntegerGene.java, put it into a fresh JGAPv3.2rc1 install and did a full rebuild. I then tried running examples.multidimension.Knapsack with color=1, and it works nicely.

    Thanks for the great speedy work!

    take care
    John.

     
  • Klaus

    Klaus - 2007-03-10

    Logged In: YES
    user_id=722855
    Originator: NO

    John,
    thanx a lot for your great and helpful feedback :-)

    Best

    Klaus

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks