0

I am a newbie in python. I am trying to implement a genetic algorithm, which I have previously implemented in MatLab. I need to create several chromosomes (individuals) for my initial population. In MatLab, I used to do this with a rand function and It used to give me plenty of unique (or at least different enough) initial population. But Here in python, I have tried different methods of random but among 50 individuals I have only 3 to 4 unique chromosomes. Here this is my python code in __main__.py :

for i in range(pop_size):
        popSpace.append(Chromosom(G=mG,M=mM))
        sum_Q+=popSpace[i].Q

And my Chromosom class:

class Chromosom:   
    def __init__(self,G,M):
            self.Q = 0
            self.V = []
            self.Chr = [0]*len(self.M)
            self.M= M
            self.G= G
            self.randomChromosom() 
            self.updateQ_Qs() 

    def randomChromosom(self):
        for m in range(len(self.M)):
            if (random.random()< 0.5):
                self.Chr[m] = 1
            else:
                self.Chr[m] = 0

I have also tried to get random bits but results still were the same. For example, I have used print(str(main.mRand.getrandbits(6)) to see the results in the console and realized that there were too much-duplicated numbers. Is there a method to create more unique random numbers? in MatLab, the same code with rand function was working well (of course pretty slowly). Having such a close initial population causes poor results in the next steps (also I should mention that the randomness problem causes similar mutations too). My problem is that there are so many similar chromosomes. For example, I have several 01111001s which is strange considering their probability of occurrence.

Sajjad
  • 151
  • 2
  • 12
  • A "more unique" random number is then no more random. Can you also define the function `updateQ_Qs()`, what is `M` and what is `G` ? Also python do not work like matlab, you cannot assign `Chr[m]` if `Chr` is empty since `Chr[m]` does not exist. This code should return an error. Provide a working example and give us the expected result. – obchardon Mar 10 '20 at 17:27
  • `updateQ_Qs()` is my fitness function that returns a float value for each chromosome. Saying "more unique" I wanted to mention that for example, I have several chromosomes with `01111001` gene order which is strange (I have edited the code). – Sajjad Mar 10 '20 at 17:34
  • 1
    The probability to have at least two identical series (of length 8) among 50 series is `1-prod(1-[(0:49)/2^8])` = 99.41% so this is not really surprising – obchardon Mar 10 '20 at 17:36
  • `numpy.random.randint(0, 2, 8)` – gboffi Mar 10 '20 at 17:40
  • 1
    8 bits → 0÷255<365 - Birthday paradox in the offing. – gboffi Mar 10 '20 at 17:44
  • I have tried `NumPy` but the results didn't change. – Sajjad Mar 10 '20 at 18:05
  • Why would you say that its probability is `1-prod(1-[(0:49)/2^8])`? I thought the probability of selecting the same individual k times over 50 independent iterations should be `length(combntns([1:50],k))*1/256^k`. – Sajjad Mar 10 '20 at 18:29
  • 1
    You ikely hve other problems in this code. random.random - and other functions in random is just good enough for these - but the code you've posted here would raise `AttributeError` on trying to get to `self.M` before assiging it on `__init__`. Otherwise, the code is very litle idiomatic in Python - it is likely you are ending up with the same object holding chromosome data in more than one instance. – jsbueno Mar 10 '20 at 21:16
  • Please, try to elaborate a minimal, complete, example that exhibits the behaviour you are complaining about - some stand alone script we could just paste here and run. Otherwise, there is no way to help there. – jsbueno Mar 10 '20 at 21:18
  • @Sajjad As jsbueno said, there is no way the code you wrote could work as written. And there are a bunch of method attributes that aren't used. Please provide a minimal example with, say, 100 results. That being said, this is a really, really, really slow approach. Using a numpy array of random numbers and calculating everything at once would be much, much faster. – TheBlackCat Mar 11 '20 at 19:27

0 Answers0