echo.optimiser module

class echo.optimiser.OptimisationResult(scenario_settings: ScenarioSettings, objective: NumericExpression | None, model: EchoConcreteModel, objective_set: ObjectiveSet | None, graph: OptimisationGraph, opt_status: SolverStatus, termination_condition: TerminationCondition, model_attribute_tracker: AttributeTracker | None = None)

Bases: object

Describes the set of results from a successful optimisation run. It will provide a set of utilities for accessing result data for the scenario that created this result

df() → DataFrame

Extract all vars from the solution as a dataframe

df_by_node() → DataFrame

Returns a df of results by node, using the port names given in each node’s ‘ports’ dict

df_by_port() → DataFrame

Returns a df of results by port, using port names given in each node’s ‘ports’ dict

df_objective_by_port(index: set | None = None) → DataFrame

Return the value of each objective assigned to each port.

get_single_objective_total_value(objective_obj: Objective) → float

Returns the value of a single objective.

get_total_objective_at_port(port_obj: Port) → float

Sums all objectives that take the defined port as their component.

get_total_objective_value() → float

Returns the value of the objective function.

graph: OptimisationGraph
model: EchoConcreteModel
model_attribute_tracker: AttributeTracker | None = None
node_values(node: Node, expansion_period: int = 0) → dict[str, ndarray]

Returns all values of all ports in a specified node for a single specified expansion period.

objective: NumericExpression | None
objective_set: ObjectiveSet | None
opt_status: SolverStatus
scenario_settings: ScenarioSettings
termination_condition: TerminationCondition
values(variable_name: str, expansion: int = 0) → ndarray

Returns the value of a single specified variable during a single specified expansion period.

echo.optimiser.build_model_and_objective(graph: OptimisationGraph, scenario_settings: ScenarioSettings, small_m: float, big_m: int, profile: DataFrame | None, objective_set: ObjectiveSet | None) → tuple[EchoConcreteModel, NumericExpression | None, AttributeTracker]

Builds an EchoConcreteModel for a particular Echo Scenario definition and a related objective to optimise against the model

echo.optimiser.logged_stdout(logfile: str | None, mode: str = 'w') → Generator[None]

Context manager that while held open will redirect all stdout to logfile. If the logfile is None then no redirection will occur

echo.optimiser.optimise(scenario_settings: ScenarioSettings, engine_settings: EngineSettings, graph: OptimisationGraph, objective_set: ObjectiveSet | None = None, profile: DataFrame | None = None, verbose: bool = False, show_solver_output: bool = False, logfile: str | None = None, time_limit: int | None = None, mip_gap_relative: float | None = None, mip_gap_absolute: float | None = None, acceptable_conditions: Collection[TerminationCondition] = {TerminationCondition.feasible, TerminationCondition.globallyOptimal, TerminationCondition.locallyOptimal, TerminationCondition.maxIterations, TerminationCondition.maxTimeLimit, TerminationCondition.optimal}) → OptimisationResult

Runs the optimiser with the specified settings. Returns an OptimisationResult that can be queried using the supplied graph.

Will attempt to validate the graph and will raise exceptions if it appears invalid / unsolvable

Parameters:
  • scenario_settings – The settings that describe the optimisation problem.

  • engine_settings – Settings of the engine to be used for this optimisation.

  • graph – The OptimisationGraph that describes the optimisation problem.

  • objective_set – The set of objective functions for this optimisation. Defaults to None.

  • profile – The pandas dataframe containing node and objective timeseries data. Defaults to None.

  • verbose – If set to True the solver will operate in verbose mode and print additional output to stdout. Defaults to False.

  • show_solver_output – If set to True, the optimiser will pass through its logs. Defaults to False.

  • logfile – If set to a file path, the stdout will be redirected to this file (for the duration of this run).

  • time_limit – Optional time_limit in seconds after which the solver should return a solution if other termination conditions have not already been reached.

  • mip_gap_relative – The relative gap between known mixed integer problem solutions and the linearised version. Optional. Defaults to None.

  • mip_gap_absolute – The absolute gap between known mixed integer problem solutions and the linearised version. Optional. Defaults to None.

  • acceptable_conditions – OptimiserResultError will be raised if the pyomo termination condition is not in this set. Defaults to DEFAULT_ACCEPTABLE_TERMINATION_CONDITIONS.

Returns:

A collection of outputs from the optimisation, including some relevant inputs, namely

scenario_settings, graph an objective_set.

Return type:

OptimisationResult

echo.optimiser.validate_network_graph(graph: OptimisationGraph) → None

Validates that a pyomo model can be built from the provided network graph. Checks for: - name consistency between objects (e.g. node.node_name) and graph nodes - floating nodes that have no edge connecting them to another node