def default_disassembly(model_src, model_tgt=None) -> List[Command]:

    finish, start = bounding_box(model_src)
    start = Pos(finish.x, start.y, finish.z)

    return apply_default_strategy(model_src, Model(model_src.R), (3, -1, 1),
                                  start, False)
Exemplo n.º 2
0
def test_is_inside_region():
    matrix = [
        #   z
        #  ---->
        [
            [1, 0, 0],  # |
            [0, 1, 0],  # | y
            [0, 0, 0]
        ],  # v
        # x = 0
        [[0, 0, 0], [0, 1, 0], [0, 0, 0]],
        # x = 1
        [[0, 0, 0], [1, 1, 0], [0, 0, 0]],
        # x = 2
    ]
    m = Model(3)
    for x, slice in enumerate(matrix):
        for y, row in enumerate(slice):
            for z, cell in enumerate(row):
                m[Pos(x, y, z)] = bool(cell)

    bbox_min, bbox_max = bounding_box(m)

    assert is_inside_region(Pos(1, 1, 1), bbox_min, bbox_max)
    assert is_inside_region(Pos(2, 0, 0), bbox_min, bbox_max)
    assert not is_inside_region(Pos(2, 0, 3), bbox_min, bbox_max)
Exemplo n.º 3
0
def set_cpp_state():
    m = Model(5)
    m[Pos(2, 0, 2)] = True
    m[Pos(2, 1, 2)] = True
    m[Pos(2, 2, 2)] = True
    m[Pos(2, 3, 2)] = True

    s = State(5)
    s.matrix = m
    s.harmonics = LOW
    s.energy = 0
    s.bots = [
        Bot(bid=1, pos=Pos(0, 0, 1), seeds=[]),
        Bot(bid=2, pos=Pos(0, 0, 2), seeds=list(range(4, 40))),
        Bot(bid=40, pos=Pos(1, 0, 0), seeds=[])
    ]

    return state_to_cpp(s)
Exemplo n.º 4
0
def test_projection_top():
    matrix = [
        #   z
        #  ---->
        [
            [1, 0, 0],  # |
            [0, 1, 0],  # | y
            [0, 0, 0]
        ],  # v
        # x = 0
        [[0, 0, 0], [0, 1, 0], [0, 0, 0]],
        # x = 1
        [[0, 0, 0], [1, 1, 0], [0, 0, 0]],
        # x = 2
    ]
    m = Model(3)
    for x, slice in enumerate(matrix):
        for y, row in enumerate(slice):
            for z, cell in enumerate(row):
                m[Pos(x, y, z)] = bool(cell)

    assert projection_top(m) == [[1, 1, 0], [0, 1, 0], [1, 1, 0]]
Exemplo n.º 5
0
def test_bounding_box():
    matrix = [
        #   z
        #  ---->
        [
            [1, 0, 0],  # |
            [0, 1, 0],  # | y
            [0, 0, 0]
        ],  # v
        # x = 0
        [[0, 0, 0], [0, 1, 0], [0, 0, 0]],
        # x = 1
        [[0, 0, 0], [1, 1, 0], [0, 0, 0]],
        # x = 2
    ]
    m = Model(3)
    for x, slice in enumerate(matrix):
        for y, row in enumerate(slice):
            for z, cell in enumerate(row):
                m[Pos(x, y, z)] = bool(cell)

    assert bounding_box(m) == (Pos(0, 0, 0), Pos(2, 1, 1))
Exemplo n.º 6
0
# Returns [] on failure
def navigate(m, src, dst):
    return [x for x in navigate_gen(m, src, dst)]


def test_navigation(m, a, b):
    pos = a
    for diff in [x.lld for x in navigate(m, a, b)]:
        new_pos = pos + diff
        assert (region_is_clear(m, pos, new_pos))
        pos = new_pos
    assert (pos == b)


if __name__ == '__main__':
    test_navigation(Model(3), Pos(0, 0, 0), Pos(2, 2, 2))

    m1 = Model(3)
    m1[Pos(0, 0, 1)] = True
    m1[Pos(1, 0, 1)] = True
    m1[Pos(2, 0, 1)] = True
    m1[Pos(0, 1, 1)] = True
    m1[Pos(1, 1, 1)] = True
    m1[Pos(2, 1, 1)] = True
    m1[Pos(1, 2, 1)] = True
    m1[Pos(2, 2, 1)] = True
    test_navigation(m1, Pos(0, 0, 0), Pos(2, 0, 2))

    m2 = Model(3)
    m2[Pos(0, 0, 1)] = True
    m2[Pos(1, 0, 1)] = True
Exemplo n.º 7
0
 def __init__(self, R):
     self.R = R
     self.matrix = Model(R)
     self.harmonics = LOW
     self.energy = 0
     self.bots = [Bot(1, Pos(0, 0, 0), list(range(39)))]  # TODO
Exemplo n.º 8
0
def main_run_interactive():

    import production.utils as utils

    # assuming we have left state and expecting right state
    #   x->
    # z ...  ...  ...  |  2..  ...  ...
    # | 2o.  ...  ...  |  .o.  .o.  ...
    # v ...  ...  ...  |  3..  ...  ...
    #   y ---------->

    m = Model(3)
    m[Pos(1, 0, 1)] = 1

    s = State(3)
    s.matrix = m
    s.harmonics = LOW
    s.energy = 30
    s.bots = [Bot(bid=2, pos=Pos(0, 0, 1), seeds=[3, 4, 5])]

    cmds = [
        commands.Fill(Diff(1, 1, 0)),
        commands.Fission(Diff(0, 0, 1), 2),
        commands.SMove(Diff(0, 0, -1)),
        commands.Wait()
    ]

    # pass state to emulator (step count set to 0)

    em = Cpp.Emulator(state_to_cpp(s))

    # LOGGING -- temporary out of service
    # if no logfile name given, emulator doesn't log
    # problemname & solutionname are optional

    from production import utils
    em.setlogfile(str(utils.project_root() / 'outputs' / 'cpp_emulator.log'))
    em.setproblemname("some handmade problem")
    em.setsolutionname("John Doe's ingenious alg")

    # OPTION 1: Run set of commands

    cpp_cmds = list(map(cmd_to_cpp, cmds))
    em.run_commands(cpp_cmds)

    cs = em.get_state()
    print("Energy: ", cs.energy)
    print("Central cell: ", cs[Cpp.Pos(1, 1, 1)])
    print("Active bots: ", sum(b.active for b in cs.bots))

    # OPTION 2: Command by command

    #    x->
    # z  2..  ...  ...
    # |  .o.  .o.  ...
    # v  3..  ...  ...
    #    y ---------->

    ccmd = cmd_to_cpp(commands.LMove(Diff(1, 0, 0), Diff(0, 0, 1)))
    msg = em.check_command(ccmd)
    print(msg == '', 'Error: ', msg)

    # void string if command is valid

    ccmd = cmd_to_cpp(commands.Fill(Diff(0, 0, 1)))
    msg = em.check_command(ccmd)
    print(msg == '', 'Error: ', msg)

    em.add_command(ccmd)

    ccmd = cmd_to_cpp(commands.SMove(Diff(0, 0, -1)))
    msg = em.check_command(ccmd)
    print(msg == '', 'Error: ', msg)

    # to check command and add iff it's valid

    ccmd = cmd_to_cpp(commands.Wait())
    msg = em.check_add_command(ccmd)
    print(msg == '', 'Error: ', msg)

    # if there are enough commands for next step, new commands cannot
    # be checked until change of state, and every check will fail

    ccmd = cmd_to_cpp(commands.Wait())
    msg = em.check_add_command(ccmd)
    print(msg == '', 'Error: ', msg)

    # you can still add command without checks

    print('Trace is full: ', em.steptrace_is_complete())
    print('Energy: ', em.energy())
    em.add_command(ccmd)
    em.run_step()
    print('Trace is full: ', em.steptrace_is_complete())
    print('Energy: ', em.energy())
def default_assembly(model_src, model_tgt) -> List[Command]:

    start, finish = bounding_box(model_tgt)

    return apply_default_strategy(Model(model_tgt.R), model_tgt, (3, 1, 1),
                                  start)