Skip to content

Commit

Permalink
apply black
Browse files Browse the repository at this point in the history
  • Loading branch information
sadavis1 committed Jun 20, 2024
1 parent c95c34c commit a75048c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 58 deletions.
37 changes: 26 additions & 11 deletions pyomo/contrib/piecewise/piecewise_linear_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,17 @@ def _construct_from_function_and_points(self, obj, parent, nonlinear_function):
obj, parent, nonlinear_function, simplices_are_user_defined=False
)

def _construct_from_univariate_function_and_segments(self, obj, parent, func, segments_are_user_defined=True):
def _construct_from_univariate_function_and_segments(
self, obj, parent, func, segments_are_user_defined=True
):
# We can trust they are nicely ordered if we made them, otherwise anything goes.
if segments_are_user_defined:
obj._triangulation = Triangulation.Unknown
tri = self._triangulation_rule(parent, obj._index)
if tri not in (None, Triangulation.Delaunay):
logger.warn(f"Non-default triangulation request {tri} was ignored because the simplices were provided. If you meant to override the tag, use `override_triangulation` instead.")
logger.warn(
f"Non-default triangulation request {tri} was ignored because the simplices were provided. If you meant to override the tag, use `override_triangulation` instead."
)
else:
obj._triangulation = Triangulation.AssumeValid

