2D binary string pyevolve

116 views Asked by At

I am new to pyevolve and GA in Python.
I am trying to make a 2D binary array representing matches. Something like this:

  A B C
1 1 0 0
2 0 1 0
3 1 0 0
4 0 0 1

My goal is to have only one "1" in each row and the "1" in the array should be equal to the number of rows. One number can be matched with only one letter but a letter can be matched with multiple numbers. I wrote this code in Evaluation function

def eval_func(chromosome):
    score = 0.0
    num_of_rows = chromosome.getHeight()
    num_of_cols = chromosome.getWidth()
    # create 2 lists. One with the sums of each row and one
    # with the sums of each column
    row_sums = [sum(chromosome[i]) for i in xrange(num_of_rows)]
    col_sums = [sum(x) for x in zip(*chromosome)]
    # if the sum of "1"s in a row is > 1 then a number (1,2,3,4) is matched with
    # more than one letter. We want to avoid that.
    for row_sum in row_sums:
        if row_sum <= 1:
            score += 0.5
        else:
            score -= 1.0
    # col_sum is actually the number of "1"s in the array 
    col_sum = sum(col_sums)
    # if all the numbers are matched we increase the score
    if col_sum == num_of_rows:
        score += 0.5
    if score < 0:
        score = 0.0

    return score

Seems to work but when I add some other checks, eg if 1 is in A, 2 can not be in C, it fails. how can this become possible? (many checks)

Thanks in advance.

0

There are 0 answers