Skip to content

Commit

Permalink
min acpt rate to stop
Browse files Browse the repository at this point in the history
  • Loading branch information
faizan90 committed Feb 10, 2020
1 parent d1a4ea4 commit 10fa533
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
42 changes: 30 additions & 12 deletions core/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,23 @@ class PhaseAnnealingAlgObjective:
def _get_obj_scorr_val(self):

obj_val = (
((self._ref_scorrs -
self._sim_scorrs) ** 2).sum() /
((self._ref_scorrs - self._sim_scorrs) ** 2).sum() /
self._sett_obj_lag_steps.size)

return obj_val

def _get_obj_asymms_1_val(self):

obj_val = (
((self._ref_asymms_1 -
self._sim_asymms_1) ** 2).sum() /
((self._ref_asymms_1 - self._sim_asymms_1) ** 2).sum() /
self._sett_obj_lag_steps.size)

return obj_val

def _get_obj_asymms_2_val(self):

obj_val = (
((self._ref_asymms_2 -
self._sim_asymms_2) ** 2).sum() /
((self._ref_asymms_2 - self._sim_asymms_2) ** 2).sum() /
self._sett_obj_lag_steps.size)

return obj_val
Expand All @@ -67,7 +64,7 @@ def _get_obj_ecop_etpy_val(self):

return obj_val

def _obj_nth_ord_diffs_val(self):
def _get_obj_nth_ord_diffs_val(self):

obj_val = 0.0
for nth_ord in self._sett_obj_nth_ords:
Expand All @@ -80,14 +77,23 @@ def _obj_nth_ord_diffs_val(self):

sim_probs = ftn(sim_diffs)

# obj_val += (
# ((ref_probs - sim_probs) ** 2).sum() /
# self._sett_obj_nth_ords.size)
# obj_val += (
# ((ref_probs - sim_probs) ** 2).sum() /
# self._sett_obj_nth_ords.size)

corr = np.corrcoef(ref_probs, sim_probs)[0, 1]

obj_val += ((1 - corr) ** 2) / self._sett_obj_nth_ords.size

# max_diff = np.abs(ref_probs - sim_probs).max()
#
# obj_val += (max_diff ** 0.01) / self._sett_obj_nth_ords.size

# diffs_corr = np.corrcoef(
# sim_diffs, self._ref_nth_ords_cdfs_dict[nth_ord].x)[0, 1]
#
# obj_val += ((1 - diffs_corr) ** 2) / self._sett_obj_nth_ords.size

return obj_val

def _get_obj_ftn_val(self):
Expand All @@ -110,7 +116,7 @@ def _get_obj_ftn_val(self):
obj_val += self._get_obj_ecop_etpy_val()

if self._sett_obj_nth_ord_diffs_flag:
obj_val += self._obj_nth_ord_diffs_val()
obj_val += self._get_obj_nth_ord_diffs_val()

assert np.isfinite(obj_val), 'Invalid obj_val!'

Expand Down Expand Up @@ -625,6 +631,10 @@ def _auto_temp_search(self):

min_acpt_tem = +np.inf
max_acpt_tem = -np.inf

min_acpt_rate = +np.inf
max_acpt_rate = -np.inf

not_acptd_ct = 0
auto_temp_search_ress = []
for i in range(self._sett_misc_n_cpus):
Expand All @@ -649,6 +659,12 @@ def _auto_temp_search(self):
min_acpt_tem = min(min_acpt_tem, acpt_rates_temps[:, 1].min())
max_acpt_tem = max(max_acpt_tem, acpt_rates_temps[:, 1].max())

min_acpt_rate = min(
min_acpt_rate, acpt_rates_temps[:, 0].min())

max_acpt_rate = max(
max_acpt_rate, acpt_rates_temps[:, 0].max())

best_acpt_rate_idx = np.argmin(
(acpt_rates_temps[:, 0] -
self._sett_ann_auto_init_temp_trgt_acpt_rate) ** 2)
Expand All @@ -674,6 +690,8 @@ def _auto_temp_search(self):
min_acpt_tem,
max_acpt_tem)

print('And acceptance rates:', min_acpt_rate, max_acpt_rate)

if not np.all(np.isfinite([min_acpt_tem, max_acpt_tem])):
raise RuntimeError(
'Could not find initial temperatures automatically!')
Expand Down Expand Up @@ -878,7 +896,7 @@ def _get_stopp_criteria(self, test_vars):
(tol > self._sett_ann_obj_tol),
(not np.isclose(temp, 0.0)),
(not np.isclose(phs_red_rate, 0.0)),
(not np.isclose(acpt_rate, 0.0)),
(acpt_rate > self._sett_ann_stop_acpt_rate),
)

