Beispiel #1
0
def Crossover():
    global funEval
    for i in range(0, popSize):

        if (random.random() <= CR):

            # Choose two random indices
            i1, i2 = random.sample(range(0, popSize), 2)

            # Parents
            p1 = deepcopy(pop[i1])
            p2 = deepcopy(pop[i2])

            # Choose a crossover point
            pt = random.randint(1, ff.D - 2)

            # Generate new childs
            c1 = p1.chromosome[0:pt] + p2.chromosome[pt:]
            c2 = p2.chromosome[0:pt] + p1.chromosome[pt:]

            # Get the fitness of childs
            c1Fitness = ff.FitnessFunction(c1)
            funEval = funEval + 1
            c2Fitness = ff.FitnessFunction(c2)
            funEval = funEval + 1

            # Select between parent and child
            if c1Fitness < p1.fitness:
                pop[i1].fitness = c1Fitness
                pop[i1].chromosome = c1

            if c2Fitness < p2.fitness:
                pop[i2].fitness = c2Fitness
                pop[i2].chromosome = c2
Beispiel #2
0
def Mutation():
    global UB, LB, funEval
    for i in range(0, popSize):

        if (random.random() <= MR):

            # Choose random index
            r = random.randint(0, popSize - 1)

            # Choose a parent
            p = deepcopy(pop[r])

            # Choose mutation point
            pt = random.randint(0, ff.D - 1)

            # Generate new childs
            c = deepcopy(p.chromosome)

            # Mutation
            c[pt] = round(random.uniform(ff.LB, ff.UB), 2)

            #Get the fitness of childs
            cFitness = ff.FitnessFunction(c)
            funEval = funEval + 1
            # Select between parent and child
            if cFitness < p.fitness:
                pop[r].fitness = cFitness
                pop[r].chromosome = c
Beispiel #3
0
def Init():
    global funEval
    for i in range(0, popSize):
        chromosome = []
        for j in range(0, ff.D):
            chromosome.append(round(random.uniform(ff.LB, ff.UB), 2))
        fitness = ff.FitnessFunction(chromosome)
        funEval = funEval + 1
        newIndividual = Individual(chromosome, fitness)
        pop.append(newIndividual)
Beispiel #4
0
    y2 = y1 + SubGroupSize
    y3 = y2 + SubGroupSize
    y4 = y3 + SubGroupSize
    print('Begin of training SubGroup: ' + str(y1))
    print('Begin of select SubGroup: ' + str(y2))
    print('Begin of testing SubGroup: ' + str(y3))
    print('End of testing SubGroup: ' + str(y4))
    if (y4 + SubGroupSize) > len(parsed):
        y4 = len(parsed) - 1
        print('Final end of testing SubGroup: ' + str(y4))

    flogic = fuzzy.FuzzyLogic(y1, y3, parsed.loc[:, :], True, False)

    for j in range(0,GA_Iterations):
        print("Processing Group ", i+1, "out of ", NumberOfGroups, "GA iteration ", j+1, "out of ", GA_Iterations)
        FFTrain =FitnessFunction.FitnessFunction(y1,y3,parsed.loc[:,:],collection_records[j],flogic,[10000000.0,0,0,0,0,0,0],True,False,TradeWhenIntersection)
        resultTrain = FFTrain.getRreturn(parsed.loc[:,:])

        FFSelection =FitnessFunction.FitnessFunction(y2,y3,parsed.loc[:,:],collection_records[j],flogic,[10000000.0,0,0,0,0,0,0],True,False,TradeWhenIntersection)
        resultSelection = FFSelection.getRreturn(parsed.loc[:,:])

        ReturnAverage = ((((1 + resultTrain) ** 0.5) - 1)+resultSelection)*0.5
        BestReturnAverage = max(ReturnAverage)
        BestIndividualAverage = collection_records[j][ReturnAverage.idxmax(axis=0)]

        if j == 0:
            BestIndividual.append(BestIndividualAverage)
            rreturnLog.append(ReturnAverage[ReturnAverage.idxmax(axis=0)])
            BestReturn = resultSelection[ReturnAverage.idxmax(axis=0)]
        elif BestReturnAverage < BestReturn:
            BestIndividual.append(BestIndividual[j-1])