From c235e02d5943afab26d13bebeefab58e3c6b0c38 Mon Sep 17 00:00:00 2001 From: dsp Date: Sat, 5 Oct 2019 23:58:26 -0400 Subject: [PATCH 1/2] mimic noise feature addition --- mlrose/algorithms.py | 7 ++++++- mlrose/opt_probs.py | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/mlrose/algorithms.py b/mlrose/algorithms.py index a00d431d..88142a01 100644 --- a/mlrose/algorithms.py +++ b/mlrose/algorithms.py @@ -456,7 +456,7 @@ def genetic_alg(problem, pop_size=200, mutation_prob=0.1, max_attempts=10, def mimic(problem, pop_size=200, keep_pct=0.2, max_attempts=10, - max_iters=np.inf, curve=False, random_state=None, fast_mimic=False): + max_iters=np.inf, curve=False, random_state=None, fast_mimic=False,noise=0): """Use MIMIC to find the optimum for a given optimization problem. Parameters @@ -539,6 +539,11 @@ def mimic(problem, pop_size=200, keep_pct=0.2, max_attempts=10, else: problem.mimic_speed=fast_mimic + if (noise < 0) or (noise > 0.1): + raise Exception("""noise must be between 0 and 0.1.""") + else: + problem.noise=noise + # Initialize problem, population and attempts counter problem.reset() problem.random_pop(pop_size) diff --git a/mlrose/opt_probs.py b/mlrose/opt_probs.py index f3831b7d..32ef21d1 100644 --- a/mlrose/opt_probs.py +++ b/mlrose/opt_probs.py @@ -364,10 +364,24 @@ def eval_node_probs(self): if not len(subset): probs[i, j] = 1/self.max_val else: - probs[i, j] = np.histogram(subset[:, i], + + temp_probs = np.histogram(subset[:, i], np.arange(self.max_val + 1), density=True)[0] + #Check if noise argument is not default (in epsilon) + if self.noise!=0: + #Add noise, from the mimic argument "noise" + temp_probs = (temp_probs + self.noise) + #All probability adds up to one + temp_probs=np.divide(temp_probs,np.sum(temp_probs)) + #Handle floating point error to ensure probability adds up to 1 + if sum(temp_probs) != 1.0: + temp_probs = np.divide(temp_probs, np.sum(temp_probs)) + #Set probability + + probs[i, j]=temp_probs + # Update probs and parent self.node_probs = probs self.parent_nodes = parent From ff1344facc5472c707b12947ef168f09bd29e6c0 Mon Sep 17 00:00:00 2001 From: dsp Date: Sun, 6 Oct 2019 14:33:01 -0400 Subject: [PATCH 2/2] mimic noise description added, included noise for discreteopt initialization --- mlrose/algorithms.py | 3 +++ mlrose/opt_probs.py | 1 + 2 files changed, 4 insertions(+) diff --git a/mlrose/algorithms.py b/mlrose/algorithms.py index 88142a01..1b0faf43 100644 --- a/mlrose/algorithms.py +++ b/mlrose/algorithms.py @@ -484,6 +484,9 @@ def mimic(problem, pop_size=200, keep_pct=0.2, max_attempts=10, fast_mimic: bool, default: False Activate fast mimic mode to compute the mutual information in vectorized form Faster speed but requires more memory. + noise: float, default: 0 + Adds noise to the probability and conditional probability estimates. + 0.01 corresponds to 1% noise. Returns ------- diff --git a/mlrose/opt_probs.py b/mlrose/opt_probs.py index d0a68d8e..00a0e5a2 100644 --- a/mlrose/opt_probs.py +++ b/mlrose/opt_probs.py @@ -272,6 +272,7 @@ def __init__(self, length, fitness_fn, maximize=True, max_val=2): self.sample_order = [] self.prob_type = 'discrete' self.mimic_speed = False + self.noise=0 def eval_node_probs(self): """Update probability density estimates.