Example #1
0
    def load(
        self,
        problem_configuration: FASTOADProblemConfigurator,
    ):
        """
        Loads the FAST-OAD problem and stores its data.

        :param problem_configuration: the FASTOADProblem instance.
        """

        self.problem_configuration = problem_configuration

        if pth.isfile(self.problem_configuration.input_file_path):
            input_variables = DataFile(
                self.problem_configuration.input_file_path)
        else:
            # TODO: generate the input file by default ?
            raise FastMissingFile(
                "Please generate input file before using the optimization viewer"
            )

        if pth.isfile(self.problem_configuration.output_file_path):
            output_variables = DataFile(
                self.problem_configuration.output_file_path)
        else:
            problem = self.problem_configuration.get_problem()
            problem.setup()
            output_variables = VariableList.from_problem(problem)

        optimization_variables = VariableList()
        opt_def = problem_configuration.get_optimization_definition()
        # Design Variables
        if KEY_DESIGN_VARIABLES in opt_def:
            for name, design_var in opt_def[KEY_DESIGN_VARIABLES].items():
                metadata = {
                    "type": "design_var",
                    "initial_value": input_variables[name].value,
                    "lower": design_var.get("lower"),
                    "value": output_variables[name].value,
                    "upper": design_var.get("upper"),
                    "units": input_variables[name].units,
                    "desc": input_variables[name].description,
                }
                optimization_variables[name] = metadata

        # Constraints
        if KEY_CONSTRAINTS in opt_def:
            for name, constr in opt_def[KEY_CONSTRAINTS].items():
                metadata = {
                    "type": "constraint",
                    "initial_value": None,
                    "lower": constr.get("lower"),
                    "value": output_variables[name].value,
                    "upper": constr.get("upper"),
                    "units": output_variables[name].units,
                    "desc": output_variables[name].description,
                }
                optimization_variables[name] = metadata

        # Objectives
        if KEY_OBJECTIVE in opt_def:
            for name in opt_def[KEY_OBJECTIVE]:
                metadata = {
                    "type": "objective",
                    "initial_value": None,
                    "lower": None,
                    "value": output_variables[name].value,
                    "upper": None,
                    "units": output_variables[name].units,
                    "desc": output_variables[name].description,
                }
                optimization_variables[name] = metadata

        self.load_variables(optimization_variables)
    def load(
        self,
        problem_configuration: FASTOADProblemConfigurator,
    ):
        """
        Loads the FAST-OAD problem and stores its data.

        :param problem_configuration: the FASTOADProblem instance.
        :param file_formatter: the formatter that defines file format. If not provided,
               default format will be assumed.
        """

        self.problem_configuration = problem_configuration
        problem = self.problem_configuration.get_problem()
        if pth.isfile(problem.input_file_path):
            input_variables = VariableIO(problem.input_file_path).read()
        else:
            # TODO: generate the input file by default ?
            raise FastMissingFile(
                "Please generate input file before using the optimization viewer"
            )

        if pth.isfile(problem.output_file_path):
            output_variables = VariableIO(problem.output_file_path).read()
        else:
            output_variables = VariableList.from_problem(problem)

        optimization_variables = VariableList()
        opt_def = problem_configuration.get_optimization_definition()
        # Design Variables
        if "design_var" in opt_def:
            for name, design_var in opt_def["design_var"].items():
                initial_value = input_variables[name].value
                if "lower" in design_var:
                    lower = design_var["lower"]
                else:
                    lower = None
                value = output_variables[name].value
                if "upper" in design_var:
                    upper = design_var["upper"]
                else:
                    upper = None
                units = input_variables[name].units
                desc = input_variables[name].description
                metadata = {
                    "type": "design_var",
                    "initial_value": initial_value,
                    "lower": lower,
                    "value": value,
                    "upper": upper,
                    "units": units,
                    "desc": desc,
                }
                optimization_variables[name] = metadata

        # Constraints
        if "constraint" in opt_def:
            for name, constr in opt_def["constraint"].items():
                if "lower" in constr:
                    lower = constr["lower"]
                else:
                    lower = None
                value = output_variables[name].value
                if "upper" in constr:
                    upper = constr["upper"]
                else:
                    upper = None
                units = output_variables[name].units
                desc = output_variables[name].description
                metadata = {
                    "type": "constraint",
                    "initial_value": None,
                    "lower": lower,
                    "value": value,
                    "upper": upper,
                    "units": units,
                    "desc": desc,
                }
                optimization_variables[name] = metadata

        # Objectives
        if "objective" in opt_def:
            for name, obj in opt_def["objective"].items():
                value = output_variables[name].value
                units = output_variables[name].units
                desc = output_variables[name].description
                metadata = {
                    "type": "objective",
                    "initial_value": None,
                    "lower": None,
                    "value": value,
                    "upper": None,
                    "units": units,
                    "desc": desc,
                }
                optimization_variables[name] = metadata

        self.load_variables(optimization_variables)