Skip to content

Commit 51a55b5

Browse files
committed
factoring out ph_main
1 parent b90a975 commit 51a55b5

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

Diff for: mpisppy/opt/ph.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ class PH(mpisppy.phbase.PHBase):
2828
# uncomment the line below to get per-rank profile outputs, which can
2929
# be examined with snakeviz (or your favorite profile output analyzer)
3030
#@profile(filename="profile_out")
31-
def ph_main(self, finalize=True):
31+
def ph_main(self, finalize=True, attach_prox=True):
3232
""" Execute the PH algorithm.
3333
3434
Args:
3535
finalize (bool, optional, default=True):
3636
If True, call `PH.post_loops()`, if False, do not,
3737
and return None for Eobj
38+
attach_prox (bool, optional, default=True):
39+
If True, use a proximal term, if False, no proximal
40+
term is added to the objective function
3841
3942
Returns:
4043
tuple:
@@ -56,15 +59,15 @@ def ph_main(self, finalize=True):
5659
"""
5760
verbose = self.options['verbose']
5861
smoothed = self.options['smoothed']
59-
self.PH_Prep(attach_smooth = smoothed)
62+
self.PH_Prep(attach_prox=attach_prox, attach_smooth = smoothed)
6063

6164
if (verbose):
62-
print('Calling PH Iter0 on global rank {}'.format(global_rank))
65+
print(f'Calling {self.__class__.__name__} Iter0 on global rank {global_rank}')
6366
trivial_bound = self.Iter0()
6467
if self._can_update_best_bound():
6568
self.best_bound_obj_val = trivial_bound
6669
if (verbose):
67-
print ('Completed PH Iter0 on global rank {}'.format(global_rank))
70+
print(f'Completed {self.__class__.__name__} Iter0 on global rank {global_rank}')
6871
if ('asynchronousPH' in self.options) and (self.options['asynchronousPH']):
6972
raise RuntimeError("asynchronousPH is deprecated; use APH")
7073

Diff for: mpisppy/opt/subgradient.py

+5-25
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,13 @@ def subgradient_main(self, finalize=True):
3737
The "trivial bound", computed by solving the model with no
3838
nonanticipativity constraints (immediately after iter 0).
3939
"""
40-
verbose = self.options['verbose']
41-
smoothed = self.options['smoothed']
42-
if smoothed != 0:
43-
raise RuntimeError("Cannnot use smoothing with Subgradient algorithm")
44-
self.PH_Prep(attach_prox=False, attach_smooth=smoothed)
45-
46-
if (verbose):
47-
print('Calling Subgradient Iter0 on global rank {}'.format(_global_rank))
48-
trivial_bound = self.Iter0()
49-
# set self.best_bound_obj_val if we don't have any additional fixed variables
50-
if self._can_update_best_bound():
51-
self.best_bound_obj_val = trivial_bound
52-
if (verbose):
53-
print('Completed Subgradient Iter0 on global rank {}'.format(_global_rank))
54-
55-
self.iterk_loop()
56-
57-
if finalize:
58-
Eobj = self.post_loops(self.extensions)
59-
else:
60-
Eobj = None
61-
62-
return self.conv, Eobj, trivial_bound
40+
return self.ph_main(finalize=finalize)
6341

6442
def ph_main(self, finalize=True):
65-
# for working with a PHHub
66-
return self.subgradient_main(finalize=finalize)
43+
# for use with the PH hub
44+
if self.options["smoothed"] != 0:
45+
raise RuntimeError("Cannnot use smoothing with Subgradient algorithm")
46+
return super().ph_main(finalize=finalize, attach_prox=False)
6747

6848
def solve_loop(self,
6949
solver_options=None,

0 commit comments

Comments
 (0)