[Python] Why does the build_model-function in mpi-sppy not work?

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    Could someone please help me understand the error in my code? I based it on the farmer example from the link provided, but I’m unable to figure out why the model isn’t being created. I have noted the error message in the last line.

    Thanks in advance!

    https://mpi-sppy.readthedocs.io/en/latest/examples.html

    import numpy as np

    sims = 1000

    mu_1, sigma_1 = 0.83, 1.462
    w_1 = np.random.normal(mu_1, sigma_1, sims)

    mu_2, sigma_2 = 0.5, 1.384
    w_2 = np.random.normal(mu_2, sigma_2, sims)

    mu_3, sigma_3 = 0.5, 1.258
    w_3 = np.random.normal(mu_3, sigma_3, sims)

    w = [w_1, w_2, w_3]

    Szenario = {}
    for k in range(sims):
    j=k+1
    Szenario[f's_{j}'] = [w_i[k] for w_i in w]

    import mpisppy.utils.sputils as sputils

    def scenario_creator(scenario_name):
    if scenario_name in Szenario:
    Rendite = Szenario[scenario_name]
    else:
    raise ValueError("Unrecognized scenario name")

    model = build_model(Rendite)
    sputils.attach_root_node(model, model.Stufe_1, [model.X])
    model._mpisppy_probability = 1.0 / sims
    return model

    from mpisppy.opt.ef import ExtensiveForm

    options = {
    "solver": "ipopt",
    "tol": 1e-7,
    "max_iter": 100,
    "tee": True
    }
    all_scenario_names = list(Szenario.keys())
    ef = ExtensiveForm(options, all_scenario_names, scenario_creator)
    results = ef.solve_extensive_form()

    objval = ef.get_objective_value()
    print(f"{objval:.10f}")


    Error:

    NameError: name 'build_model' is not defined

    Continue reading...

Compartilhe esta Página