Skip to content

Commit

Permalink
feat: initial point generator [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
avinashresearch1 committed Jul 12, 2024
1 parent 07517e1 commit 02cd497
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Plots
using JuMP
using Test

using BARON
#using BARON

#exportall(CompHENS)

Expand Down
15 changes: 9 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ authors = ["Avinash Subramanian"]
version = "0.1.4"

[deps]
BARON = "2e2ca445-9e14-5b13-8677-4410f177f82b"
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand All @@ -15,11 +14,16 @@ Kwonly = "18d08c8c-0732-55ee-a446-91a51d7b4206"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
NamedArrays = "86f7a689-2022-50b4-a561-43c23ac3c673"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
XLSX = "fdbf4ff8-1666-58a4-91e7-1b58723a45e0"

[weakdeps]
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"

[extensions]
CompHENSNetworkXPlotsExt = "PyCall"

[compat]
BARON = "0.8"
Conda = "1"
DataFrames = "1"
DocStringExtensions = "0.9"
HiGHS = "1"
Expand All @@ -29,10 +33,9 @@ Kwonly = "0.1"
MathOptInterface = "1"
NamedArrays = "0.9"
Plots = "1"
XLSX = "0.8, 0.9"
julia = "1.6"
PyCall = "1"
Conda = "1"
XLSX = "0.8, 0.9"
julia = "1.10"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
156 changes: 156 additions & 0 deletions ext/CompHENSNetworkXPlotsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
module CompHENSNetworkXPlotsExt

using CompHENS
using PyCall
using Conda

const nx = PyNULL()
const plt = PyNULL()
const back_pdf = PyNULL()


function __init__()
copy!(nx, pyimport_conda("networkx", "networkx"))
copy!(plt, pyimport_conda("matplotlib.pyplot", "matplotlib"))
copy!(back_pdf, pyimport_conda("matplotlib.backends.backend_pdf", "matplotlib"))
end

"""
$(TYPEDSIGNATURES)
The visualization tools for the HEN uses the Python `NetworkX` and `MatPlotlib` packages.
By default, OS-X and Windows users should PyCall configured to use the Miniconda environment installed at `.julia/conda/` (this can be attained by calling `Conda.ROOTENV`).
For Linux users: By default the default system installation is used, and it is necessary to over-ride this as follows:
- Set
```
ENV["PYTHON"]=""
using Pkg
Pkg.build("PyCall")
```
Then **restart** the Julia process.
"""
function CompHENS.plot_HEN_streamwise(prob::ClassicHENSProblem, model::AbstractModel, overall_network::Dict{String, AbstractSuperstructure}, file_name; digits = 1)
__init__()
pdf = CompHENS.back_pdf.PdfPages(file_name)
for stream in prob.all_names
plt.close()
(g, edge_labels, node_labels, position, node_size) = get_stream_graph(prob.all_dict[stream], prob, model, overall_network[stream]; digits = digits)
nx.draw_networkx(g, position, node_size = node_size, with_labels=false, arrowstyle="->", node_shape = "s")
nx.draw_networkx_labels(g, position, labels = node_labels, font_size = 3, horizontalalignment = "left", verticalalignment = "top")
nx.draw_networkx_edge_labels(g, position, edge_labels = edge_labels, horizontalalignment = "center", verticalalignment = "center", font_size = 2, rotate=false)
pdf.savefig()
end
pdf.close()
end

"""
Gets a NetworkX Digraph for each stream
Returns: (g, edge_labels, node_labels, position, node_size)
"""
function CompHENS.get_stream_graph(stream::AbstractStream, prob::ClassicHENSProblem, model::AbstractModel, superstructure::AbstractSplitSuperstructure; digits = 1)
g = nx.DiGraph()
edge_labels = Dict()
node_labels = Dict()

# Add nodes to NetworkX graph
for node in superstructure.nodes
g.add_node(node.name)
if node isa HX
match = node.match
if stream isa HotStream
node_labels[node.name] = "match: $(match), Q = $(round((prob.results_dict[:Q][match, stream.name]), digits = digits))"
elseif stream isa ColdStream
node_labels[node.name] = "match: $(match), Q = $(round((prob.results_dict[:Q][stream.name, match]), digits = digits))"
end
end
end

# Only add edges if f_edge is greater than 0
for edge in superstructure.edges
if value(model[:f][(stream.name, edge)]) > 0.0
g.add_edge(edge.in.name, edge.out.name)
edge_labels[(edge.in.name, edge.out.name)] = "m: $(round(value(model[:f][(stream.name, edge)]); digits = digits)) T: $(round(value(model[:t][(stream.name, edge)]); digits = digits))"
end
end

num_matches = length(prob.results_dict[:HLD_list][stream.name])
# Copied from SeqHENS.jl
# Setting the coordinates of the nodes
position = Dict()
coordinates = [] # Array to keep pushing paired coordinates to, will then go through and assign to dictionary
bfix = 0.4 # Determines the vertical spacing

push!(coordinates, (0,0))
push!(coordinates, (1,0))
for e in 1:num_matches
push!(coordinates,(2,0-(bfix*(e-1))))
push!(coordinates,(5,0-(bfix*(e-1))))
push!(coordinates,(9,0-(bfix*(e-1))))
end
push!(coordinates,(10,0))
push!(coordinates,(11,0))

i = 0 # hack for now
for node in superstructure.nodes
i += 1
position[node.name] = coordinates[i]
end

node_size = fill(1.0, length(superstructure.nodes))
return(g, edge_labels, node_labels, position, node_size)
end

function CompHENS.get_stream_graph(stream::AbstractUtility, prob::ClassicHENSProblem, model::AbstractModel, superstructure::AbstractSplitSuperstructure; digits = 1)
g = nx.DiGraph()
edge_labels = Dict()
node_labels = Dict()

# Add nodes to NetworkX graph
for node in superstructure.nodes
g.add_node(node.name)
if node isa HX
match = node.match
if stream isa HotStream
node_labels[node.name] = "o: $(match), Q = $(round((prob.results_dict[:Q][match, stream.name]), digits = digits))"
elseif stream isa ColdStream
node_labels[node.name] = "o: $(match), Q = $(round((prob.results_dict[:Q][stream.name, match]), digits = digits))"
end
end
end

# Only add edges if f_edge is greater than 0
for edge in superstructure.edges
if edge.in isa HX || edge.out isa HX # Only care about edges attached to HX
g.add_edge(edge.in.name, edge.out.name)
edge_labels[(edge.in.name, edge.out.name)] = "T: $(round(value(model[:t][(stream.name, edge)]); digits = digits))"
end
end

num_matches = length(prob.results_dict[:HLD_list][stream.name])
# Copied from SeqHENS.jl
# Setting the coordinates of the nodes
position = Dict()
coordinates = [] # Array to keep pushing paired coordinates to, will then go through and assign to dictionary
bfix = 0.4 # Determines the vertical spacing

push!(coordinates, (0,0))
push!(coordinates, (1,0))
for e in 1:num_matches
push!(coordinates,(2,0-(bfix*(e-1))))
push!(coordinates,(5,0-(bfix*(e-1))))
push!(coordinates,(9,0-(bfix*(e-1))))
end
push!(coordinates,(10,0))
push!(coordinates,(11,0))

i = 0 # hack for now
for node in superstructure.nodes
i += 1
position[node.name] = coordinates[i]
end

node_size = fill(1.0, length(superstructure.nodes))
return(g, edge_labels, node_labels, position, node_size)
end
end
2 changes: 1 addition & 1 deletion src/CompHENS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ include("Superstructures/ParallelSplit.jl")
export generate_network!, postprocess_network!, plot_HEN_streamwise, print_stream_results, get_design_area, AreaArithmeticMean, AreaPaterson, CostScaledPaterson, get_stream_graph
include("SubProblems/Network_generation/network_generator_JuMP.jl")
include("SubProblems/Network_generation/network_postprocess.jl")
include("SubProblems/Network_generation/conda_networkx_plots.jl")
#include("SubProblems/Network_generation/conda_networkx_plots.jl")



Expand Down
131 changes: 2 additions & 129 deletions src/SubProblems/Network_generation/conda_networkx_plots.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
using Conda, PyCall

const nx = PyNULL()
const plt = PyNULL()
const back_pdf = PyNULL()


function __init__()
copy!(nx, pyimport_conda("networkx", "networkx"))
copy!(plt, pyimport_conda("matplotlib.pyplot", "matplotlib"))
copy!(back_pdf, pyimport_conda("matplotlib.backends.backend_pdf", "matplotlib"))
end

"""
$(TYPEDSIGNATURES)
The visualization tools for the HEN uses the Python `NetworkX` and `MatPlotlib` packages.
Expand All @@ -24,18 +11,7 @@ Pkg.build("PyCall")
```
Then **restart** the Julia process.
"""
function plot_HEN_streamwise(prob::ClassicHENSProblem, model::AbstractModel, overall_network::Dict{String, AbstractSuperstructure}, file_name; digits = 1)
__init__()
pdf = CompHENS.back_pdf.PdfPages(file_name)
for stream in prob.all_names
plt.close()
(g, edge_labels, node_labels, position, node_size) = get_stream_graph(prob.all_dict[stream], prob, model, overall_network[stream]; digits = digits)
nx.draw_networkx(g, position, node_size = node_size, with_labels=false, arrowstyle="->", node_shape = "s")
nx.draw_networkx_labels(g, position, labels = node_labels, font_size = 3, horizontalalignment = "left", verticalalignment = "top")
nx.draw_networkx_edge_labels(g, position, edge_labels = edge_labels, horizontalalignment = "center", verticalalignment = "center", font_size = 2, rotate=false)
pdf.savefig()
end
pdf.close()
function plot_HEN_streamwise()
end

"""
Expand All @@ -44,108 +20,5 @@ end
Gets a NetworkX Digraph for each stream
Returns: (g, edge_labels, node_labels, position, node_size)
"""
function get_stream_graph(stream::AbstractStream, prob::ClassicHENSProblem, model::AbstractModel, superstructure::AbstractSplitSuperstructure; digits = 1)
g = nx.DiGraph()
edge_labels = Dict()
node_labels = Dict()

# Add nodes to NetworkX graph
for node in superstructure.nodes
g.add_node(node.name)
if node isa HX
match = node.match
if stream isa HotStream
node_labels[node.name] = "match: $(match), Q = $(round((prob.results_dict[:Q][match, stream.name]), digits = digits))"
elseif stream isa ColdStream
node_labels[node.name] = "match: $(match), Q = $(round((prob.results_dict[:Q][stream.name, match]), digits = digits))"
end
end
end

# Only add edges if f_edge is greater than 0
for edge in superstructure.edges
if value(model[:f][(stream.name, edge)]) > 0.0
g.add_edge(edge.in.name, edge.out.name)
edge_labels[(edge.in.name, edge.out.name)] = "m: $(round(value(model[:f][(stream.name, edge)]); digits = digits)) T: $(round(value(model[:t][(stream.name, edge)]); digits = digits))"
end
end

num_matches = length(prob.results_dict[:HLD_list][stream.name])
# Copied from SeqHENS.jl
# Setting the coordinates of the nodes
position = Dict()
coordinates = [] # Array to keep pushing paired coordinates to, will then go through and assign to dictionary
bfix = 0.4 # Determines the vertical spacing

push!(coordinates, (0,0))
push!(coordinates, (1,0))
for e in 1:num_matches
push!(coordinates,(2,0-(bfix*(e-1))))
push!(coordinates,(5,0-(bfix*(e-1))))
push!(coordinates,(9,0-(bfix*(e-1))))
end
push!(coordinates,(10,0))
push!(coordinates,(11,0))

i = 0 # hack for now
for node in superstructure.nodes
i += 1
position[node.name] = coordinates[i]
end

node_size = fill(1.0, length(superstructure.nodes))
return(g, edge_labels, node_labels, position, node_size)
end

function get_stream_graph(stream::AbstractUtility, prob::ClassicHENSProblem, model::AbstractModel, superstructure::AbstractSplitSuperstructure; digits = 1)
g = nx.DiGraph()
edge_labels = Dict()
node_labels = Dict()

# Add nodes to NetworkX graph
for node in superstructure.nodes
g.add_node(node.name)
if node isa HX
match = node.match
if stream isa HotStream
node_labels[node.name] = "o: $(match), Q = $(round((prob.results_dict[:Q][match, stream.name]), digits = digits))"
elseif stream isa ColdStream
node_labels[node.name] = "o: $(match), Q = $(round((prob.results_dict[:Q][stream.name, match]), digits = digits))"
end
end
end

# Only add edges if f_edge is greater than 0
for edge in superstructure.edges
if edge.in isa HX || edge.out isa HX # Only care about edges attached to HX
g.add_edge(edge.in.name, edge.out.name)
edge_labels[(edge.in.name, edge.out.name)] = "T: $(round(value(model[:t][(stream.name, edge)]); digits = digits))"
end
end

num_matches = length(prob.results_dict[:HLD_list][stream.name])
# Copied from SeqHENS.jl
# Setting the coordinates of the nodes
position = Dict()
coordinates = [] # Array to keep pushing paired coordinates to, will then go through and assign to dictionary
bfix = 0.4 # Determines the vertical spacing

push!(coordinates, (0,0))
push!(coordinates, (1,0))
for e in 1:num_matches
push!(coordinates,(2,0-(bfix*(e-1))))
push!(coordinates,(5,0-(bfix*(e-1))))
push!(coordinates,(9,0-(bfix*(e-1))))
end
push!(coordinates,(10,0))
push!(coordinates,(11,0))

i = 0 # hack for now
for node in superstructure.nodes
i += 1
position[node.name] = coordinates[i]
end

node_size = fill(1.0, length(superstructure.nodes))
return(g, edge_labels, node_labels, position, node_size)
function get_stream_graph()
end
Loading

0 comments on commit 02cd497

Please sign in to comment.