Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
topupopt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pmag
topupopt
Commits
6327bb3d
Commit
6327bb3d
authored
Jul 23, 2024
by
Pedro L. Magalhães
Browse files
Options
Downloads
Patches
Plain Diff
Made add_node and add_nodes_from methods identical to those of networkx.
parent
5272df18
Branches
Branches containing commit
No related tags found
1 merge request
!7
Added support for convex price functions using the delta formulation and...
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/topupopt/problems/esipp/network.py
+77
-30
77 additions, 30 deletions
src/topupopt/problems/esipp/network.py
tests/test_esipp_network.py
+6
-16
6 additions, 16 deletions
tests/test_esipp_network.py
with
83 additions
and
46 deletions
src/topupopt/problems/esipp/network.py
+
77
−
30
View file @
6327bb3d
...
...
@@ -17,7 +17,7 @@ from math import inf
import
networkx
as
nx
from
...data.gis.identify
import
get_edges_involving_node
from
...data.gis.identify
import
find_unconnected_nodes
from
.resource
import
are_prices_time_invariant
,
ResourcePrice
#
from .resource import are_prices_time_invariant, ResourcePrice
# *****************************************************************************
# *****************************************************************************
...
...
@@ -625,9 +625,6 @@ class Network(nx.MultiDiGraph):
def
__init__
(
self
,
network_type
=
NET_TYPE_HYBRID
,
**
kwargs
):
# run base class init routine
nx
.
MultiDiGraph
.
__init__
(
self
,
**
kwargs
)
# initialise the node type
self
.
import_nodes
=
set
()
self
.
export_nodes
=
set
()
...
...
@@ -643,6 +640,9 @@ class Network(nx.MultiDiGraph):
# nodes without outgoing directed arcs limitations
self
.
nodes_w_out_dir_arc_limitations
=
dict
()
# run base class init routine
nx
.
MultiDiGraph
.
__init__
(
self
,
**
kwargs
)
# process the input data
for
node_key
in
self
.
nodes
():
self
.
_process_node_data
(
node_key
,
data
=
self
.
nodes
[
node_key
])
...
...
@@ -722,30 +722,6 @@ class Network(nx.MultiDiGraph):
# *************************************************************************
# *************************************************************************
# TODO: use a decorator function to prevent the original method(s) from being used inappropriately
def
add_node
(
self
,
node_key
,
**
kwargs
):
self
.
_handle_node
(
node_key
,
**
kwargs
)
# *************************************************************************
# *************************************************************************
# TODO: automatically check if node already exists and implications when "adding" one
def
add_nodes
(
self
,
node_key_data
:
list
):
# process the input data
for
entry
in
node_key_data
:
if
type
(
entry
)
!=
tuple
:
raise
ValueError
(
'
The input must be a list of tuples.
'
)
self
.
_process_node_data
(
entry
[
0
],
entry
[
1
])
# add the nodes
nx
.
MultiDiGraph
.
add_nodes_from
(
self
,
node_key_data
)
# *************************************************************************
# *************************************************************************
def
is_export_node
(
self
,
node_key
)
->
bool
:
"
Returns True if the key matches that of an export node.
"
return
node_key
in
self
.
export_nodes
...
...
@@ -783,11 +759,82 @@ class Network(nx.MultiDiGraph):
# *************************************************************************
# *************************************************************************
# TODO: use a decorator function to prevent the original method(s) from being used inappropriately
def
add_node
(
self
,
node_key
,
**
kwargs
):
# check if the node can be added and add it
self
.
_handle_node
(
node_key
,
**
kwargs
)
# *************************************************************************
# *************************************************************************
def
modify_node
(
self
,
node_key
,
**
kwargs
):
if
not
self
.
has_node
(
node_key
):
raise
ValueError
(
'
The node indicated does not exist.
'
)
self
.
_handle_node
(
node_key
,
**
kwargs
)
# *************************************************************************
# *************************************************************************
# TODO: automatically check if node already exists and implications when "adding" one
# def add_nodes(self, node_key_data: list):
# # process the input data
# for entry in node_key_data:
# if type(entry) != tuple :
# raise ValueError('The input must be a list of tuples.')
# # self._handle_node(entry[0], **entry[1])
# self._process_node_data(entry[0], entry[1])
# # add the nodes
# nx.MultiDiGraph.add_nodes_from(self, node_key_data)
# *************************************************************************
# *************************************************************************
def
add_nodes_from
(
self
,
nodes_for_adding
,
**
kwargs
):
# input formats:
# 1) container of node keys
# 2) container of tuples
# process the input data
for
entry
in
nodes_for_adding
:
if
type
(
entry
)
==
tuple
and
len
(
entry
)
==
2
and
type
(
entry
[
1
])
==
dict
:
# option 2
# update the dict
new_dict
=
kwargs
.
copy
()
new_dict
.
update
(
entry
[
1
])
self
.
_handle_node
(
entry
[
0
],
**
new_dict
)
else
:
# option 1
self
.
_handle_node
(
entry
,
**
kwargs
)
# *************************************************************************
# *************************************************************************
def
modify_nodes_from
(
self
,
nodes_for_adding
,
**
kwargs
):
# input formats:
# 1) container of node keys
# 2) container of tuples
# process the input data
for
entry
in
nodes_for_adding
:
if
type
(
entry
)
==
tuple
and
len
(
entry
)
==
2
and
type
(
entry
[
1
])
==
dict
:
# option 2
new_dict
=
kwargs
.
copy
()
new_dict
.
update
(
entry
[
1
])
if
not
self
.
has_node
(
entry
[
0
]):
raise
ValueError
(
'
The node indicated does not exist.
'
)
self
.
_handle_node
(
entry
[
0
],
**
new_dict
)
else
:
# option 1
if
not
self
.
has_node
(
entry
):
raise
ValueError
(
'
The node indicated does not exist.
'
)
self
.
_handle_node
(
entry
,
**
kwargs
)
# *************************************************************************
# *************************************************************************
def
_handle_node
(
self
,
node_key
,
**
kwargs
):
# node has to exist
...
...
This diff is collapsed.
Click to expand it.
tests/test_esipp_network.py
+
6
−
16
View file @
6327bb3d
# imports
# standard
import
random
from
networkx
import
binomial_tree
,
MultiDiGraph
# local
from
src.topupopt.problems.esipp.network
import
Arcs
,
Network
from
src.topupopt.problems.esipp.network
import
ArcsWithoutLosses
from
src.topupopt.problems.esipp.network
import
ArcsWithoutProportionalLosses
from
src.topupopt.problems.esipp.network
import
ArcsWithoutStaticLosses
from
src.topupopt.problems.esipp.resource
import
ResourcePrice
from
src.topupopt.data.misc.utils
import
generate_pseudo_unique_key
# *****************************************************************************
...
...
@@ -2179,11 +2171,8 @@ class TestNetwork:
# create network
network
=
Network
()
# add node A
network
.
add_waypoint_node
(
"
A
"
)
# add node B
network
.
add_waypoint_node
(
"
B
"
)
# add nodes A and B
network
.
add_nodes_from
([
'
A
'
,
'
B
'
])
# add arcs
key_list
=
[
...
...
@@ -2303,11 +2292,12 @@ class TestNetwork:
# add nodes
node_a
=
'
A
'
net
.
add_waypoint_node
(
node_a
)
#
net.add_waypoint_node(node_a)
node_b
=
'
B
'
net
.
add_waypoint_node
(
node_b
)
#
net.add_waypoint_node(node_b)
node_c
=
'
C
'
net
.
add_waypoint_node
(
node_c
)
# net.add_waypoint_node(node_c)
net
.
add_nodes_from
([
node_a
,
node_b
,
node_c
])
# add arcs
node_pairs
=
((
node_a
,
node_b
),
(
node_b
,
node_a
),)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment