echo.models.base package

class echo.models.base.Edge(vertices: tuple[Port, Port], tariff: list | None = None, uid: str = NOTHING, edge_name: str = NOTHING)

Bases: object

Edges are used to connect nodes. For an edge (x, y) where x and y are nodes, the edge value is equal to the flow from x->y plus the flow from y->x.

add_edge_to_model(model: EchoConcreteModel) → None

Applies edge constraint: port1 = -1 *port2 to a concrete model.

Parameters:

model – pyomo concrete model

Returns:

None

add_vertices(obj1: Port, obj2: Port) → None

Adds edge vertices (which are ports on nodes)

Parameters:
  • obj1 – port object

  • obj2 – port object

Returns:

None

edge_name: str
get_max_flow_along_edge(forwards: bool = True) → ndarray | list | float | int | None

Returns the max flow along an edge.

Parameters:

forwards – True is flow is from the 0th port to the 1st port in self.vertices. False if from 1st to the 0th. Defaults to True.

Returns:

The max flow across this edge.

Return type:

ConstraintValueType

tariff: list | None
uid: str
verify_edge() → None

Verifies that flows on ports associated with either end of an edge are the same.

Returns:

None

Raises:

ConfigurationError – If the flows on the ports at either end of an edge are not the same.

vertices: tuple[Port, Port]
class echo.models.base.Node(uid: str = NOTHING, node_name: str = NOTHING, ports: dict[str, Port] = NOTHING, objective: NumericExpression | float = 0)

Bases: object

Nodes are collections of one or more ports that can include non-trivial relationships between the ports, this allows transformations to be implemented.

add_node_to_model(model: EchoConcreteModel, profile: DataFrame) → None

Add this node to a concrete model.

Parameters:
  • model – The concrete model to add the node to.

  • profile – The data associated with this node.

add_objective(model: EchoConcreteModel) → None

Define objective/s and add to self.objective. To be overwritten.

Should be implemented as an expression that needs to be concatanated to self.objective. For example:

def add_objective(self, model: EchoConcreteModel) -> None:

total = 0

if self.regularise is True:
total += (
sum(

getattr(model, self.flow_value)[p, t] * getattr(model, self.flow_value)[p, t] for p in model.Expansion for t in model.Time

) * 0.0000001

)

self.objective += total

Parameters:

model – The model to add the objective/s to.

Returns:

None

add_port(name: str, port: Port) → None
add_ports_from_list(names: Iterable[str], port_type: type[Port], **kwargs) → None

Creates a set of ports (using port_type) and adds them to this Node. The ports will be constructed using port_type and the supplied kwargs

apply_node_constraints(model: EchoConcreteModel) → None

Apply constraints associated with this node to a concrete model.

Intended to be overridden in subclasses.

Define constraints as functions that returns a Constraint, EqualityExpression or InequalityExpression.

Define a subfunction of the form detailed in the example below, then apply with setattr. For example:

def apply_node_constraints(self, model: EchoConcreteModel) -> None:
def tellegen_node_rule(model: EchoConcreteModel, p: int, t:int) -> EqualityExpression:

a = 0 for port in node_ports.values():

a += getattr(model, port.port_name)[p, t]

return a == 0

node_ports = self.ports con_name = “reliability_con_” + self.node_name setattr(model, con_name, en.Constraint(model.Expansion, model.Time, rule=tellegen_node_rule))

Parameters:

model – The model to add the constraints to.

Returns:

None

get_port(port_name: str) → Port | None

Returns the Port object with the name port_name.

Parameters:

port_name – The name of the Port.

Returns:

The port object with the name port_name, or None if the port isn’t found

Return type:

Port

get_port_name_to_port_dict_name_map() → dict[str, str]

Map the port name key in self.ports to the corresponding Port.port_name.

Returns:

property inflow: str
node_name: str
num_ports() → int

Returns the number of ports associated with this node.

Returns:

The number of ports for this node.

objective: NumericExpression | float
ports: dict[str, Port]
uid: str
verify_node() → None

