# -*- coding: utf-8 -*- import pandas as pd import networkx as nx from matplotlib import pyplot as plt # solph imports from oemof.solph.exnet import * from oemof.solph import * from oemof.outputlib import processing, views from oemof.graph import create_nx_graph from collections import namedtuple from oemof.solph.exnet import * # from oemof.network_graph import * from oemof.graph import create_nx_graph def draw_graph(grph, edge_labels=True, node_color='#AFAFAF', edge_color='#CFCFCF', plot=True, node_size=2000, with_labels=True, arrows=True, layout='neato'): """ Parameters ---------- grph : networkxGraph A graph to draw. edge_labels : boolean Use nominal values of flow as edge label node_color : dict or string Hex color code oder matplotlib color for each node. If string, all colors are the same. edge_color : string Hex color code oder matplotlib color for edge color. plot : boolean Show matplotlib plot. node_size : integer Size of nodes. with_labels : boolean Draw node labels. arrows : boolean Draw arrows on directed edges. Works only if an optimization_model has been passed. layout : string networkx graph layout, one of: neato, dot, twopi, circo, fdp, sfdp. """ if type(node_color) is dict: node_color = [node_color.get(g, '#AFAFAF') for g in grph.nodes()] # set drawing options options = { 'prog': 'dot', 'with_labels': with_labels, 'node_color': node_color, 'edge_color': edge_color, 'node_size': node_size, 'arrows': arrows, 'font_size': 6 } # try to use pygraphviz for graph layout try: import pygraphviz pos = nx.drawing.nx_agraph.graphviz_layout(grph, prog=layout) except ImportError: logging.error('Module pygraphviz not found, I won\'t plot the graph.') return # draw graph nx.draw(grph, pos=pos, **options) # add edge labels for all edges if edge_labels is True and plt: labels = nx.get_edge_attributes(grph, 'weight') nx.draw_networkx_edge_labels(grph, pos=pos, edge_labels=labels) # show output if plot is True: plt.show() class Label(namedtuple('label', ['location','name', 'energy_carrier'])): __slots__ = () def __str__(self): return '_'.join(map(str, self._asdict().values())) datetimeindex = pd.date_range('1/1/2017', periods=2, freq='H') es = EnergySystem(timeindex=datetimeindex) b_Electrical1 = ElectricalBus(label=Label(name='b_Electrical1', location='C1', energy_carrier=''), v_min=-0.12, v_max=1) b_Electrical2 = ElectricalBus(label=Label(name='b_Electrical2', location='C2', energy_carrier=''), v_min=-0.12, v_max=1) b_Electrical3 = ElectricalBus(label=Label(name='b_Electrical3', location='C2', energy_carrier=''), v_min=-0.12, v_max=1) b_Electrical4 = ElectricalBus(label=Label(name='b_Electrical4', location='C1', energy_carrier=''), v_min=-0.12, v_max=1) source_1 = Source(label="source_1", outputs={b_Electrical1: Flow(nominal_value=100,variable_costs=25)}) sink_1 = Sink(label="load", inputs={b_Electrical4: Flow(nominal_value=25, actual_value=[1, 1], fixed=True)}) Electricallline_1=ElectricalLine(label="line1", inputs={b_Electrical1: Flow()}, outputs={b_Electrical2: Flow(nominal_value=100,variable_costs=0, min=0, max=1)}, reactance=0.01) Electricallline_2=ElectricalLine(label="line2", inputs={b_Electrical1: Flow()}, outputs={b_Electrical3: Flow(nominal_value=100,variable_costs=25,min=0, max=1)}, reactance=0.01) Electricallline_3=ElectricalLine(label="line3", inputs={b_Electrical2: Flow()}, outputs={b_Electrical4: Flow(nominal_value=100,variable_costs=25,min=0, max=1)}, reactance=0.01) Electricallline_4=ElectricalLine(label="line4", inputs={b_Electrical3: Flow()}, outputs={b_Electrical4: Flow(nominal_value=100,variable_costs=25,min=0, max=1)}, reactance=0.01) es.add(b_Electrical1) es.add(b_Electrical2) es.add(b_Electrical3) es.add(b_Electrical4) es.add(Electricallline_1) es.add(Electricallline_2) es.add(Electricallline_3) es.add(Electricallline_4) es.add(source_1) es.add(sink_1) model=Model(es) model.x=variable() graph=create_nx_graph(es) # #draw_graph(g) model.write('lopf.lp', io_options={'symbolic_solver_labels': True}) model.solve(solver='gurobi', solve_kwargs={'tee': True, 'keepfiles': False}) model.results() results = processing.results(model) draw_graph(grph=graph, plot=True, layout='neato', node_size=500)