0

I am using python-3.x, and I am trying to do mutation on a binary string that will flip one bit of the elements from 0 to 1 or 1 to 0 by random, I tried some methods but didn't work I don't know where is the problem:

x=[0, 0, 0, 0, 0]

def mutation (x, muta):
    for i in range(len(x)):
        if random.random() < muta:
            x[i] = type(x[i])(not x[i])
    return x,
print (x)

The output for example should be x=[0, 0, 0, 1, 0] or x=[1, 0, 0, 0, 0] and so on....

Also, I tried this one:

MUTATION_RATE = 0.5
CHROMO_LEN = 6
def mutate(x):
    x = ""
    for i in range(CHROMO_LEN):
        if (random.random() < MUTATION_RATE):
            if (x[i] == 1):
                x += 0
            else:
                x += 1
        else:
            x += x[i]
    return x
print(x)

please any suggestion or advice will be appreciated

Georgy
  • 12,464
  • 7
  • 65
  • 73
azeez
  • 469
  • 3
  • 6
  • 17

2 Answers2

0

Are you sure you're calling the function before printing x:

def mutation(x):
    # your code without the trailing comma

mutation(x)
print(x)

In Python, creating a new list is usually preferable to mutating an old one. I would write your first function like this (I converted the integers to booleans because you're just flipping them:

x = [False, False, False, False]


def mutation(x, muta):
    return [not e if random.random() < muta else e
            for e in x]

Change x by assigning to it again:

x = mutation(x, .5)

Your original function is working if you remove the comma after the return:

def mutation(x, muta):
    for i in range(len(x)):
        if random.random() < muta:
            x[i] = type(x[i])(not x[i])
    return x
x = [False, False, False, False]


mutation(x, .5)
Out[8]: [False, False, True, False]

mutation(x, .5)
Out[9]: [True, True, True, False]
Ben
  • 5,952
  • 4
  • 33
  • 44
  • 1
    I wouldn't say it is preferable to create a new list rather than mutating an old one... that totally depends on your use-case. – juanpa.arrivillaga Mar 01 '17 at 23:50
  • @juanpa.arrivillaga I guess it is more of an opinion, but unless performance is an issue, I really appreciate functions not messing with the values I put into them without explicit assignment when I call the function. – Ben Mar 01 '17 at 23:59
  • @Ben I am using Spyder python environment, does it make a difference? for me it returning the same result [0,0,0,0,0] – azeez Mar 02 '17 at 00:10
  • @azeez it shouldn't... Just to be sure though, close Spyder, start it up again, then paste the code in. That will restart the interpreter and clear away any possible definitions you've already made. – Ben Mar 02 '17 at 00:11
  • @Ben I did but now it gives me an error: NameError: name 'random' is not defined – azeez Mar 02 '17 at 00:43
  • @Ben sorry I was missing "import random" – azeez Mar 02 '17 at 00:48
0

You could also use python's XOR operator to flip bits, which will flip between '1' and '0':

x[1] = x[1] ^ 1

See also: Python XOR preference: bitwise operator vs. boolean operators

Community
  • 1
  • 1