Checks there is at least one port associated with this node.

Raises:

ConfigurationError – If there are no ports present on this node.

class echo.models.base.OptimisationGraph(nodes: dict[str, Node] = NOTHING, edges: dict[tuple[str, str], Edge] = NOTHING, paths: dict[tuple, Path] = NOTHING)

Bases: object

add_edge(edge: Edge, nodes: tuple[str, str] | None = None) → None
add_edge_obj(edge: list[Edge] | Edge) → None
add_edges_from(edge: list[Edge]) → None
add_node(node: Node) → None
add_node_obj(node: list | Node) → None

Adds either a single node or list of nodes to graph

add_nodes(nodes: Node | Iterable[Node]) → None

Adds either a single node or list of nodes to graph

add_nodes_from(nodes: list[Node]) → None

Adds a list of nodes to the graph.

apply_path_constraints(model: EchoConcreteModel) → None

Applies path tracing constraints to model

connect_ports_and_create_edge(port1: Port, port2: Port, edge_name: str | None = None) → None

Creates an edge between port1 and port2 and adds it to the graph

convert_to_nx() → Graph

Converts the OptimisationGraph to a networkx graph, where nx nodes are echo node names and nx edges are nx node pairs

create_path_objects(sources: list[Node] | list[str], sinks: list[Node] | list[str], path_unit: Units = Units.KW, regularise: bool = False) → None

Creates path objects according to source/sink lists provided.

delete_edge(edge_nodes: tuple[str, str]) → None
delete_node(node_name: str) → None
draw_on_axes(axes: Axes, with_labels: bool = False, labels: dict[str, str] | None = None, **kwargs) → None

Draws the network on a matplotlib plot

Parameters:
  • axes (matplotlib.axes.Axes) – The (sub-)plot on which to draw the network

  • with_labels (bool) – Set to True to draw labels on the nodes. Defaults to False. Uses the node’s names as the label.

  • labels (dict) – Optional way of supplying node labels as a dictionary of labels (strings) keyed by node. Default = None.

  • **kwargs – Optional keyword arguments for customising the drawing of the network. See networkx.drawing.nx_pylab.draw_networkx documentation for more information.

Examples

The following example shows how to draw an already created OptimisationGraph with the name network

>>> import matplotlib.pyplot as plt
>>> network_figure = plt.figure()
>>> network_axes = network_figure.add_subplot()
>>> network.draw_on_axes(axes=network_axes, with_labels=True)
>>> plt.show()
edge_list() → list[str]
property edge_obj: dict[tuple[str, str], Edge]
edges: dict[tuple[str, str], Edge]
get_edge(nodes: tuple[str, str]) → Edge | None

Retrieves the edge that connects a tuple of nodes, if an edge exists.

get_node(node_name: str) → Node | None

Returns node object given node name

get_path(path_vertices: list[Node] | list[str]) → Path

Looks up a path using a list of path vertices (nodes, or node names).

get_port_names_from_edges() → set[str]
get_port_names_from_nodes(skip_dangling_ports: bool = False) → set[str]
get_ports_on_edge_from_nodes(node1: str, node2: str) → tuple[Port, Port] | None

Returns the ports that are on the edge from node1 to node2.

get_sources_and_sinks() → set[Node]

Returns a set that contains all source and sink nodes.

inject_data_into_ev(node_name: str, available: ndarray | list | str | None = None, usage: ndarray | list | str | None = None, initial_state_of_charge: float | None = None, interval_duration: int | None = None) → None

Injects stateful data into an EV node in an OptimisationGraph.

This is a convenience method to be used for networks constructed by MESNetwork.to_echo(). It will update all data in the relevant node and edges through re-initialisation of nodes, and delete and recreated of the edge.

Parameters:
  • node_name – The node_name of the EV to have stateful data injected.

  • available – The avability data of the EV to be injected.

  • usage – The usage data of the EV to be injected.

  • initial_state_of_charge – The initial state of charge of the EV to be injected.

  • interval_duration – The interval duration of the EV to be injected.

