Background:
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 anUnsupportedOperationException
, since most of the operations don't make sense for aRange
. - 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; aDouble
. Anyone using theRange
will need to and fromDouble
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?
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 heList
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.