def cal_opt(numParm, fintness, funcname, func, stock_data, stockName):
    datarange = [[1, 20], [6, 50], [20, 100]]
    datarange = datarange[:numParm]
    genome = G1DList.G1DList(numParm)
    genome.evaluator.set(func(fintness, funcname, stock_data, stockName))
    # genome.setParams(allele=Grid_Constructor(numline,data1))
    genome.setParams(allele=Grid_Constructor(1, numParm, datarange))
    genome.initializator.set(Initializators.G1DListInitializatorAllele)
    genome.mutator.set(Mutators.G1DListMutatorAllele)

    ga = GSimpleGA.GSimpleGA(genome, seed=400)
    ga.setPopulationSize(80)
    ga.setGenerations(150)
    ga.setCrossoverRate(0.8)
    ga.setMutationRate(0.2)
    ga.selector.set(Selectors.GRankSelector)
    # ga.terminationCriteria.set(GSimpleGA.FitnessStatsCriteria)
    # Change the scaling method
    pop = ga.getPopulation()
    pop.scaleMethod.set(Scaling.SigmaTruncScaling)
    ga.evolve(freq_stats=10)
    best = ga.bestIndividual()
    return [best.genomeList, best.score]
Example #2
0
def main_run():
    genome = G1DList.G1DList(42)

    genome.evaluator.set(
        lambda chromosome: evaluate(chromosome.getInternalList()))
    genome.crossover.set(Crossovers.G1DListCrossoverEdge)
    genome.initializator.set(Initializators.G1DBinaryStringInitializator)

    ga = GSimpleGA.GSimpleGA(genome)
    ga.setGenerations(500)
    ga.setMinimax(Consts.minimaxType["maximize"])
    ga.setCrossoverRate(0.6)
    ga.setMutationRate(0.01)
    ga.setPopulationSize(15)

    try:
        ga.evolve(freq_stats=1000)
    except:
        print("\n")
    best = ga.bestIndividual()

    ch = best.genomeList
    evaluate(ch)
def run_main():
    # Genome instance
    genome = G1DList.G1DList(40)

    # The gauss_mu and gauss_sigma is used to the Gaussian Mutator, but
    # if you don't specify, the mutator will use the defaults
    genome.setParams(rangemin=0, rangemax=10, gauss_mu=4, gauss_sigma=6)
    genome.mutator.set(Mutators.G1DListMutatorIntegerGaussian)

    # The evaluator function (objective function)
    genome.evaluator.set(eval_func)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    #ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(800)

    # Do the evolution, with stats dump
    # frequency of 10 generations
    ga.evolve(freq_stats=150)

    # Best individual
    print ga.bestIndividual()
Example #4
0
    def run(self):
        # Allele define valid chromosome value
        alleles = GAllele.GAlleles()

        # Define gene with 2 chromosomes
        # MA type
        alleles.add(GAllele.GAlleleList([0, 1, 2, 3, 4]))
        # MA range
        alleles.add(GAllele.GAlleleRange(1, 99))

        # Genome instance, 1D List
        genome = G1DList.G1DList(len(alleles))
        # Sets the range max and min of the 1D List
        genome.setParams(allele=alleles)
        # The evaluator function (evaluation function)
        genome.evaluator.set(self.fitness)
        # This mutator and initializator will take care of
        # initializing valid individuals based on the allele set
        # that we have defined before
        genome.mutator.set(Mutators.G1DListMutatorAllele)
        genome.initializator.set(Initializators.G1DListInitializatorAllele)

        # Genetic Algorithm Instance
        ga = GSimpleGA.GSimpleGA(genome)
        # Set the Roulette Wheel selector method, the number of generations and
        # the termination criteria
        ga.selector.set(Selectors.GRouletteWheel)
        ga.setGenerations(self.gen)
        ga.setPopulationSize(self.pop)
        ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)

        pop = ga.getPopulation()
        pop.scaleMethod.set(Scaling.SigmaTruncScaling)

        ga.evolve(freq_stats=10)
        # Best individual
        self.best = ga.bestIndividual()
Example #5
0
def main(f_phi, f_psi):
    phi = numpy.loadtxt(f_phi)
    psi = numpy.loadtxt(f_psi)

    l_phi = (phi <= -30.) * (phi > -100.) * 1
    l_psi = (psi <= -7.) * (psi >= -67.) * 1

    h_matrix = l_phi * l_psi

    logical_mat = tovw(h_matrix)

    number_conf = len(logical_mat)
    number_helical_w = ((logical_mat > 0) * 1).sum()
    number_helical_v = ((logical_mat < 0) * 1).sum()
    n_res = len(logical_mat[0])
    partition_f = make_partition(len(logical_mat[0]), v=1, w=1)

    print logical_mat
    print number_conf, number_helical_w, number_helical_v, partition_f

    eval_func = scoring_function_factory(number_conf, number_helical_w,
                                         number_helical_v, n_res)

    genome = G1DList.G1DList(2)
    genome.evaluator.set(eval_func)
    genome.setParams(rangemin=0.000001, rangemax=1.0, gauss_sigma=0.01)
    genome.initializator.set(Initializators.G1DListInitializatorReal)
    genome.mutator.set(Mutators.G1DListMutatorRealGaussian)
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setElitism(True)
    #ga.selector.set(Selectors.GRouletteWheel)
    ga.selector.set(Selectors.GRankSelector)
    ga.nGenerations = n_steps
    ga.setMutationRate(0.20)
    ga.evolve(freq_stats=10)
    print ga.bestIndividual()
def run_main():
    # Genome instance
    genome = G1DList(20)
    genome.setParams(rangemin=-6.0, rangemax=6.0)

    # Change the initializator to Real values
    genome.initializator.set(G1DListInitializatorReal)

    # Change the mutator to Gaussian Mutator
    genome.mutator.set(G1DListMutatorRealGaussian)

    # The evaluator function (objective function)
    genome.evaluator.set(eval_func)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(100)

    # Do the evolution
    ga.evolve(freq_stats=10)

    # Best individual
    print(ga.bestIndividual())
Example #7
0
def evolve():

    genome = G1DList.G1DList(TOTAL)
    genome.setParams(rangemin=0, rangemax=TOTAL - 1)
    genome.evaluator.set(eval_func)
    ga = GSimpleGA.GSimpleGA(genome)

    ga.selector.set(Selectors.GRouletteWheel)
    ga.setMutationRate(0.6)
    ga.setGenerations(100)
    ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)

    ga.evolve(freq_stats=20)
    '''
    pop = ga.getPopulation()
    bf = pop.bestFitness()
    print bf

    br = pop.bestRaw()
    print br

    for ind in pop:
        print ind.fitness

    '''

    best = ga.bestIndividual()
    print best

    pop = ga.getPopulation()
    print pop

    for c in best.genomeList:
        result.append(A[c])

    print result
def save_best_average_worst(ga_engine=GSimpleGA.GSimpleGA(G1DList.G1DList()),
                            temp_fname='.__current_best_average_worst__.csv'):
    pop, fitnesses = get_population(ga_engine)
    optimization_type = ga_engine.getMinimax()

    high_ind = pop[np.argmax(fitnesses)]
    low_ind = pop[np.argmin(fitnesses)]
    average = np.mean(pop, axis=0)

    if optimization_type == Consts.minimaxType['maximize']:
        best = np.copy(high_ind)
        worst = np.copy(low_ind)
    elif optimization_type == Consts.minimaxType['minimize']:
        best = np.copy(low_ind)
        worst = np.copy(high_ind)

    df = pd.DataFrame()
    df['best'] = best
    df['average'] = average
    df['worst'] = worst
    fname = os.path.join(ga_folder(), temp_fname)
    df.to_csv(fname, index=False)

    return False
