Beispiel #1
0
def main():
    # This handles the logger. The standard setting is that HyperMapper always logs both on screen and on the log file.
    # In cases like the interactive mode we only want to log on the file.
    sys.stdout = Logger()

    list_of_dirs = []
    parameters_file = ""
    if len(sys.argv) >= 4:
        parameters_file = sys.argv[1]
        output_hvi_file_name = sys.argv[2]
        for dir in sys.argv[3:]:
            list_of_dirs.append(dir)
    else:
        print("Error: more arguments needed.")

    if parameters_file == "--help" or len(sys.argv) < 4:
        print("################################################")
        print("### Example: ")
        print(
            "### hm-plot-hvi example_scenarios/spatial/DotProduct_scenario.json hvi_output_image_dotproduct.pdf /home/hypermapper_DotProduct /home/heuristic_DotProduct"
        )
        print("################################################")
        exit(1)

    plot_hvi(parameters_file, output_hvi_file_name, list_of_dirs)
    print("End of the plot_hvi script!")
def main():
    # This handles the logger. The standard setting is that HyperMapper always logs both on screen and on the log file.
    # In cases like the interactive mode we only want to log on the file.
    sys.stdout = Logger()

    parameters_file = ""
    input_data_file = None
    output_pareto_file = None
    if len(sys.argv) >= 2:
        parameters_file = sys.argv[1]
        if len(sys.argv) >= 3:
            input_data_file = sys.argv[2]
            if len(sys.argv) >= 4:
                output_pareto_file = sys.argv[3]
            if len(sys.argv) >= 5:
                print("Error: too many arguments.")
    else:
        print("Error: only one argument needed, the parameters json file.")

    if parameters_file == "--help" or len(sys.argv) < 2 or len(sys.argv) >= 5:
        print("################################################")
        print("### Example 1: ")
        print(
            "### hm-compute_pareto example_scenarios/spatial/app_scenario.json"
        )
        print("### Example 2: ")
        print(
            "### hm-compute_pareto example_scenarios/spatial/app_scenario.json /path/to/input_data_file.csv /path/to/output_pareto_file.csv "
        )
        print("################################################")
        exit(1)

    compute(parameters_file, input_data_file, output_pareto_file)
Beispiel #3
0
def main():
    # This handles the logger. The standard setting is that HyperMapper always logs both on screen and on the log file.
    # In cases like the interactive mode we only want to log on the file.
    sys.stdout = Logger()

    list_of_pairs_of_files = []
    image_output_file = None
    parameters_file = ""
    if len(sys.argv) >= 2:
        parameters_file = sys.argv[1]
        if len(sys.argv) >= 3:
            i = 2
            try:
                image_output_file = sys.argv[i]
                filename, file_extension = os.path.splitext(
                    image_output_file
                )  # Test on the file to have a pdf extension
                if file_extension != ".pdf":
                    print(
                        "Error: file extension has to be a pdf. Given: %s"
                        % file_extension
                    )
                    exit(1)
                i += 1
            except:
                print("Error reading the image name file for arguments.")
                exit(1)

            while i < len(sys.argv):
                try:
                    list_of_pairs_of_files.append((sys.argv[i], sys.argv[i + 1]))
                except:
                    print(
                        "Error: wrong number of files. Files have to be in pairs of pareto and search."
                    )
                    exit(1)
                i += 2
    else:
        print("Error: more arguments needed.")

    if parameters_file == "--help" or len(sys.argv) < 2:
        print("################################################")
        print("### Example 1: ")
        print("### hm-plot-dse example_scenarios/spatial/BlackScholes_scenario.json")
        print("### Example 2: ")
        print(
            "### hm-plot-dse example_scenarios/spatial/BlackScholes_scenario.json /path/to/output/image.pdf file1_pareto file1_search file2_pareto file2_search file3_pareto file3_search"
        )
        print("################################################")
        exit(1)

    plot(parameters_file, list_of_pairs_of_files, image_output_file)
    print("End of the plot_dse script!")
Beispiel #4
0
        config = json.load(f)

    schema = json.load(resource_stream("hypermapper", "schema.json"))

    try:
        DefaultValidatingDraft4Validator = extend_with_default(Draft4Validator)
        DefaultValidatingDraft4Validator(schema).validate(config)
    except exceptions.ValidationError as ve:
        print("Failed to validate json:")
        print(ve)
        raise SystemExit

    run_directory = config["run_directory"]
    if run_directory == ".":
        run_directory = initial_directory
        config["run_directory"] = run_directory
    log_file = config["log_file"]
    if log_file == "hypermapper_logfile.log":
        log_file = deal_with_relative_and_absolute_path(
            run_directory, log_file)
    sys.stdout = Logger(log_file)

    main(config)

    try:
        os.chdir(hypermapper_pwd)
    except:
        pass

    print("### End of bayesian optimization script.")
Beispiel #5
0
        from pygmo import hypervolume
    except ImportError as e:
        warnings.warn(
            "Failed to import pygmo. You can still use HyperMapper but plot_hvi.py won't work. To use it, please install pygmo according to https://esa.github.io/pygmo2/install.html .",
            ImportWarning,
            2,
        )
    warnings.warn(
        "Using 'scripts/plot_hvi' is deprecated and it will be removed in the future. Use 'hypermapper/plot_hvi' instead.",
        DeprecationWarning,
        2,
    )

    # This handles the logger. The standard setting is that HyperMapper always logs both on screen and on the log file.
    # In cases like the interactive mode we only want to log on the file.
    sys.stdout = Logger()

    list_of_dirs = []
    parameters_file = ""
    if len(sys.argv) >= 4:
        parameters_file = sys.argv[1]
        output_hvi_file_name = sys.argv[2]
        for dir in sys.argv[3:]:
            list_of_dirs.append(dir)
    else:
        print("Error: more arguments needed.")

    if parameters_file == "--help" or len(sys.argv) < 4:
        print("################################################")
        print("### Example: ")
        print("### cd hypermapper")
Beispiel #6
0
def optimize(parameters_file, black_box_function=None):
    try:
        hypermapper_pwd = os.environ["PWD"]
        hypermapper_home = os.environ["HYPERMAPPER_HOME"]
        os.chdir(hypermapper_home)
        warnings.warn(
            "Found environment variable 'HYPERMAPPER_HOME', used to update the system path. Support might be discontinued in the future. Please make sure your installation is working without this environment variable, e.g., by installing with 'pip install hypermapper'.",
            DeprecationWarning,
            2,
        )
    except:
        hypermapper_pwd = "."

    if not parameters_file.endswith(".json"):
        _, file_extension = os.path.splitext(parameters_file)
        print(
            "Error: invalid file name. \nThe input file has to be a .json file not a %s"
            % file_extension)
        raise SystemExit
    with open(parameters_file, "r") as f:
        config = json.load(f)

    schema = json.load(resource_stream("hypermapper", "schema.json"))

    DefaultValidatingDraft4Validator = extend_with_default(Draft4Validator)
    try:
        DefaultValidatingDraft4Validator(schema).validate(config)
    except exceptions.ValidationError as ve:
        print("Failed to validate json:")
        print(ve)
        raise SystemExit

    # This handles the logger. The standard setting is that HyperMapper always logs both on screen and on the log file.
    # In cases like the client-server mode we only want to log on the file.
    run_directory = config["run_directory"]
    if run_directory == ".":
        run_directory = hypermapper_pwd
        config["run_directory"] = run_directory
    log_file = config["log_file"]
    log_file = deal_with_relative_and_absolute_path(run_directory, log_file)
    sys.stdout = Logger(log_file)

    optimization_method = config["optimization_method"]
    if config["profiling"]:
        profiling = Profiler(config)
        profiling.run()
    else:
        profiling = None

    if ((optimization_method == "random_scalarizations")
            or (optimization_method == "bayesian_optimization")
            or (optimization_method == "prior_guided_optimization")):
        data_array = bo.main(config,
                             black_box_function=black_box_function,
                             profiling=profiling)
    elif optimization_method == "local_search":
        data_array = local_search.main(config,
                                       black_box_function=black_box_function,
                                       profiling=profiling)
    elif optimization_method == "evolutionary_optimization":
        data_array = evolution.main(config,
                                    black_box_function=black_box_function,
                                    profiling=profiling)
    else:
        print("Unrecognized optimization method:", optimization_method)
        raise SystemExit
    if config["profiling"]:
        profiling.stop()

    try:
        os.chdir(hypermapper_pwd)
    except:
        pass

    # If mono-objective, compute the best point found
    objectives = config["optimization_objectives"]
    inputs = list(config["input_parameters"].keys())
    if len(objectives) == 1:
        explored_points = {}
        for parameter in inputs + objectives:
            explored_points[parameter] = data_array[parameter]
        objective = objectives[0]
        feasible_output = config["feasible_output"]
        if feasible_output["enable_feasible_predictor"]:
            feasible_parameter = feasible_output["name"]
            explored_points[feasible_parameter] = data_array[
                feasible_parameter]
            best_point = get_min_feasible_configurations(
                explored_points, 1, objective, feasible_parameter)
        else:
            best_point = get_min_configurations(explored_points, 1, objective)
        keys = ""
        best_point_string = ""
        for parameter in inputs + objectives:
            keys += f"{parameter},"
            best_point_string += f"{best_point[parameter][0]},"
        keys = keys[:-1]
        best_point_string = best_point_string[:-1]

    # If there is a best point, return it according the user's preference
    print_best = config["print_best"]
    if (print_best is not True) and (print_best is not False):
        if print_best != "auto":
            print(
                f"Warning: unrecognized option for print_best: {print_best}. Should be either 'auto' or a boolean."
            )
            print("Using default.")
        hypermapper_mode = config["hypermapper_mode"]
        print_best = False if hypermapper_mode == "client-server" else True

    if print_best:
        if len(objectives) == 1:
            sys.stdout.write_protocol("Best point found:\n")
            sys.stdout.write_protocol(f"{keys}\n")
            sys.stdout.write_protocol(f"{best_point_string}\n\n")
        else:
            if (
                    config["print_best"] is True
            ):  # If the user requested this, let them know it is not possible
                sys.stdout.write_protocol(
                    "\nMultiple objectives, there is no single best point.\n")
    else:
        if len(objectives) > 1:
            sys.stdout.write_to_logfile(
                "\nMultiple objectives, there is no single best point.\n")
        else:
            sys.stdout.write_to_logfile("Best point found:\n")
            sys.stdout.write_to_logfile(f"{keys}\n")
            sys.stdout.write_to_logfile(f"{best_point}\n\n")

    sys.stdout.write_protocol("End of HyperMapper\n")