from fuzzywuzzy import fuzz
import random
import string
chars = ["T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "T9", "R1", "R2", "N1",
"N2", "G1", "G2", "G3", "H1", "H2", "H3", "K1", "K2", "K3", "K4", "D1",
"D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9"]
class Agent:
def __init__(self, length):
self.string = ' '.join(random.choice(chars) for _ in xrange(length))
self.fitness = -1
def __str__(self):
return 'String: ' + str(self.string) + ' Fitness: ' + str(self.fitness)
in_str = None
in_str_len = None
population = 20
generations = 10000
def ga():
agents = init_agents(population, in_str_len)
for generation in xrange(generations):
print 'Generation: ' + str(generation)
agents = fitness(agents)
agents = selection(agents)
agents = crossover(agents)
agents = mutation(agents)
if any(agent.fitness >= 100 for agent in agents):
print 'Threshold met!'
exit(0)
def init_agents(population, length):
return [Agent(length) for _ in xrange(population)]
def fitness(agents):
for agent in agents:
agent.fitness = fuzz.ratio(agent.string, in_str)
return agents
def selection(agents):
agents = sorted(agents, key=lambda agent: agent.fitness, reverse=True)
print '\n'.join(map(str, agents))
agents = agents[:int(0.2 * len(agents))]
return agents
def crossover(agents):
offspring = []
for _ in xrange((population - len(agents))/2):
parent1 = random.choice(agents)
parent2 = random.choice(agents)
child1 = Agent(in_str_len)
child2 = Agent(in_str_len)
split = random.randint(0, in_str_len)
child1.string = parent1.string[0:split] + parent2.string[split:in_str_len]
child2.string = parent2.string[0:split] + parent1.string[split:in_str_len]
offspring.append(child1)
offspring.append(child2)
agents.extend(offspring)
return agents
def mutation(agents):
for agent in agents:
for idx, param in enumerate(agent.string):
if random.uniform(0.0, 1.0) <= 0.1:
agent.string = agent.string[0:idx] + random.choice(chars) + agent.string[idx+1:in_str_len]
return agents
if __name__ == '__main__':
in_str = 'T1T3N1N2H1H2'
in_str_len = len(in_str)
ga()
在此代碼中,我使用交叉函數和mutate函數來開發更強大的羣體。 但是,我需要這些功能來將輸入字符作爲一個整體單元處理。 例如: 雖然變異,函數替換1或T從'T1'或K或1從'K1' 我需要它將T1和K1作爲一個單元而不是字符串,並將它們替換爲整體與其他單位如T2,T3,T4等 任何建議或提示,將不勝感激。 感謝你。如何在python中將多個字符定義爲單個組?
請創建一個[mcve]。 –
如果你想要這樣的行爲,你可能不應該在'mutation()'中遍歷'agent.string':它將遍歷字符串的單個字符。 – Evert
也許更好,不要在初始化時連接各個「基因」,但將它們保存爲列表。像'self.string = [random.choice(chars)for _ in xrange(length)]''。也許可以將'string'重命名爲'chromosome',並相應地調整其餘的代碼。 – Evert