Exemple #1
0
def main(stdscr, model_path: str, num: int, result_logger: ResultLogger):
    terminal = Terminal(stdscr, create_char_map())
    terminal.init_window(FIELD_WIDTH, FIELD_ROW)
    program_set = BubblesortProgramSet()
    Bubblesort_env = BubblesortEnv(FIELD_ROW, FIELD_WIDTH, FIELD_DEPTH)

    questions = create_questions(num)
    if DEBUG_MODE:
        questions = questions[-num:]
    system = RuntimeSystem(terminal=terminal)
    npi_model = BubblesortNPIModel(system, model_path, program_set)
    npi_runner = TerminalNPIRunner(terminal, npi_model, recording=False)
    npi_runner.verbose = DEBUG_MODE
    correct_count = wrong_count = 0
    for data in questions:
        Bubblesort_env.reset()
        try:
            run_npi(Bubblesort_env, npi_runner, program_set.BUBBLESORT, data)
            if data['correct']:
                correct_count += 1
            else:
                wrong_count += 1
        except StopIteration:
            wrong_count += 1
            pass
        result_logger.write(data)
        terminal.add_log(data)
    return correct_count, wrong_count
Exemple #2
0
def main(stdscr, model_path: str, num: int, result_logger: ResultLogger):
    terminal = Terminal(stdscr, create_char_map())
    terminal.init_window(FIELD_WIDTH, FIELD_ROW)
    program_set = MultiplicationProgramSet()
    multiplyition_env = MultiplicationEnv(FIELD_ROW, FIELD_WIDTH, FIELD_DEPTH)

    questions = create_questions(num)
    if DEBUG_MODE:
        questions = questions[-num:]
    system = RuntimeSystem(terminal=terminal)
    npi_model = MultiplicationNPIModel(system, terminal, model_path,
                                       program_set)
    npi_runner = TerminalNPIRunner(terminal, npi_model, recording=False)
    npi_runner.verbose = DEBUG_MODE
    correct_count = wrong_count = 0
    for data in questions:
        multiplyition_env.reset()
        run_npi(multiplyition_env, npi_runner, program_set.ADD, data)
        result_logger.write(data)
        terminal.multiply_log(data)
        if data['correct']:
            correct_count += 1
        else:
            wrong_count += 1
    return correct_count, wrong_count
def main(stdscr, filename: str, num_of_questions: int,
         result_logger: ResultLogger):

    terminal = Terminal(stdscr, create_char_map())
    terminal.init_window(FIELD_WIDTH, FIELD_ROW)
    program_set = MultiplicationProgramSet()
    multiplication_env = MultiplicationEnv(FIELD_ROW, FIELD_WIDTH, FIELD_DEPTH,
                                           terminal)

    questions = create_questions(num_of_questions)
    teacher = MultiplicationTeacher(program_set, terminal)
    npi_runner = TerminalNPIRunner(terminal, teacher, result_logger)
    npi_runner.verbose = DEBUG_MODE
    steps_list = []
    for data in questions:
        multiplication_env.reset()
        q = copy(data)
        run_npi(multiplication_env, npi_runner, program_set.MUL, data)
        steps_list.append({"q": q, "steps": npi_runner.step_list})
        result_logger.write(data)
        terminal.add_log(data)

    if filename:
        with open(filename, 'wb') as f:
            pickle.dump(steps_list, f, protocol=pickle.HIGHEST_PROTOCOL)
