Exemple #1
0
    def explore(self, current_patch, current_fitness):
        assert self.debug_patch is not None
        self.report['best_fitness'] = None
        self.report['best_patch'] = None

        for edit in cleaned_patch.edit_list:
            # move
            patch = Patch(self.program)
            patch.add(edit)

            # compare
            run = self.evaluate_patch(patch)
            h = [self.stats['steps'] + 1, run.status, ' ', run.fitness, patch]
            if run.status == 'SUCCESS':
                accept = True
                if self.dominates(run.fitness, self.report['best_fitness']):
                    self.report['best_fitness'] = run.fitness
                    self.report['best_patch'] = patch
                    h[2] = '*'

            # log
            self.program.logger.debug(self.program.diff(patch))
            self.program.logger.debug(run)
            self.program.logger.info('{}\t{}\t{}{}\t{}'.format(*h))

            # next
            self.stats['steps'] += 1

        self.report['stop'] = 'validation end'
        return current_patch, current_fitness
Exemple #2
0
 def test_diff(self, setup_tree):
     program = setup_tree
     patch = Patch(program)
     assert not program.diff(patch).strip()
     patch.add(
         StmtInsertion(('triangle.py', 1), ('triangle.py', 10),
                       direction='after'))
     assert program.diff(patch).strip()
Exemple #3
0
 def test_diff(self, setup_tree):
     program = setup_tree
     patch = Patch()
     print(patch.raw())
     assert not program.diff(patch).strip()
     patch.add(
         StmtInsertion(('triangle.py', 'stmt', 1),
                       ('triangle.py', 'stmt', 10)))
     assert program.diff(patch).strip()
 def crossover(self, sol1, sol2):
     c = Patch(self.program)
     k1 = random.randint(0, len(sol1))
     k2 = random.randint(0, len(sol2))
     for edit in sol1.edit_list[:k1]:
         c.add(edit)
     for edit in sol2.edit_list[k2:]:
         c.add(edit)
     return c
Exemple #5
0
    def explore(self, current_patch, current_fitness):
        if self.debug_patch is None:
            raise ValueError()
        self.cleaned_patch = self.do_clean_patch(self.debug_patch)
        self.program.logger.debug('CLEAN_PATCH: {}'.format(str(self.cleaned_patch)))
        self.program.logger.debug('CLEAN_SIZE: %d (was %d)', len(self.cleaned_patch), len(self.debug_patch))
        self.report['best_fitness'] = None
        self.report['best_patch'] = None
        self.report['cleaned_patch'] = self.cleaned_patch

        # full patch first
        if self.debug_patch.edit_list:
            run = self.do_eval_patch(self.debug_patch)
        else:
            self.report['stop'] = 'validation end (empty patch)'
            return current_patch, current_fitness

        # ranking
        self.program.logger.info('-- ranking --')
        ranking = list()
        for edit in self.debug_patch.edit_list:
            patch = Patch([edit])
            run = self.do_eval_patch(patch)
            if run.fitness:
                ranking.append((edit, run.fitness))
        ranking.sort(key=lambda c: c[1])

        # rebuild
        if ranking[0][1] > self.report['initial_fitness']:
            self.program.logger.info('-- rebuild --')
            rebuild = Patch(ranking[0])
            rebuild_fitness = ranking[0][1]
            for (edit,fit) in ranking[1:]:
                patch = copy.deepcopy(rebuild)
                patch.add(edit)
                run = self.do_eval_patch(patch)
                if run.status == 'SUCCESS' and self.dominates(run.fitness, rebuild_fitness):
                    rebuild_fitness = run.fitness
                    rebuild.add(edit)

        # round robin ablation
        self.program.logger.info('-- ablation --')
        n = len(self.debug_patch.edit_list)+1
        last_i = 0
        while n > len(self.report['best_patch'].edit_list):
            n = len(self.report['best_patch'].edit_list)
            for i in range(n):
                patch = Patch([e for (j, e) in enumerate(self.report['best_patch'].edit_list) if (j+last_i)%n != i])
                run = self.do_eval_patch(patch)
                if run.fitness == self.report['best_fitness']:
                    self.report['best_patch'] = patch # accept because smaller
                    last_i = i # round robin
                    break

        self.report['stop'] = 'validation end'
        return current_patch, current_fitness
Exemple #6
0
 def test_apply(self, setup_tree):
     program = setup_tree
     patch = Patch()
     patch.add(
         StmtInsertion(('triangle.py', 'stmt', 1),
                       ('triangle.py', 'stmt', 10)))
     program.apply(patch)
     file_contents = open(os.path.join(program.work_path, 'triangle.py'),
                          'r').read()
     assert file_contents == program.dump(
         program.get_modified_contents(patch), 'triangle.py')
Exemple #7
0
 def test_apply(self, setup_line):
     program = setup_line
     patch = Patch(program)
     patch.add(
         LineInsertion(('triangle.py', 1), ('triangle.py', 10),
                       direction='after'))
     program.apply(patch)
     file_contents = open(os.path.join(program.tmp_path, 'triangle.py'),
                          'r').read()
     assert file_contents == program.dump(
         program.get_modified_contents(patch), 'triangle.py')
 def crossover(self, sol1, sol2):
     c = Patch(self.program)
     for edit in sol1.edit_list:
         if random.random() > self.config['uniform_rate']:
             c.add(edit)
     for edit in sol2.edit_list:
         if random.random() > self.config['uniform_rate']:
             c.add(edit)
     if len(c) == 0:
         sol3, sol4 = [sol1, sol2
                       ] if random.random() > 0.5 else [sol2, sol1]
         if len(sol3) > 0:
             c.add(random.choice(sol3.edit_list))
         elif len(sol4) > 0:
             c.add(random.choice(sol4.edit_list))
     return c
 def crossover(self, sol1, sol2):
     c = Patch(self.program)
     k1 = random.randint(0, len(sol1))
     k2 = random.randint(0, len(sol1))
     k3 = random.randint(0, len(sol2))
     k4 = random.randint(0, len(sol2))
     for edit in sol1.edit_list[:min(k1, k2)]:
         c.add(edit)
     for edit in sol2.edit_list[min(k3, k4):max(k3, k4)]:
         c.add(edit)
     for edit in sol1.edit_list[max(k1, k2):]:
         c.add(edit)
     return c
Exemple #10
0
    def explore(self, current_patch, current_fitness):
        if self.debug_patch is None:
            raise ValueError()
        self.cleaned_patch = self.do_clean_patch(self.debug_patch)
        self.program.logger.debug('CLEAN_PATCH: {}'.format(str(self.cleaned_patch)))
        self.program.logger.debug('CLEAN_SIZE: %d (was %d)', len(self.cleaned_patch), len(self.debug_patch))
        self.report['best_fitness'] = None
        self.report['best_patch'] = None
        self.report['cleaned_patch'] = self.cleaned_patch

        # full patch first
        if self.debug_patch.edit_list:
            self.do_eval_patch(self.debug_patch)
        else:
            self.report['stop'] = 'validation end (empty patch)'
            return current_patch, current_fitness

        # ranking
        ranking = list()
        for edit in self.debug_patch.edit_list:
            patch = Patch([edit])
            run = self.do_eval_patch(patch)
            ranking.append((edit, run.fitness))
        ranking.sort(key=lambda c: c[1])

        if ranking[0][1] > self.report['initial_fitness']:
            self.report['stop'] = 'validation end (all bad)'
            return current_patch, current_fitness

        # rebuild
        rebuild = Patch()
        rebuild_fitness = None
        for (k,(edit,fit)) in enumerate(ranking):
            # move
            patch = copy.deepcopy(rebuild)
            patch.add(edit)

            # compare
            if k == 0:
                rebuild.add(edit)
                rebuild_fitness = fit
                continue
            run = self.do_eval_patch(patch)
            if run.status == 'SUCCESS' and self.dominates(run.fitness, rebuild_fitness):
                rebuild_fitness = run.fitness
                rebuild.add(edit)

        self.report['stop'] = 'validation end'
        return current_patch, current_fitness
 def crossover(self, sol1, sol2):
     c = Patch(self.program)
     l1 = [(i / len(sol1), 0) for i in sorted(
         random.sample(range(len(sol1)),
                       math.ceil(len(sol1) * self.config['uniform_rate'])))]
     l2 = [(i / len(sol2), 1) for i in sorted(
         random.sample(range(len(sol2)),
                       math.ceil(len(sol2) * self.config['uniform_rate'])))]
     for (x, k) in sorted(l1 + l2):
         sol = [sol1, sol2][k]
         edit = sol.edit_list[int(x * len(sol))]
         c.add(edit)
     if len(c) == 0:
         sol3, sol4 = [sol1, sol2
                       ] if random.random() > 0.5 else [sol2, sol1]
         if len(sol3) > 0:
             c.add(random.choice(sol3.edit_list))
         elif len(sol4) > 0:
             c.add(random.choice(sol4.edit_list))
     return c
Exemple #12
0
    def explore(self, current_patch, current_fitness):
        assert self.debug_patch is not None
        self.cleaned_patch = self.clean_patch(self.debug_patch)
        self.program.logger.debug('CLEAN_PATCH: {}'.format(
            str(self.cleaned_patch)))
        self.program.logger.debug('CLEAN_SIZE: %d (was %d)',
                                  len(self.cleaned_patch),
                                  len(self.debug_patch))
        self.report['best_fitness'] = None
        self.report['best_patch'] = None

        # ranking
        ranking = list()
        for edit in cleaned_patch.edit_list:
            # move
            patch = Patch(self.program)
            patch.add(edit)

            # compare
            run = self.evaluate_patch(patch)
            accept = best = False
            if run.status == 'SUCCESS':
                accept = True
                if self.dominates(run.fitness, self.report['best_fitness']):
                    self.report['best_fitness'] = run.fitness
                    self.report['best_patch'] = patch
                    best = True
                ranking.append((edit, run.fitness))

            # hook
            self.hook_evaluation(patch, run, accept, best)

            # next
            self.stats['steps'] += 1

        # rebuild
        ranking.sort(key=lambda c: c[1])
        rebuild = Patch(self.program)
        # todo: fail if first bad
        for (k, (edit, _)) in enumerate(ranking):
            # move
            patch = copy.deepcopy(rebuild)
            patch.add(edit)

            # compare
            if k == 0:
                rebuild.add(edit)
                continue
            run = self.evaluate_patch(patch)
            accept = best = False
            if run.status == 'SUCCESS':
                accept = True
                # todo: accept only over a threshold
                if self.dominates(run.fitness, self.report['best_fitness']):
                    self.program.logger.debug(self.program.diff(patch))
                    self.report['best_fitness'] = run.fitness
                    self.report['best_patch'] = patch
                    best = True
                    rebuild.add(edit)

            # hook
            self.hook_evaluation(patch, run, accept, best)

            # next
            self.stats['steps'] += 1

        self.report['stop'] = 'validation end'
        return current_patch, current_fitness
Exemple #13
0
def eval_patch(program, string):
    patch = Patch(program)
    for s in string.split(' | '):
        patch.add(eval_edit(s))
    return patch