job_shop_lib.visualization.graphs¶
Contains functions and classes for visualizing job shop scheduling problems.
Plots the disjunctive graph of the given job shop instance or graph. |
|
Returns a plot of the hetereogeneous graph of the instance. |
|
Generates coordinates for a three-column grid layout. |
|
Returns a label for the node with the processing time. |
|
Returns a function that assigns a color to a node based on its type. |
- plot_disjunctive_graph(job_shop, *, title=None, figsize=(6, 4), node_size=1600, edge_width=2, font_size=10, arrow_size=35, alpha=0.95, operation_node_labeler=<function duration_labeler>, node_font_color='white', machine_colors=None, color_map='Dark2_r', disjunctive_edge_color='red', conjunctive_edge_color='black', layout=None, draw_disjunctive_edges=True, conjunctive_edges_additional_params=None, disjunctive_edges_additional_params=None, conjunctive_patch_label='Conjunctive edges', disjunctive_patch_label='Disjunctive edges', legend_text='$p_{ij}=$duration of $O_{ij}$', show_machine_colors_in_legend=True, machine_labels=None, legend_location='upper left', legend_bbox_to_anchor=(1.01, 1), start_node_label='$S$', end_node_label='$T$', font_family='sans-serif')[source]¶
Plots the disjunctive graph of the given job shop instance or graph.
- Parameters:
job_shop (JobShopGraph | JobShopInstance) -- The job shop instance or graph to plot. Can be either a
JobShopGraphor aJobShopInstance. If a job shop instance is given, the disjunctive graph is built before plotting using thebuild_disjunctive_graph().title (str | None) -- The title of the graph (default is
"Disjunctive Graph Visualization: {job_shop.instance.name}").figsize (tuple[float, float]) -- The size of the figure (default is (6, 4)).
node_size (int) -- The size of the nodes (default is 1600).
edge_width (int) -- The width of the edges (default is 2).
font_size (int) -- The font size of the node labels (default is 10).
arrow_size (int) -- The size of the arrows (default is 35).
alpha (float) -- The transparency level of the nodes and edges (default is 0.95).
operation_node_labeler (Callable[[Node], str]) -- A function that formats labels for operation nodes. Receives a
Nodeand returns a string. The default isduration_labeler(), which labels the nodes with their duration.node_font_color (str) -- The color of the node labels (default is
"white").machine_colors (dict[int, tuple[float, float, float, float]] | None) -- A dictionary that maps machine ids to colors. If not provided, the colors are generated using the
color_map. If provided, the colors are used as the base for the node colors. The dictionary should have the form{machine_id: (r, g, b, a)}. For source and sink nodes use-1as the machine id.color_map (str) -- The color map to use for the nodes (default is
"Dark2_r").disjunctive_edge_color (str) -- The color of the disjunctive edges (default is
"red").conjunctive_edge_color (str) -- The color of the conjunctive edges (default is
"black").layout (Callable[[Graph], dict[str, tuple[float, float]]] | None) -- The layout of the graph (default is
graphviz_layoutwithprog="dot"andargs="-Grankdir=LR"). If not available, the spring layout is used. To install pygraphviz, check pygraphviz documentation.draw_disjunctive_edges (bool | str) -- Whether to draw disjunctive edges (default is
True). IfFalse, only conjunctive edges are drawn. If"single_edge",the disjunctive edges are drawn as undirected edges by removing one of the directions. If using this last option is recommended to set the "arrowstyle" parameter to"-"or"<->"in thedisjunctive_edges_additional_paramsto make the edges look better. See matplotlib documentation on arrow styles and nx.draw_networkx_edges for more information.conjunctive_edges_additional_params (dict[str, Any] | None) --
Additional parameters to pass to the conjunctive edges when drawing them (default is
None). See the documentation of nx.draw_networkx_edges for more information. The parameters that are explicitly set by this function and should not be part of this dictionary areedgelist,pos,width,edge_color, andarrowsize.disjunctive_edges_additional_params (dict[str, Any] | None) -- Same as
conjunctive_edges_additional_params, but for disjunctive edges (default isNone).conjunctive_patch_label (str) -- The label for the conjunctive edges in the legend (default is
"Conjunctive edges").disjunctive_patch_label (str) -- The label for the disjunctive edges in the legend (default is
"Disjunctive edges").legend_text (str) -- Text to display in the legend after the conjunctive and disjunctive edges labels (default is
"$p_{ij}=$duration of $O_{ij}$").show_machine_colors_in_legend (bool) -- Whether to show the colors of the machines in the legend (default is
True).machine_labels (Sequence[str] | None) -- The labels for the machines (default is
[f"Machine {i}" for i in range(num_machines)]). Not used ifshow_machine_colors_in_legendisFalse.legend_location (str) -- The location of the legend (default is "upper left").
legend_bbox_to_anchor (tuple[float, float]) -- The anchor of the legend box (default is
(1.01, 1)).start_node_label (str) -- The label for the start node (default is
"$S$").end_node_label (str) -- The label for the end node (default is
"$T$").font_family (str) -- The font family of the node labels (default is
"sans-serif").
- Returns:
A matplotlib Figure object representing the disjunctive graph.
- Return type:
tuple[Figure, Axes]
Example
job_shop_instance = JobShopInstance(...) # or a JobShopGraph fig = plot_disjunctive_graph(job_shop_instance)
- plot_resource_task_graph(job_shop_graph, *, title=None, figsize=(10, 10), layout=None, node_size=1200, node_font_color='k', font_size=10, alpha=0.95, add_legend=False, node_shapes=None, node_color_map=None, default_node_color='lightblue', machine_color_map_name='tab10', legend_text='$p_{ij}$ = duration of $O_{ij}$', edge_additional_params=None, draw_only_one_edge=False)[source]¶
Returns a plot of the hetereogeneous graph of the instance.
Machine and job nodes are represented by squares, and the operation nodes are represented by circles.
- Parameters:
job_shop_graph (JobShopGraph) -- The job shop graph instance.
title (str | None) -- The title of the plot. If
None, the title "Heterogeneous Graph Visualization: {instance_name}" is used. The default isNone.figsize (tuple[int, int]) -- The size of the figure. It should be a tuple with the width and height in inches. The default is
(10, 10).layout (dict[Node, tuple[float, float]] | None) -- A dictionary with the position of each node in the graph. The keys are the node ids, and the values are tuples with the x and y coordinates. If
None, thethree_columns_layout()function is used. The default isNone.node_size (int) -- The size of the nodes. The default is 1000.
alpha (float) -- The transparency of the nodes. It should be a float between 0 and 1. The default is 0.95.
add_legend (bool) -- Whether to add a legend with the meaning of the colors and shapes. The default is
False.node_shapes (dict[str, str] | None) -- A dictionary with the shapes of the nodes. The keys are the node types, and the values are the shapes. The default is
{"machine": "s", "job": "d", "operation": "o", "global": "o"}.node_color_map (Callable[[Node], tuple[float, float, float, float]] | None) -- A function that receives a node and returns a tuple with the RGBA values of the color to use in the plot. If
None,color_nodes_by_machine()is used.machine_color_map_name (str) -- The name of the colormap to use for the machines. This argument is only used if
node_color_mapisNone. The default is"tab10".node_font_color (str)
font_size (int)
default_node_color (str | tuple[float, float, float, float])
legend_text (str)
edge_additional_params (dict[str, Any] | None)
draw_only_one_edge (bool)
- Returns:
The figure of the plot. This figure can be used to save the plot to a file or to show it in a Jupyter notebook.
- Return type:
Figure
- three_columns_layout(job_shop_graph, *, leftmost_position=0.1, rightmost_position=0.9, topmost_position=1.0, bottommost_position=0.0)[source]¶
Generates coordinates for a three-column grid layout.
Left column: Machine nodes (M1, M2, etc.)
Middle column: Operation nodes (O_ij where i=job, j=operation)
Right column: Job nodes (J1, J2, etc.)
The operations are arranged vertically in groups by job, with a global node (G) at the bottom.
For example, in a 2-machine, 3-job problem:
Machine nodes (M1, M2) appear in the left column where needed
Operation nodes (O_11 through O_33) form the central column
Job nodes (J1, J2, J3) appear in the right column at the middle of their respective operations
The global node (G) appears at the bottom of the middle column
- Parameters:
job_shop_graph (JobShopGraph) -- The job shop graph instance. It should be already initialized with the instance with a valid agent-task graph representation.
leftmost_position (float) -- The center position of the leftmost column of the layout. It should be a float between 0 and 1. The default is 0.1.
rightmost_position (float) -- The center position of the rightmost column of the layout. It should be a float between 0 and 1. The default is 0.9.
topmost_position (float) -- The center position of the topmost node of the layout. It should be a float between 0 and 1. The default is 0.9.
bottommost_position (float) -- The center position of the bottommost node of the layout. It should be a float between 0 and 1. The default is 0.1.
- Returns:
A dictionary with the position of each node in the graph. The keys are the node ids, and the values are tuples with the x and y coordinates.
- Return type:
dict[Node, tuple[float, float]]
- duration_labeler(node)[source]¶
Returns a label for the node with the processing time.
In the form
"$p_{ij}=duration$", where $i$ is the job id and $j$ is the position in the job.
- color_nodes_by_machine(machine_colors, default_color)[source]¶
Returns a function that assigns a color to a node based on its type.
The function returns a color based on the node type. If the node is an operation, the color is based on the machine it is assigned to. If the node is a machine, the color is based on the machine id. If the node is a job or global node, the color is the default color.
- Parameters:
machine_colors (dict[int, tuple[float, float, float, float]]) -- A dictionary with the colors of each machine. The keys are the machine ids, and the values are tuples with the RGBA values.
default_color (str | tuple[float, float, float, float]) -- The default color to use for job and global nodes. It can be a string with a color name or a tuple with the RGBA values.
- Returns:
A function that receives a node and returns a tuple with the RGBA values of the color to use in the plot.
- Return type:
Callable[[Node], tuple[float, float, float, float]]