Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pyomo/network/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,52 @@ def construct(self, data=None):

timer.report()

def connect_to(self, port, block=None, arc_name=None):
"""Method for connecting current port to another port via an Arc
Args:
port: Port
The destination port to connect to
block: Block, optional
The block on which to construct the Arc. If None,
the Arc will be constructed on the port parent block
arc_name: str, optional
The name of the Arc to create. If None, the name
will be generated as
"<current_port_name>_to_<dest_port_name>"
Returns:
created arc
"""
# NOTE import Arc here to avoid circular import issues,
# this is the only place in the Port class where Arc is needed
from pyomo.network.arc import Arc

if block is None:
block = self.parent_block()
if block is None:
raise ValueError(
"Cannot connect Port '%s' to Port '%s' because '%s' port "
"does not have a parent block. Please specify a block to "
"construct the Arc on." % (self.name, port.name, self.name)
)

if arc_name is None:
arc_name = unique_component_name(
block,
f"{self.parent_component().local_name}_to_"
f"{port.parent_component().local_name}",
)

block.add_component(arc_name, Arc(source=self, destination=port))
# verify its created as add_component can fail silently when arc name
# is not a valid python name (e.g arc_name='my-arc')
created_arc = block.find_component(arc_name)
if created_arc is None:
raise RuntimeError(
f"Failed to create Arc '{arc_name}' connecting Port "
f"'{self.name}' to Port '{port.name}'."
)
return created_arc

def _initialize_members(self, initSet):
for idx in initSet:
tmp = self[idx]
Expand Down
33 changes: 33 additions & 0 deletions pyomo/network/tests/test_arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,39 @@ def test_clone(self):
m2.y.value = 1.25
self.assertAlmostEqual(value(c.body), 0)

def test_expand_auto_connect(self):
m = ConcreteModel()
m.x = Var()
m.prt = Port()
m.prt.add(m.x, "a")
m.prt.connect_to(m.prt)
created_arc = m.find_component("prt_to_prt")
self.assertEqual(len(list(m.component_objects(Constraint))), 0)
self.assertEqual(len(list(m.component_data_objects(Constraint))), 0)

TransformationFactory('network.expand_arcs').apply_to(m)

self.assertEqual(len(list(m.component_objects(Constraint))), 1)
self.assertEqual(len(list(m.component_data_objects(Constraint))), 1)
self.assertFalse(created_arc.active)
blk = m.component('prt_to_prt_expanded')
self.assertTrue(blk.active)
self.assertTrue(blk.component('a_equality').active)

os = StringIO()
blk.pprint(ostream=os)
self.assertEqual(
os.getvalue(),
"""prt_to_prt_expanded : Size=1, Index=None, Active=True
1 Constraint Declarations
a_equality : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : 0.0 : x - x : 0.0 : True

1 Declarations: a_equality
""",
)


if __name__ == "__main__":
unittest.main()
59 changes: 59 additions & 0 deletions pyomo/network/tests/test_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pyomo.environ import (
ConcreteModel,
AbstractModel,
Block,
Var,
Set,
NonNegativeReals,
Expand Down Expand Up @@ -552,6 +553,64 @@ def _IN(m, i):
""",
)

def test_auto_connect(self):
m = ConcreteModel()
m.x = Var()
m.y = Var()
m.p1 = Port()
m.p1.add(m.x)
m.p2 = Port()
m.p2.add(m.y)
created_arc = m.p1.connect_to(m.p2)
# test name creation
created_arc_custom_name = m.p1.connect_to(m.p2, arc_name='custom_name')
self.assertIs(m.p1.x, m.x)
self.assertIs(m.p2.y, m.y)
self.assertIsNotNone(m.find_component('p1_to_p2'))
self.assertIs(created_arc, m.find_component('p1_to_p2'))
self.assertIsNotNone(m.find_component('custom_name'))
self.assertIs(created_arc_custom_name, m.find_component('custom_name'))

def test_auto_connect_with_block(self):
m = ConcreteModel()
m.block_a = Block()
m.block_b = Block()
m.block_a.x = Var()
m.block_b.y = Var()
m.block_a.p1 = Port()
m.block_a.p1.add(m.block_a.x)
m.block_b.p2 = Port()
m.block_b.p2.add(m.block_b.y)
created_arc = m.block_a.p1.connect_to(m.block_b.p2)
# test creation on diff block
created_arc_2 = m.block_a.p1.connect_to(m.block_b.p2, block=m.block_b)

self.assertIs(m.block_a.p1.x, m.block_a.x)
self.assertIs(m.block_b.p2.y, m.block_b.y)
self.assertIsNotNone(m.block_a.find_component('p1_to_p2'))
self.assertIsNotNone(m.block_b.find_component('p1_to_p2'))

self.assertIs(created_arc, m.block_a.find_component('p1_to_p2'))
self.assertIs(created_arc_2, m.block_b.find_component('p1_to_p2'))
# verify these are different objects
self.assertIsNot(created_arc, created_arc_2)

def test_auto_connect_with_indexed_block(self):
m = ConcreteModel()
m.block = Block([1, 2])
m.block[1].x = Var()
m.block[2].y = Var()
m.block[1].p1 = Port()
m.block[1].p1.add(m.block[1].x)
m.block[2].p2 = Port()
m.block[2].p2.add(m.block[2].y)
created_arc = m.block[1].p1.connect_to(m.block[2].p2)

self.assertIs(m.block[1].p1.x, m.block[1].x)
self.assertIs(m.block[2].p2.y, m.block[2].y)
self.assertIsNotNone(m.block[1].find_component('p1_to_p2'))
self.assertIs(created_arc, m.block[1].find_component('p1_to_p2'))


if __name__ == "__main__":
unittest.main()