コード例 #1
0
ファイル: onemax_mimc.py プロジェクト: zparnold/cs7641
def main():
    name_of_exp = "One Max"
    fitness = mlrose.OneMax()
    mimic = []
    z_s = ['RHC', 'SA', 'GA', 'MIMIC']
    for i in [100, 200, 300, 400, 500]:
        problem = mlrose.DiscreteOpt(length=15,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=2)
        print("MIMC")
        max_atts = 10
        best_state, best_fitness, learning_curve, timing_curve = mlrose.mimic(
            problem,
            pop_size=i,
            keep_pct=0.1,
            max_attempts=100,
            max_iters=100,
            curve=True,
            random_state=1,
            fast_mimic=True)
        mimic.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
    for x, z in zip([100, 200, 300, 400, 500], mimic):
        plt.plot(z, label=str(x))
    plt.legend()
    plt.title(
        'MIMIC Randomized Optimization PopSize vs Fitness Curve (OneMax)')
    plt.xlabel('Function iteration count')
    plt.ylabel('Fitness function value')
    plt.show()
コード例 #2
0
def run():
    # basePath = 'C:\\Users\\mwest\\Desktop\\ML\\source\\Machine-Learning-Local - Copy\\Graphs\\randomized\\Complexity\\One Max\\'
    basePath = None

    # lengths = range(1, 501, 25)
    # lengths = range(1, 101, 50)
    lengths = [10, 100, 200, 300, 400, 500]
    lengths = [10, 50, 100, 150, 200]
    lengths = [
        10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150
    ]
    # lengths = [5, 10]
    fitness = mlrose.OneMax()
    runComplexity('One Max', fitness, lengths)

    fitness = mlrose.FourPeaks(t_pct=0.10)
    runComplexity('Four Peaks', fitness, lengths)

    fitness = mlrose.FlipFlop()
    runComplexity('Flip Flop', fitness, lengths)

    # runComplexity('TSP', None, lengths)

    # fitness = mlrose.Queens()
    # runComplexity('Queens', fitness, lengths)

    return
コード例 #3
0
def gen_problem_onemax(problem_size):
    fitness = mlr.OneMax()
    maximize = True
    problem = mlr.DiscreteOpt(length=problem_size,
                              fitness_fn=fitness,
                              maximize=maximize)
    return problem, maximize
コード例 #4
0
def one_max(bit_length=50):
    fitness_fn = mlrose.OneMax()

    problem = mlrose.DiscreteOpt(length=bit_length,
                                 fitness_fn=fitness_fn,
                                 max_val=2)

    return problem
コード例 #5
0
def onemax(max_iter=500,
           early_stop=None,
           mimic_early_stop=100,
           n_runs=10,
           savedir=None):
    print('\n\n|========= One Max =========|\n')
    fitness = mlrose.OneMax()
    problem_size = [10, 100, 500]
    max_attempts = max_iter * 2 if early_stop is None else early_stop
    mimic_early_stop = max_attempts if mimic_early_stop is None else mimic_early_stop
    hyperparams = {
        'rhc': {
            'restarts': 0,
            'max_attempts': max_attempts
        },
        'mimic': {
            'pop_size': 1000,
            'keep_pct': 0.3,
            'max_attempts': mimic_early_stop,
            'fast_mimic': True
        },
        'sa': {
            'schedule': mlrose.GeomDecay(),
            'init_state': None,
            'max_attempts': max_attempts
        },
        'ga': {
            'pop_size': 1000,
            'mutation_prob': 0.25,
            'pop_breed_percent': 0.60,
            'elite_dreg_ratio': 0.90,
            'max_attempts': max_attempts
        }
    }
    print('Hyperparameters: ', hyperparams)

    results = []
    runtimes = []
    timings = {}
    for ps in problem_size:
        problem = mlrose.DiscreteOpt(ps, fitness, max_val=2, maximize=True)
        print('Running with input size', ps)
        print('-----------------------------')

        r, t, wt = util.optimize_iters(problem, max_iter, hyperparams, n_runs)
        results.append(r)
        runtimes.append(t)
        timings['ps{}'.format(ps)] = wt

    print('final runtimes')
    t = pd.DataFrame(runtimes, index=problem_size)
    print(t)

    if savedir:
        util.save_output('onemax', savedir, t, results, timings, problem_size)

    return t, results, timings
コード例 #6
0
 def OneMax(self, length=10, verbose=False):
     self.problem = 'onemax{l}'.format(l=length)
     self.verbose = verbose
     np.random.seed(0)
     problem_size = 1000
     fitness = mlrose.OneMax()
     state = np.random.randint(2, size=problem_size)
     self.problem_fit = mlrose.DiscreteOpt(length=problem_size,
                                           fitness_fn=fitness,
                                           maximize=True)
コード例 #7
0
ファイル: onemax.py プロジェクト: evilChipmunk/ML2
def run(): 
    # basePath = 'C:\\Users\\mwest\\Desktop\\ML\\source\\Machine-Learning-Local - Copy\\Graphs\\randomized\\One Max\\'
    basePath = None
    arrLen = 200 
    fitness = mlrose.FourPeaks(t_pct=0.15) 
    fitness = mlrose.OneMax()
    problem = mlrose.DiscreteOpt(length=arrLen, fitness_fn=fitness)  
    runHill(problem, basePath) 
    runAnnealing(problem, basePath) 
    runGenetic(problem, basePath)
    runMimic(problem, basePath)

    return
コード例 #8
0
ファイル: optima.py プロジェクト: JensenCoding/cs7641-2
    def get_prob(self, t_pct=None, p_length=None):
        if self.prob_name == 'Four Peaks':
            fitness = mlrose.FourPeaks(t_pct)
            p_len = 100
            self.schedule = mlrose.ExpDecay()
            self.restarts = 0
            self.mutation_prob = 0.1
            self.keep_pct = 0.1
            self.pop_size = 500
        elif self.prob_name == "Continuous Peaks":
            fitness = mlrose.ContinuousPeaks(t_pct)
            p_len = 100
            self.schedule = mlrose.GeomDecay()
            self.restarts = 0
            self.mutation_prob = 0.1
            self.keep_pct = 0.2
            self.pop_size = 200
        elif self.prob_name == "Max K Color":
            fitness = mlrose.MaxKColor(self.COLOREDGE)
            p_len = 100
            self.schedule = mlrose.ExpDecay()
            self.restarts = 0
            self.mutation_prob = 0.2
            self.keep_pct = 0.2
            self.pop_size = 200
        elif self.prob_name == "Flip Flop":
            fitness = mlrose.FlipFlop()
            p_len = 100
            self.schedule = mlrose.ArithDecay()
            self.restarts = 0
            self.mutation_prob = 0.2
            self.keep_pct = 0.5
            self.pop_size = 500
        elif self.prob_name == "One Max":
            fitness = mlrose.OneMax()
            p_len = 100
            self.schedule = mlrose.GeomDecay()
            self.restarts = 0
            self.mutation_prob = 0.2
            self.keep_pct = 0.1
            self.pop_size = 100
        else:
            fitness = None
            p_len = 0

        if p_length is None:
            p_length = p_len

        problem = mlrose.DiscreteOpt(length=p_length, fitness_fn=fitness)
        init_state = np.random.randint(2, size=p_length)
        return problem, init_state
コード例 #9
0
def OptCounOnes():
    fitness = mlrose.OneMax()
    validate(fitness, size, 'CounOnes')
コード例 #10
0
def main():
    name_of_exp = "One Max"
    fitness = mlrose.OneMax()
    rhc = []
    sa = []
    ga = []
    mimic = []
    z_s = ['RHC', 'SA', 'GA', 'MIMIC']
    for i in [5, 10, 15]:
        problem = mlrose.DiscreteOpt(length=i,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=2)
        # Define initial state
        init_state = np.zeros(i)

        # Solve problem using simulated annealing
        best_fitness = 0
        learning_curve = []
        max_atts = 10
        print("RHC")
        while best_fitness != i:
            best_state, best_fitness, learning_curve, timing_curve = mlrose.random_hill_climb(
                problem,
                max_attempts=max_atts,
                max_iters=max_atts,
                restarts=1,
                init_state=init_state,
                curve=True,
                random_state=1)
            max_atts += 10

        rhc.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
        print("SA")
        best_fitness = 0
        learning_curve = []
        max_atts = 10
        while best_fitness != i:
            best_state, best_fitness, learning_curve, timing_curve = mlrose.simulated_annealing(
                problem,
                max_attempts=max_atts,
                max_iters=max_atts,
                schedule=mlrose.ExpDecay(),
                init_state=init_state,
                curve=True,
                random_state=1)
            max_atts += 10

        sa.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
        print("GA")
        best_fitness = 0
        learning_curve = []
        max_atts = 10
        while best_fitness != i:
            best_state, best_fitness, learning_curve, timing_curve = mlrose.genetic_alg(
                problem,
                pop_size=100,
                mutation_prob=0.1,
                max_attempts=max_atts,
                max_iters=max_atts,
                curve=True,
                random_state=1)
            max_atts += 10
        ga.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
        print("MIMC")
        best_fitness = 0
        learning_curve = []
        max_atts = 10
        while best_fitness != i:
            best_state, best_fitness, learning_curve, timing_curve = mlrose.mimic(
                problem,
                pop_size=300,
                keep_pct=0.1,
                max_attempts=max_atts,
                max_iters=max_atts,
                curve=True,
                random_state=1,
                fast_mimic=True)
            max_atts += 10
        mimic.append(learning_curve)
        print(i)
        print(best_fitness)
        print(max_atts)
    f, axarr = plt.subplots(1, 4)
    f.set_figheight(3)
    f.set_figwidth(12)
    for y in rhc:
        for i in ['5', '10', '15']:
            axarr[0].plot(y, label='{}'.format(i))
    axarr[0].set_title('RHC vs Input Size')
    for y in sa:
        for i in ['5', '10', '15']:
            axarr[1].plot(y, label='{}'.format(i))
    axarr[1].set_title('SA vs Input Size')
    for y in ga:
        for i in ['5', '10', '15']:
            axarr[2].plot(y, label='{}'.format(i))
    axarr[2].set_title('GA vs Input Size')
    for y in mimic:
        for i in ['5', '10', '15']:
            axarr[3].plot(y, label='{}'.format(i))
    axarr[3].set_title('MIMC vs Input Size')
    # Fine-tune figure; hide x ticks for top plots and y ticks for right plots
    #plt.setp([a.get_xticklabels() for a in axarr[0]], visible=False)
    #plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
    plt.legend(handles=[
        Line2D([0], [0], color='g', lw=4, label='5'),
        Line2D([0], [0], color='brown', lw=4, label='10'),
        Line2D([0], [0], color='y', lw=4, label='15')
    ])
    #plt.title('Input size vs fitness curve One Max')
    # plt.xlabel('Function iteration count')
    #plt.ylabel('Fitness function value')

    plt.show()
コード例 #11
0
 def create_problem():
     initial_state = numpy.random.randint(2, size=size)
     problem = mlrose.DiscreteOpt(size, mlrose.OneMax())
     return initial_state, problem
コード例 #12
0
import six
import sys
sys.modules['sklearn.externals.six'] = six

import mlrose
import numpy as np
import time
import matplotlib.pyplot as plt

# Select Optimization Problems
fitness = mlrose.OneMax()
ProblemName = "One Max"
#fitness = mlrose.FlipFlop()
#ProblemName = "Flip Flop"
#fitness = mlrose.FourPeaks(t_pct=0.15)
#ProblemName = "Four Peaks"

# Define optimization problem object [24 bits String]
problem = mlrose.DiscreteOpt(length=24,
                             fitness_fn=fitness,
                             maximize=True,
                             max_val=2)

# Define Interation Parameter
#iterator = [5, 10, 15, 20, 25, 50, 100, 150, 200, 250]
iterator = range(5, 100, 5)
print(iterator)

# Solve using (1) simulated annealing [SA]
fitness_score1 = []
fitness_time1 = []
コード例 #13
0
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.style as style
import random
import pandas as pd

lengths = [10, 20, 40, 60, 80, 100]
avgAcross = 10

print("SOLVING ContinuousPeaks")
print("Get fitness for 100 iters on all algos")

itersList = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
problem = mlrose.DiscreteOpt(length=50,
                             maximize=True,
                             fitness_fn=mlrose.OneMax())

fitnessRHCAll = []
fitnessSAAll = []
fitnessGAAll = []
fitnessMIMICAll = []

fitnessRHCMean = []
fitnessSAMean = []
fitnessGAMean = []
fitnessMIMICMean = []

fitnessRHCFilter = []
fitnessSAFilter = []
fitnessGAFilter = []
fitnessMIMICFilter = []
コード例 #14
0
def __discrete_bit_size_problems(problem,
                                 algorithm,
                                 length,
                                 max_iter,
                                 max_attempt,
                                 init_state,
                                 edges=None,
                                 coords=None):
    if problem == 'fourpeaks':
        __fit = mlrose.FourPeaks()
        __problem = mlrose.DiscreteOpt(length=length,
                                       fitness_fn=__fit,
                                       maximize=True,
                                       max_val=2)
    elif problem == 'kcolor':
        __fit = mlrose.MaxKColor(edges=edges)
        __problem = mlrose.DiscreteOpt(length=length,
                                       fitness_fn=__fit,
                                       maximize=True)
    elif problem == 'flipflop':
        __fit = mlrose.OneMax()
        __problem = mlrose.DiscreteOpt(length=length,
                                       fitness_fn=__fit,
                                       maximize=True,
                                       max_val=2)
    elif problem == 'continouspeaks':
        __fit = mlrose.ContinuousPeaks()
        __problem = mlrose.DiscreteOpt(length=length,
                                       fitness_fn=__fit,
                                       maximize=True,
                                       max_val=2)
    elif problem == 'travellingsales':
        __fit = mlrose.TravellingSales(coords=coords)
        __problem = mlrose.TSPOpt(length=length,
                                  fitness_fn=__fit,
                                  maximize=False)

    if algorithm == 'random_hill_climb':
        start_time = time.time()
        best_state, best_fitness, best_curve = mlrose.random_hill_climb(
            __problem,
            max_iters=max_iter,
            max_attempts=max_attempt,
            init_state=init_state,
            curve=True)
        end_time = time.time() - start_time
    elif algorithm == 'simulated_annealing':
        start_time = time.time()
        best_state, best_fitness, best_curve = mlrose.simulated_annealing(
            __problem,
            max_iters=max_iter,
            max_attempts=max_attempt,
            init_state=init_state,
            curve=True)
        end_time = time.time() - start_time
    elif algorithm == 'genetic_alg':
        start_time = time.time()
        best_state, best_fitness, best_curve = mlrose.genetic_alg(
            __problem,
            max_iters=max_iter,
            max_attempts=max_attempt,
            curve=True)
        end_time = time.time() - start_time
    elif algorithm == 'mimic':
        start_time = time.time()
        best_state, best_fitness, best_curve = mlrose.mimic(
            __problem,
            max_iters=max_iter,
            max_attempts=max_attempt,
            curve=True)
        end_time = time.time() - start_time

    return best_fitness, end_time, best_curve
コード例 #15
0
def one_max():
    problem = mlrose.DiscreteOpt(length=20,
                                 fitness_fn=mlrose.OneMax(),
                                 maximize=True,
                                 max_val=2)
    init_state = np.array([0] * 20)
    startTime = datetime.now()
    best_state, best_fitness, fitness_curve_rhc = mlrose.random_hill_climb(
        problem,
        max_attempts=1000,
        max_iters=2500,
        restarts=0,
        init_state=init_state,
        curve=True,
        random_state=1,
        state_fitness_callback=None,
        callback_user_info=None)

    totalTime = datetime.now() - startTime
    rhcTime = totalTime.total_seconds()
    print("RHC")
    print("Time: ", rhcTime)
    print("best_fitness: ", best_fitness)
    print("Iteration: %d " % len(fitness_curve_rhc))

    ###############################
    startTime = datetime.now()
    best_statesa, best_fitnesssa, fitness_curve_sa = mlrose.simulated_annealing(
        problem,
        max_attempts=1000,
        max_iters=2500,
        init_state=init_state,
        curve=True,
        random_state=1,
        state_fitness_callback=None,
        callback_user_info=None)

    totalTime = datetime.now() - startTime
    saTime = totalTime.total_seconds()
    print("SA")
    print("Time: ", saTime)
    print("best_fitness: ", best_fitnesssa)
    print("Iteration: %d " % len(fitness_curve_sa))

    ###############################
    startTime = datetime.now()
    best_statega, best_fitnessga, fitness_curve_ga = mlrose.genetic_alg(
        problem,
        max_attempts=1000,
        max_iters=2500,
        curve=True,
        random_state=1,
        state_fitness_callback=None,
        callback_user_info=None)

    totalTime = datetime.now() - startTime
    gaTime = totalTime.total_seconds()
    print("GA")
    print("Time: ", gaTime)
    print("best_fitness: ", best_fitnessga)
    print("Iteration:  %d " % len(fitness_curve_ga))

    ###############################
    startTime = datetime.now()
    best_statemm, best_fitnessmm, fitness_curve_mm = mlrose.mimic(
        problem,
        max_attempts=1000,
        max_iters=2500,
        curve=True,
        random_state=1,
        state_fitness_callback=None,
        callback_user_info=None)

    totalTime = datetime.now() - startTime
    mmTime = totalTime.total_seconds()
    print("MIMIC")
    print("Time: ", mmTime)
    print("best_fitness: ", best_fitnessmm)
    print("Iteration: %d " % len(fitness_curve_mm))
コード例 #16
0
import sklearn as sk
import mlrose as ml
import numpy as np
import matplotlib.pyplot as plt
import random
import time

sample_sizes = []
fitness = ml.OneMax()

main_best_fit_gene = []
main_execution_times_gene = []
main_total_iterations_gene = []
main_opt_iterations_gene = []

for y in range(10):
    best_fit_gene = []
    execution_times_gene = []
    total_iterations_gene = []
    opt_iterations_gene = []
    for x in range(5):
        start = time.time()
        opt = ml.DiscreteOpt((y * 6) + 6, fitness)
        #best_state, best_fitness, curve = ml.random_hill_climb(opt, max_iters=5, restarts = 0, curve=True, random_state = 2)
        best_state_gene, best_fitness_gene, curve_gene = ml.genetic_alg(
            opt,
            pop_size=(y * 6) + 6,
            max_attempts=100,
            curve=True,
            random_state=x)