I'm writing a genetic algorithm to be used to control the weights of a neural net.

To specify the bases ("bits" of the code) of the "DNA", I'm allowing the caller to pass in a List<T> of all the possible bases. When I need to mutate a base or generate a random sequence, it will be based on this passed List.

So what's the problem?

I didn't realize this until now, but the neural net may require a massive List of possible weights (possibly thousands of different bases; our DNA has 4). Obviously, passing in a List that actually contains every possible base would be very inefficient; it could potentially be huge.

My "solution" was to implement a Range class that implements List that can represent all the possible bases by only storing 3 numbers (assuming they follow a regular pattern). My attempt is on CR here: Range Code Review.

It kind of solves the problem, but has its own problems:

  • It barely implements List; half of the required methods throw an UnsupportedOperationException, since most of the operations don't make sense for a Range.
  • Since Java doesn't seem to have a usable Number interface that allows for math, I'm using the most "general" number type I could think of; a Double. Anyone using the Range will need to and from Double if they're using another type.
  • The container is subject to floating-point errors since the whole "container" relies heavily on math to get elements.

My question:

I need to be able to allow the user of the genetic algorithm to specify the bases that they want it to use. The bases could be of any type, and there could be any number of possible bases.

How can I allow them to specify all the possible bases without using a potentially inefficient like storing every possible base explicitly in a list?

  • What do the indexes into the list mean? Maybe you want a map, not a list. If memory or speed is a problem maybe you want a persistent map so you don't have to copy the entire structure every time you make a mutation. High-level suggestion: write a concrete example and make it more generic later Jul 12, 2015 at 18:48
  • @Robert A single bases collection is stored, and is never modified. My only requirement is to be to get a random base from the list. I'm not sure what your first question about lists and indices is referring to though. I'm currently using he List interface to allow bases to be supplied, but I'm looking for alternate suggestions, since in this case, I may be required to store thousands of numbers representing every possible base. Jul 12, 2015 at 19:03
  • If the base values predecessors and successors like integers (a Discrete Domain), you can use ContiguousSet.asList(). I think that answers your question. But why are you worried about storing thousands of things in a list? If the size of the list is a few megabytes and the only thing you do with the list is indexing into random positions, why are you worried about having a big list? Jul 12, 2015 at 20:17
  • @Robert I thought that's something I should be concerned about. I'm guessing now that it's not? Jul 12, 2015 at 20:23
  • 1
    "Obviously, passing in a List that actually contains every possible base would be very inefficient; it could potentially be huge." I don't follow. In Java, when you pass an argument around you're just passing around a 4 or 8 byte reference to the list, not copying the whole list itself. Jul 12, 2015 at 20:42

1 Answer 1


Make an interface for the features you need for bases, or use an already existing interface.

Just need to generate new random bases? Make an interface to do that

interface BaseGenerator<T> {
    T randomBase();

or use an already existing interface, like Supplier<T>.

Chromosome<T> mutate(Chromosome<T> old, Supplier<T> randomBaseGenerator) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.