Skip to content

Commit

Permalink
chore: added progrss bar to DE optimiser
Browse files Browse the repository at this point in the history
  • Loading branch information
rabii-chaarani committed Jun 6, 2024
1 parent 5cca9eb commit f846d3e
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 4,337 deletions.
4,465 changes: 136 additions & 4,329 deletions FoldOptLib/examples/axial_surface_optimisation_part_1.ipynb

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions FoldOptLib/optimisers/axial_surface_optimiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def generate_bounds_and_initial_guess(self):
The initial guess is generated using the Von Mises Fisher distribution.
"""
self.bounds = [(0, 360), (0, 90)]
self.bounds = [(0, numpy.pi*2), (0, numpy.pi/2)]

if self.geological_knowledge is not None:

Expand All @@ -134,6 +134,7 @@ def generate_bounds_and_initial_guess(self):
# Sample from the distribution
initial_guess = vmf.draw_samples(size=20, random_state=180564327)
initial_guess = normal_vector_to_strike_and_dip(initial_guess)
initial_guess = numpy.deg2rad(initial_guess)
return initial_guess

if self.geological_knowledge is None:
Expand Down Expand Up @@ -218,7 +219,6 @@ def build_optimisation_function(
knowledge_function: GeologicalKnowledgeFunctions = None,
):
def optimisation_function(strike_dip):
print(strike_dip)
# Convert the strike-dip to a unit vector
unit_vector = strike_dip_to_vector(strike_dip[0], strike_dip[1])
# Normalize the unit vector
Expand All @@ -229,7 +229,6 @@ def optimisation_function(strike_dip):
angle_difference = ObjectiveFunction[ObjectiveType.ANGLE](
predicted_foliation, self.gradient_data
)
print("angle difference: ",angle_difference)

# If the optimisation type is angle, return the angle difference
if self.optimisation_type == OptimisationType.ANGLE:
Expand Down
13 changes: 10 additions & 3 deletions FoldOptLib/solvers/solvers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Callable, Dict, Any, Tuple, Union, List
import numpy
from ..utils.utils import *
from ..utils.utils import create_progress_bar
from scipy.optimize import minimize, differential_evolution
from ..datatypes import SolverType
import beartype
Expand Down Expand Up @@ -32,9 +32,10 @@ def differential_evolution(
init: Union[str, numpy.ndarray] = "halton",
maxiter: int = 5000,
seed: int = 80,
polish: bool = True,
polish: bool = False,
strategy: str = "best2exp",
mutation: Tuple[float, float] = (0.3, 0.99),
callback=create_progress_bar(5000),
**kwargs,
) -> Dict:
"""
Expand Down Expand Up @@ -77,6 +78,7 @@ def differential_evolution(
polish=polish,
strategy=strategy,
mutation=mutation,
callback=callback,
**kwargs,
)

Expand All @@ -85,7 +87,11 @@ def differential_evolution(
@beartype.beartype
@staticmethod
def constrained_trust_region(
objective_function: Callable, x0: numpy.ndarray, constraints=None, **kwargs
objective_function: Callable,
x0: numpy.ndarray,
constraints=None,
callback=create_progress_bar(5000),
**kwargs
) -> Dict:
"""
Solves the optimisation problem using the trust region method.
Expand All @@ -111,6 +117,7 @@ def constrained_trust_region(
method="trust-constr",
jac="2-point",
constraints=constraints,
callback=callback,
**kwargs,
)

Expand Down
20 changes: 18 additions & 2 deletions FoldOptLib/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ..from_loopstructural._svariogram import SVariogram
import mplstereonet
import dill
from tqdm import tqdm


def calculate_semivariogram(fold_frame, fold_rotation, lag=None, nlag=None):
Expand Down Expand Up @@ -117,8 +118,8 @@ def strike_dip_to_vector(strike, dip):
raise TypeError(f"Expected dip to be a number, got {type(dip).__name__}")

# Convert degrees to radians
s_r = numpy.deg2rad(strike)
d_r = numpy.deg2rad(dip)
s_r = strike
d_r = dip

# Calculate the components of the strike-dip vector
nx = numpy.sin(d_r) * numpy.cos(s_r)
Expand Down Expand Up @@ -459,3 +460,18 @@ def objective_function(x):
return func1(x) + func2(x)

return objective_function

def create_progress_bar(max_iter):
pbar = tqdm(dynamic_ncols=True, desc="Optimisation Progress")

# def callback(xk, convergence):
def callback(xk, convergence):
# pbar.set_description(f"Strike/Dip: {numpy.rad2deg(xk):.4f}")
pbar.update(1)
if convergence < 1e-6:
pbar.close()
return True
return False

return callback

0 comments on commit f846d3e

Please sign in to comment.