Skip to main content
added 176 characters in body
Source Link
John Kemeny
  • 17.1k
  • 3
  • 43
  • 67

You can choose a random threshold $t \leq n$ such that the first $t$ of the first individual is taken and then you take the rest from the other individual, so in your case, if $t = 4$, your new individual is $(3,1,3,2,22,5,5)$. As Richerby clarifies below, you take the first $t$ elements from the first individual and put the remaining $n-t$ elements in the order they appear in the second individual.

Making sure that you take the correct number is done by simply counting using a hashmap. Below, correct_count is a map from an element in an individual to the number of times it occurs, ind1 and ind2 are the two individuals you're trying to crossover.

correct_count = {}
for x in ind1:
    correct_count{x} += 1

t = random(n)
new_ind = ind1[0:t]
count = {}
for a in new_ind:
    count{a} += 1
for x in ind2:
    if count{x} != correct_count{x}:
        new_ind.append(x)
        count{x} += 1

You can choose a random threshold $t \leq n$ such that the first $t$ of the first individual is taken and then you take the rest from the other individual, so in your case, if $t = 4$, your new individual is $(3,1,3,2,22,5,5)$.

Making sure that you take the correct number is done by simply counting using a hashmap. Below, correct_count is a map from an element in an individual to the number of times it occurs, ind1 and ind2 are the two individuals you're trying to crossover.

correct_count = {}
for x in ind1:
    correct_count{x} += 1

t = random(n)
new_ind = ind1[0:t]
count = {}
for a in new_ind:
    count{a} += 1
for x in ind2:
    if count{x} != correct_count{x}:
        new_ind.append(x)
        count{x} += 1

You can choose a random threshold $t \leq n$ such that the first $t$ of the first individual is taken and then you take the rest from the other individual, so in your case, if $t = 4$, your new individual is $(3,1,3,2,22,5,5)$. As Richerby clarifies below, you take the first $t$ elements from the first individual and put the remaining $n-t$ elements in the order they appear in the second individual.

Making sure that you take the correct number is done by simply counting using a hashmap. Below, correct_count is a map from an element in an individual to the number of times it occurs, ind1 and ind2 are the two individuals you're trying to crossover.

correct_count = {}
for x in ind1:
    correct_count{x} += 1

t = random(n)
new_ind = ind1[0:t]
count = {}
for a in new_ind:
    count{a} += 1
for x in ind2:
    if count{x} != correct_count{x}:
        new_ind.append(x)
        count{x} += 1
Source Link
John Kemeny
  • 17.1k
  • 3
  • 43
  • 67

You can choose a random threshold $t \leq n$ such that the first $t$ of the first individual is taken and then you take the rest from the other individual, so in your case, if $t = 4$, your new individual is $(3,1,3,2,22,5,5)$.

Making sure that you take the correct number is done by simply counting using a hashmap. Below, correct_count is a map from an element in an individual to the number of times it occurs, ind1 and ind2 are the two individuals you're trying to crossover.

correct_count = {}
for x in ind1:
    correct_count{x} += 1

t = random(n)
new_ind = ind1[0:t]
count = {}
for a in new_ind:
    count{a} += 1
for x in ind2:
    if count{x} != correct_count{x}:
        new_ind.append(x)
        count{x} += 1