Skip to content

spec: editing model.parameters changes evaluate() but not the built model #1069

Closed

Description

@FabianHofmann

Note

The following content was generated by AI.

Found in the API review for #1068 (stacked on #958); linopy.spec is unreleased.

Describe the feature you'd like to see

A spec-built model keeps its data in model.parameters. Editing that data changes what linopy.spec.evaluate returns, but the built variables, constraints and objective keep the coefficients read at build time. The two views silently disagree, and there is no way to update data and re-solve without rebuilding the whole model with Model.from_spec.

Minimal reproducible example

import warnings

import pandas as pd

import linopy
from linopy.spec import evaluate

warnings.simplefilter("ignore", linopy.EvolvingAPIWarning)

SPEC = """
dimensions:
  generator: {}
parameters:
  cost: {dims: [generator]}
  load: {dims: []}
variables:
  p: {dims: [generator], bounds: {lower: 0}}
constraints:
  balance: {dims: [], expression: "sum(p, over=generator) == load"}
expressions:
  unit_cost: {dims: [generator], expression: "cost * 1"}
objective: {sense: minimize, expression: "sum(p * cost)"}
"""
sources = {"generator": ["a", "b"], "cost": pd.Series({"a": 1.0, "b": 3.0}), "load": 10.0}
m = linopy.Model.from_spec(SPEC, sources)
m.solve("highs", output_flag=False)
print("before:", m.objective.value, evaluate(SPEC, m, "unit_cost")["unit_cost"].values)

m.parameters["cost"] = m.parameters["cost"] * 10
m.solve("highs", output_flag=False)
print("after editing m.parameters['cost'] * 10:")
print("  evaluate unit_cost:", evaluate(SPEC, m, "unit_cost")["unit_cost"].values)
print("  objective value:   ", m.objective.value)
print("  objective coeffs:  ", m.objective.expression.coeffs.values)

Output on spec-simplification:

before: 10.0 [1. 3.]
after editing m.parameters['cost'] * 10:
  evaluate unit_cost: [10. 30.]
  objective value:    10.0
  objective coeffs:   [1. 3.]

Expected

Either the edit reaches the model (objective coefficients [10, 30], objective 100), or model.parameters refuses the edit, so evaluate cannot report numbers the solved model never saw.

Proposal

Add an explicit linopy.spec.update(spec, model, sources) that rewrites the affected coefficients, bounds and right-hand sides in place, and make the stored parameters read-only otherwise.

Activity

  1. added
    enhancementNew feature or request
    math-specYAML/text math spec: schema, from_spec/add_spec, operators, expr container
    on Oct 27, 2026
  2. FabianHofmann commented on Oct 27, 2026

    @FabianHofmann
    CollaboratorAuthor

    Note

    The following content was generated by AI.

    Resolved by the new design in #958: Model.from_spec(spec, sources, store=False) writes data to model.parameters only with store=True. Edits to stored parameters take effect on rebuild via Model.from_spec(spec, m.parameters, store=True). There is no live update. A test pins edit followed by rebuild, also after a netcdf round trip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestmath-specYAML/text math spec: schema, from_spec/add_spec, operators, expr container

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions