def test_lamarckian(): p = Problem(data.data, data.target, lamarckian=True) ch = Chromosome(p.nin, p.nout) ch.random(c) fitness = p.get_fitness(ch) print("fitness: ", fitness) assert True
def test_tensor(): ch = Chromosome(5, 2) ch.random(c) ch.outputs[0] = 9 ch.outputs[1] = 9 out = ch.get_tensors() print(out) assert True
class Evolver: def __init__(self, problem, config, logname='test', root_dir='.'): self.config = config self.problem = problem self.max_fit = 0.0 self.best = Chromosome(self.problem.nin, self.problem.nout) self.best.random(config) self.logfile = os.path.join(root_dir, 'logs', logname + '.log') self.logname = logname self.generation = 0 def run(self, n_steps): for i in range(n_steps): self.step() def mutate(self, chromosome): child_genes = np.copy(chromosome.genes) change = np.random.rand( len(child_genes)) < self.config.cfg["mutation_rate"] child_genes[change] = np.random.rand(np.sum(change)) child = Chromosome(self.problem.nin, self.problem.nout) child.from_genes(child_genes, self.config) return child def step(self): next_best = self.best next_max_fit = self.max_fit for i in range(self.config.cfg["lambda"]): child = self.mutate(self.best) fitness = 0.0 history = [] try: fitness, history = self.problem.get_fitness(child) except Exception as e: print(e) if fitness >= next_max_fit: if fitness > next_max_fit: with open(self.logfile, 'a') as f: for i in range(len(history)): # type,logname,gen,eval,epoch,total_epochs,loss,acc,best f.write('L,%s,%d,%d,%d,%d,%0.10f,%0.10f,%0.10f\n' % (self.logname, self.generation, self.problem.eval_count, i, self.problem.epochs, history[i][0], history[i][1], fitness)) f.write('E,%s,%d,%d,%d,%d,%0.10f,%0.10f,%0.10f\n' % (self.logname, self.generation, self.problem.eval_count, 0, self.problem.epochs, 0.0, 0.0, fitness)) next_best = child next_max_fit = fitness else: del child self.best = next_best self.max_fit = next_max_fit self.generation += 1
def test_run(): ch = Chromosome(2, 1) ch.random(c) ch.outputs[0] = 2 ch.nodes[2].x = 0 ch.nodes[2].y = 1 ch.nodes[2].function = tf.square ch.nodes[2].arity = 1 outv = ch.run() print(outv) assert outv == ch.nodes[2].param
def __init__(self, problem, config, logname='test', root_dir='.'): self.config = config self.problem = problem self.max_fit = 0.0 self.population = [] for i in range(self.config.cfg["ga_population"]): ch = Chromosome(self.problem.nin, self.problem.nout) ch.random(config) self.population += [ch] self.fits = -np.inf * np.ones(self.config.cfg["ga_population"]) self.logfile = os.path.join(root_dir, 'logs', logname + '.log') self.logname = logname self.generation = 0
def test_active(): ch = Chromosome(5, 2) ch.random(c) ch.nodes[5].x = 0 ch.nodes[5].y = 1 ch.nodes[5].arity = 2 ch.nodes[6].x = 3 ch.nodes[7].x = 5 ch.nodes[7].y = 6 ch.nodes[7].arity = 1 ch.outputs[0] = 7 ch.outputs[1] = 7 ch.set_active() print(ch.get_active()) assert ch.get_active() == [0, 1, 5, 7]
def test_creation(): ch = Chromosome(5, 2) ch.random(c) assert len(ch.nodes) == c.cfg["num_nodes"] + 5 assert len(ch.outputs) == 2
def test_multiout_run(): ch = Chromosome(10, 10) ch.random(c) outv = ch.run() print(outv) assert len(outv) > 1
def test_visul(): ch = Chromosome(8, 8) ch.random(c) ch.visualize("test") assert True
from tfcgp.config import Config from tfcgp.chromosome import Chromosome from tfcgp.classifier import Classifier from tfcgp.problem import Problem import numpy as np import tensorflow as tf from sklearn import datasets c = Config() c.update("cfg/test.yaml") data = datasets.load_iris() p = Problem(data.data, data.target) ch = Chromosome(p.nin, p.nout) ch.random(c) orig_genes = np.copy(ch.genes) clf = Classifier(ch, p.x_train, p.x_test, p.y_train, p.y_test, batch_size=p.batch_size, epochs=p.epochs, seed=p.seed, lamarckian=p.lamarckian) fit = 0.0 def test_eval(): acc = clf.evaluate()
class LearnEvolver: def __init__(self, problem, config, logname='test', root_dir='.'): self.config = config self.problem = problem self.epochs = 1 * self.problem.epochs self.max_learn_fit = 0.0 self.max_evo_fit = 0.0 self.max_fit = 0.0 self.evo_best = Chromosome(self.problem.nin, self.problem.nout) self.evo_best.random(config) self.learn_best = Chromosome(self.problem.nin, self.problem.nout) self.learn_best.from_genes(self.evo_best.genes, config) self.logfile = os.path.join(root_dir, 'logs', logname + '.log') self.logname = logname self.generation = 0 def run(self, n_steps): for i in range(n_steps): self.step() def mutate(self, chromosome): child_genes = np.copy(chromosome.genes) change = np.random.rand( len(child_genes)) < self.config.cfg["mutation_rate"] child_genes[change] = np.random.rand(np.sum(change)) child = Chromosome(self.problem.nin, self.problem.nout) child.from_genes(child_genes, self.config) return child def step(self): evo_delete = True learn_delete = True evo_child = self.mutate(self.evo_best) self.problem.epochs = 0 fitness, history = self.problem.get_fitness(evo_child) if fitness >= self.max_evo_fit: self.evo_best = evo_child self.max_evo_fit = fitness evo_delete = False if fitness >= self.max_learn_fit: self.learn_best = evo_child self.max_learn_fit = fitness evo_delete = False learn_child = self.mutate(self.learn_best) self.problem.epochs = self.epochs fitness, history = self.problem.get_fitness(learn_child) if fitness >= self.max_learn_fit: self.learn_best = learn_child self.max_learn_fit = fitness learn_delete = False new_max = max(self.max_evo_fit, self.max_learn_fit) if new_max > self.max_fit: self.max_fit = new_max with open(self.logfile, 'a') as f: for i in range(len(history)): # type,logname,gen,eval,epoch,total_epochs,loss,acc,best f.write('L,%s,%d,%d,%d,%d,%0.10f,%0.10f,%0.10f\n' % (self.logname, self.generation, self.problem.eval_count, i, self.problem.epochs, history[i][0], history[i][1], new_max)) f.write( 'E,%s,%d,%d,%d,%d,%0.10f,%0.10f,%0.10f\n' % (self.logname, self.generation, self.problem.eval_count, 0, self.problem.epochs, 0.0, 0.0, new_max)) if evo_delete: del evo_child if learn_delete: del learn_child self.generation += 1