Example #9
0
def run_main():

    print "Starting the experiment."
    print "Raw score is calculated as 100000 - predicted_score"
    print "Because the pyevolve can only maximize the fitness function"
    genome = G1DList.G1DList(15)
    genome.setParams(rangemin=0, rangemax=119)
    genome.mutator.set(Mutators.G1DListMutatorSwap)
    genome.mutator.add(Mutators.G1DListMutatorIntegerRange)
    genome.crossover.set(Crossovers.G1DListCrossoverUniform)
    genome.evaluator.set(eval_func)

    ga = GSimpleGA.GSimpleGA(genome)
    ga.setPopulationSize(900)
    ga.selector.set(Selectors.GTournamentSelector)
    ga.setGenerations(100)
    ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)

    sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True)
    ga.setDBAdapter(sqlite_adapter)
    ga.evolve(freq_stats=1)

    print "Best ga: "
    print ga.bestIndividual()
Example #10
0
def main_run():
    genome = GTree.GTreeGP()
    root = GTree.GTreeNodeGP('a', Consts.nodeType["TERMINAL"])
    genome.setRoot(root)

    genome.setParams(max_depth=2, method="ramped")
    genome.evaluator += eval_func
    genome.mutator.set(Mutators.GTreeGPMutatorSubtree)

    ga = GSimpleGA.GSimpleGA(genome)
    ga.setParams(gp_terminals=['a', 'b'], gp_function_prefix="gp")

    ga.setMinimax(Consts.minimaxType["maximize"])
    ga.setGenerations(500)
    ga.setCrossoverRate(1.0)
    ga.setMutationRate(0.08)
    ga.setPopulationSize(80)

    ga(freq_stats=1)
    print ga.bestIndividual()

    graph = pydot.Dot()
    ga.bestIndividual().writeDotGraph(graph)
    graph.write_jpeg('tree.png', prog='dot')
Example #11
0
def synthesizeByGA(positives, negatives):
    """Finds an NFA consistent with the input by means of GA
  Input: a sample, S = (positives, negatives)
  Output: an NFA or None"""
    global negPart, PTA
    negPart = negatives
    PTA = buildPTA(positives).toNFA()
    genome = G1DList.G1DList(len(PTA.States))
    genome.setParams(rangemin=0, rangemax=len(PTA.States) - 1)
    genome.evaluator.set(eval_func)
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setGenerations(500)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setCrossoverRate(0.9)
    ga.setMutationRate(0.05)
    ga.setPopulationSize(200)
    ga.evolve()
    best_indiv = ga.bestIndividual()
    if best_indiv.getRawScore() == float('infinity'):
        return None
    else:
        Pi = decodeToPi(best_indiv)
        A = inducedNFA(Pi, PTA)
        return A
Example #12
0
def run_main():
    # Genome instance, 1D List of 50 elements
    genome = G1DList.G1DList(50)

    # Sets the range max and min of the 1D List
    genome.setParams(rangemin=0, rangemax=10)

    # The evaluator function (evaluation function)
    genome.evaluator.set(eval_func)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)

    # Set the Roulette Wheel selector method, the number of generations and
    # the termination criteria
    ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(500)
    ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)

    # Sets the DB Adapter, the resetDB flag will make the Adapter recreate
    # the database and erase all data every run, you should use this flag
    # just in the first time, after the pyevolve.db was created, you can
    # omit it.
    sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True)
    ga.setDBAdapter(sqlite_adapter)


    # Do the evolution, with stats dump
    # frequency of 20 generations
    best_individual = ga.evolve(freq_stats=20)

    # Best individual
    print(best_individual)

    # Another way to find best individual
    print(ga.bestIndividual())
Example #13
0
    def run_main(num_weights,
                 ga_min=ga_min,
                 ga_max=ga_max,
                 Stat=Stats,
                 freq=freq_stat,
                 gen=gen,
                 mutp=mutp,
                 popsize=popsize,
                 cc=cc,
                 fitness=fitness,
                 initializator=initializator,
                 mutator=mutator,
                 crossover=crossover,
                 scaling=scaling,
                 selector=selector,
                 termination=termination):
        # Genome instance
        genome = G1DList.G1DList(num_weights)
        # print 'genome', genome
        genome.setParams(rangemin=ga_min,
                         rangemax=ga_max,
                         bestrawscore=0.0000,
                         rounddecimal=4)
        genome.initializator.set(initializator)
        genome.mutator.set(mutator)
        # genome.crossover.set(crossover)
        # genome.mutator.set(Mutators.G1DListMutatorSwap)

        # The evaluator function (objective function)
        if fitness == 'rmse':
            genome.evaluator.set(cal_pop_fitness_rmse)
        elif fitness == 'chi2red':
            genome.evaluator.set(cal_pop_fitness_chi2red)

        # Genetic Algorithm Instance
        ga = GSimpleGA.GSimpleGA(genome)
        # ga.setMultiProcessing(flag=False, full_copy=False)
        pop = ga.getPopulation()
        # pop.scaleMethod.set(Scaling.SigmaTruncScaling)
        pop.scaleMethod.set(scaling)

        ga.selector.set(selector)
        # ga.selector.set(Selectors.GRouletteWheel)

        ga.setMinimax(Consts.minimaxType["minimize"])
        ga.setGenerations(gen)
        ga.setMutationRate(mutp)
        ga.setPopulationSize(popsize)
        ga.terminationCriteria.set(termination)
        # ga.setCrossoverRate(0.95)
        # ga.stepCallback.set(evolve_callback)
        # ga.terminationCriteria.set(GSimpleGA.FitnessStatsCriteria)
        # ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
        # ga.setInteractiveGeneration(2)
        # Sets the DB Adapter, the resetDB flag will make the Adapter recreate
        # the database and erase all data every run, you should use this flag
        # just in the first time, after the pyevolve.db was created, you can
        # omit it.
        # sqlite_adapter = DBAdapters.DBSQLite(identify="eniigma_"+str(cc), resetDB=True)
        # ga.setDBAdapter(sqlite_adapter)

        if os.path.exists(home1 + 'pyevolve.db') == False:
            sqlite_adapter = DBAdapters.DBSQLite(identify="eniigma_" + str(cc),
                                                 resetDB=True)
            ga.setDBAdapter(sqlite_adapter)
        else:
            sqlite_adapter = DBAdapters.DBSQLite(identify="eniigma_" + str(cc),
                                                 resetDB=False)
            ga.setDBAdapter(sqlite_adapter)

        # Do the evolution, with stats dump
        # frequency of 10 generations
        if Stat == 'True':
            ga.evolve(freq_stats=freq)
        else:
            ga.evolve(freq_stats=0)

        # for i in xrange(len(pop)):
        #	print(pop[i].fitness)
        # Best individual
        best = ga.bestIndividual()
        # print 'Best weights (genes) in the last combination', best
        numpy.savetxt(home1 + 'Best_values.txt', best)
        # print "Best individual score in the last combination: %.2e" % best.getRawScore()
        numpy.savetxt(home1 + 'Best_score.txt', [best.getRawScore()],
                      fmt='%1.4e')
        f = open('comb_score.txt', 'a')
        f.write('{0:f} {1:f}\n'.format(best.getRawScore(), cc))
        f.close()
        f2 = open('comb_score0.txt', 'w')
        f2.write('{0:f} {1:f}\n'.format(best.getRawScore(), cc))
        f2.close()