Exemple #4
0
def main(stdscr, model_path: str, num: int, result_logger: ResultLogger):
    terminal = Terminal(stdscr, create_char_map())
    terminal.init_window(FIELD_WIDTH, FIELD_ROW)
    program_set = BubblesortProgramSet()
    Bubblesort_env = BubblesortEnv(FIELD_ROW, FIELD_WIDTH, FIELD_DEPTH)

    questions = create_questions(num)
    if DEBUG_MODE:
        questions = questions[-num:]
    system = RuntimeSystem(terminal=terminal)
    npi_model = BubblesortNPIModel(system, model_path, program_set)
    npi_runner = TerminalNPIRunner(terminal, npi_model, recording=False)
    npi_runner.verbose = DEBUG_MODE
    correct_count = wrong_count = 0

    slot_num = 10  # 10 means 10 slots, each
    # slot's size is 0.1
    for i in range(slot_num):
        arr.append([])

    # Put array elements in different buckets
    for j in x:
        index_b = int(slot_num * j)
        arr[index_b].append(j)

    # Sort individual buckets
    for i in range(slot_num):
        Bubblesort_env.reset()
        try:
            run_npi(Bubblesort_env, npi_runner, program_set.BUBBLESORT, data)
            if data['correct']:
                correct_count += 1
            else:
                wrong_count += 1
        except StopIteration:
            wrong_count += 1
            pass
        result_logger.write(data)
        terminal.add_log(data)
    return correct_count, wrong_count

    # concatenate the result
    k = 0
    for i in range(slot_num):
        for j in range(len(arr[i])):
            x[k] = arr[i][j]
            k += 1
    return x
def main(stdscr, filename: str, num: int, result_logger: ResultLogger):
    terminal = Terminal(stdscr, create_char_map())
    terminal.init_window(FIELD_WIDTH, FIELD_ROW)
    program_set = AdditionProgramSet()
    addition_env = AdditionEnv(FIELD_ROW, FIELD_WIDTH, FIELD_DEPTH)

    questions = create_questions(num)
    teacher = AdditionTeacher(program_set)
    npi_runner = TerminalNPIRunner(terminal, teacher)
    npi_runner.verbose = DEBUG_MODE
    steps_list = []
    for data in questions:
        addition_env.reset()
        q = copy(data)
        run_npi(addition_env, npi_runner, program_set.ADD, data)
        steps_list.append({"q": q, "steps": npi_runner.step_list})
        result_logger.write(data)
        terminal.add_log(data)

    if filename:
        with open(filename, 'wb') as f:
            pickle.dump(steps_list, f, protocol=pickle.HIGHEST_PROTOCOL)
Exemple #6
0
def main(stdscr, filename: str, num: int, result_logger: ResultLogger):
    terminal = Terminal(stdscr, create_char_map())
    terminal.init_window(FIELD_WIDTH, FIELD_ROW)
    program_set = BubblesortProgramSet()
    bubblesort_env = BubblesortEnv(FIELD_ROW, FIELD_WIDTH, FIELD_DEPTH)

    questions = create_questions(number=num)
    teacher = BubblesortTeacher(program_set)
    npi_runner = TerminalNPIRunner(terminal, teacher)
    npi_runner.verbose = DEBUG_MODE
    steps_list = []
    f = open("debug_log.csv", "w")
    for data in questions:
        bubblesort_env.reset()
        q = copy(data)
        run_npi(bubblesort_env, npi_runner, program_set.BUBBLESORT, data)
        steps_list.append({"q": q, "steps": npi_runner.step_list})
        for step in npi_runner.step_list:
            f.write("{},".format(step.input.env[0]))
            f.write("{},".format(step.input.env[1]))
            f.write("{},".format(step.input.env[2]))
            f.write("{},".format(step.input.env[3]))
            f.write("{},".format(step.input.env[4]))
            f.write("{},".format(step.input.env[5]))
            f.write("{},".format(step.input.arguments))
            f.write("{},".format(step.input.program))
            f.write("{},".format(step.output.program))
            f.write("{},".format(step.output.r))
            f.write("{},".format(step.output.arguments))
            f.write("{}\n".format(q))
        result_logger.write(data)
        terminal.add_log(data)
    f.close()

    if filename:
        with open(filename, 'wb') as f:
            pickle.dump(steps_list, f, protocol=pickle.HIGHEST_PROTOCOL)
