Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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
Exemplo n.º 12
0
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