Example #14
0
def run_ga():

    global ga

    #___________________Genome instance
    #

    setOfAlleles = GAllele.GAlleleList()

    pars_min = velocity_min + depth_min + vpvs_min
    pars_max = velocity_max + depth_max + vpvs_max
    num_pars = len(pars_min)

    for (vmin, vmax) in zip(pars_min, pars_max):

        tmp = GAllele.GAlleleRange(vmin, vmax, real=True)
        setOfAlleles.add(tmp)

    genome = G1DList.G1DList(num_pars)
    genome.setParams(allele=setOfAlleles)

    genome.initializator.set(Initializators.G1DListInitializatorAllele)
    genome.mutator.set(Mutators.G1DListMutatorAllele)
    genome.crossover.set(Crossovers.G1DListCrossoverUniform)

    #___________________The evaluator function (objective function)
    #

    genome.evaluator.set(eval_func)

    #___________________Genetic Algorithm Instance
    #

    ga = GSimpleGA.GSimpleGA(genome, seed=int(seed))

    if num_cpu: ga.setMultiProcessing(True, True, int(num_cpu))

    if ga_selector == 'T': ga.selector.set(Selectors.GTournamentSelector)
    if ga_selector == 'R': ga.selector.set(Selectors.GRouletteWheel)
    if ga_selector == 'N': ga.selector.set(Selectors.GRankSelector)

    if ga_selector == 'M':

        ga.selector.setRandomApply(True)
        ga.selector.set(Selectors.GTournamentSelector, 0.75)
        ga.selector.add(Selectors.GRouletteWheel, 0.20)
        ga.selector.add(Selectors.GRankSelector)

    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setGenerations(int(generationSize))
    ga.setPopulationSize(int(populationSize))
    ga.setCrossoverRate(pCrossover)
    ga.setMutationRate(pMutation)
    ga.setElitism(True)
    ga.setElitismReplacement(int(num_etlsm))

    #___________________Sets the DB Adapter
    #

    sqlite_adapter = DBAdapters.DBSQLite(identify=dbase_name,
                                         resetDB=eval(resetDB))
    ga.setDBAdapter(sqlite_adapter)

    #___________________Do the evolution
    #

    ga.evolve(freq_stats=5)

    #___________________Print Best individual
    #

    best = ga.bestIndividual()
    best_rs = best.getRawScore()
    best_v = best.genomeList[:len(velocity_min)]
    best_d = best.genomeList[len(velocity_min):2 * len(velocity_min)]
    best_r = best.genomeList[2 * len(velocity_min):]

    print ''
    print '+++ Best Raw Score =', best_rs
    print '+++ FinalModel :'
    print '   +++ Velocity :', rnd(best_v, 2)
    print '   +++ Depth    :', rnd(best_d, 2)
    print '   +++ VpVs     :', rnd(best_r, 2)

    return best, best_rs, best_v, best_d, best_r
Example #15
0
def evolve(hackers, hosts, initial_matching, num_gens, stat_freq,
           population_size, mutation_rate, crossover_rate, elitism, selector):
    num_hackers = len(hackers)
    (assignments, m_gp_assigned_hackers, f_gp_assigned_hackers,
     np_assigned_hackers) = initial_matching

    # 1. Add Fake Hackers to represent unused spaces.
    for host in hosts:
        fakes_needed = host.capacity - host.fill
        for i in range(fakes_needed):
            fake_hacker = FHacker()
            assignments.append((fake_hacker, host))
            hackers.append(fake_hacker)

    # 2. Sort the assignments by their hacker id, same order as the hackers list
    assignments = sorted(assignments, key=lambda pair: pair[0].id)
    hackers = sorted(hackers, key=lambda hacker: hacker.id)
    hosts = sorted(hosts, key=lambda host: host.id)

    def eval_func(matching, dev=0):
        score = 0.0
        host_to_hack = {}
        team_to_hosts = {}
        for e in range(len(matching)):
            # dictionary of hosts to lists of hackers who they are hosting
            if hosts[matching[e]] not in host_to_hack:
                host_to_hack[hosts[matching[e]]] = []

            # TODO(Ben): Add fakeness
            if not hackers[e].is_fake:
                host_to_hack[hosts[matching[e]]].append(hackers[e])

                # dictionary of teams to who is hosting its members
            if hackers[e].team not in team_to_hosts:
                team_to_hosts[hackers[e].team] = []
            team_to_hosts[hackers[e].team].append(hosts[matching[e]])

        total_cap_var = calc_fullness_var(host_to_hack)
        score_cap_var = -25 * (tanh(0.05 * total_cap_var) - 1)

        total_team_split = calc_team_division(team_to_hosts, dev=dev)
        score_team_split = -25 * (tanh(0.05 * total_team_split - 2) - 1)

        total_gender_mismatches = calc_gender_mismatch(host_to_hack)
        score_gender_mismatches = -13 * \
            (tanh(0.1 * (total_gender_mismatches - 15)) - 1)

        total_sleeptime_diff = calc_sleeptime_diff(host_to_hack)
        score_sleeptime = -25 * (tanh(0.0025 * total_sleeptime_diff) - 1)

        score = score_cap_var + score_team_split + \
            score_gender_mismatches + score_sleeptime

        # Diagnostic tools
        if dev > 0:
            print("Capacity variance: " + str(total_cap_var))
            print("      Team splits: " + str(total_team_split))
            print("Gender mismatches: " + str(total_gender_mismatches))
            print("   Sleeptime diff: " + str(total_sleeptime_diff))
            print("")
            print("  SCORE BREAKDOWN  ")
            print("Capacity var: " + str(score_cap_var))
            print(" Team splits: " + str(score_team_split))
            print("Gender prefs: " + str(score_gender_mismatches))
            print("   Sleep var: " + str(score_sleeptime))
            print("-----------------------------")
            # 80 is max possible score
            print("Total Score " + str(score) + "/80")

        return score

    def init_genome(genome, **args):
        # Initialise hacker_assignments to the outcome of the greedy algorithm
        genome.genomeList = [hosts.index(host) for _, host in assignments]

    def mutate_genome(genome, **args):
        """
        Mutations that respect gender preference, by mutating only within
        gender/gender preference groups.
        """
        global m_gp_assigned_hackers
        global f_gp_assigned_hackers
        global np_assigned_hackers

        def indices_of_subset(subset):
            return [i for i, x in enumerate(genome) if hackers[x] in subset]

        def mutate_subset(subset):
            mutations = 0
            for idx in xrange(len(subset)):
                if Util.randomFlipCoin(args["pmut"]):
                    Util.listSwapElement(
                        genome, subset[idx],
                        subset[rand_randint(0,
                                            len(subset) - 1)])
                    mutations += 1

            return mutations

        if args["pmut"] <= 0.0:
            return 0
        listSize = len(genome)
        mutations = args["pmut"] * listSize

        # 1. Find subsets to do mutations in.
        m_gp = indices_of_subset(m_gp_assigned_hackers)
        f_gp = indices_of_subset(f_gp_assigned_hackers)
        np_ = indices_of_subset(np_assigned_hackers)

        # 2. Run random mutations on each subset.
        if mutations < 1.0:
            mutations = mutate_subset(m_gp) + mutate_subset(
                f_gp) + mutate_subset(np_)
        else:
            for _ in xrange(int(round(mutations))):
                a = [m_gp, f_gp, np_]
                len_a = np.array(map(len, a))
                subset = choice(a, p=(len_a / norm(len_a, ord=1)))
                Util.listSwapElement(genome, randint(0,
                                                     len(subset) - 1),
                                     randint(0,
                                             len(subset) - 1))

        return int(mutations)

    # Hacker assignment is the mapping between hacker index and host index
    hacker_assignments = G1DList.G1DList(num_hackers)
    hacker_assignments.evaluator.set(eval_func)
    hacker_assignments.mutator.set(mutate_genome)
    hacker_assignments.crossover.set(Crossovers.G1DListCrossoverCutCrossfill)
    hacker_assignments.initializator.set(init_genome)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(hacker_assignments)
    ga.setDBAdapter(DBFileCSV(identify="stage_2", frequency=10, reset=True))
    ga.selector.set(selector)
    ga.setGenerations(num_gens)
    ga.setCrossoverRate(crossover_rate)
    ga.setPopulationSize(population_size)
    ga.setMutationRate(mutation_rate)
    ga.setElitismReplacement(elitism)

    # Do the evolution, with stats dump
    ga.evolve(freq_stats=stat_freq)

    best = ga.bestIndividual()
    best_assignments = [(hackers[i], hosts[best[i]]) for i in range(len(best))]

    eval_func(best, dev=1)
    ga.printStats()

    return best, best_assignments
