Exemple #1
0
    def npi_program_interface(self, env, program: Program, arguments: IntegerArguments, depth=0):
        if self.max_depth < depth or self.max_step < self.steps:
            raise StopIteration()

        self.model.enter_function()

        result = StepOutput(0, None, None)
        while result.r < self.alpha:
            self.steps += 1
            if self.max_step < self.steps:
                raise StopIteration()

            env_observation = env.get_observation()
            result = self.model.step(env_observation, program, arguments.copy())
            if self.recording:
                self.step_list.append(StepInOut(StepInput(env_observation, program, arguments.copy()), result))
            self.display_information(program, arguments, result, depth)

            if program.output_to_env:
                program.do(env, arguments.copy())
                self.display_env(env)
            else:
                if result.program:  # modify original algorithm
                    self.npi_program_interface(env, result.program, result.arguments, depth=depth+1)

        self.model.exit_function()
Exemple #2
0
    def npi_program_interface(self, env, program: Program, arguments: IntegerArguments, depth=0):
        if self.max_depth < depth or self.max_step < self.steps:
            raise StopIteration()

        self.model.enter_function()

        result = StepOutput(0, None, None)
        while result.r < self.alpha:
            self.steps += 1
            if self.max_step < self.steps:
                raise StopIteration()

            env_observation = env.get_observation()
            result = self.model.step(env_observation, program, arguments.copy())
            if self.recording:
                self.step_list.append(StepInOut(StepInput(env_observation, program, arguments.copy()), result))
            self.display_information(program, arguments, result, depth)

            if program.output_to_env:
                program.do(env, arguments.copy())
                self.display_env(env)
            else:
                if result.program:  # modify original algorithm
                    self.npi_program_interface(env, result.program, result.arguments, depth=depth+1)

        self.model.exit_function()
    def npi_program_interface(self,
                              env,
                              program: Program,
                              arguments: IntegerArguments,
                              depth=0):
        if self.max_depth < depth or self.max_step < self.steps:
            if self.max_step < self.steps:
                print("stop iteration becasue there are too many steps")
                self.terminal.add_log("stop iteration becasue it's too deep")
            if self.max_depth < depth:
                print("stop iteration becasue there are too many steps")
                self.terminal.add_log("stop iteration becasue it's too deep")
            raise StopIteration()

        self.model.enter_function()
        # self.terminal.add_log("enter function")
        result = StepOutput(0, None, None)
        # self.terminal.add_log(result)
        while result.r < self.alpha:
            self.terminal.add_log("in a function")
            self.steps += 1
            if self.max_step < self.steps:
                print("stop iteration becasue there are too many steps")
                self.terminal.add_log(
                    "stop iteration becasue there are too many steps")
                raise StopIteration()
            env_observation = env.get_observation()
            #self.terminal.add_log(env_observation)
            # run our model for one step
            # result is a StepOutput
            result = self.model.step(env_observation, program,
                                     arguments.copy())
            self.terminal.add_log("after excuting funciton")
            # self.terminal.add_log(result)
            # what's the intuition of recording
            if self.recording:
                self.step_list.append(
                    StepInOut(
                        StepInput(env_observation, program, arguments.copy()),
                        result))
            self.display_information(program, arguments, result, depth)

            if program.output_to_env:
                # self.terminal.add_log("output_to_env")
                program.do(env, arguments.copy())
                self.display_env(env)
            else:
                self.terminal.add_log(str(program))
                if result.program:  # modify original algorithm
                    # self.terminal.add_log("execute sub_program")
                    self.npi_program_interface(env,
                                               result.program,
                                               result.arguments,
                                               depth=depth + 1)
        self.terminal.add_log("exit funtion")
        self.model.exit_function()
Exemple #4
0
class BubblesortProgramSet:
    NOP = Program('NOP')
    MOVE_PTR = MovePtrProgram('MOVE_PTR', 3, 2)
    SWAP = SwapProgram('SWAP')
    BUBBLESORT = Program('BUBBLESORT')
    BUBBLE = Program('BUBBLE')
    RESET = Program('RESET')
    BSTEP = Program('BSTEP')
    COMPSWAP = Program('COMPSWAP')
    LSHIFT = Program('LSHIFT')
    RSHIFT = Program('RSHIFT')

    def __init__(self):
        self.map = {}
        self.program_id = 0
        self.register(self.NOP)
        self.register(self.MOVE_PTR)
        self.register(self.SWAP)
        self.register(self.BUBBLESORT)
        self.register(self.BUBBLE)
        self.register(self.RESET)
        self.register(self.BSTEP)
        self.register(self.COMPSWAP)
        self.register(self.LSHIFT)
        self.register(self.RSHIFT)

    def register(self, pg: Program):
        pg.program_id = self.program_id
        self.map[pg.program_id] = pg
        self.program_id += 1

    def get(self, i: int):
        return self.map.get(i)
