Esempio n. 1
0
    def summary(self):
        from terminaltables import AsciiTable
        incumbent = self.solver.incumbent
        if not incumbent:
            return 'No incumbents in history. Please run fit() first.'
        configs_table = []
        nil = "-"
        parameters = list(incumbent.copy().keys())
        for para in parameters:
            row = []
            row.append(para)
            val = incumbent.get(para, None)
            if val is None:
                val = nil
            if isinstance(val, float):
                val = "%.6f" % val
            elif not isinstance(val, str):
                val = str(val)
            row.append(val)
            configs_table.append(row)
        configs_title = ["Parameters", "Optimal Value"]

        total_eval_dict = self.solver.eval_dict.copy()
        num_configs = len(self.solver.eval_dict)
        failed_configs = 0
        for key in total_eval_dict:
            perf, _, state = total_eval_dict[key]
            if state != SUCCESS:
                failed_configs += 1

        table_data = ([configs_title] +
                      configs_table +
                      [["Optimal Validation Performance", self.solver.incumbent_perf]] +
                      [['Number of Configurations', num_configs]] +
                      [['Number of Failed Configurations', failed_configs]] +
                      [['Search Runtime', '%.3f sec' % self.eval_time]] +  # TODO: Precise search time.
                      [['Total Runtime', '%.3f sec' % self.total_time]] +
                      [['Average Evaluation Time', 0]] +  # TODO: Wait for OpenBOX
                      [['Maximum Valid Evaluation Time', 0]] +
                      [['Minimum Evaluation Time', 0]]
                      )

        M = 8
        raw_table = AsciiTable(
            table_data
            # title="Result of Optimization"
        ).table
        lines = raw_table.splitlines()
        title_line = lines[1]
        st = title_line.index("|", 1)
        col = "Optimal Value"
        L = len(title_line)
        lines[0] = "+" + "-" * (L - 2) + "+"
        new_title_line = title_line[:st + 1] + (" " + col + " " * (L - st - 3 - len(col))) + "|"
        lines[1] = new_title_line
        bar = "\n" + lines.pop() + "\n"
        finals = lines[-M:]
        prevs = lines[:-M]
        render_table = "\n".join(prevs) + bar + bar.join(finals) + bar
        return render_table
Esempio n. 2
0
    def get_str(self):  # todo all configs
        from terminaltables import AsciiTable
        incumbents = self.get_incumbents()
        if not incumbents:
            return 'No incumbents in history. Please run optimization process.'

        configs_table = []
        nil = "-"
        parameters = list(incumbents[0][0].get_dictionary().keys())
        for para in parameters:
            row = []
            row.append(para)
            for config, perf in incumbents:
                val = config.get(para, None)
                if val is None:
                    val = nil
                if isinstance(val, float):
                    val = "%.6f" % val
                elif not isinstance(val, str):
                    val = str(val)
                row.append(val)
            configs_table.append(row)
        configs_title = ["Parameters"] + [
            "" if i else "Optimal Value" for i, _ in enumerate(incumbents)
        ]

        table_data = (
            [configs_title] + configs_table +
            [["Optimal Objective Value"] +
             [perf for config, perf in incumbents]] +
            [["Num Configs"] + [str(self.config_counter)]]  # todo: all configs
        )

        M = 2
        raw_table = AsciiTable(table_data
                               # title="Result of Optimization"
                               ).table
        lines = raw_table.splitlines()
        title_line = lines[1]
        st = title_line.index("|", 1)
        col = "Optimal Value"
        L = len(title_line)
        lines[0] = "+" + "-" * (L - 2) + "+"
        new_title_line = title_line[:st + 1] + (" " + col + " " *
                                                (L - st - 3 - len(col))) + "|"
        lines[1] = new_title_line
        bar = "\n" + lines.pop() + "\n"
        finals = lines[-M:]
        prevs = lines[:-M]
        render_table = "\n".join(prevs) + bar + bar.join(finals) + bar
        return render_table
Esempio n. 3
0
    def get_str(self):
        # todo: 更好看的打印
        table_data = ([self.configs_title] + self.configs_table +
                      [["Optimal Loss"] + [
                          f"{self.budget2info[budget]['loss']:.4f}"
                          for budget in self.budget2info
                      ]] + [["Num Configs"] + [
                          str(self.budget2info[budget]["num_configs"])
                          for budget in self.budget2info
                      ]])
        if self.is_multi_fidelity:
            M = 3
            table_data.insert(-2, ["Budgets"] + [
                f"{pbudget(budget)} (max)"
                if budget == self.max_budget else pbudget(budget)
                for budget in self.budget2info
            ])
        else:
            M = 2

        raw_table = AsciiTable(table_data
                               # title="Result of UltraOpt's fmin"
                               ).table
        lines = raw_table.splitlines()
        title_line = lines[1]
        st = title_line.index("|", 1)
        col = "Optimal Value"
        L = len(title_line)
        lines[0] = "+" + "-" * (L - 2) + "+"
        new_title_line = title_line[:st + 1] + (" " + col + " " *
                                                (L - st - 3 - len(col))) + "|"
        lines[1] = new_title_line
        bar = "\n" + lines.pop() + "\n"
        finals = lines[-M:]
        prevs = lines[:-M]
        render_table = "\n".join(prevs) + bar + bar.join(finals) + bar
        return render_table
Esempio n. 4
0
        def ask_for_user_input(stdscr):
            # c 99 up 259 down 258
            man = f"howto: up/down for run id ; 'c' clears table, 'p' prints cmd ## {len(all_configs)} configs "
            stdscr.addstr(man + ' ')
            stdscr.refresh()
            stdscr.move(1, 0)
            run_id = 0
            sweeps = []
            keys = ['--id']
            max_size = stdscr.getmaxyx()
            error = ""
            if len(all_configs) == 0:
                raise NameError("there are no configs available")
            while True:
                params = diff_to_default(
                    all_configs[run_id]
                ) if run_id != 0 else all_configs[run_id]
                assert (params['--id'] == run_id)

                sweeps.append(params)
                # update columns?
                for k in params:
                    if k not in keys:
                        keys.append(k)

                table = AsciiTable(
                    [['\n'.join(k.split('_')) for k in keys]] +
                    [[shorten_text(run[k]) if k in run else '/' for k in keys]
                     for run in sweeps]).table

                row = 1
                for l in table.splitlines():
                    if row >= max_size[0] - 3:
                        error = "too many lines"
                        break
                    stdscr.move(row, 0)
                    row += 1
                    stdscr.addstr(l[:max_size[1]] + ' ')

                if len(error) > 0:
                    stdscr.move(0, len(man))
                    if error == " ":
                        stdscr.addstr(" " * 40)
                        error = ""
                    else:
                        stdscr.addstr("####  ERROR: " + error + ' ####')
                    error = " "

                stdscr.refresh()

                c = stdscr.getch()
                if c == 99:
                    sweeps = []
                    keys = ['--id']
                    stdscr.move(1, 0)
                    stdscr.clrtobot()

                while True:
                    if c == 259:
                        run_id += 1
                    elif c == 258:
                        run_id -= 1
                    elif c == 339:
                        run_id *= 2
                        c = 259
                    elif c == 338:
                        run_id = run_id // 2
                        c = 258
                    elif c == 99:
                        break
                    elif c == 112:
                        # print current config
                        stdscr.move(1, 0)
                        stdscr.addstr("\ncmd for run:" + str(run_id) + "\n")
                        stdscr.addstr(" ".join([
                            f"{x} {all_configs[run_id][x]}"
                            if all_configs[run_id][x] is not None else f"{x}"
                            for x in all_configs[run_id]
                        ]))
                        stdscr.addstr(
                            "\n\n\nPress any key to refresh table...\n\n\n")
                        c = stdscr.getch()
                        continue

                    else:
                        error = f"Unknown Keypress ({c})"
                        break

                    if run_id < 0 or run_id > largest_config_id:
                        run_id = 0

                    if run_id in all_configs:
                        break

                stdscr.move(1, 0)