def ga_layout(dim, objfunc, initfunc, statsfile, coordx, coordy, coordz,
              **kwargs):
    """ Run Genetic Algorithm

    Runs the Genetic Algorithm using the Pyevolve module, an implementation of
    the Simple GA is used to minimize the objective function.

    :param dim, dimension of the problem (number of nodes of the network)
    :param objfunc, objective function object
    :param initfunc, initialize function object
    :param statsfile, the file name of a CSV tect file to save the results
    :param coordx, list with x-axis coordinates
    :param coordy, list with y-axis coordinates
    :param run, the number of the current execution
    :param gen, generations of the GA
    :param pop, population size of the GA
    :param cross, crossover rate of the GA
    :param mut, mutation rate of the GA
    :return best, a list of the diameters with the maximum fitness
    """
    # Get the arguments
    run = kwargs.get('run', 0)
    gen = kwargs.get('gen', 100)
    pop = kwargs.get('pop', 80)
    xover = kwargs.get('xover', 0.9)
    mut = kwargs.get('mut', 0.02)

    # Genome instance
    genome = G1DList.G1DList(dim)
    genome.initializator.set(lambda x, **args: initfunc(x, dim))

    # The evaluator function (objective function)
    genome.evaluator.set(lambda x, **args: objfunc(x, coordx, coordy, coordz))

    genome.mutator.set(Mutators.G1DListMutatorSwap)
    genome.crossover.set(Crossovers.G1DListCrossoverTwoPoint)

    # Genetic Algorithm instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setMinimax(Consts.minimaxType["minimize"])

    # Use roulette wheel with linear scaling
    p = ga.getPopulation()
    p.scaleMethod.set(Scaling.LinearScaling)

    ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(gen)
    ga.setPopulationSize(pop)
    ga.setCrossoverRate(xover)
    ga.setMutationRate(mut)

    # Save stats to CSV file for later analysis
    csv_adapter = DBAdapters.DBFileCSV(identify="run" + str(run),
                                       filename=statsfile,
                                       reset=False)
    ga.setDBAdapter(csv_adapter)

    # Do the evolution, with stats dump
    # frequency of certain generations
    ga.evolve(freq_stats=100)
    stats = ga.getStatistics()
    print(stats)

    best = ga.bestIndividual()
    return best
Example #17
0
def run_sim(astr, max_del=5):
    """
    given an example deletion/retention string, where
    "_" is a deletion, run a ga simulation to determine
    the deletion lengths likely to have created that pattern
    of deletion-lengths
    """
    # hack to let pygene do it's minimization.
    BIGNUMBER = 1e8

    num_deletions = astr.count('_')
    region_length = len(astr)

    real_runs = count_deletion_runs(astr)
    max_real_run_len = real_runs[-1][0]

    def evaluator(chromosome):
        deletion_lengths = list(chromosome)

        # since gen_deletions is random, do multiple tries to
        # make sure an outlier doesnt screw it up.
        ntries = 10
        observed_counts = dict(real_runs)
        observed_counts = np.array(
            [observed_counts.get(i, 0) for i in range(1, max_real_run_len)],
            'f')
        ll = 0

        for tries in range(ntries):
            sim_str, sim_runs = gen_deletions(
                region_length,
                deletion_lengths=deletion_lengths,
                num_deletions=num_deletions,
                count_retentions=False)
            sim_runs = dict(sim_runs)
            # fill in zeros. add 1 to avoid probs with log.
            sim_runs = np.array(
                [sim_runs.get(i, 0)
                 for i in range(1, max_real_run_len)], 'f') + 1
            sim_freqs = sim_runs / sim_runs.sum()

            ll += np.sum(observed_counts * np.log(sim_freqs))
        return BIGNUMBER + ll

    genome = G1DList.G1DList(len(astr))
    # deletion lenghts vary between 1 and 5 (max_del)
    genome.setParams(rangemin=1, rangemax=max_del, roundDecimal=1)
    genome.initializator.set(initializator)
    genome.mutator.set(mutator)
    genome.evaluator.set(evaluator)

    ga = GSimpleGA.GSimpleGA(genome)
    ga.setMinimax(minimaxType['maximize'])
    ga.setGenerations(GA_GENERATIONS)
    ga.evolve(freq_stats=0)
    best = ga.bestIndividual()
    return {
        'deletion_lengths': sorted(list(best)),
        'fitness': best.fitness,
        'score': best.score
    }
Example #18
0
def evolve(hackers, hosts, num_gens, stat_freq, population_size, mutation_rate,
           crossover_rate, elitism, selector):
    def init_genome(genome, **args):
        # Initialise hacker_assignments to the outcome of the greedy algorithm
        genome.genomeList = range(0, len(hackers))

    def eval_func(genome, dev=0):
        # 1. Generate the matching.
        matching = greedy_match_for_genome(genome, hackers, hosts)[0]

        # 2. Preprocess for scoring.
        host_to_hack = {}
        team_to_hosts = {}
        for hacker, host in matching:
            # dictionary of hosts to lists of hackers who they are hosting
            if host not in host_to_hack:
                host_to_hack[host] = []
            host_to_hack[host].append(hacker)

            # dictionary of teams to who is hosting its members
            if hacker.team not in team_to_hosts:
                team_to_hosts[hacker.team] = []
            team_to_hosts[hacker.team].append(host)

        # 3. Do the scoring.
        total_cap_var = calc_fullness_var(host_to_hack)
        score_cap_var = -25 * (tanh(0.05 * total_cap_var) - 1)

        total_team_split = calc_team_division(team_to_hosts, dev=dev)
        score_team_split = -25 * (tanh(0.05 * total_team_split - 2) - 1)

        total_gender_mismatches = calc_gender_mismatch(host_to_hack)
        score_gender_mismatches = -13 * \
            (tanh(0.1 * (total_gender_mismatches - 15)) - 1)

        total_sleeptime_diff = calc_sleeptime_diff(host_to_hack)
        score_sleeptime = -25 * (tanh(0.0025 * total_sleeptime_diff) - 1)

        score = score_cap_var + score_team_split + \
            score_gender_mismatches + score_sleeptime

        # Diagnostic tools
        if dev > 0:
            print("Capacity variance: " + str(total_cap_var))
            print("      Team splits: " + str(total_team_split))
            print("Gender mismatches: " + str(total_gender_mismatches))
            print("   Sleeptime diff: " + str(total_sleeptime_diff))
            print("")
            print("  SCORE BREAKDOWN  ")
            print("Capacity var: " + str(score_cap_var))
            print(" Team splits: " + str(score_team_split))
            print("Gender prefs: " + str(score_gender_mismatches))
            print("   Sleep var: " + str(score_sleeptime))
            print("-----------------------------")
            # 80 is max possible score
            print("Total Score " + str(score) + "/80")

        return score

    # Hacker assignment is the mapping between hacker index and host index
    hacker_order = G1DList.G1DList(len(hackers))
    hacker_order.evaluator.set(eval_func)
    hacker_order.crossover.set(Crossovers.G1DListCrossoverCutCrossfill)
    hacker_order.mutator.set(Mutators.G1DListMutatorSwap)
    hacker_order.initializator.set(init_genome)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(hacker_order)
    ga.setDBAdapter(DBFileCSV(identify="stage_1", frequency=10, reset=True))
    ga.selector.set(selector)
    ga.setGenerations(num_gens)
    ga.setCrossoverRate(crossover_rate)
    ga.setPopulationSize(population_size)
    ga.setMutationRate(mutation_rate)
    ga.setElitismReplacement(elitism)

    # Do the evolution, with stats dump
    ga.evolve(freq_stats=stat_freq)

    # Print outcome.
    best = ga.bestIndividual()
    eval_func(best, dev=1)
    ga.printStats()

    return best
    # iterate over the chromosome
    for value in chromosome:
        if value == 0:
            score += 0.5
    return score