Returns:

None

lookup_node_names_from_port(port: Port) → str

Returns node name of the node that a specified port belongs to, if the port belongs to a node.

node_name_list() → list[str]
property node_obj: dict[str, Node]
nodes: dict[str, Node]
paths: dict[tuple, Path]
print_port_names() → None

Prints port name-uid pairs, useful for debugging infeasible optimisation

rebuild_all_edges() → None

Deletes and rebuilds all edges. Deprecated and replaced with a pass (i.e. no operation)

To be used after stateful data has been injected into ports. Injecting stateful data into ports alters the port. The new altered port correctly remains associated with the node, but does not remain associated with any edges this port may be associated with. This method builds new edges for each old edge based on the port name associated with each end of the edge. The original edge is deleted.

Returns:

None

split_graph_on_edge(node1: str, node2: str) → tuple[OptimisationGraph, OptimisationGraph]

Splits a graph between node1 and node 2, and returns two echo optimisation graphs. The ports on the split edge are kept in the two new graphs.

to_cytoscape_json() → str

Converts the optimisation graph to json that can be read by cytoscape (https://js.cytoscape.org/)

update_node(node_name: str, **kwargs) → None
verify_graph() → None

Checks that the graph is connected (all nodes have at least one edge), and warns if there are unconnected ports

verify_paths() → None

Verifies that our paths meet the assumptions required to correctly do flow tracing.

class echo.models.base.Path(vertices: list, edge_ports: list[tuple[Port, Port]] = NOTHING, units: Units = Units.KW, regularise: bool = False, objective: NumericExpression | float = 0, contingency_neg: str | None = None, contingency_pos: str | None = None, path_tariff: str | None = None, slack: str | None = None, uid: str = NOTHING, path_name: str = NOTHING, flow_value: str = NOTHING)

Bases: object

A path is a sequence of distinct vertices (nodes).

add_objective(model: EchoConcreteModel) → None
add_path_to_model(model: EchoConcreteModel) → None
add_vertices(vertex_list: list) → None
contingency_neg: str | None
contingency_pos: str | None
edge_ports: list[tuple[Port, Port]]
flow_value: str
objective: NumericExpression | float
path_name: str
path_tariff: str | None
regularise: bool
slack: str | None
uid: str
units: Units
vertices: list
class echo.models.base.Port(uid: str = NOTHING, port_name: str = NOTHING, units: Units = Units.NA, initial_value: dict[tuple[int, int], float] | float = 0, initial_value_ref: str | None = None, initial_value_scaling: int | None = None, flow_type: OptimisationType = OptimisationType.NA, flows: Flows = Flows.NA, import_constraint: FlowConstraint = FlowConstraint.NA, import_constraint_value: ndarray | list | float | int | None = None, export_constraint: FlowConstraint = FlowConstraint.NA, export_constraint_value: ndarray | list | float | int | None = None, active_periods: dict[tuple[int, int], Any] | None = None, slack: bool = False, objective: NumericExpression | float = 0, allow_dangling_port: bool = False)

Bases: object

active_periods: dict[tuple[int, int], Any] | None
add_objective(model: EchoConcreteModel) → None

Populates the port attribute ‘objectives’ with any pyomo expressions that are needed :param model: pyomo concrete model

add_port_to_model(model: EchoConcreteModel, profile: DataFrame | None) → None

Creates pyomo vars, params, and constraints for the port.

allow_dangling_port: bool
constrain_pos_neg(model: EchoConcreteModel) → None

Applies a mixed integer constraint that splits a port var into positive and negative components

property export_con_val: str
export_constraint: FlowConstraint
export_constraint_value: ndarray | list | float | int | None
property export_slack: str
property export_slack_max: str
static factory_pos_neg_flows(var_name: str, pos_name: str, neg_name: str) → Callable
flow_type: OptimisationType
flows: Flows
property import_con_val: str
import_constraint: FlowConstraint
import_constraint_value: ndarray | list | float | int | None
property import_slack: str
property import_slack_max: str
initial_value: dict[tuple[int, int], float] | float
initial_value_ref: str | None
initial_value_scaling: int | None
property is_pos: str
property neg: str
objective: NumericExpression | float
port_name: str
property pos: str
process_initial_value(initial_val: dict[tuple[int, int], int | float] | list[int | float] | ndarray | str, expansion_periods: int = 1, time_periods: int | None = None) → None
set_active_periods_from_array(array: list[bool] | list[int], expansion_periods: int = 1, time_periods: int | None = None) → None

Sets port active periods

Parameters:
  • array – array, list of active periods as bool values

  • expansion_periods – number of expansion periods (int)

set_flow_constraints(max_import: ndarray | list | float | int | None, max_export: ndarray | list | float | int | None, slack: bool | None = False) → None

Sets the values of port flow constraints.

Parameters:
  • max_import – max allowable import into port (float, array, or None)

  • max_export – max allowable export out of port (float, array, or None)

  • slack – bool, whether we want to allow slack in the constraint

set_initial_value(initial_value: dict) → None

Sets initial port value which will be used to initialise the pyomo var/param

Parameters:

initial_value – dict of initial values

Returns:

None

set_initial_value_from_array(array: list[float] | ndarray, expansion_periods: int = 1, time_periods: int | None = None) → None

Sets initial port value which is used to initialise the pyomo var/param

Parameters:
  • array – array, list of initial values. Should have either: length = time_periods, or length = time_periods*expansion_periods

  • time_periods – int, optional number of time periods. If=None, assume that time_periods = len(array)

  • expansion_periods – number of expansion periods

set_initial_value_from_timeseriesdata(time_series_data: TimeSeriesData) → None
slack: bool
uid: str
units: Units
verify_port() → None

Used to verify that a port has been set up appropriately

class echo.models.base.Transform(uid: str = NOTHING, lhs_terms: list[TransformTerm] = NOTHING, rhs: NumericExpression | float = 0)

Bases: object

An object for carrying a generic linear node transformation.

property lhs: list[TransformTerm]

lhs is an alias for lhs_terms

lhs_terms: list[TransformTerm]
rhs: NumericExpression | float
property transform_name: str
uid: str
class echo.models.base.TransformNode(uid: str = NOTHING, node_name: str = NOTHING, ports: dict[str, Port] = NOTHING, objective: NumericExpression | float = 0, transformations: dict[str, Transform] = NOTHING)

Bases: Node

Implements node constraints using Transforms

add_emission_transformation(emitting_port: Port, carbon_port: Port, emission_factor: float) → None

Creates an emission transformation and adds to the node.

Parameters:
  • emitting_port – port object that generates emissions when exporting (when negative)

  • carbon_port – port object that represents carbon flows out of the node

  • emission_factor – a ratio = emissions generated/emitting unit generated (float), or an array of values

Returns:

None

add_input_output_transformation(input_port: Port, output_port: Port, input_weight: float) → None

Adds an input/output transformation to this node.

Parameters:
  • input_port – The input port for the transformation

  • output_port – The output port for the transformation

  • input_weight – The weighting for the TransformTerm of the input port.

Returns:

None

add_transformation(transformation_obj: Transform) → None

Adds a transformation object to a node.

Parameters:

transformation_obj – The Transform to be added to the node.

Returns:

None

apply_node_constraints(model: EchoConcreteModel) → None

Add the constraints assoicated with this node to a concrete model.

Parameters:

model – The concrete model the constraints associated with this node will be added to.

transformations: dict[str, Transform]
verify_node() → None

Checks if the node has ports (super), and if the node has transformations.

Returns:

None

Raises:

ConfigurationError – If no transformations are associated with this node.

class echo.models.base.TransformTerm(var: 'Port', rule: 'TransformRule', weight: 'TimeExpandableType')

Bases: object

rule: TransformRule
var: Port
weight: float | list[float] | list[list[float]] | ndarray

Submodules