def recover_popfromfolder(N): # Linux only. entirety = [] k = 0 X = 0 for file in os.listdir(machine_dir): if file.endswith(".mac"): X += 1 Fo = open(machine_dir + '/' + file, "r+") entirety.append(machine(file)) for line in Fo.readlines(): if line == "\n": continue L = line.split() if len(L) == 3: L.append(0) entirety[k].read(L) k += 1 population = [] if N > X: N = X for W in range(N): Best = [0, 0] for M in range(len(entirety)): if entirety[M].ELO > Best[1]: Best[1] = entirety[M].ELO Best[0] = M population.append(copy.deepcopy(entirety[Best[0]])) entirety[Best[0]].ELO = 0 return population
def recover_popfromfolder(N): # Linux only. entirety = [] k = 0 X = 0 for file in os.listdir(settings.machineDIR): if file.endswith(".mac"): X += 1 Fo = open(settings.machineDIR + '/' + file, "r+") entirety.append(machine(file)) for line in Fo.readlines(): if line == "\n": continue L = line.split() if len(L) == 3: L.append(0) entirety[k].read(L) k += 1 population = [] if N > X: N = X for W in range(N): Best = [0, 0] for M in range(len(entirety)): if entirety[M].ELO > Best[1]: Best[1] = entirety[M].ELO Best[0] = M population.append(copy.deepcopy(entirety[Best[0]])) entirety[Best[0]].ELO = 0 return population
def Mate(individuals, nchild, ID=None): Children = [] for N in range(nchild): Child = machine(NewMacName(ID=ID, Tail="#")) for P in range(len(Child.PARAMETERS)): Child.PARAMETERS[P] = copy.deepcopy( random.choice(individuals).PARAMETERS[P]) Children.append(Child) return Children
def populate(population, popsize, Randomize, ID=""): NEWINDS = [] for i in range(popsize): NEWINDS.append(machine(NewMacName(ID=ID))) for I in NEWINDS: if Randomize: I.randomize() population.append(I) return population
def loadmachines(DIR=machine_dir): population = [] k = 0 machinelist = "%s/machines.list" % DIR if not os.path.isfile(machinelist): return population Fo = open(machinelist, 'r') mLIST = Fo.readlines() Fo.close() for file in mLIST: file = file.strip("\n") if file.endswith(".mac"): population.append(machine(file,DIR=DIR)) population[-1].Load() return population
def create_hybrid(population): K = random.randrange(len(population)) K_ = range(population[K].ELO - 20, population[K].ELO + 20) for I in range(len(population)): if population[I].ELO in K_: if random.randrange(100) < 60: CHILD = (machine(NewMacName())) for P in range(len(population[I].PARAMETERS)): chance = random.randrange(100) if chance < 50: CHILD.PARAMETERS[P].value = population[ I].PARAMETERS[P].value else: CHILD.PARAMETERS[P].value = population[ K].PARAMETERS[P].value print('new hybrid created. son of %i & %i' % (I, K)) return CHILD
def create_hybrid(population): K = random.randrange(len(population)) K_ = range(population[K].ELO - 20, population[K].ELO + 20) for I in range(len(population)): if population[I].ELO in K_: if random.randrange(100) < 60: CHILD = (machine(NewMacName())) for P in range(len(population[I].PARAMETERS)): chance = random.randrange(100) if chance < 50: CHILD.PARAMETERS[P].value = population[I].PARAMETERS[ P].value else: CHILD.PARAMETERS[P].value = population[K].PARAMETERS[ P].value print('new hybrid created. son of %i & %i' % (I, K)) return CHILD
def loadmachines(DIR=settings.machineDIR): population = [] k = 0 ''' machinelist = "%s/machines.list" % DIR if not os.path.isfile(machinelist): return population Fo = open(machinelist, 'r') mLIST = Fo.readlines() Fo.close()''' mLIST = listdir(DIR) for File in mLIST: File = File.strip("\n") if File.endswith(".mac"): population.append(machine(File, DIR=DIR)) population[-1].Load() return population
def clone_from_template(ID=None): TemplatePool = open('%s/top_machines/machines.list' % machine_dir, 'r') POOL = [] for line in TemplatePool.readlines(): if '.mac' in line: POOL.append(line.replace('\n', '')) X = random.randrange(len(POOL)) model = open('%s/top_machines/%s' % (machine_dir, POOL[X]), 'r') CHILD = machine(NewMacName(ID=ID, Tail="&")) for line in model.readlines(): CHILD.read(line) CHILD.onTOP = 0 # for N in range(NUMBER): CHILD.mutate(3, 3) return CHILD
def clone_from_template(ID=None): TemplatePool = os.listdir("%s/halloffame" % settings.machineDIR) POOL = [] for line in TemplatePool.readlines(): if '.mac' in line: POOL.append(line.replace('\n', '')) X = random.randrange(len(POOL)) model = open('%s/%s' % (settings.HoFmachineDIR, POOL[X]), 'r') CHILD = machine(NewMacName(ID=ID, Tail="&")) for line in model.readlines(): CHILD.read(line) CHILD.onTOP = 0 # for N in range(NUMBER): CHILD.mutate(3, 3) return CHILD