# Genome instance
genome = G1DList.G1DList(100)
genome.setParams(rangemin=0, rangemax=10)

# The evaluator function (objective function)
genome.evaluator.set(eval_func)

# Genetic Algorithm Instance
ga = GSimpleGA.GSimpleGA(genome, 666)
ga.setGenerations(80)
ga.setMutationRate(0.2)

# Create DB Adapter and set as adapter
#sqlite_adapter = DBAdapters.DBSQLite(identify="ex6", resetDB=True)  # noqa
#ga.setDBAdapter(sqlite_adapter)  # noqa

# Using CSV Adapter
#csvfile_adapter = DBAdapters.DBFileCSV()  # noqa
#ga.setDBAdapter(csvfile_adapter)  # noqa

# Using the URL Post Adapter
# urlpost_adapter = DBAdapters.DBURLPost(url="http://whatismyip.oceanus.ro/server_variables.php", post=False)
# ga.setDBAdapter(urlpost_adapter)
Example #20
0
def Genetic():
    genome = G1DList.G1DList(20)
    genome.evaluator.set(eval_func)
    ga = GSimpleGA.GSimpleGA(genome)
    ga.evolve(freq_stats=10)
    print ga.bestIndividual()
Example #21
0
    def run(self):
        """ run the Genetic Training"""
        genome = G2DList.G2DList(*self.shapeParamList)
        genome.setParams(rangemin=0,
                         rangemax=1,
                         gauss_mu=0,
                         gauss_sigma=self.mutationSigma)
        genome.evaluator.set(self.evaluateParam)

        genome.initializator.set(Initializators.G2DListInitializatorReal)
        genome.mutator.set(Mutators.G2DListMutatorRealGaussian)

        if self.crossover == "uniform":  # Uniform means not regarding the location
            genome.crossover.set(Crossovers.G2DListCrossoverUniform)
        elif self.crossover == "type":  # keep the same type of arguments together
            genome.crossover.set(Crossovers.G2DListCrossoverSingleHPoint)
        elif self.crossover == "node":  # keep parameter of the same node together
            genome.crossover.set(Crossovers.G2DListCrossoverSingleVPoint)
        else:
            raise NotImplementedError

        ga = GSimpleGA.GSimpleGA(genome)
        if self.maximization:
            ga.setMinimax(Consts.minimaxType["maximize"])
        else:
            ga.setMinimax(Consts.minimaxType["minimize"])

        if self.selector == "tournament":
            ga.selector.set(Selectors.GTournamentSelector)
        elif self.selector == "roulette":
            ga.selector.set(Selectors.GRouletteWheel)
        else:
            raise NotImplementedError

        ga.setCrossoverRate(self.crossoverRate)
        ga.setMutationRate(self.mutationRate)

        ga.setPopulationSize(self.populationSize)
        ga.setGenerations(self.noGenerations)

        sqlite_adapter = DBAdapters.DBSQLite(
            dbname=self.databaseName, identify="default")  # save statistics
        csv_adapter = DBAdapters.DBFileCSV(
            filename=self.csvName, identify="default")  # save statistics
        ga.setDBAdapter(sqlite_adapter)
        #ga.setDBAdapter(csv_adapter)

        pop = ga.getPopulation()

        if self.scaling == "sigma":
            pop.scaleMethod.set(Scaling.SigmaTruncScaling)
        elif self.scaling == "exponential":
            pop.scaleMethod.set(Scaling.ExponentialScaling)
        elif self.scaling == "rank":
            pop.scaleMethod.set(GeneticTraining.rankScaling)  # change Class
        elif self.scaling == "linear":
            pop.scaleMethod.set(GeneticTraining.linearScaling)  # change Class
        else:
            raise NotImplementedError

        ga.setElitism(True)
        ga.setElitismReplacement(2)

        t_init = time.time()
        ga.evolve()
        t_tot = time.time() - t_init

        best = ga.bestIndividual()
        self.data = self.fetchData()
        #self.data = self.fetchCSVData()
        return best.genomeList, best.getRawScore(), t_tot
Example #22
0
    def __init__(self,
                 datafile="filtered_data.txt",
                 classes_file="classe.txt",
                 output_file="filtered_data",
                 generations=100,
                 population_size=5,
                 chromossome_lenght=4039,
                 ga_selector="GRouletteWheel",
                 optimization_method="distance",
                 mutation_ratio=0.02,
                 crossover_ratio=0.8,
                 method_dist="euclidean",
                 method_hclust="average",
                 n_bootstrap=100,
                 size_weight=2,
                 groups_weight=1,
                 bootstrap_weight=2,
                 distance_ratio_weight=9,
                 variation_weight=1,
                 parallel=False,
                 biosignature_max_size=50,
                 biosignature_min_size=10,
                 biosignature_min_acceptable_size=5,
                 buffer_penalty=1,
                 display_output=1,
                 F_factor=0.2,
                 ga_repetition=1,
                 n_extremes_values=1):
        """
            BioSig is a program developed to assist Biomarker discovery from microarray or proteomic data.
            Given samples belonging to different groups, find those variables (expressed mRNAs or proteins, 
            for transcriptomic or proteomic analysis respectively) that, by a hierarchical clustering, 
            could recovery those groups. 
            The solution is optimized by a genetic algorithm procedure, implemented by pyevolve. 
            Inputs: 
                normalized and filtered data file name (argument: datafile); 
                file containing the groups of each sample (classes_file); 
                output filename (output_file);
                number of generations for the G.A. (generations); default: 100
                population size for the G.A. (population_size); default: 5
                chromossome lenght for the G.A. (chromossome_lenght); 
                selector (ga_selector)
                optimization method (optimization_method): 'distance' or 'clustering'; default: clustering; 
                mutation ratio (mutation_ratio); default: 0.8
                crossover ratio(crossover_ratio); default: 0.02
                distance method for hierachical clustering and distance calculation (method_dist);
                - for Hierarchical clustering
                    clustering method for hierachical clustering (method_hclust); default: average;
                    number of bootstrap pseudo replications for hierachical clustering (n_bootstrap); default: 100;
                weight for chromossome size ratio (size_weight), i.e., number of genes included (standardized);
                weight for bootstrap support (bootstrap_weight);
                weight for distance ratio (distance_ratio_weight);
                weight for F factor (F_factor), default: 0.2;
                - for Biosignature size               
                    biosignature_max_size, default: 50;
                    biosignature_min_size, default: 10;
                    biosignature_min_acceptable_size, default: 5;
                    buffer_penalty, default: 1;

                display_output: 1 if GA progression should be displayed

            Output:
                those mRNA/proteins that gave best solution
        """
        threading.Thread.__init__(self)

        #Define optimization parameters
        self.display_output = display_output
        self._independent_best_solution = -1000
        self._independent_best_chromossome = []
        self.optimization_method = optimization_method
        self.method_dist = method_dist
        self.method_hclust = method_hclust
        self.n_bootstrap = n_bootstrap
        self.parallel = parallel
        #Define I/O
        self.classes_file = classes_file
        self.data_file = datafile
        self.output_file = output_file
        self.output = open(self.output_file + ".txt", "w")
        self.plot_output_filename = output_file[:-4] + ".png"
        self.groups = self.parse_groups(self.classes_file)
        print "\nGroups and their Samples"
        for group, samples in self.groups.iteritems():
            print group, samples
        #Define GA parameter
        self.chromossome_lenght = chromossome_lenght
        self.population_size = population_size
        self.generations = generations
        self.ga_selector = ga_selector
        self.mutation_ratio = mutation_ratio
        self.crossover_ratio = crossover_ratio
        #Define weights
        self.size_weight = size_weight
        self.groups_weight = groups_weight
        self.bootstrap_weight = bootstrap_weight
        self.distance_ratio_weight = distance_ratio_weight
        self.variation_weight = variation_weight
        self.F_factor = F_factor
        self.n_extremes_values = n_extremes_values
        #Define Bisignature size parameter
        self.biosigbiosignature_max_size = biosignature_max_size
        self.biosigbiosignature_min_size = biosignature_min_size
        self.biosigbiosignature_min_acceptable_size = biosignature_min_acceptable_size
        self.buffer_penalty = buffer_penalty
        #The next lines record analysis's parameters
        self.output.write("I/O parameters\n")
        self.output.write("\tInput data: %s\n" % datafile)
        self.output.write("\tOutput data: %s\n" % output_file)
        self.output.write("\tGroups data: %s\n" % classes_file)
        self.output.write("Genetic Algorithms parameters\n")
        self.output.write("\tChromossome Lenght: %s\n" %
                          str(chromossome_lenght))
        self.output.write("\tPopulation size: %s\n" % str(population_size))
        self.output.write("\tGenerations: %s\n" % str(generations))
        self.output.write("\tGA selector: %s\n" % str(ga_selector))
        self.output.write("\tMutation Ratio: %s\n" % str(mutation_ratio))
        self.output.write("\tCrossOver Ratio: %s\n" % str(crossover_ratio))
        self.output.write("Weights parameters\n")
        self.output.write("\tSize weight: %s\n" % str(size_weight))
        self.output.write("\tGroups weight: %s\n" % str(groups_weight))
        self.output.write("\tBootstrap weight: %s\n" % str(bootstrap_weight))
        self.output.write("\tDistance ratio weight: %s\n" %
                          str(distance_ratio_weight))
        self.output.write("\tVariation weight: %s\n" % str(variation_weight))
        self.output.write("Biosignature size parameters\n")
        self.output.write("\tmax size: %s\n" % str(biosignature_max_size))
        self.output.write("\tmin size: %s\n" % str(biosignature_min_size))
        self.output.write("\tmin acceptable size: %s\n" %
                          str(biosignature_min_acceptable_size))
        self.output.write("\tBuffer penalty: %s\n" % str(buffer_penalty))
        self.output.write("\tObjective Function parameters:")
        self.output.write("\tF-factor: %s\n" % str(F_factor))
        self.output.write("\tNumber of extreme values included: %s\n" %
                          self.n_extremes_values)
        self.output.write("\tDistance measure: %s\n" % self.method_dist)
        # load data from microarrays using RPy
        r('library("pvclust")')
        r('data <- read.table("%s", header=TRUE, row.names=1)' %
          self.data_file)
        r('data_sd <- sd(t(data))')
        #Getting and setting samples indexes and colors
        self.matrix_labels = r('colnames(data)')
        self.groups_indexes = {}
        self.samples_colors = []
        self.get_sample_indexes_and_colors()
        #Creating group_median_matrix
        #this matrix will contain the median of each sonde for each group
        r('group_median_matrix <- matrix(nrow=nrow(data), ncol=%s)' %
          (str(len(self.groups.keys()))))
        r('dimnames(group_median_matrix)[[1]] <- labels(data)[[1]]')
        r('dimnames(group_median_matrix)[[2]] <- c(%s)' %
          str(self.groups.keys()).replace(" ", "")[1:-1])
        #Creating within_group_dispersion_matrix
        #this matrix will contain the a dispersion measure of each sonde for each group
        r('within_group_dispersion_matrix <- matrix(nrow=nrow(data), ncol=%s)'
          % (str(len(self.groups.keys()))))
        r('dimnames(within_group_dispersion_matrix)[[1]] <- labels(data)[[1]]')
        r('dimnames(within_group_dispersion_matrix)[[2]] <- c(%s)' %
          str(self.groups.keys()).replace(" ", "")[1:-1])
        #Creating between_groups_dispersion_matrix
        #this matrix will contain the a dispersion measure of each sonde among all groups
        r('between_groups_dispersion_matrix <- matrix(nrow=nrow(data), ncol=1)'
          )
        r('dimnames(between_groups_dispersion_matrix)[[1]] <- labels(data)[[1]]'
          )
        #Calculate group means and standard deviation
        print "\nGroups and their indexes"
        for group, indexes in self.groups_indexes.iteritems():
            print group, indexes
            r('group_median_matrix[,"%s"] <- apply(data[,c(%s)],1,median)' %
              (group.strip(), str(indexes).replace(" ", "")[1:-1]))
            r('within_group_dispersion_matrix[,"%s"] <- apply(data[,c(%s)],1,IQR)'
              % (group.strip(), str(indexes).replace(" ", "")[1:-1]))

        r('between_groups_dispersion_matrix <- apply(group_median_matrix,1,IQR)'
          )

        #Starting
        print "Dimensions of loaded data: ", r('dim(data)')
        print "Microarray data loaded"
        print "Setting G.A. parameters"
        self.genome = G1DList.G1DList(self.chromossome_lenght)
        if self.optimization_method == "clusters":
            self.genome.evaluator.set(self.check_clusters)
            self.output.write("Clustering: %s; Bootstrap: %s; Chromossome lenght: %s; Population size: %s; Generations: %s \n" \
                    %(method_hclust, str(n_bootstrap), str(chromossome_lenght), str(population_size), str(generations)))
            self.output.write("Groups weight: %s; Size weight: %s; Bootstrap weight: %s\n" \
                    %(str(self.groups_weight), str(self.size_weight), str(self.bootstrap_weight)))

        elif self.optimization_method == "distance":
            self.genome.evaluator.set(self.check_distance)
            self.output.write("Chromossome lenght: %s; Population size: %s; Generations: %s \n" \
                    %(str(chromossome_lenght), str(population_size), str(generations)))
            self.output.write("Distance weight: %s; Size weight: %s\n" \
                    %(str(self.distance_ratio_weight), str(self.size_weight)))

        self.genome.setParams(rangemin=0.0, rangemax=1.0)
        self.genome.crossover.set(Crossovers.G1DBinaryStringXTwoPoint)
        self.ga = GSimpleGA.GSimpleGA(self.genome, interactiveMode=False)
        self.ga.setPopulationSize(self.population_size)
        self.ga.setGenerations(self.generations)
        self.ga.selector.set(Selectors.GRouletteWheel)
        self.ga.setMutationRate(self.mutation_ratio)
        self.ga.setCrossoverRate(self.crossover_ratio)
