def test_assembly_factory(self): def make_g3(fval): g = G3() g.fval = fval return g @pyvotune.sparse_input @pyvotune.pfloat(range=(-3, 72)) @pyvotune.factory(make_g3) class G3: def __init__(self): pass gen = pyvotune.Generate( gene_pool=[G1, G3], max_length=2, noop_frequency=0.) genome = pyvotune.Genome("testid") genome.add_gene([2, True], G1) genome.add_gene([2.5], G2) self.assertTrue(genome.assemble()) individual = genome.assembled self.assertEqual(individual[0].ival, 2) self.assertEqual(individual[0].bval, True) self.assertEqual(individual[1].fval, 2.5)
def test_params(self): gen = pyvotune.Generate( gene_pool=[G1, G2], max_length=2, noop_frequency=0.) for i in range(10): genome = gen.generate() self.assertTrue(genome.assemble())
def test_gen(self): gen = pyvotune.Generate( gene_pool=[G1, G2], max_length=2, noop_frequency=0.) for i in range(10): genome = gen.generate() self.assertTrue(genome) self.assertEqual(len(genome), 5) self.assertEqual(genome[2][1], G1) self.assertEqual(genome[4][1], G2) gen = pyvotune.Generate( gene_pool=[G1, G2], max_length=2, noop_frequency=1.) for i in range(10): genome2 = gen.generate() self.assertFalse(genome2)
def test_assembly(self): gen = pyvotune.Generate( gene_pool=[G1, G2], max_length=2, noop_frequency=0.) genome = pyvotune.Genome("testid") genome.add_gene([2, True], G1) genome.add_gene([2.5], G2) self.assertTrue(genome.assemble()) individual = genome.assembled self.assertEqual(individual[0].ival, 2) self.assertEqual(individual[0].bval, True) self.assertEqual(individual[1].fval, 2.5)
def test_random_reset(self): @pyvotune.pint( range=(0, 5)) class P1: def __init__(self, intparam): self.intparam = intparam @pyvotune.pint( range=(5, 10)) class P2: def __init__(self, intparam): self.intparam = intparam gen = pyvotune.Generate( gene_pool=[P1, P2], max_length=2, noop_frequency=0.) g1 = pyvotune.Genome(get_id()) g1.add_gene([3], P1) g1.add_gene([7], P2) self.assertTrue(g1.assemble()) r = random.Random() prev_val = g1[0][1] some_diff = False for i in range(100): (g2,) = pyvotune.variators.random_reset_mutation( r, [g1], { 'mutation_rate': 1.0, 'pyvotune_generator': gen }) new_val = g2[0][1] if g1[0] != g2[0] or g1[1] != g2[1]: some_diff = True self.assertTrue(some_diff)
pyvotune.set_debug(True) # Dummy data n_features = 28 * 28 rng = random.Random() ################################# # Initialize PyvoTune Generator # ################################# gen = pyvotune.Generate( initial_state={ 'sparse': False }, gene_pool=pyvotune.sklearn.get_classifiers(n_features, rng) + pyvotune.sklearn.get_decomposers(n_features, rng) + pyvotune.sklearn.get_image_features(n_features, rng) + pyvotune.sklearn.get_preprocessors(n_features, rng), max_length=4, noop_frequency=0.2, rng=rng) args = { 'crossover_rate': 0.5, 'mutation_rate': 0.3, 'pyvotune_generator': gen } # Use PyvoTun variators variators = [ pyvotune.variators.random_reset_mutation,
log.error("Need path to model -v") sys.exit(0) classify_models(app_args.validate[0], app_args.classify[0]) sys.exit(1) elif app_args.validate: validate_models(app_args.validate[0]) sys.exit(1) if not app_args.worker_mode: ################################# # Initialize PyvoTune Generator # ################################# gen = pyvotune.Generate(initial_state={'sparse': False}, gene_pool=gene_pool, max_length=app_args.max_length, noop_frequency=0.2, rng=rng) #################################### # Initialize Inspyred Genetic Algo # #################################### ea = inspyred.ec.cEA(rng) ea.logger = log ea.terminator = [ #inspyred.ec.terminators.time_termination, inspyred.ec.terminators.average_fitness_termination ] ea.selector = inspyred.ec.selectors.fitness_proportionate_selection ea.archiver = pyvotune.archivers.pickle_wrap_archiver
print "Actual Solution:" "E = m *", SPEED_OF_LIGHT, "*", SPEED_OF_LIGHT print "Best Solution:", best_eq print "Actual C:", SPEED_OF_LIGHT**2 print "Our C:", best_eq(1) print "Diff:", abs(best_eq(1) - SPEED_OF_LIGHT**2) print "Diff Pct:", round( abs(best_eq(1) - SPEED_OF_LIGHT**2) / (SPEED_OF_LIGHT**2.) * 100, 2) print "Fitness", best.fitness print "MSE", sum_errs / samps if __name__ == '__main__': pyvotune.set_debug(False) gen = pyvotune.Generate(gene_pool=[mass, someconst, oper], max_length=10, noop_frequency=0.2) ea = inspyred.ec.GA(random.Random()) ea.terminator = [ inspyred.ec.terminators.time_termination, inspyred.ec.terminators.average_fitness_termination ] ea.observer = inspyred.ec.observers.stats_observer ea.variator = [ pyvotune.variators.param_reset_mutation, pyvotune.variators.scramble_mutation, pyvotune.variators.n_point_crossover ]
# Split the dataset into training, testing and then validation parts train_X, temp_X, train_y, temp_y = train_test_split(X, y, test_size=0.25) test_X, validate_X, test_y, validate_y = train_test_split(temp_X, temp_y, test_size=0.5) n_features = X.shape[1] ################################# # Initialize PyvoTune Generator # ################################# gen = pyvotune.Generate( initial_state={'sparse': False}, gene_pool=pyvotune.sklearn.get_regressors(n_features) + pyvotune.sklearn.get_decomposers(n_features) + pyvotune.sklearn.get_preprocessors(n_features), max_length=4, noop_frequency=0.2) #################################### # Initialize Inspyred Genetic Algo # #################################### ea = inspyred.ec.GA(random.Random()) ea.terminator = [ inspyred.ec.terminators.time_termination, inspyred.ec.terminators.average_fitness_termination ] ea.observer = inspyred.ec.observers.stats_observer