def test_illegal_smoves(): em = Cpp.Emulator(set_cpp_state()) # bot 2 moves out of bounds assert em.check_add_command(ctp(pc.Wait())) == '' msg = em.check_command(ctp(pc.SMove(Diff(0, 0, 3)))) assert msg != '' # bot 2 moves through the filled area msg = em.check_command(ctp(pc.SMove(Diff(3, 0, 0)))) assert msg != '' # ... c = ctp(pc.Wait()) em.add_command(c) em.add_command(c) em.run_step() # bot 1 moves through bot 2 msg = em.check_command(ctp(pc.SMove(Diff(0, 0, 3)))) assert msg != '' # bot 2 moves through volatile area assert em.check_add_command(ctp(pc.Fill(Diff(0, 1, 1)))) == '' msg = em.check_command(ctp(pc.SMove(Diff(0, 1, 0)))) assert msg != '' # bots 2 & 3 move freely assert em.check_add_command(ctp(pc.SMove(Diff(1, 0, 0)))) == '' assert em.check_add_command(ctp(pc.SMove(Diff(0, 4, 0)))) == '' assert em.steptrace_is_complete() em.run_step()
def run_full(src_model_data: Optional[bytes], tgt_model_data: Optional[bytes], trace_data: bytes) -> EmulatorResult: em = Cpp.Emulator() if src_model_data: em.set_model(src_model_data, 's') if tgt_model_data: em.set_model(tgt_model_data, 's') em.set_trace(trace_data) em.setlogfile(str(utils.project_root() / 'outputs' / 'cpp_emulator.log')) try: em.run() except Cpp.SimulatorException as e: return EmulatorResult(energy=None, extra={'error message': str(e)}) return EmulatorResult(energy=em.energy(), extra={})
def test_illegal_lmoves(): em = Cpp.Emulator(set_cpp_state()) # bot 2 moves out of bounds assert em.check_add_command(ctp(pc.Wait())) == '' msg = em.check_add_command(ctp(pc.LMove(Diff(0, -1, 0), Diff(0, 0, 3)))) assert msg != '' msg = em.check_add_command(ctp(pc.LMove(Diff(0, 1, 0), Diff(0, 0, 3)))) assert msg != '' msg = em.check_add_command(ctp(pc.LMove(Diff(0, 0, 2), Diff(0, 0, 2)))) assert msg != '' # bot 2 moves through the filled area msg = em.check_add_command(ctp(pc.LMove(Diff(2, 0, 0), Diff(0, 0, 1)))) assert msg != '' msg = em.check_add_command(ctp(pc.LMove(Diff(0, 3, 0), Diff(3, 0, 0)))) assert msg != '' # ... em.add_command(ctp(pc.Wait())) # bot 3 moves through bots 1 & 2 msg = em.check_add_command(ctp(pc.LMove(Diff(-1, 0, 0), Diff(0, 0, 4)))) assert msg != '' # ... em.add_command(ctp(pc.Wait())) em.run_step() # bot 2 moves through volatile area assert em.check_add_command(ctp(pc.Fill(Diff(0, 1, 0)))) == '' msg = em.check_add_command(ctp(pc.LMove(Diff(0, 1, 0), Diff(0, 0, -1)))) assert msg != '' # bot 2 moves freely assert em.check_add_command(ctp(pc.LMove(Diff(0, 2, 0), Diff(0, 0, -1)))) == '' # bot 2 dangles around freely assert em.check_add_command(ctp(pc.LMove(Diff(2, 0, 0), Diff(-3, 0, 0)))) == ''
def test_run_from_file(): modelfile = utils.project_root() / 'julie_scratch' / 'LA014_tgt.mdl' tracefile = utils.project_root() / 'julie_scratch' / 'LA014_dflt.nbt' logfilename = str(utils.project_root() / 'outputs' / 'cpp_emulator.log') mf = open(modelfile, 'rb') m = Cpp.Matrix.parse(mf.read()) mf.close() em = Cpp.Emulator(None, m) # (source, target) errmsg = "" tf = open(tracefile, 'rb') cmds = commands.parse_commands(tf.read(), errmsg) tf.close() commandlist = list(map(cppm.cmd_to_cpp, cmds)) em.set_trace(commandlist) em.setlogfile(logfilename) em.run() assert em.energy() == 501700108
def main_run_file(): from production import utils modelfile = utils.project_root() / 'julie_scratch' / 'LA014_tgt.mdl' tracefile = utils.project_root() / 'julie_scratch' / 'LA014_dflt.nbt' logfile = str(utils.project_root() / 'outputs' / 'cpp_emulator.log') mf = open(modelfile, 'rb') m = Cpp.Matrix.parse(mf.read()) mf.close() em = Cpp.Emulator(None, m) # (source, target) error = "" tf = open(tracefile, 'rb') cmds = commands.parse_commands(tf.read(), error) tf.close() cmdlist = list(map(cmd_to_cpp, cmds)) em.set_trace(cmdlist) em.setlogfile(logfile) em.run() print("Energy: ", em.energy())
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())