Example #23
0
def run_main():
    # First arg -f name of the file that contains the results.
    # Second arg -s [0,1] if 1 then RouletteWheel elif 0 then Tournament
    # Third arg -e [0,1] if 1 then Elitism elif 0 then noElitism
    # Fourth arg -c the crossover rate
    # Fifth arg -m the mutation rate
    
    name = "GABIL_Results.txt"

    # Genome instance
    genome = G1DVariableBinaryString.G1DVariableBinaryString(ruleLength=60)

    # The evaluator function (objective function)
    genome.evaluator.set(fitness)
    genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.selector.set(Selectors.GTournamentSelector)
    ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
    ga.setMultiProcessing()
    if len(sys.argv) > 1:
        if len(sys.argv[1:]) % 2 == 0:
            i = 1
            while i < len(sys.argv):
                if sys.argv[i] == '-f':
                    name = sys.argv[i+1]
                elif sys.argv[i] == '-s':
                    if sys.argv[i+1] == '0':
                        ga.selector.set(Selectors.GTournamentSelector)
                    elif sys.argv[i+1] == '1':
                        ga.selector.set(Selectors.GRouletteWheel)
                    else:
                        print "ERROR: Unknown argument!"
                        sys.exit(1)
                elif sys.argv[i] == '-e':
                    if sys.argv[i+1] == '0':
                        ga.setElitism(False)
                    elif sys.argv[i+1] == '1':
                        ga.setElitism(True)
                    else:
                        print "ERROR: Unknown argument!"
                        sys.exit(1)
                elif sys.argv[i] == '-c':
                    try:
                        crossoverRate = float(sys.argv[i+1])
                        ga.setCrossoverRate(crossoverRate)
                    except ValueError:
                        print "ERROR: Unknown argument!"
                        sys.exit(1)
                elif sys.argv[i] == '-m':
                    try:
                        mutationRate = float(sys.argv[i+1])
                        ga.setMutationRate(mutationRate)
                    except ValueError:
                        print "ERROR: Unknown argument!"
                        sys.exit(1)
                i += 2
        else:
            print "ERROR: Incorrect number of arguments!"
            sys.exit(1)

    # GABIL.
    eval_func = 0
    j = 1
    for i in data_set[1:]:
        if eval_func == 0:
            ga.setGenerations(100*j)
            # Do the evolution, with stats dump
            # frequency of 10 generations
            print " ********************************************************************************"
            ga.evolve(freq_stats=10)
            print " ********************************************************************************"
            j += 1

        if matches(ga.bestIndividual(),i):
            eval_func = 1
        else:
            eval_func = 0

        examples.append(i)

    f = open(name, 'a')

    f.write(str(ga.bestIndividual()))
    f.write("Prediccion " + str(predict(ga.bestIndividual().genomeList)) + "%" + "\n")

    f.close()
Example #24
0
 def test_fails_with_negative_evaluator(self):
     self.genome.evaluator.set(lambda _: -1)
     self.ga = GSimpleGA.GSimpleGA(self.genome)
     self.assertRaises(ValueError, self.ga.evolve, {'freq_stats': 1})
def checkStop(
        ga_engine=GSimpleGA.GSimpleGA(G1DList.G1DList()), stop_fname='.stop'):
    condition_1 = GSimpleGA.ConvergenceCriteria(ga_engine)
    fname = os.path.join(ga_folder(), stop_fname)
    condition_2 = os.path.exists(fname)
    return condition_1 or condition_2
Example #26
0
 def test_gp_mode_is_set_for_tree_genome(self):
     ga = GSimpleGA.GSimpleGA(GTreeGP())
     self.assertTrue(ga.GPMode)
Example #27
0
def test():
        global day_index_test
        if day_index_test<all_row:
                
                #print day_index
                row=all_row[day_index_test:day_index_test+day*day_test]
                day_index_test=day_index_test+day*day_run-1
                m=len(row)
                print m/54.0
                #print m
                ##    for z in row:
                ##            print z
                ##       

                def Open(i):
                        return row[i][3]
                def High(i):
                        return row[i][4]
                def Low(i):
                        return row[i][5]
                def Close(i):       
                        return row[i][6]
                def Time(i):
                        start=datetime.datetime(2014,1,1,0,0,0,0)+row[i][2]
                        return start.strftime("%H:%M:%S")
                def Date(i):
                        return row[i][0].strftime("%Y-%m-%d")
                     

                def return_day_row(i):
                        a=i-60
                        if a<0:
                                a=0
                        b=i+60
                        if b>m:
                                b=m
                        if i<0:
                                i=0
                        if i>m-1:
                                i=m-1
                        n=[]
                        for j in range(a,b):
                                if cmp(Date(j),Date(i))==0:
                                        n.append(j)
                        return n
                high_d=[]
                close_d=[]
                open_d=[]
                low_d=[]
                l=0

                while(l<m):
                    
                    a=return_day_row(l)
                    #print str(len(a))+'\n'
                    b=[]
                    c=[]
                    for j in a:
                            close_d.append(Close(a[-1]))
                            open_d.append(Open(a[0]))
                            b.append(High(j))
                            c.append(Low(j))
                    high=max(b)
                    low=min(c)
                    for z in a:
                            high_d.append(high)
                            low_d.append(low)
                    l=len(high_d)
                def openD(i):

                        #global open_d
                        return open_d[i]
                def closeD(i):
                        #global close_d
                        return close_d[i]
                def lowD(i):
                        #global low_d
                        return low_d[i]
                def highD(i):
                        #global high_d
                        return high_d[i]
                def day_break(list_f):
                        stop_percent=0.005
                        profit=[]
                        totalprofit=[]
                        entry_price=[]
                        exit_price=[]
                        Lots=1
                        entry_count=-1
                        exit_count=-1
                        con=0
                        day_con=0
                        status=0
                        for i in range(0,m):
                                if i>=day*13:

                                        #print openD(0)
                                        
                                        if con==0:
                                                #print openD[0]
                                                s=0
                                                for num in range(0,10):
                                                        a=max(highD(i-day*(1+num)),closeD(i-day*(2+num)))
                                                        b=min(lowD(i-day*(1+num)),closeD(i-day*(2+num)))
                                                        s=s+(a-b)
                                                avetruerange=s/10.0
                                                truehigh=max(highD(i-day),closeD(i-day*2))
                                                truelow=min(lowD(i-day),closeD(i-day*2))
                                                truerange=truehigh-truelow
                                                f=list_f[0]*openD(i-day)+list_f[1]*highD(i-day)+list_f[2]*lowD(i-day)+list_f[3]*closeD(i-day)+list_f[4]*closeD(i-day*2)
                                                f=f+list_f[5]*truehigh+list_f[6]*truelow+list_f[7]*truerange+list_f[8]*avetruerange
                                                
                                                up=Open(i)+f
                                                down=Open(i)-f
                                                ooo=Open(i)
                                                con=1
                                        if High(i)>up and day_con==0:
                                                if status==0:
                                                        #entry_count=entry_count+1
                                                        entry_price.append(max(up,Open(i)))
                                                        entry_time=Time(i)

                                                        #start=datetime.datetime(2014,1,1,0,0,0,0)+Time(i)
                                                        #write(Date(i).strftime("%Y-%m-%d")+" "+start.strftime("%H:%M:%S")+" "+str(entry_price[-1])+"jin")
                                                        status=1
                                                        day_con=1
                                        if Low(i)<down and day_con==0:
                                                if status==0:
                                                        #entry_count=entry_count+1
                                                        entry_time=Time(i)
                                                        entry_price.append(min(down,Open(i)))
                                                        #start=datetime.datetime(2014,1,1,0,0,0,0)+Time(i)
                                                        #write(Date(i).strftime("%Y-%m-%d")+" "+start.strftime("%H:%M:%S")+" "+str(entry_price[-1])+"jin")
                                                        status=-1
                                                        day_con=1
                                        if  status==1:
                                                stop=(1-stop_percent)*entry_price[-1]
                                                if Low(i)<stop and cmp(Time(i),entry_time)!=0:
                                                        exit_price.append(min(Open(i),stop))
                                                        profit.append((exit_price[-1]-entry_price[-1])*Lots*300-23)
                                                        totalprofit.append(sum(profit))
                                                        #start=datetime.datetime(2014,1,1,0,0,0,0)+Time(i)
                                                        #write(Date(i).strftime("%Y-%m-%d")+" "+start.strftime("%H:%M:%S")+" "+str(exit_price[-1])+"ping")
                                                        status=0
                                                        day_con=1
                                        if  status==-1:
                                                stop=(1+stop_percent)*entry_price[-1]
                                                if High(i)>stop and cmp(Time(i),entry_time)!=0: 
                                                        #exit_count=exit_count+1
                                                        exit_price.append(max(Open(i),stop))
                                                        profit.append((entry_price[-1]-exit_price[-1])*Lots*300-23)
                                                        totalprofit.append(sum(profit))
                                                        #start=datetime.datetime(2014,1,1,0,0,0,0)+Time(i)
                                                        #write(Date(i).strftime("%Y-%m-%d")+" "+start.strftime("%H:%M:%S")+" "+str(exit_price[-1])+"ping")
                                                        status=0
                                                        day_con=1
                                        if cmp(Time(i),"15:00:00")==0:
                                                
                                                con=0
                                                day_con=0
                                                if status==1:
                                                        exit_price.append(Close(i))
                                                        profit.append((exit_price[-1]-entry_price[-1])*Lots*300-23)
                                                        totalprofit.append(sum(profit))
                                                        #start=datetime.datetime(2014,1,1,0,0,0,0)+Time(i)
                                                        #write(Date(i).strftime("%Y-%m-%d")+" "+start.strftime("%H:%M:%S")+" "+str(exit_price[-1])+"ping_rimo")
                                                        status=0
                                                
                                                if  status==-1:
                                                        #exit_count=exit_count+1
                                                        exit_price.append(Close(i))
                                                        profit.append((entry_price[-1]-exit_price[-1])*Lots*300-23)
                                                        totalprofit.append(sum(profit))
                                                        #start=datetime.datetime(2014,1,1,0,0,0,0)+Time(i)
                                                        #write(Date(i).strftime("%Y-%m-%d")+" "+start.strftime("%H:%M:%S")+" "+str(exit_price[-1])+"ping_rimo")
                                                        status=0 
                        if len(totalprofit)!=0:
                                return totalprofit[-1]+100000000
                        else:
                                return 100000000
                
                genome = G1DList.G1DList(9)
                genome.setParams(rangemin=-2.0, rangemax=2.0)
                genome.initializator.set(Initializators.G1DListInitializatorReal)
                genome.mutator.set(Mutators.G1DListMutatorRealGaussian)

                #genome.crossover.clear()
                # The evaluator function (objective function)
                genome.evaluator.set(day_break)
                #genome.evaluator.add(func_1)

                # Genetic Algorithm Instance
                ga = GSimpleGA.GSimpleGA(genome)
                ga.selector.set(Selectors.GRouletteWheel)
                ga.setMutationRate(0.98)
                ga.setPopulationSize(50)
                #ga.setCrossoverRate(0.8)
                ga.setGenerations(100)
                #ga.stepCallback.set(evolve_callback)

                # Do the evolution
                ga.evolve(10)
                best=ga.bestIndividual()
                
                return best.genomeList
