Ejemplo n.º 1
0
 def test_append(self):
     _genome = G1DBinaryString.G1DBinaryString(length=3)
     _genome.append(0)
     _genome.append(1)
     self.assertEqual(_genome[0], 0)
     with self.assertRaises(ValueError):
         _genome.append(2)
Ejemplo n.º 2
0
 def test_create1DBinary_len(self):
     _genome = G1DBinaryString.G1DBinaryString(length=5)
     self.assertTrue(hasattr(_genome, 'genomeList'))
     self.assertEqual(_genome.genomeSize, 5)
     self.assertTrue(hasattr(_genome, 'initializator'))
     self.assertTrue(hasattr(_genome, 'mutator'))
     self.assertTrue(hasattr(_genome, 'crossover'))
Ejemplo n.º 3
0
 def test_create1DBinary_default(self):
     _genome = G1DBinaryString.G1DBinaryString()
     self.assertTrue(hasattr(_genome, 'genomeList'))
     self.assertTrue(hasattr(_genome, 'genomeSize'))
     self.assertIsInstance(_genome.genomeSize, int)
     self.assertTrue(hasattr(_genome, 'initializator'))
     self.assertTrue(hasattr(_genome, 'mutator'))
     self.assertTrue(hasattr(_genome, 'crossover'))
Ejemplo n.º 4
0
 def test_binarystring(self):
     genome = G1DBinaryString.G1DBinaryString(10)
     target = [0, 1, 0, 0, 1, 1, 0, 1, 1, 0]
     genome.initializator.applyFunctions(genome).next()
     for i in range(len(target)):
         genome[i] = target[i]
     raw = raw_genes(genome)
     self.assertEqual(target, raw)
Ejemplo n.º 5
0
def ga():
    genome = G1DBinaryString.G1DBinaryString(sx.shape[1])
    genome.evaluator.set(eval_func)
    genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)
    ga = GSimpleGA.GSimpleGA(genome)
    ga.selector.set(Selectors.GTournamentSelector)
    ga.setGenerations(20)
    ga.evolve(freq_stats=10)
    best = ga.bestIndividual()
    print best
Ejemplo n.º 6
0
 def test_1DBinarySetitem(self):
     _genome = G1DBinaryString.G1DBinaryString(length=5)
     _genome.append(0)
     _genome.append(1)
     _genome.append(1)
     self.assertEqual(_genome[0], 0)
     with self.assertRaises(ValueError):
         _genome[0] = 2
     with self.assertRaises(ValueError):
         _genome[0:2] = [0, 1, 2]
Ejemplo n.º 7
0
def run_main():
    # Define chromosome
    genome = G1DBinaryString.G1DBinaryString(NUM_NODES)

    genome.evaluator.set(eval_func)
    genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)
    genome.crossover.set(Crossovers.G1DBinaryStringXSinglePoint)

    # Initialize Genetic algorithm
    ga = GSimpleGA.GSimpleGA(genome)

    # Utilize all processors if more than one are available
    ga.setMultiProcessing(True)

    # Set population size
    ga.setPopulationSize(POP_SIZE)

    # Set generation at which evolution pauses and interactive mode starts
    #ga.setInteractiveGeneration(500)

    # Selection
    ga.selector.set(Selectors.GTournamentSelector)
    #ga.selector.set(Selectors.GRankSelector)
    #ga.selector.set(Selectors.GUniformSelector)
    #ga.selector.set(Selectors.GRouletteWheel)

    # Set Probabilities for Genetic Algorithm
    ga.setCrossoverRate(Pc)
    ga.setMutationRate(Pm)

    ga.setElitism(True)
    ga.setElitismReplacement(NUM_NODES / 4)

    ga.setGenerations(NUM_GENERATIONS)

    # Set up database for data storage and graphing with pyevolve_graph.py
    #sqlite_adapter = DBAdapters.DBSQLite(identify="ex10", resetDB=True)
    #adapter = DBAdapters.DBVPythonGraph(identify="run_01", frequency = 1)
    #ga.setDBAdapter(adapter)

    # Run genetic Algorithm
    ga.evolve(freq_stats=NUM_GENERATIONS / 4)

    # Show best chromosome
    best = ga.bestIndividual()
    print
    print "Best Chromosome:", '\n', best.getBinary()
    print "Fitness Raw Score:", best.getRawScore()
    print

    # Graph best chromosome
    i, nearest_supers = eval_func(best, graph=True)
    title = r'$%d \ Nodes$' % NUM_NODES