return stopp_criteria
Expand Down
18 changes: 17 additions & 1 deletion core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, verbose=True):
self._sett_ann_obj_tol = None
self._sett_ann_obj_tol_iters = None
self._sett_ann_acpt_rate_iters = None
self._sett_ann_stop_acpt_rate = None
self._sett_ann_phs_red_rate_type = None
self._sett_ann_phs_red_rate = None
self._sett_ann_mag_spec_cdf_idxs_flag = None
Expand Down Expand Up @@ -239,9 +240,10 @@ def set_annealing_settings(
objective_tolerance,
objective_tolerance_iterations,
acceptance_rate_iterations,
stop_acpt_rate,
phase_reduction_rate_type,
mag_spec_index_sample_flag,
phase_reduction_rate=None):
phase_reduction_rate):

'''
Simulated annealing algorithm parameters
Expand Down Expand Up @@ -272,6 +274,9 @@ def set_annealing_settings(
acceptance_rate_iterations : integer
Number of iterations to take for mean acceptance rate. Should be
greater than 0.
stop_acpt_rate : float
The acceptance rate at or below which the optimization stops.
Should be >= 0 and <= 1.
phase_reduction_rate_type : integer
How to limit the magnitude of the newly generated phases.
A number between 0 and 3.
Expand Down Expand Up @@ -321,6 +326,9 @@ def set_annealing_settings(
assert isinstance(acceptance_rate_iterations, int), (
'acceptance_rate_iterations not an integer!')

assert isinstance(stop_acpt_rate, float), (
'stop_acpt_rate not a float!')

assert 0 < initial_annealing_temperature < np.inf, (
'Invalid initial_annealing_temperature!')

Expand All @@ -344,6 +352,9 @@ def set_annealing_settings(
assert acceptance_rate_iterations >= 0, (
'Invalid acceptance_rate_iterations!')

assert 0 <= stop_acpt_rate <= 1.0, (
'Invalid stop_acpt_rate!')

assert 0 <= phase_reduction_rate_type <= 3, (
'Invalid phase_reduction_rate_type!')

Expand Down Expand Up @@ -372,6 +383,7 @@ def set_annealing_settings(
self._sett_ann_obj_tol = objective_tolerance
self._sett_ann_obj_tol_iters = objective_tolerance_iterations
self._sett_ann_acpt_rate_iters = acceptance_rate_iterations
self._sett_ann_stop_acpt_rate = stop_acpt_rate
self._sett_ann_phs_red_rate_type = phase_reduction_rate_type
self._sett_ann_mag_spec_cdf_idxs_flag = mag_spec_index_sample_flag

Expand Down Expand Up @@ -414,6 +426,10 @@ def set_annealing_settings(
'Acceptance rate iterations:',
self._sett_ann_acpt_rate_iters)

print(
'Stopping acceptance rate:',
self._sett_ann_stop_acpt_rate)

print(
'Phase reduction rate type:',
self._sett_ann_phs_red_rate_type)
Expand Down
21 changes: 10 additions & 11 deletions test/test_phsann.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def main():

verbose = True

sim_label = 'obj_000001'
sim_label = 'obj_100000_with_shifted_corrs'

h5_name = 'phsann.h5'

Expand All @@ -58,11 +58,7 @@ def main():
long_test_flag = True
# long_test_flag = False

# TODO: make auto init eff, limiting beg and end temps after first n_cpus
# sims. Some back and forth of tem mvmnt as well.
# For new sims, random temp b/w min and max temps that we have.
# If none found then stop. If some found then they can be used for the
# ones with no ini. temp.
# TODO: number of auto init temp sims
# TODO: add logging
# TODO: summary table plot
auto_init_temperature_flag = True
Expand All @@ -76,15 +72,15 @@ def main():
nth_order_diffs_flag = True

# scorr_flag = False
# asymm_type_1_flag = False
# asymm_type_2_flag = False
# ecop_dens_flag = False
# ecop_etpy_flag = False
asymm_type_1_flag = False
asymm_type_2_flag = False
ecop_dens_flag = False
ecop_etpy_flag = False
nth_order_diffs_flag = False

n_reals = 7
outputs_dir = main_dir / sim_label
n_cpus = 7
n_cpus = 'auto'

lag_steps = np.array([1, 2, 3, 4, 5])
ecop_bins = 50
Expand All @@ -103,6 +99,7 @@ def main():
objective_tolerance = 1e-8
objective_tolerance_iterations = 100
phase_reduction_rate = 0.999
stop_acpt_rate = 0.025

temperature_lower_bound = 0.00001
temperature_upper_bound = 1000.0
Expand All @@ -125,6 +122,7 @@ def main():
objective_tolerance = 1e-8
objective_tolerance_iterations = 20
phase_reduction_rate = 0.99
stop_acpt_rate = 0.1

temperature_lower_bound = 0.0001
temperature_upper_bound = 1000.0
Expand Down Expand Up @@ -170,6 +168,7 @@ def main():
objective_tolerance,
objective_tolerance_iterations,
acceptance_rate_iterations,
stop_acpt_rate,
phase_reduction_rate_type,
mag_spec_index_sample_flag,
phase_reduction_rate)
Expand Down

0 comments on commit 10fa533

Please sign in to comment.