Expand Down Expand Up @@ -432,15 +436,16 @@ def _construct_from_function_and_simplices(
return self._construct_from_univariate_function_and_segments(
obj, parent, nonlinear_function, simplices_are_user_defined
)
# If we triangulated, then this tag was already set. If they provided it,

# If we triangulated, then this tag was already set. If they provided it,
# then it should be unknown.
if simplices_are_user_defined:
obj._triangulation = Triangulation.Unknown
tri = self._triangulation_rule(parent, obj._index)
if tri not in (None, Triangulation.Delaunay):
logger.warn(f"Non-default triangulation request {tri} was ignored because the simplices were provided. If you meant to override the tag, use `override_triangulation` instead.")

logger.warn(
f"Non-default triangulation request {tri} was ignored because the simplices were provided. If you meant to override the tag, use `override_triangulation` instead."
)

# evaluate the function at each of the points and form the homogeneous
# system of equations
Expand Down Expand Up @@ -496,7 +501,9 @@ def _construct_from_linear_functions_and_simplices(
obj._triangulation = Triangulation.Unknown
tri = self._triangulation_rule(parent, obj._index)
if tri not in (None, Triangulation.Delaunay):
logger.warn(f"Non-default triangulation request {tri} was ignored because the simplices were provided. If you meant to override the tag, use `override_triangulation` instead.")
logger.warn(
f"Non-default triangulation request {tri} was ignored because the simplices were provided. If you meant to override the tag, use `override_triangulation` instead."
)
return obj

@_define_handler(_handlers, False, False, False, False, True)
Expand All @@ -514,14 +521,20 @@ def _construct_from_tabular_data(self, obj, parent, nonlinear_function):
# avoid a dependence on scipy.
self._construct_one_dimensional_simplices_from_points(obj, points)
return self._construct_from_univariate_function_and_segments(
obj, parent, _tabular_data_functor(tabular_data, tupleize=True), segments_are_user_defined=False
obj,
parent,
_tabular_data_functor(tabular_data, tupleize=True),
segments_are_user_defined=False,
)

self._construct_simplices_from_multivariate_points(
obj, parent, points, dimension
)
return self._construct_from_function_and_simplices(
obj, parent, _tabular_data_functor(tabular_data), simplices_are_user_defined=False
obj,
parent,
_tabular_data_functor(tabular_data),
simplices_are_user_defined=False,
)

def _getitem_when_not_present(self, index):
Expand Down Expand Up @@ -563,10 +576,12 @@ def _getitem_when_not_present(self, index):
# If the user wanted to override the triangulation tag, do it after we
# are finished setting it ourselves.
if self._triangulation_override_rule is not None:
triangulation_override = self._triangulation_override_rule(parent, obj._index)
triangulation_override = self._triangulation_override_rule(
parent, obj._index
)
if triangulation_override is not None:
obj._triangulation = triangulation_override

return obj


Expand Down
4 changes: 3 additions & 1 deletion pyomo/contrib/piecewise/tests/test_incremental.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def g2(x1, x2):
[(3, 4), (3, 7), (0, 7)],
]
m.pw_paraboloid = PiecewiseLinearFunction(
simplices=simplices, linear_functions=[g1, g1, g2, g2], override_triangulation=Triangulation.AssumeValid
simplices=simplices,
linear_functions=[g1, g1, g2, g2],
override_triangulation=Triangulation.AssumeValid,
)
m.paraboloid_expr = m.pw_paraboloid(m.x1, m.x2)

Expand Down
12 changes: 11 additions & 1 deletion pyomo/contrib/piecewise/tests/test_piecewise_linear_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,17 @@ def test_pw_linear_approx_of_paraboloid_points(self):
def test_pw_linear_approx_of_paraboloid_j1(self):
m = self.make_model()
m.pw = PiecewiseLinearFunction(
points=[(0, 1), (0, 4), (0, 7), (3, 1), (3, 4), (3, 7), (4, 1), (4, 4), (4, 7)],
points=[
(0, 1),
(0, 4),
(0, 7),
(3, 1),
(3, 4),
(3, 7),
(4, 1),
(4, 4),
(4, 7),
],
function=m.g,
triangulation=Triangulation.OrderedJ1,
)
Expand Down
22 changes: 12 additions & 10 deletions pyomo/contrib/piecewise/tests/test_triangulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ def test_J1_small(self):
self.assertTrue(
np.array_equal(
triangulation.simplices,
np.array([
[0, 1, 4],
[1, 2, 4],
[4, 6, 7],
[4, 7, 8],
[0, 3, 4],
[2, 4, 5],
[3, 4, 6],
[4, 5, 8]
])
np.array(
[
[0, 1, 4],
[1, 2, 4],
[4, 6, 7],
[4, 7, 8],
[0, 3, 4],
[2, 4, 5],
[3, 4, 6],
[4, 5, 8],
]
),
)
)

Expand Down
69 changes: 34 additions & 35 deletions pyomo/contrib/piecewise/triangulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ class Triangulation(Enum):
J1 = 3
OrderedJ1 = 4


# Duck-typed thing that looks reasonably similar to an instance of scipy.spatial.Delaunay
# Fields:
# - points: list of P points as P x n array
# - simplices: list of M simplices as P x (n + 1) array of point _indices_
# - coplanar: list of N points omitted from triangulation as tuples of (point index,
# nearest simplex index, nearest vertex index), stacked into an N x 3 array
class FakeScipyTriangulation():
class FakeScipyTriangulation:
def __init__(self, points, simplices, coplanar):
self.points = points
self.simplices = simplices
Expand All @@ -41,7 +42,11 @@ def __init__(self, points, simplices, coplanar):
def get_unordered_j1_triangulation(points, dimension):
points_map, num_pts = _process_points_j1(points, dimension)
simplices_list = _get_j1_triangulation(points_map, num_pts - 1, dimension)
return FakeScipyTriangulation(points=np.array(points), simplices=np.array(simplices_list), coplanar=np.array([]))
return FakeScipyTriangulation(
points=np.array(points),
simplices=np.array(simplices_list),
coplanar=np.array([]),
)


def get_ordered_j1_triangulation(points, dimension):
Expand All @@ -54,7 +59,11 @@ def get_ordered_j1_triangulation(points, dimension):
simplices_list = _get_ordered_j1_triangulation_4d_and_above(
points_map, num_pts - 1, dimension
)
return FakeScipyTriangulation(points=np.array(points), simplices=np.array(simplices_list), coplanar=np.array([]))
return FakeScipyTriangulation(
points=np.array(points),
simplices=np.array(simplices_list),
coplanar=np.array([]),
)


# Does some validation but mostly assumes the user did the right thing
Expand Down Expand Up @@ -134,32 +143,22 @@ class Direction(Enum):

# make it easier to read what I'm doing
def add_bottom_right():
simplices.append((
points_map[x, y],
points_map[x + 1, y],
points_map[x + 1, y + 1],
))
simplices.append(
(points_map[x, y], points_map[x + 1, y], points_map[x + 1, y + 1])
)

def add_top_right():
simplices.append((
points_map[x, y + 1],
points_map[x + 1, y],
points_map[x + 1, y + 1],
))
simplices.append(
(points_map[x, y + 1], points_map[x + 1, y], points_map[x + 1, y + 1])
)

def add_bottom_left():
simplices.append((
points_map[x, y],
points_map[x, y + 1],
points_map[x + 1, y],
))
simplices.append((points_map[x, y], points_map[x, y + 1], points_map[x + 1, y]))

def add_top_left():
simplices.append((
points_map[x, y],
points_map[x, y + 1],
points_map[x + 1, y + 1],
))
simplices.append(
(points_map[x, y], points_map[x, y + 1], points_map[x + 1, y + 1])
)

# identify square by bottom-left corner
x, y = start_square
Expand Down Expand Up @@ -330,9 +329,11 @@ def _get_ordered_j1_triangulation_3d(points_map, num_pts):
start_data = (tuple(-1 * i for i in direction_to_next), 2)

for simplex_data in current_cube_path:
simplices.append(get_one_j1_simplex(
current_v_0, simplex_data[1], simplex_data[0], 3, points_map
))
simplices.append(
get_one_j1_simplex(
current_v_0, simplex_data[1], simplex_data[0], 3, points_map
)
)

# fill in the last cube. We have a good start_data but we need to invent a
# direction_to_next. Let's go straight in the direction we came from.
Expand All @@ -351,9 +352,11 @@ def _get_ordered_j1_triangulation_3d(points_map, num_pts):
]

for simplex_data in current_cube_path:
simplices.append(get_one_j1_simplex(
current_v_0, simplex_data[1], simplex_data[0], 3, points_map
))
simplices.append(
get_one_j1_simplex(
current_v_0, simplex_data[1], simplex_data[0], 3, points_map
)
)

fix_vertices_incremental_order(simplices)
return simplices
Expand Down Expand Up @@ -389,15 +392,11 @@ def _get_ordered_j1_triangulation_4d_and_above(points_map, num_pts, dim):
if c % 2 == 0:
perm_sequence = get_Gn_hamiltonian(dim, start_perm, j, False)
for pi in perm_sequence:
simplices.append(get_one_j1_simplex(
v_0, pi, sign, dim, points_map
))
simplices.append(get_one_j1_simplex(v_0, pi, sign, dim, points_map))
else:
perm_sequence = get_Gn_hamiltonian(dim, start_perm, j, True)
for pi in perm_sequence:
simplices.append(get_one_j1_simplex(
v_0, pi, sign, dim, points_map
))
simplices.append(get_one_j1_simplex(v_0, pi, sign, dim, points_map))
# should be true regardless of odd or even? I hope
start_perm = perm_sequence[-1]

Expand Down

0 comments on commit a75048c

Please sign in to comment.