def save_pre(n, pre_prob): if None in (n, pre_prob): raise PreventUpdate if pre_prob == 'trav': configs.params['prob_type'] = Single(configs.params) configs.params['num_objs'] = 1 configs.params['objs'] = [min] configs.params['obj_names'] = ['Distance'] configs.params['enc_name'] = 'Permutation' configs.params['enc_type'] = Perm(configs.params) configs.params['gene_size'] = 29 configs.params['enc_type'].params['min_value'] = 0 configs.params['enc_type'].params['max_value'] = 28 configs.params['eval_type'] = TSP configs.params['cust_vis'] = network elif pre_prob == 'knap': configs.params['prob_type'] = Multi(configs.params) configs.params['num_objs'] = 2 configs.params['objs'] = [max, min] configs.params['obj_names'] = ['Value', 'Weight'] configs.params['enc_name'] = 'Binary String' configs.params['enc_type'] = Binary(configs.params) configs.params['gene_size'] = 64 configs.params['enc_type'].params['min_value'] = 0 configs.params['enc_type'].params['max_value'] = 1 configs.params['eval_type'] = knapsack configs.params['cust_vis'] = selection configs.params['n'] = 8 elif pre_prob == 'queens': configs.params['prob_type'] = Single(configs.params) configs.params['num_objs'] = 1 configs.params['objs'] = [min] configs.params['obj_names'] = ['Threats'] configs.params['enc_name'] = 'Permutation' configs.params['enc_type'] = Perm(configs.params) configs.params['gene_size'] = 64 configs.params['enc_type'].params['min_value'] = 0 configs.params['enc_type'].params['max_value'] = 63 configs.params['eval_type'] = eight_queens configs.params['cust_vis'] = chess_board elif pre_prob == 'sudoku': configs.params['prob_type'] = Single(configs.params) configs.params['num_objs'] = 1 configs.params['objs'] = [min] configs.params['obj_names'] = ['Conflicts'] configs.params['enc_name'] = 'Permutation' configs.params['enc_type'] = Perm(configs.params) configs.params['gene_size'] = 100 configs.params['enc_type'].params['min_value'] = 0 configs.params['enc_type'].params['max_value'] = 99 configs.params['eval_type'] = sudoku configs.params['cust_vis'] = sudoku_board return 'Saved'
def save(n, prob_type, num_objs, obj_1_name, obj_2_name, obj_3_name, obj_1_goal, obj_2_goal, obj_3_goal, enc_type, gene_size, min_value, max_value): if None in (n, prob_type, num_objs, min_value, max_value): raise PreventUpdate elif num_objs == 1 and None in (obj_1_name, obj_1_goal): raise PreventUpdate elif num_objs == 2 and None in (obj_1_name, obj_1_goal, obj_2_name, obj_2_goal): raise PreventUpdate elif num_objs == 3 and None in (obj_1_name, obj_1_goal, obj_2_name, obj_2_goal, obj_3_name, obj_3_goal): raise PreventUpdate elif gene_size is not None and gene_size < 0: raise PreventUpdate elif min_value is not None and min_value >= max_value: raise PreventUpdate elif max_value is not None and max_value <= min_value: raise PreventUpdate if prob_type == 'sing-obj': configs.params['prob_type'] = Single(configs.params) else: configs.params['prob_type'] = Multi(configs.params) configs.params['num_objs'] = num_objs configs.params['objs'], configs.params['obj_names'] = [], [] dirs, names = [obj_1_goal, obj_2_goal, obj_3_goal], [obj_1_name, obj_2_name, obj_3_name] for i in range(configs.params['num_objs']): if dirs[i] == 'min': configs.params['objs'].append(min) else: configs.params['objs'].append(max) configs.params['obj_names'].append(names[i]) if enc_type == 'perm': configs.params['enc_name'] = 'Permutation' configs.params['enc_type'] = Perm(configs.params) elif enc_type == 'binary': configs.params['enc_name'] = 'Binary String' configs.params['enc_type'] = Binary(configs.params) elif enc_type == 'int': configs.params['enc_name'] = 'Integer String' configs.params['enc_type'] = Integer(configs.params) elif enc_type == 'real': configs.params['enc_name'] = 'Real-Valued String' configs.params['enc_type'] = Real(configs.params) configs.params['gene_size'] = gene_size configs.params['enc_type'].params['min_value'] = min_value configs.params['enc_type'].params['max_value'] = max_value return 'Saved'
class Run(): def __init__(self): pass def create_triangular(self): self.triangular = Triangular() def exec_command(self): # self.create_triangular() # self.triangular.loop() self.single = Single() self.single.loop() def init_logger(self): level = logging.DEBUG logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', level=level) def main(self): self.init_logger() self.exec_command()
class Comment: content: str extension: str single: Single multi: Multi todo: TODO def __init__(self, content, extension): self.content = content self.extension = extension self.single = Single(self.content, self.extension) self.multi = Multi(self.content, self.extension) self.todo = TODO(self.content) def singleLines(self): singles = self.single.lines() multiSingles = self.single.multiSingleLines() falsePositives = self.single.falsePositivesLines( ) #special case pattern for single false positives return str(singles - multiSingles - falsePositives) def blockComments(self): blocks = self.multi.blocks() multiSingleBlocks = int(self.single.multiSingleBlocks()) return str(blocks + multiSingleBlocks) def blockLines(self): lines = self.multi.lines() multiSingleLines = self.single.multiSingleLines() return str(lines + multiSingleLines) def commentLines(self): return str(int(self.singleLines()) + int(self.blockLines())) def TODOLines(self): return str(int(self.todo.lines()))
def test_falsePositive(self): string = "print(\"#Hello\")\n" test = Single(string, "py") numOfSingleComments = test.falsePositivesLines() self.assertEqual(1, int(numOfSingleComments))
def test_linesInBlock(self): string = "#Comment1\n#Comment2\n" test = Single(string, "py") numOfSingleComments = test.multiSingleLines() self.assertEqual(2, int(numOfSingleComments))
def test_twoSingleCommentsAsBlock(self): string = "#Comment1\n#Comment2\n" test = Single(string, "py") numOfSingleComments = test.multiSingleBlocks() self.assertEqual(1, numOfSingleComments)
def test_besideSingleComment(self): string = "print(\"Hello\") #Comment\n" test = Single(string, "py") numOfSingleComments = test.lines() self.assertEqual(1, numOfSingleComments)
def test_singleComment(self): string = "#Comment\n" test = Single(string, "py") numOfSingleComments = test.lines() self.assertEqual(1, numOfSingleComments)
from single import Single import flappy import gc GENERATIONS = 15 SURVIVORS = 3 #survivors per generation CHILDREN = 5 #children per survivor INIT_POPULATION = 40 def sort(e): return e.getTime() ais = [Single() for i in range(0, INIT_POPULATION)] for i in range(0, GENERATIONS): print("-> generation " + str(i) + " <-") for ai in ais: ai.setTime(flappy.main(ai)) print(str(ai.getTime())) ais.sort(key=sort, reverse=True) ais = ais[0:SURVIVORS] for i, ai in enumerate(ais[0:SURVIVORS]): print("procreating survivor " + str(i + 1)) for j in range(0, CHILDREN): ais.append(ai.procreate()) gc.collect()
def exec_command(self): # self.create_triangular() # self.triangular.loop() self.single = Single() self.single.loop()
def __init__(self, content, extension): self.content = content self.extension = extension self.single = Single(self.content, self.extension) self.multi = Multi(self.content, self.extension) self.todo = TODO(self.content)
from single import Single import flappy ai = Single.load() print("Life: "+str(flappy.main(ai)))