Example #28
0
 def setUp(self):
     self.genome = G1DList.G1DList(2)
     self.genome.evaluator.set(lambda _: 0)
     self.ga = GSimpleGA.GSimpleGA(self.genome)
genome.initializator.set(init_pop)

# Set mutator function
genome.mutator.set(Mutators.G1DListMutatorSwap)

# Set Crossover function
genome.crossover.set(Crossovers.G1DListCrossoverCutCrossfill)

genome.evaluator.set(fitness_func)

crossover_rate = []
fitness_value = []
best_value = [1, 1]
for x in numpy.arange(150, 500, 10):
    ga = GSimpleGA.GSimpleGA(genome)
    # ga.setPopulationSize(20)
    ga.setPopulationSize(50)
    ga.selector.set(Selectors.GTournamentSelector)
    ga.setMutationRate(0.02)
    ga.setCrossoverRate(0.9)
    # Set type of objective/ fitness function: Convergence
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setGenerations(x)
    ga.evolve()
    best = ga.bestIndividual()
    # print best.score
    if check_precedence_criteria(list(best)) == True:
        print "X: ", x, best.fitness
        if best.fitness < best_value[1]:
            best_value = [x, best.fitness]
def st_ga1(objfunc, par_b, par_n, scale, offset):

    import pylab as pl
    try:
        import pyevolve as pe
        from pyevolve import GSimpleGA, G1DList, Initializators, Crossovers, Selectors, Mutators, Scaling, Consts
    except ImportError:
        pe = None
    assert pe is not None, 'Library for pyevolve is not installed'

    lb = [v[0] for v in par_b]  # Normalised lower bound
    ub = [v[1] for v in par_b]  # Normalised upper bound

    ind_size = len(par_n)  # Number of design parameters
    pop_size = 100  # Set number of individuals in population
    ngen = 2  # Total number of generation to run
    cxpb = 0.98  # Crossover probability
    mutpb = 0.01  # Mutation probability
    freq_stats = 10  # Frequency of stats
    paral_eval = False  # To enable parallel evaluation. Only use it when the fitness function is slow!

    pl.ion()

    genome = G1DList.G1DList(ind_size)  # Genome instance
    genome.setParams(
        rangemin=lb[0],
        rangemax=ub[0])  # Set the range min and max of the 1D List
    genome.evaluator.set(
        objfunc)  # The evaluator function (fitness/objective function)
    genome.initializator.set(Initializators.G1DListInitializatorReal
                             )  # Real initialization function of G1DList
    genome.crossover.set(
        Crossovers.G1DListCrossoverUniform)  # The G1DList Uniform Crossover
    genome.mutator.set(Mutators.G1DListMutatorRealRange
                       )  # Simple real range mutator for G1DList

    ga = GSimpleGA.GSimpleGA(genome)  # Genetic algorithm instance
    ga.selector.set(Selectors.GTournamentSelector)  # Set the selector method
    ga.setMinimax(Consts.minimaxType["minimize"])
    #if framework == 'soo_min':
    #	ga.setMinimax(Consts.minimaxType["minimize"])
    #else:
    #	ga.setMinimax(Consts.minimaxType["maximize"])
    ga.setPopulationSize(
        pop_size)  # Set the population size for each generation
    ga.setGenerations(ngen)  # Set the number of generation
    ga.setCrossoverRate(cxpb)  # Set the crossover rate
    ga.setMutationRate(mutpb)  # Set the mutation rate
    ga.terminationCriteria.set(
        GSimpleGA.ConvergenceCriteria
    )  # Terminate the evolution when the population have converged
    pop = ga.getPopulation()  # Return the internal population of GA Engine
    pop.scaleMethod.set(
        Scaling.SigmaTruncScaling
    )  # Sigma Truncation scaling scheme, allows negative scores
    ga.evolve(
        freq_stats=freq_stats
    )  # Run the optimisation and print the stats of the ga every n generation
    ga.setMultiProcessing(
        flag=paral_eval, full_copy=False
    )  # Set the flag to enable/disable the use of python multiprocessing module

    res = ga.bestIndividual()  # Best individual in normalised form
    cand = [scale[i] * v + offset[i] for i, v in enumerate(res.genomeList)
            ]  # Denormalised best individual

    return res.score, cand