Ejemplo n.º 1
0
def set_algorithm(name, no_obj, setup):
    """
    Text.

    Args:
        name (str): Name of the optimization algorithm.
        n_obj (int): Number of objectives.
        setup (dict): Optimization setup parameters.

    Returns:
        algorithm (): Optization algorithm object.
    """
    if name == "default":
        if no_obj == 1:
            name = "ga"
        elif no_obj > 1:
            name = "nsga3"

    # Get settings
    setup_gen = load_json(
        os.path.join(settings["root"], "app", "config", "opticonf", "general"))
    setup_alg = load_json(
        os.path.join(settings["root"], "app", "config", "opticonf", name))
    algorithm_args = {}

    # Get optimization settings objects
    algorithm_args["sampling"] = get_sampling(setup_gen["sampling"])

    if "operators" in setup:
        for operator in setup["operators"]:
            algorithm_args[operator] = get_operator(operator, setup)

    # Get reference directions
    if name == "nsga3":
        algorithm_args["ref_dirs"] = get_reference_directions(
            "energy", no_obj, setup_alg["ref_dirs_coef"] * no_obj)

    # Modify population
    if "n_offsprings" in setup:
        algorithm_args["n_offsprings"] = setup["n_offsprings"]
    if "pop_size" in setup:
        algorithm_args["pop_size"] = setup["pop_size"]

    algorithm = get_algorithm(name,
                              eliminate_duplicates=True,
                              **algorithm_args)

    return algorithm
Ejemplo n.º 2
0
    def __init__(self):
        super().__init__()
        
        self.ansys_project_folder = settings["data"]["project_folder"]
        self.input_param_name = settings["data"]["input_names"]

        self.setup = load_json(os.path.join(settings["root"],"app","config","dataconf","ansys"))
        self.valid_licences = [name for name in self.setup["licenses"].values()]
Ejemplo n.º 3
0
    def save(self):
        """
        Saves the surrogate.
        """
        if self.name.startswith("ann"):
            self.surrogate.save()
        else:
            pass


##            dump_object("meta",self.surrogate)

        status = load_json(os.path.join(settings["folder"], "status"))
        to_update = {
            "surrogate_trained": True,
            "range_out": self.surrogate.range_out.tolist()
        }
        status.update(to_update)
        dump_json(os.path.join(settings["folder"], "status"), status)
Ejemplo n.º 4
0
def reload_info():
    """
    Get information about the problem.

    Returns:
        range_in (np.array): Input parameter allowable ranges.
        dim_in (int): Number of input dimensions.
        dim_out (int): Number of output dimensions.
        n_constr (int): Number of constraints.
    """
    status = load_json(os.path.join(settings["folder"], "status"))
    if status["surrogate_trained"]:
        range_in, dim_in, dim_out, n_const = np.array(
            status["range_in"]
        ), status["dim_in"], status["dim_out"], status["n_const"]
    else:
        raise Exception("There is no surrogate to load")

    return range_in, dim_in, dim_out, n_const
Ejemplo n.º 5
0
    def reload(self):
        """
        Reloads the surrogate.
        """
        status = load_json(os.path.join(settings["folder"], "status"))

        if "ann" in os.listdir(os.path.join(settings["folder"], "logs")):
            from metamod.ANN import ANN  ############################################
            from tensorflow.keras.models import load_model as load_keras_model  ############################################

            interp = ANN()
            interp.nx = status["dim_in"]
            interp.ny = status["dim_out"]
            interp.model = load_keras_model(
                os.path.join(settings["folder"], "logs", "ann"))
            interp.range_out = np.array(status["range_out"])
            interp.options["print_global"] = False
            self.surrogate = interp
        else:
            self.surrogate = load_object("meta")[0]
Ejemplo n.º 6
0
def set_surrogate(name, dim_in, dim_out):
    """
    Select the desired surrogate model.

    Arguments:
        name (str): Name of the surrogate.
        dim_in (int): Number of input dimensions.
        dim_out (int): Number of output dimensions.

    Returns:
        surrogate (object): Initialized surrogate model.

    Raises:
        NameError: If the surrogate is not defined-
    """
    # Obtain default settings
    setup = load_json(
        os.path.join(settings["root"], "app", "config", "metaconf", name))

    # Optionaly update with setting specified by the input file
    if "setup" in settings["surrogate"].keys():
        setup.update(settings["surrogate"]["setup"])

    # If the ANN surrogate is used, add it to available options
    if name == "ann":
        from metamod.ANN_tf import ANN  ### import only when actually used, its slow due to tensorflow
        available.update({"ann": ANN})
        setup.update({"dims": (dim_in, dim_out)})

    if name == "ann_pt":
        setup.update({"dims": (dim_in, dim_out)})

    # Load the actual surrogate
    surrogate = available[name](**setup)

    # Disable printing during using
    surrogate.options["print_global"] = False

    return surrogate
Ejemplo n.º 7
0
        Arguments
        ---------
        nt : int
            Number of points requested.

        Returns
        -------
        ndarray[nt, nx]
            The sampling locations in the input space.
        """
        xlimits = self.options["xlimits"]
        nx = xlimits.shape[0]

        x = halton(nx, nt)

        return x


adaptive_methods = load_json(
    os.path.join(settings["root"], "app", "config", "dataconf", "adaptive"))

sample_bounds = (-1, 1)

samplings = {
    "halton": Halton,
    "grid": FullFactorial,
    "lhs": LHS,
    "random": Random
}