Exemple #7
0
def main(stdscr, model_path: str, num: int, result_logger: ResultLogger):
    terminal = Terminal(stdscr, create_char_map())
    terminal.init_window(FIELD_WIDTH, FIELD_ROW)
    program_set = AdditionProgramSet()
    addition_env = AdditionEnv(FIELD_ROW, FIELD_WIDTH, FIELD_DEPTH)

    questions = create_questions(num)
    if DEBUG_MODE:
        questions = questions[-num:]
    system = RuntimeSystem(terminal=terminal)
    npi_model = AdditionNPIModel(system, model_path, program_set)
    npi_runner = TerminalNPIRunner(terminal, npi_model, recording=False)
    npi_runner.verbose = DEBUG_MODE
    correct_count = wrong_count = 0
    for data in questions:
        addition_env.reset()
        run_npi(addition_env, npi_runner, program_set.ADD, data)
        result_logger.write(data)
        terminal.add_log(data)
        if data["correct"]:
            correct_count += 1
        else:
            wrong_count += 1
    return correct_count, wrong_count
 def __init__(self,
              terminal: Terminal,
              model: NPIStep = None,
              recording=True,
              max_depth=100,
              max_step=10000,
              result_logger=ResultLogger('result_multiplication.log')):
     self.terminal = terminal
     self.model = model
     self.steps = 0
     self.step_list = []
     self.alpha = 0.5
     self.verbose = True
     self.recording = recording
     self.max_depth = max_depth
     self.max_step = max_step
     self.result_logger = result_logger
Exemple #9
0
    questions = create_questions(num)
    teacher = AdditionTeacher(program_set)
    npi_runner = TerminalNPIRunner(terminal, teacher)
    npi_runner.verbose = DEBUG_MODE
    steps_list = []
    for data in questions:
        addition_env.reset()
        q = copy(data)
        run_npi(addition_env, npi_runner, program_set.ADD, data)
        steps_list.append({"q": q, "steps": npi_runner.step_list})
        result_logger.write(data)
        terminal.add_log(data)

    if filename:
        with open(filename, 'wb') as f:
            pickle.dump(steps_list, f, protocol=pickle.HIGHEST_PROTOCOL)

if __name__ == '__main__':
    import sys
    DEBUG_MODE = os.environ.get('DEBUG')
    if DEBUG_MODE:
        output_filename = None
        num_data = 3
        log_filename = 'result.log'
    else:
        output_filename = sys.argv[1] if len(sys.argv) > 1 else None
        num_data = int(sys.argv[2]) if len(sys.argv) > 2 else 1000
        log_filename = sys.argv[3] if len(sys.argv) > 3 else 'result.log'
    curses.wrapper(main, output_filename, num_data, ResultLogger(log_filename))
    print("create %d training data" % num_data)
Exemple #10
0
    system = RuntimeSystem(terminal=terminal)
    npi_model = BubblesortNPIModel(system, model_path, program_set)
    npi_runner = TerminalNPIRunner(terminal, npi_model, recording=False)
    npi_runner.verbose = DEBUG_MODE
    correct_count = wrong_count = 0
    for data in questions:
        Bubblesort_env.reset()
        try:
            run_npi(Bubblesort_env, npi_runner, program_set.BUBBLESORT, data)
            if data['correct']:
                correct_count += 1
            else:
                wrong_count += 1
        except StopIteration:
            wrong_count += 1
            pass
        result_logger.write(data)
        terminal.add_log(data)
    return correct_count, wrong_count


if __name__ == '__main__':
    import sys
    DEBUG_MODE = os.environ.get('DEBUG')
    model_path_ = sys.argv[1]
    num_data = int(sys.argv[2]) if len(sys.argv) > 2 else 1000
    log_filename = sys.argv[3] if len(sys.argv) > 3 else 'result.log'
    cc, wc = curses.wrapper(main, model_path_, num_data,
                            ResultLogger(log_filename))
    print("Accuracy %s(OK=%d, NG=%d)" % (cc / (cc + wc), cc, wc))