Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -8,77 +8,77 @@ rand = new(Random)
|
|||
|
||||
// returns a random string of the specified length
|
||||
def rand_string(length)
|
||||
global tbl
|
||||
global rand
|
||||
global tbl
|
||||
global rand
|
||||
|
||||
ret = ""
|
||||
ret = ""
|
||||
|
||||
for i in range(1, length)
|
||||
ret += tbl[rand.getInt(len(tbl))]
|
||||
end
|
||||
for i in range(1, length)
|
||||
ret += tbl[rand.getInt(len(tbl))]
|
||||
end
|
||||
|
||||
return ret
|
||||
return ret
|
||||
end
|
||||
|
||||
// gets the fitness of a given string
|
||||
def fitness(string)
|
||||
global target
|
||||
fit = 0
|
||||
global target
|
||||
fit = 0
|
||||
|
||||
for i in range(0, len(string) - 1)
|
||||
if string[i] != target[i]
|
||||
fit -= 1
|
||||
end
|
||||
end
|
||||
for i in range(0, len(string) - 1)
|
||||
if string[i] != target[i]
|
||||
fit -= 1
|
||||
end
|
||||
end
|
||||
|
||||
return fit
|
||||
return fit
|
||||
end
|
||||
|
||||
// mutates the specified string with a chance of 0.09 by default
|
||||
def mutate(string)
|
||||
global chance
|
||||
global rand
|
||||
global tbl
|
||||
mutated = string
|
||||
global chance
|
||||
global rand
|
||||
global tbl
|
||||
mutated = string
|
||||
|
||||
for i in range(0, len(mutated) - 1)
|
||||
if rand.getFloat() <= chance
|
||||
mutated[i] = tbl[rand.getInt(len(tbl))]
|
||||
end
|
||||
end
|
||||
for i in range(0, len(mutated) - 1)
|
||||
if rand.getFloat() <= chance
|
||||
mutated[i] = tbl[rand.getInt(len(tbl))]
|
||||
end
|
||||
end
|
||||
|
||||
return mutated
|
||||
return mutated
|
||||
end
|
||||
|
||||
// a function to find the index of the string with the best fitness
|
||||
def most_fit(strlist)
|
||||
global target
|
||||
|
||||
best_score = -(len(target) + 1)
|
||||
best_index = 0
|
||||
|
||||
for j in range(0, len(strlist) - 1)
|
||||
fit = fitness(strlist[j])
|
||||
if fit > best_score
|
||||
best_index = j
|
||||
best_score = fit
|
||||
end
|
||||
end
|
||||
global target
|
||||
|
||||
return {best_index, best_score}
|
||||
best_score = -(len(target) + 1)
|
||||
best_index = 0
|
||||
|
||||
for j in range(0, len(strlist) - 1)
|
||||
fit = fitness(strlist[j])
|
||||
if fit > best_score
|
||||
best_index = j
|
||||
best_score = fit
|
||||
end
|
||||
end
|
||||
|
||||
return {best_index, best_score}
|
||||
end
|
||||
|
||||
parent = rand_string(len(target)); iter = 1
|
||||
while parent != target
|
||||
children = {}
|
||||
children = {}
|
||||
|
||||
for i in range(1, 30)
|
||||
children.append(mutate(parent))
|
||||
end
|
||||
for i in range(1, 30)
|
||||
children.append(mutate(parent))
|
||||
end
|
||||
|
||||
fit = most_fit(children)
|
||||
parent = children[fit[0]]
|
||||
print format("iter %d, score %d: %s\n", iter, fit[1], parent)
|
||||
fit = most_fit(children)
|
||||
parent = children[fit[0]]
|
||||
print format("iter %d, score %d: %s\n", iter, fit[1], parent)
|
||||
|
||||
iter += 1
|
||||
iter += 1
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue