Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature addition, add noise to mimic #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mlrose/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please update the parameters list to include a description of the noise parameter.

"""Use MIMIC to find the optimum for a given optimization problem.

Parameters
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion mlrose/opt_probs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see where self.noise gets updated to the noise value set in the mimic function.


def eval_node_probs(self):
"""Update probability density estimates.
Expand Down Expand Up @@ -387,10 +388,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
Expand Down