HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Genetic algorithm to find the maximum binary number of a given length

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
themaximumnumberlengthgeneticalgorithmbinaryfindgiven

Problem

I have written some codes in java implementing simplest genetic algorithm. The code finds (or rather tries to) the maximum value possible for a user-defined number of bits. For example, for 16 bit chromosomes, the code tries to get 216-1. I have never had my code reviewed by any one good, so the thing I am looking for is quite obvious: how can I improve my code?

So here goes the code:

Gene.java

public class Gene {

    private int value;

    public Gene() {
        value = Math.random() < 0.5 ? 0 : 1;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        if (value != 0 && value != 1) {
            throw new IllegalArgumentException("value must be either 0 or 1");
        }
        this.value = value;
    }

    public void mutate() {
        value = 1 - value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}


Chromosome.java

```
import java.util.ArrayList;
import java.util.List;

public class Chromosome implements Comparable {

private ArrayList genes;
private final int chromosomeLength;

public Chromosome(int length) {
this.genes = new ArrayList<>();

this.chromosomeLength = length > 0 ? length : 16;

for (int i = 0; i getGenes() {
return genes;
}

public void setGenes(ArrayList genes) {
this.genes = genes;
}

public List getAllele(int fromIndex, int toIndex) {
return new ArrayList<>(genes.subList(fromIndex, toIndex));
}

public void setAllele(int fromIndex, List allele) {

int lastIndex = fromIndex + allele.size();
if (lastIndex > chromosomeLength) {
throw new IndexOutOfBoundsException("the allele exceeds beyond the size of the chromosome");
}
for (int i = fromIndex, j = 0; i chromosome.append(g));
return chromosome.toString();
}

@Override
public int compareTo(Object anotherChromosome) {
Chro

Solution

I can offer a few points.

  • It seems like a Gene is essentially a single bit, having only 2 values, so a Chromosome, having no more than 16 Genes, could be modeled as a short or an int, using 16 bits to represent the values of the genes.



  • Avoid using concrete collection types like ArrayList except when creating them. It makes code less maintainable since you can't change the implementation type without hunting through the code. See Chromosome.setGenes()



  • myCollection.forEach() is more efficient than myCollection.stream().forEach()



  • quite often, when I see people jump straight to forEach, I find that they are missing opportunities to leverage the power of the Stream api. One thing I do is look at what is in the forEach() and if I see the block split cleanly by an if{} statement, I see a place where Stream.filter() should be used.

Context

StackExchange Code Review Q#127386, answer score: 2

Revisions (0)

No revisions yet.