Ejemplo n.º 8
0
def run_main():
    # Genome instance
    genome = G1DBinaryString.G1DBinaryString(n)
    # The evaluator function (objective function)
    genome.evaluator.set(eval_func)
    genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)
    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.selector.set(Selectors.GTournamentSelector)
    ga.setGenerations(10)
    # Do the evolution, with stats dump
    # frequency of 10 generations
    ga.evolve(freq_stats=10)
    # Best individual
    return ga.bestIndividual()
Ejemplo n.º 9
0
def run_main(generation_number=3000):
   # Genome instance
   genome = G1DBinaryString.G1DBinaryString(n)
   # The evaluator function (objective function) 
   genome.evaluator.set(eval_func)
   genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)
   # Genetic Algorithm Instance
   ga = GSimpleGA.GSimpleGA(genome)
   ga.selector.set(Selectors.GTournamentSelector)
   ga.setGenerations(generation_number)
   ga.setCrossoverRate(Crossover_r)
   ga.setMutationRate(Mutation_r)#0.03
   ga.setPopulationSize(Population_s)
   # Do the evolution, with stats dump
   # frequency of 20 generations
   ga.evolve(freq_stats=200)
   # Best individual
   return ga.bestIndividual()
Ejemplo n.º 10
0
    def setUp(self):
        alleles = AllelesWithOperators()
        alleles.add(G2DList.G2DList(3, 3), weight=9)
        alleles.add(G1DBinaryString.G1DBinaryString(10), weight=10)
        alleles.add(G1DList.G1DList(1), weight=1)
        self.genome = G1DList.G1DList(3)
        self.genome.setParams(allele=alleles)
        allele_initializer(self.genome)

        self.target_2d = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
        self.target_bitmap = [0, 1, 0, 0, 1, 1, 0, 1, 1, 0]
        self.target_1d = [13]

        for i in range(3):
            for j in range(3):
                self.genome[0][i][j] = self.target_2d[i][j]
        for i in range(len(self.target_bitmap)):
            self.genome[1][i] = self.target_bitmap[i]
        self.genome[2][:] = self.target_1d[:]
Ejemplo n.º 11
0
def SGA(evaluator, chromlen, popsize=50, elitism=False):
    from pyevolve import G1DList, G1DBinaryString
    from pyevolve import GSimpleGA, Selectors, Consts, Scaling
    genome = G1DBinaryString.G1DBinaryString(chromlen)
    genome.evaluator.set(evaluator)
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setElitism(elitism)
    ga.setPopulationSize(popsize)
    #ga.setGenerations(160)
    #ga.setCrossoverRate(.65) #
    ga.setMutationRate(
        .02
    )  # .02 na poziomie chromosomu i na poziomie populacji (to to samo!)
    #ga.setMutationRate(0)
    ga.setSortType(Consts.sortType["raw"])  # !!!
    ga.selector.set(Selectors.GRouletteWheel)  # !!!
    ga.setMinimax(Consts.minimaxType["maximize"])
    #pop = ga.getPopulation()
    #pop.scaleMethod.set(Scaling.SigmaTruncScaling)  # not used due to raw sorting
    return ga
Ejemplo n.º 12
0
def do_selection(name_file):
    global name_file_current
    name_file_current = name_file
    global index_converter
    index_converter = [
        (ind, segment_info)
        for ind, segment_info in sorted(map_segment[name_file].iteritems())
    ]

    print len(index_converter)
    genome = G1DBinaryString.G1DBinaryString(len(index_converter))

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

    #G1DBinaryStringMutatorFlip, G1DBinaryStringMutatorSwap
    genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)

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

    #GRankSelector, GRouletteWheel, GRouletteWheel_PrepareWheel, GTournamentSelector, GUniformSelector
    ga.selector.set(Selectors.GRankSelector)

    #G1DBinaryStringXSinglePoint, G1DBinaryStringXTwoPoint, G1DBinaryStringXUniform
    # ga.crossover.set(Crossovers.G1DBinaryStringXTwoPoint)

    ga.setGenerations(100)
    # ga.setMultiProcessing(True)
    # Do the evolution, with stats dump
    # frequency of 10 generations
    ga.evolve(freq_stats=10)

    # Best individual
    best = ga.bestIndividual()
    output = [
        index_converter[ind][0] for ind, i in zip(range(len(best)), best)
        if i == 1
    ]
    print best
    return output
Ejemplo n.º 13
0
def ga_bot():
    global writable_params
    # Genome instance
    genome = G1DBinaryString.G1DBinaryString(200)

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

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.selector.set(Selectors.GTournamentSelector)
    ga.setGenerations(25)
    # Do the evolution, with stats dump
    # frequency of 10 generations
    xyz = ga.evolve(freq_stats=1)
    print xyz
    # Best individual
    best = ga.bestIndividual()
    writable_params = best
    print best
Ejemplo n.º 14
0
def run_main():
    
    
    # Genome instance
    genome = G1DBinaryString.G1DBinaryString(bitstrlen)

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

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    #ga.setElitism(True)
    ga.setMutationRate(0.02) # for use with long strings 0.01, default: 0.02
    ga.selector.set(Selectors.GTournamentSelector)
    ga.setPopulationSize(10)
    ga.setGenerations(30)
    # ga.setInteractiveGeneration(10)

    csv_adapter = DBAdapters.DBFileCSV(identify="run1", filename="stats.csv")
    ga.setDBAdapter(csv_adapter)

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

    # Best individual
    print ga.bestIndividual()
    print "target:", target.bin
    print "date:"
    # do timings of all components: bitgen, eval, 

    f = open("best-ind.txt", "w")
    f.write(str(ga.bestIndividual()))
    f.write("\n")
    f.write(str(target.bin))
    f.write("\n")
    f.close()
Ejemplo n.º 15
0
    I = sum(dists_to_super) + sum(dists_from_super)
    fitness = (w * (sum(dists_to_target) - I) +
               ((1 - w) * (NUM_NODES - num_super))) / 100.

    if fitness < 0: fitness = 0
    if graph == True:
        return fitness, nearest_super
    ##assert fitness > 0
    return fitness


#def chromosomes_init(genome):
#    genome.genomeString = [random.choice((0,1)) for i in range(NUM_NODES)]

# Genome instance
genome = G1DBinaryString.G1DBinaryString(NUM_NODES)
#print genome

# The evaluator function (objective function)
genome.evaluator.set(eval_func)
genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)
#genome.initializator.set(chromosomes_init)

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

ga.selector.set(Selectors.GRankSelector)
ga.setCrossoverRate(0.75)
ga.setMutationRate(0.02)
ga.setElitism(True)
Ejemplo n.º 16
0
from pyevolve import G1DBinaryString
from pyevolve import GSimpleGA
from pyevolve import Mutators
from pyevolve import Crossovers
from pyevolve import Selectors


def evaluate(chromosome):
    return sum(chromosome) / float(len(chromosome))


genome = G1DBinaryString.G1DBinaryString(100)
genome.evaluator.set(evaluate)
genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)
genome.crossover.set(Crossovers.G1DBinaryStringXSinglePoint)

ga = GSimpleGA.GSimpleGA(genome)
ga.selector.set(Selectors.GTournamentSelector)
ga.setGenerations(1000)
ga.setPopulationSize(100)
ga.evolve()
Ejemplo n.º 17
0
        for i in xrange(dim)
    ]


def eval_func(chrom):
    chrom = ''.join([str(c) for c in chrom[:]])
    return bfunc['fun'](getGeno(chrom))


def step_func(ga):
    print '%d %g' % (ga.getCurrentGeneration(),
                     ga.bestIndividual().getRawScore())
    sys.stdout.flush()


genome = G1DBinaryString.G1DBinaryString(width * dim)
genome.evaluator.set(eval_func)
ga = GSimpleGA.GSimpleGA(genome)
ga.setPopulationSize(popsize)
ga.selector.set(Selectors.GRouletteWheel)
ga.setGenerations(Max_FES / popsize)
ga.setMutationRate(0.05)
# ga.setElitism(True)
ga.setSortType(Consts.sortType["raw"])
ga.setMinimax(Consts.minimaxType["minimize"])

ga.stepCallback.set(step_func)

pop = ga.getPopulation()
pop.scaleMethod.set(Scaling.SigmaTruncScaling)
# print ga
Ejemplo n.º 18
0
 def test_getBinary(self):
     _genome = G1DBinaryString.G1DBinaryString()
     self.assertIsInstance(_genome.getBinary(), str)
Ejemplo n.º 19
0
 def test_getDecimal(self):
     _genome = G1DBinaryString.G1DBinaryString(length=3)
     _genome.append(0)
     _genome.append(1)
     _genome.append(1)
     self.assertEqual(_genome.getDecimal(), 3)
Ejemplo n.º 20
0

def eval_func(chromosome):

    features = []

    for i in xrange(len(chromosome)):
        if chromosome[i] == 1:
            features.append(i)

    scores = getKx2CVScores(clf, X[:, features], y, k=1)

    return np.mean(scores[0])


genome = G1DBinaryString.G1DBinaryString(X.shape[1])

genome.evaluator.set(eval_func)
genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip)

ga = GSimpleGA.GSimpleGA(genome)
ga.selector.set(Selectors.GTournamentSelector)

ga.setElitism(True)
ga.setGenerations(20)
ga.setPopulationSize(25)
ga.setMultiProcessing(True)

ga.evolve(freq_stats=1)
best = ga.bestIndividual()
Ejemplo n.º 21
0
        frac = int(''.join(map(str, temp_2)))
        frac = int(str(frac), 2)
        b = float(frac) / (10**(len(str(frac))))
        par.append(num + b)
    return float(1000 - (sum(par[1:10]) - sum(par[10:])))


def eval_func(chromosome):
    score = 0.0
    score = test(chromosome, 2, 8)

    return score


# Genome instance
genome = G1DBinaryString.G1DBinaryString(60)

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

# Genetic Algorithm Instance
ga = GSimpleGA.GSimpleGA(genome)
ga.selector.set(Selectors.GTournamentSelector)

ga.setGenerations(100)

# Do the evolution, with stats dump
# frequency of 10 generations
ga.evolve(freq_stats=10)
Ejemplo n.º 22
0
 def test_repr(self):
     _genome = G1DBinaryString.G1DBinaryString()
     self.assertIsInstance(repr(_genome), str)
def run_genetic_algorithm(ppi_data,
                          physical_distance,
                          included_cells,
                          population_size=200,
                          generations=200,
                          inc_percentage=0.05,
                          runs=None,
                          verbose=False):
    '''

    inc_percetange : float, 0.05 by default
        Min percentage of the objective function to improve in the last iteration with respect to the previous
        iteration. If this is not met, the runs are stopped.

    runs : int, None by default
        Number of runs of the GA. If runs is None, it iterates until the difference with the previous iteration
        is lower than 'imp_percentage' or 100 runs are performed.
    '''
    from pyevolve import G1DBinaryString
    from pyevolve import GSimpleGA
    from pyevolve import Selectors
    from pyevolve import Mutators

    print('Starting a new run of the genetic algorithm')

    results = dict()

    ref_distance_vector = scipy.spatial.distance.squareform(
        physical_distance.loc[included_cells, included_cells].values)

    theta_ppi_data = ppi_data.copy()

    runs_ = 1
    if runs is None:
        pass
    elif runs < 1:
        raise ValueError('Minimal number of runs is 1')

    old_obj = 0.1
    new_obj = old_obj * (1.1 + inc_percentage)
    while True:
        if runs is None:
            if (runs_ > 100) or (
                (new_obj - old_obj) / old_obj < inc_percentage):
                break
        else:
            if runs_ > runs:
                break
        # Sample only selected PPIs
        theta_ppi_data = theta_ppi_data.loc[theta_ppi_data.score == 1]

        bi_ppi_data = c2c.preprocessing.bidirectional_ppi_for_cci(
            ppi_data=theta_ppi_data, verbose=verbose)

        interaction_space = c2c.core.InteractionSpace(
            rnaseq_data=rnaseq_data[included_cells],
            ppi_data=bi_ppi_data,
            gene_cutoffs=cutoff_setup,
            communication_score=analysis_setup['communication_score'],
            cci_score=analysis_setup['cci_score'],
            cci_type=analysis_setup['cci_type'],
            verbose=verbose)

        def optimal_ppi_score(theta):
            # Inputs of this function
            theta_ppi_data['score'] = theta
            int_space = interaction_space  # InteractionSpace

            # Create bi_ppi_data to consider both directions (InteractionSpace previously created with bi_ppi_data)
            bi_ppi_data = c2c.preprocessing.bidirectional_ppi_for_cci(
                ppi_data=theta_ppi_data, verbose=verbose)
            int_space.ppi_data['score'] = bi_ppi_data['score']

            # Compute interactions
            int_space.compute_pairwise_cci_scores(use_ppi_score=True,
                                                  verbose=verbose)

            # Distance matrix from analysis -> It considers only paracrine interaction (autocrine are excluded)
            result_matrix = interaction_space.distance_matrix.loc[
                included_cells, included_cells]

            # Compute correlations
            result_distance_vector = scipy.spatial.distance.squareform(
                result_matrix)
            corr = scipy.stats.spearmanr(result_distance_vector,
                                         ref_distance_vector)
            return abs(np.nan_to_num(corr[0]))

        # Genome instance
        genome = G1DBinaryString.G1DBinaryString(len(theta_ppi_data))

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

        # Genetic Algorithm Instance
        ga = GSimpleGA.GSimpleGA(genome)
        ga.selector.set(Selectors.GTournamentSelector)
        ga.setPopulationSize(population_size)
        ga.setGenerations(generations)

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

        # Best result:
        best = ga.bestIndividual()
        theta_ppi_data['score'] = best

        # PRINT STATS:
        opt_score = optimal_ppi_score(best)
        drop_fraction = 1.0 - sum(best) / len(best)

        # Save PPI Networks
        tmp_ppi = ppi_data.assign(score=0)
        tmp_ppi.columns = ['A', 'B', 'score']
        tmp_ppi = tmp_ppi.merge(
            theta_ppi_data.loc[theta_ppi_data['score'] == 1],
            how='outer',
            on=['A', 'B'])

        tmp_ppi['score'] = tmp_ppi['score_y'].fillna(0)

        # Next iteration
        new_obj = opt_score
        if runs_ == 1:
            old_obj = new_obj / (1.1 + inc_percentage)
        else:
            old_obj = results['run{}'.format(runs_ - 1)]['obj_fn']

        # Save results
        results['run{}'.format(runs_)] = {
            'obj_fn': opt_score,
            'ppi_data': tmp_ppi['score'].values.tolist(),
            'drop_fraction': drop_fraction
        }

        runs_ += 1
    return results