Пример #1
0
    def test_create_problem_from_structured_data(self):
        BlockCSPProblem(
            [
                Block(
                    Polygon(
                        [
                            (0, 0),
                            (1, 0),
                            (1, 1),
                            (0, 1),
                        ]
                    ),
                    1,
                    self.domain,
                ),
                Block(
                    Polygon(
                        [
                            (0, 0),
                            (1, 0),
                            (1, 1),
                            (0, 1),
                        ]
                    ),
                    1,
                    self.domain,
                ),

            ]
        )
Пример #2
0
    def test_constraint(self):
        reference_block = Block(self.reference_polygon, 1, self.domain)
        reference_value = Block.Value(0, 0, 0)
        block_with_same_color = Block(self.reference_polygon, 1, self.domain)
        block_with_different_color = Block(self.reference_polygon, 2, self.domain)

        self.assertTrue(Block.check_constraint(
            reference_block, reference_value,
            block_with_different_color, Block.Value(1, 0, 0)
        ))

        self.assertFalse(Block.check_constraint(
            reference_block, reference_value,
            block_with_same_color, Block.Value(1, 0, 0)
        ))

        self.assertFalse(Block.check_constraint(
            reference_block, reference_value,
            block_with_different_color, Block.Value(0, 0, 0)
        ))

        self.assertFalse(Block.check_constraint(
            reference_block, reference_value,
            block_with_same_color, Block.Value(0, 0, 0)
        ))

        self.assertTrue(Block.check_constraint(
            reference_block, reference_value,
            block_with_different_color, Block.Value(1, 1, 0)
        ))

        self.assertTrue(Block.check_constraint(
            reference_block, reference_value,
            block_with_same_color, Block.Value(1, 1, 0)
        ))
Пример #3
0
    def test_arc_consistency_checking_algorithm(self):

        domain = []
        for x in range(3):
            for y in range(3):
                for rotation in range(0, 360, 90):
                    domain.append(
                        Block.Value(x, y, rotation)
                    )

        original_problem = BlockCSPProblem([
            Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain),
            Block(Polygon([(0, 0), (3, 0), (3, 3), (1, 3), (1, 2), (0, 2)]), 2, domain),
        ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)])
        )

        still_consistent = True
        for source_variable in original_problem.variables:
            for destination_variable in original_problem.variables:
                if source_variable is destination_variable:
                    continue
                if not source_variable.is_arc_consistent_with(destination_variable):
                    still_consistent = False

        self.assertFalse(still_consistent)

        consistent_problem = arc_consistency_checking_algorithm(original_problem)

        for source_variable in consistent_problem.variables:
            for destination_variable in consistent_problem.variables:
                if source_variable is destination_variable:
                    continue
                self.assertTrue(source_variable.is_arc_consistent_with(destination_variable))
Пример #4
0
    def test_simple_csp_to_logic_conversion(self):

        domain = []
        for x in range(3):
            for y in range(3):
                for rotation in range(0, 360, 90):
                    domain.append(Block.Value(x, y, rotation))

        original_problem = BlockCSPProblem([
            Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain),
            Block(Polygon([(0, 0), (3, 0), (3, 3), (1, 3), (1, 2),
                           (0, 2)]), 2, domain),
        ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)]))

        logic_problem = original_problem.get_propositional_logic_cnf()

        solved_problem = dfs_with_ac3(original_problem)

        self.assertIsNotNone(solved_problem)

        self.assertTrue(
            TestBlockLogicProblem.does_solution_satisfy_logic_problem(
                solved_problem, logic_problem))

        self.assertFalse(
            TestBlockLogicProblem.does_solution_satisfy_logic_problem(
                original_problem, logic_problem))
Пример #5
0
    def test_dfs_with_ac3_simple(self):

        domain = []
        for x in range(3):
            for y in range(3):
                for rotation in range(0, 360, 90):
                    domain.append(
                        Block.Value(x, y, rotation)
                    )

        original_problem = BlockCSPProblem([
            Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain),
            Block(Polygon([(0, 0), (3, 0), (3, 3), (1, 3), (1, 2), (0, 2)]), 2, domain),
        ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)]))

        self.help_test_dfs_with_ac3_with_problem(original_problem)
Пример #6
0
def make_function_at(static, addr):
    if static[addr]['function'] != None:
        # already function
        return
    rc = static.r2core
    rc.cmd("af @ %d" % (addr, ))
    this_function = Function(addr)
    static['functions'].add(this_function)

    info = rc.cmd_json("afj %d" % (addr, ))[0]
    callrefs = info['callrefs']
    for ref in callrefs:
        if ref["type"] == "J":
            static[ref['addr']]['crefs'].add(addr)
        if ref["type"] == "C":
            static[ref['addr']]['xrefs'].add(addr)

    function_details = rc.cmd_json("pdfj @ %d" % addr)
    if function_details['addr'] == addr:
        for opcode in function_details['ops']:
            static[opcode['offset']]['function'] = this_function
            i = static[opcode['offset']]['instruction']

    addr_re = re.compile(r'\| (0x[a-f0-9]+) ')
    blocks = rc.cmd_json("agj %d" % addr)[0]['blocks']
    for block in blocks:
        this_block = Block(block['offset'])
        this_function.add_block(this_block)
        for op in block['ops']:
            address = op['offset']
            this_block.add(address)
            static[address]['block'] = this_block
        static['blocks'].add(this_block)