Exemple #5
0
 def display_information(self, program: Program, arguments: IntegerArguments, result: StepOutput, depth: int):
     if self.verbose and self.terminal:
         information = [
             "Step %2d Depth: %2d" % (self.steps, depth),
             program.description_with_args(arguments),
             'r=%.2f' % result.r,
         ]
         if result.program:
             information.append("-> %s" % result.program.description_with_args(result.arguments))
         self.terminal.update_info_screen(information)
         self.wait()
Exemple #6
0
 def display_information(self, program: Program, arguments: IntegerArguments, result: StepOutput, depth: int):
     if self.verbose and self.terminal:
         information = [
             "Step %2d Depth: %2d" % (self.steps, depth),
             program.description_with_args(arguments),
             'r=%.2f' % result.r,
         ]
         if result.program:
             information.append("-> %s" % result.program.description_with_args(result.arguments))
         self.terminal.update_info_screen(information)
         self.wait()
class AdditionProgramSet:
    NOP = Program('NOP')
    MOVE_PTR = MovePtrProgram('MOVE_PTR', 4, 2)  # PTR_KIND(4), LEFT_OR_RIGHT(2)
    WRITE = WriteProgram('WRITE', 2, 10)       # CARRY_OR_OUT(2), DIGITS(10)
    ADD = Program('ADD')
    ADD1 = Program('ADD1')
    CARRY = Program('CARRY')
    LSHIFT = Program('LSHIFT')
    RSHIFT = Program('RSHIFT')

    def __init__(self):
        self.map = {}
        self.program_id = 0
        self.register(self.NOP)
        self.register(self.MOVE_PTR)
        self.register(self.WRITE)
        self.register(self.ADD)
        self.register(self.ADD1)
        self.register(self.CARRY)
        self.register(self.LSHIFT)
        self.register(self.RSHIFT)

    def register(self, pg: Program):
        pg.program_id = self.program_id
        self.map[pg.program_id] = pg
        self.program_id += 1

    def get(self, i: int):
        return self.map.get(i)
Exemple #8
0
class MultiplicationProgramSet:  # includes the addition Programset
    MUL = Program('MUL')  # multiply mul1 and mul2, when mul2 == 0, stop.
    COPY = CopyProgram(
        'COPY'
    )  # copy whatever on the fourth row(the output row) and write on the second row(In2)
    SUB = SubtractProgram('SUB')  # subtract mul2 by 1
    NOP = Program('NOP')
    MOVE_PTR = MovePtrProgram('MOVE_PTR', 4,
                              2)  # PTR_KIND(4), LEFT_OR_RIGHT(2)
    RESET_PTR = ResetPtrProgram('RESET_PTR',
                                4)  # PTR_KIND(4), LEFT_OR_RIGHT(2)
    WRITE = WriteProgram('WRITE', 2, 10)  # INCARRY_OR_OUT(2), DIGITS(10)
    ADD = Program('ADD')
    ADD1 = Program('ADD1')
    CARRY = Program('CARRY')
    LSHIFT = Program('LSHIFT')
    RSHIFT = Program('RSHIFT')

    def __init__(self):
        self.map = {}
        self.program_id = 0
        self.register(self.MUL)
        self.register(self.SUB)
        self.register(self.COPY)
        self.register(self.NOP)
        self.register(self.MOVE_PTR)
        self.register(self.RESET_PTR)
        self.register(self.WRITE)
        self.register(self.ADD)
        self.register(self.ADD1)
        self.register(self.CARRY)
        self.register(self.LSHIFT)
        self.register(self.RSHIFT)

    def register(self, pg: Program):
        pg.program_id = self.program_id
        self.map[pg.program_id] = pg
        self.program_id += 1

    def get(self, i: int):
        return self.map.get(i)
Exemple #9
0
 def register(self, pg: Program):
     pg.program_id = self.program_id
     self.map[pg.program_id] = pg
     self.program_id += 1
Exemple #10
0
 def register(self, pg: Program):
     pg.program_id = self.program_id
     self.map[pg.program_id] = pg
     self.program_id += 1