def test_set_optimization_definition(cleanup):
    """
    Tests the modification of the optimization definition in the configuration file
    """
    for extension in ["toml", "yml"]:
        clear_openmdao_registry()
        reference_file = pth.join(DATA_FOLDER_PATH, "valid_sellar.%s" % extension)
        editable_file = pth.join(RESULTS_FOLDER_PATH, "editable_valid_sellar.%s" % extension)

        conf = FASTOADProblemConfigurator(reference_file)

        optimization_def = {
            "design_variables": {
                "x": {"name": "x", "lower": 0, "upper": 20},
                "z": {"name": "z", "lower": 0, "upper": 10},
            },
            "constraints": {
                "gg1": {"name": "gg1", "upper": 10},
                "gg2": {"name": "gg2", "upper": 0},
            },
            "objective": {"f": {"name": "f"}},
        }

        optimization_conf = {
            "design_variables": [
                {"name": "x", "lower": 0, "upper": 20},
                {"name": "z", "lower": 0, "upper": 10},
            ],
            "constraints": [{"name": "gg1", "upper": 10}, {"name": "gg2", "upper": 0}],
            "objective": [{"name": "f"}],
        }

        read = tomlkit.loads if extension == "toml" else YAML(typ="safe").load

        with open(reference_file, "r") as file:
            d = file.read()
            conf_dict = read(d)
        conf_dict_opt = conf_dict["optimization"]
        # Should be different
        assert optimization_conf != conf_dict_opt

        conf.set_optimization_definition(optimization_def)
        conf.save(editable_file)
        with open(editable_file, "r") as file:
            d = file.read()
            conf_dict = read(d)
        conf_dict_opt = conf_dict["optimization"]
        # Should be equal
        assert optimization_conf == conf_dict_opt
Пример #2
0
def test_set_optimization_definition(cleanup):
    """
    Tests the modification of the optimization definition in the .toml
    configuration file
    """
    reference_file = pth.join(DATA_FOLDER_PATH, "valid_sellar.toml")
    editable_file = pth.join(RESULTS_FOLDER_PATH, "editable_valid_sellar.toml")

    # copy(reference_file, editable_file)

    conf = FASTOADProblemConfigurator(reference_file)

    optimization_def = {
        "design_var": {
            "x": {
                "name": "x",
                "lower": 0,
                "upper": 20
            },
            "z": {
                "name": "z",
                "lower": 0,
                "upper": 10
            },
        },
        "constraint": {
            "gg1": {
                "name": "gg1",
                "upper": 10
            },
            "gg2": {
                "name": "gg2",
                "upper": 0
            },
        },
        "objective": {
            "f": {
                "name": "f"
            }
        },
    }

    optimization_conf = {
        "design_var": [
            {
                "name": "x",
                "lower": 0,
                "upper": 20
            },
            {
                "name": "z",
                "lower": 0,
                "upper": 10
            },
        ],
        "constraint": [{
            "name": "gg1",
            "upper": 10
        }, {
            "name": "gg2",
            "upper": 0
        }],
        "objective": [{
            "name": "f"
        }],
    }

    with open(reference_file, "r") as file:
        d = file.read()
        conf_dict = tomlkit.loads(d)
    conf_dict_opt = conf_dict["optimization"]
    # Should be different
    assert optimization_conf != conf_dict_opt

    conf.set_optimization_definition(optimization_def)
    conf.save(editable_file)
    with open(editable_file, "r") as file:
        d = file.read()
        conf_dict = tomlkit.loads(d)
    conf_dict_opt = conf_dict["optimization"]
    # Should be equal
    assert optimization_conf == conf_dict_opt