Пример #7
0
    def test_simple_block_logic_problem(self):
        domain = []
        for x in range(2):
            for y in range(1):
                for rotation in range(0, 360, 90):
                    domain.append(Block.Value(x, y, rotation))

        original_problem = BlockCSPProblem([
            Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain),
            Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 0, domain),
        ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)]))

        logic_problem = original_problem.get_propositional_logic_cnf()

        solved_problem = dpll(logic_problem)

        self.assertIsNotNone(solved_problem)

        self.assertTrue(
            evaluate_clauses_with_model(logic_problem, solved_problem))

        original_problem.import_cnf_model(solved_problem)
        self.assertTrue(BlockCSPProblem.is_solution_sound(original_problem))

        domain = []
        for x in range(3):
            for y in range(3):
                for rotation in range(0, 360, 90):
                    domain.append(Block.Value(x, y, rotation))

        original_problem = BlockCSPProblem([
            Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain),
            Block(Polygon([(0, 0), (3, 0), (3, 3), (1, 3), (1, 2),
                           (0, 2)]), 2, domain),
        ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)]))

        logic_problem = original_problem.get_propositional_logic_cnf()

        solved_problem = dpll(logic_problem)

        self.assertIsNotNone(solved_problem)

        self.assertTrue(
            evaluate_clauses_with_model(logic_problem, solved_problem))

        original_problem.import_cnf_model(solved_problem)
        self.assertTrue(BlockCSPProblem.is_solution_sound(original_problem))
Пример #8
0
def add_Block(patient, describe):
    global user
    global current_block
    timestamp = time.time()

    block = Block(timestamp=timestamp,
                  doctor=user.name,
                  patient=patient,
                  describe=describe)
    if current_block == "":
        blocks = get_all_blocks()
        current_block = copy.deepcopy(blocks[0])
    block.index = int(current_block.index) + 1
    block.encryption = str(hash(current_block))  # 同态加密
    db.session.add(block)
    db.session.commit()
    ClientNode.send_ADDBLOCK_message(block)
    current_block = copy.deepcopy(block)  # 更新当前block数据
Пример #9
0
def make_function_at(static, address, recurse=True):
    if static[address]['function'] != None:
        # already function
        return
    start = time.time()
    block_starts = set([address])
    function_starts = set()
    this_function = Function(address)
    static['functions'].add(this_function)

    def disassemble(address):
        raw = static.memory(address, 0x10)
        d = static[address]['instruction']
        static[address]['function'] = this_function
        for (c, flag) in d.dests():
            if flag == DESTTYPE.call:
                static._auto_update_name(c, "sub_%x" % (c))
                function_starts.add(c)
                #print "%s %x is in %x xrefs" % (d,address, c)
                static[c]['xrefs'].add(address)
                # add this to the potential function boundary starts
                continue
            if c != address + d.size():
                #print "%s %x is in %x crefs" % (d,address, c)
                static[c]['crefs'].add(address)
                static._auto_update_name(c, "loc_%x" % (c))
                block_starts.add(c)

            #if we come after a jump and are an implicit xref, we are the start
            #of a new block
            elif d.is_jump() and not d.is_call():
                static._auto_update_name(c, "loc_%x" % (c))
                block_starts.add(c)
        return d.dests()

    # recursive descent pass
    pending = Queue.Queue()
    done = set()
    pending.put(address)
    while not pending.empty():
        dests = disassemble(pending.get())
        for (d, flag) in dests:
            if flag == DESTTYPE.call:
                #this will get handled in the function pass
                continue
            if d not in done:
                pending.put(d)
                done.add(d)
        if (time.time() - start) > 0.01:
            time.sleep(0.01)
            start = time.time()

    #print map(hex, done)

    # block finding pass
    for b in block_starts:
        this_block = Block(b)
        this_function.add_block(this_block)
        address = b
        i = static[address]['instruction']
        while not i.is_ending() and i.size() != 0:
            if address + i.size() in block_starts:
                break
            address += i.size()
            i = static[address]['instruction']
            this_block.add(address)
            static[address]['block'] = this_block
            if (time.time() - start) > 0.01:
                time.sleep(0.01)
                start = time.time()
        static['blocks'].add(this_block)

    # find more functions
    if recurse:
        for f in function_starts:
            if static[f]['function'] == None:
                make_function_at(static, f)
Пример #10
0
blocks = []
for i in range(p):
    k, c = map(int, input().split())
    pieces = []
    for j in range(k):
        line = input()
        for x in range(len(line)):
            if line[x] == '*':
                pieces.append(
                    Polygon([(x, j), (x + 1, j), (x + 1, j + 1), (x, j + 1)]))

    block_polygon = pieces[0]
    for piece in pieces:
        block_polygon = block_polygon.union(piece).simplify(0)
    blocks.append(Block(block_polygon, c, domain, i + 1))

start = timeit.default_timer()
problem = BlockCSPProblem(blocks, space)

if '--dpll' in sys.argv:
    parallelism = 0
    if '--parallelism' in sys.argv:
        parallelism = int(sys.argv[sys.argv.index('--parallelism') + 1])
    logic_problem = problem.get_propositional_logic_cnf()
    model = dpll(logic_problem, parallelism)
    solution = None
    if model is not None:
        solution = deepcopy(problem)
        solution.import_cnf_model(model)
else: