Skip to content

Commit

Permalink
Sort clamps by sensitivity before iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBangar committed Feb 14, 2024
1 parent 7347486 commit 7c2f9fd
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/classy_blocks/modify/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def fquality(params):
return self.grid.quality
return junction.quality

scipy.optimize.minimize(fquality, clamp.params, bounds=clamp.bounds, method="SLSQP", options={"maxiter": 10})
scipy.optimize.minimize(fquality, clamp.params, bounds=clamp.bounds, method="SLSQP")

current_grid_quality = self.grid.quality

Expand All @@ -190,14 +190,29 @@ def fquality(params):

return initial_grid_quality / current_grid_quality

def _get_sensitivity(self, clamp):
"""Returns maximum partial derivative at current params"""
junction = self._get_junction(clamp)

def fquality(clamp, junction, params):
clamp.update_params(params)
return junction.quality

sensitivities = scipy.optimize.approx_fprime(clamp.params, lambda p: fquality(clamp, junction, p), epsilon=1e-6)
return max([abs(s) for s in sensitivities])

def optimize_iteration(self, iteration: IterationData) -> None:
# gather points that can be moved with optimization
for junction in self.grid.get_ordered_junctions():
try:
clamp = self._get_clamp(junction)
self.optimize_clamp(clamp, iteration)
except NoClampError:
continue
# for junction in self.grid.get_ordered_junctions():
# try:
# clamp = self._get_clamp(junction)
# self.optimize_clamp(clamp, iteration)
# except NoClampError:
# continue
self.clamps.sort(key=lambda c: self._get_sensitivity(c), reverse=True)

for clamp in self.clamps:
self.optimize_clamp(clamp, iteration)

def optimize(self, max_iterations: int = 20, relaxed_iterations: int = 2, tolerance: float = 0.1) -> None:
"""Move vertices, defined and restrained with Clamps
Expand Down

0 comments on commit 7c2f9fd

Please sign in to comment.