示例#1
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
 def FlipFlop(self, length=8, verbose=False):
     self.problem = 'flipflop{l}'.format(l=length)
     self.verbose = verbose
     fitness_fn = mlrose.FlipFlop()
     self.problem_fit = mlrose.DiscreteOpt(length=length,
                                           fitness_fn=fitness_fn,
                                           maximize=True)
示例#3
0
 def create_problem(self):
     fitness = mlrose.FlipFlop()
     problem = mlrose.DiscreteOpt(length=self.length,
                                  fitness_fn=fitness,
                                  maximize=True,
                                  max_val=2)
     return problem
 def create_problem(self):
     edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]
     fitness = mlrose.FlipFlop()
     problem = mlrose.DiscreteOpt(length=self.length,
                                  fitness_fn=fitness,
                                  maximize=True,
                                  max_val=2)
     return problem
示例#5
0
def flipflop(max_iter=500,
             early_stop=None,
             mimic_early_stop=100,
             n_runs=10,
             savedir=None):
    print('\n\n|========= Flip Flop =========|\n')
    fitness = mlrose.FlipFlop()
    problem_size = [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': 500,
            'keep_pct': 0.2,
            '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.2,
            'pop_breed_percent': 0.75,
            'elite_dreg_ratio': 0.95,
            'max_attempts': mimic_early_stop
        }
    }
    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('flipflop', savedir, t, results, timings,
                         problem_size)

    return t, results, timings
示例#6
0
    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
示例#7
0
文件: a2.py 项目: rkaufholz3/a2
def fitness_function(f, bits, rs, verbose):

    if verbose:
        print('\n\n----------', f, ':', bits, 'bits ----------')

    if f == 'Four Peaks':
        fitness_fn = mlrose.FourPeaks(
            t_pct=0.15
        )  # Note: T= np.ceil(t_pct * n), per source code for FourPeaks.evaluate

    elif f == 'MaxKColor':
        # fitness_fn = mlrose.MaxKColor(edges)  # default mlrose fitness function
        # edges = [(0, 1), (0, 2), (1, 3), (2, 3)]  # 4 nodes, 2 by 2 grid, no diagonals
        # edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]
        edges = generate_graph(bits)
        kwargs = {'edges': edges}
        fitness_fn = mlrose.CustomFitness(
            kcolors_max,
            **kwargs)  # custom fitness function for maximization problem

    elif f == 'Knapsack':
        # weights = [10, 5, 2, 8, 15]
        # values = [1, 2, 3, 4, 5]
        weights, values = generate_knapsack(bits, rs)
        if verbose:
            print('\nKnapsack\n', weights, values)
        max_weight_pct = 0.6
        fitness_fn = mlrose.Knapsack(weights, values, max_weight_pct)

    elif f == 'FlipFlop':
        fitness_fn = mlrose.FlipFlop()

    # Check fitness for ad-hoc states
    # test_state = np.array([1, 0, 1, 1, 0])
    # print("Fitness for test_state", test_state, ":", fitness_fn.evaluate(test_state))

    return fitness_fn
示例#8
0
import time as tt
import numpy as np
import mlrose as mlrose
import pandas as pd

ps = [8,32,64,128]


for p in ps:
    fitness = mlrose.FlipFlop()
    istate = np.array(np.zeros(p), dtype=int)

    problem = mlrose.DiscreteOpt(length=p, fitness_fn=fitness,
                                 maximize=True, max_val=2)

    schedule = mlrose.ExpDecay(init_temp=0.5, exp_const=0.005, min_temp=0.001)

    start_fit_time = tt.time()
    best_state, best_fitness,fitness_curve = mlrose.random_hill_climb(problem,max_attempts = 500, max_iters = 500,restarts=0,init_state = istate, random_state = 1,curve=True)

    end_fit_time = tt.time()

    it = len(fitness_curve)

    time = end_fit_time - start_fit_time
    tpi = time/it
    print('SA')    
    print(f'sa, input size: {p},time={time}, iterations={it}, time per iteration={tpi}')


示例#9
0
 def create_problem(size):
     initial_state = numpy.random.randint(2, size=size)
     problem = mlrose.DiscreteOpt(size, mlrose.FlipFlop())
     return initial_state, problem
示例#10
0
                                 max_val=2,
                                 fitness_fn=saw_fitness)

base_test(saw_fitness,
          theoretical_best_fitness=lambda x: x,
          lengths=range(200, 701, 100))  # 2476s
optimize_ga(saw_problem)  # 7255s
optimize_sa(saw_problem)  # 8730s
optimize_rhc(saw_problem)  # 103s
optimize_mimic(saw_problem)  # 2192s
final_test(saw_problem, [600, 0.001], mlrose.ExpDecay(5, 0.0007), 1500,
           [500, 0.075])  # 14808

# FLIP FLOP

flipflop = mlrose.FlipFlop()
flipflop_problem = mlrose.DiscreteOpt(length=60,
                                      maximize=True,
                                      max_val=2,
                                      fitness_fn=flipflop)

base_test(flipflop,
          theoretical_best_fitness=lambda x: x - 1,
          lengths=range(50, 111, 10))  # 364s
optimize_ga(flipflop_problem)  # 1782s
optimize_sa(flipflop_problem)  # 4173s
optimize_rhc(flipflop_problem)  # 3839s
optimize_mimic(flipflop_problem)  # 5750s
final_test(flipflop_problem, [100, 0.1], mlrose.ExpDecay(1, 0.003), 1500,
           [500, 0.1])  # 1065s
示例#11
0
def run_flipflop():

    # If the output/FlipFlop directory doesn't exist, create it.
    if not os.path.exists('./output/FlipFlop/'):
        os.mkdir('./output/FlipFlop/')

    problem_size = 50
    logger = logging.getLogger(__name__)
    flip_fit = mlrose.FlipFlop()
    flop_state_gen = lambda: np.random.randint(2, size=problem_size)
    init_state = flop_state_gen()
    problem = mlrose.DiscreteOpt(length=problem_size, fitness_fn=flip_fit)
    problem2 = mlrose.DiscreteOpt(length=problem_size, fitness_fn=flip_fit)
    problem3 = mlrose.DiscreteOpt(length=problem_size, fitness_fn=flip_fit)
    problem4 = mlrose.DiscreteOpt(length=problem_size, fitness_fn=flip_fit)
    all_results = {}
    """
    print("Running random hill montecarlos")
    rhc_results, rhc_timing = rhc_runner(problem)
    rhc_best_params =plot_montecarlo_sensitivity('FlipFlop', 'rhc', rhc_results)
    plot_montecarlo_sensitivity('FlipFlop', 'rhc_timing', rhc_timing)
    all_results['RHC'] = [rhc_results, rhc_timing]

    print("Running simulated annealing montecarlos")
    sa_results, sa_timing = sim_annealing_runner(problem2)
    sa_best_param = plot_montecarlo_sensitivity('FlipFlop', 'sim_anneal', sa_results)
    plot_montecarlo_sensitivity('FlipFlop', 'sim_anneal_timing', sa_timing)
    all_results['SA'] = [sa_results, sa_timing]

    print("Running genetic algorithm montecarlos")
    ga_results, ga_timing = ga_runner(problem3, init_state)
    ga_best_param = plot_montecarlo_sensitivity('FlipFlop', 'ga', ga_results)
    plot_montecarlo_sensitivity('FlipFlop', 'ga_timing', ga_timing)
    all_results['GA'] = [ga_results, ga_timing]

    print("Running MIMIC montecarlos")
    mimic_results, mimic_timing = mimic_runner(problem4, init_state)
    MIMIC_best_param = plot_montecarlo_sensitivity('FlipFlop', 'mimic', mimic_results)
    plot_montecarlo_sensitivity('FlipFlop', 'mimic_timing', mimic_timing)
    all_results['MIMIC'] = [mimic_results, mimic_timing]
    """
    with open('./output/FlipFlop/flipflip_data.pickle', 'wb') as handle:
        pickle.dump(all_results, handle, protocol=pickle.HIGHEST_PROTOCOL)

    problem_size_space = np.linspace(10, 125, 10, dtype=int)

    best_fit_dict = {}
    best_fit_dict['Problem Size'] = problem_size_space
    best_fit_dict['Random Hill Climbing'] = []
    best_fit_dict['Simulated Annealing'] = []
    best_fit_dict['Genetic Algorithm'] = []
    best_fit_dict['MIMIC'] = []

    times = {}
    times['Problem Size'] = problem_size_space
    times['Random Hill Climbing'] = []
    times['Simulated Annealing'] = []
    times['Genetic Algorithm'] = []
    times['MIMIC'] = []

    fits_per_iteration = {}
    fits_per_iteration['Random Hill Climbing'] = []
    fits_per_iteration['Simulated Annealing'] = []
    fits_per_iteration['Genetic Algorithm'] = []
    fits_per_iteration['MIMIC'] = []

    for prob_size in problem_size_space:
        logger.info("---- Problem size: " + str(prob_size) + " ----")
        prob_size_int = int(prob_size)
        flip_fit = mlrose.FlipFlop()
        flop_state_gen = lambda: np.random.randint(2, size=prob_size_int)
        init_state = flop_state_gen()
        problem = mlrose.DiscreteOpt(length=prob_size_int,
                                     fitness_fn=flip_fit,
                                     maximize=True,
                                     max_val=2)

        start = datetime.now()
        best_state_sa, best_fitness_sa, fitness_curve_sa = mlrose.simulated_annealing(
            problem,
            schedule=mlrose.ExpDecay(exp_const=.401,
                                     init_temp=0.6,
                                     min_temp=0.101),
            max_attempts=110,
            max_iters=1100,
            curve=True)
        best_fit_dict['Simulated Annealing'].append(best_fitness_sa)
        end = datetime.now()
        times['Simulated Annealing'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_rhc, best_fitness_rhc, fitness_curve_rhc = mlrose.random_hill_climb(
            problem, max_attempts=410, max_iters=1100, restarts=40, curve=True)
        best_fit_dict['Random Hill Climbing'].append(best_fitness_rhc)
        end = datetime.now()
        times['Random Hill Climbing'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_ga, best_fitness_ga, fitness_curve_ga = mlrose.genetic_alg(
            problem,
            pop_size=174,
            mutation_prob=.001,
            max_attempts=410,
            max_iters=1000,
            curve=True)
        best_fit_dict['Genetic Algorithm'].append(best_fitness_ga)
        end = datetime.now()
        times['Genetic Algorithm'].append((end - start).total_seconds())

        start = datetime.now()
        best_state_mimic, best_fitness_mimic, fitness_curve_mimic = mlrose.mimic(
            problem,
            pop_size=252,
            keep_pct=.21,
            max_attempts=30,
            max_iters=1100,
            curve=True)
        best_fit_dict['MIMIC'].append(best_fitness_mimic)
        end = datetime.now()
        times['MIMIC'].append((end - start).total_seconds())

    # For the last fit that occurs, save off the fit arrays that are generated. We will plot fitness/iteration.
    fits_per_iteration['Random Hill Climbing'] = fitness_curve_rhc
    fits_per_iteration['Simulated Annealing'] = fitness_curve_sa
    fits_per_iteration['Genetic Algorithm'] = fitness_curve_ga
    fits_per_iteration['MIMIC'] = fitness_curve_mimic

    fit_frame = pd.DataFrame.from_dict(best_fit_dict,
                                       orient='index').transpose()
    # fit_frame.pop('Unnamed: 0') # idk why this shows up.
    time_frame = pd.DataFrame.from_dict(times, orient='index').transpose()
    # time_frame.pop('Unnamed: 0') # idk why this shows up.
    fit_iteration_frame = pd.DataFrame.from_dict(fits_per_iteration,
                                                 orient='index').transpose()

    fit_frame.to_csv('./output/FlipFlop/problem_size_fit.csv')
    time_frame.to_csv('./output/FlipFlop/problem_size_time.csv')
    fit_iteration_frame.to_csv('./output/FlipFlop/fit_per_iteration.csv')
示例#12
0
def main():
    name_of_exp = "Flip-Flop"
    fitness = mlrose.FlipFlop()
    problem = mlrose.DiscreteOpt(length=8,
                                 fitness_fn=fitness,
                                 maximize=True,
                                 max_val=2)

    # Define initial state
    init_state = np.zeros(8)
    x_s = []
    y_s = []
    z_s = ['RHC', 'SA', 'GA', 'MIMIC']
    w_s = []
    max_val = 7.0
    found_flag = False
    for restarts in np.arange(0, 5):
        if found_flag:
            break
        for max_iter_atts in np.arange(10, 1000, 10):
            if found_flag:
                break
            # Solve problem using simulated annealing
            best_state, best_fitness, learning_curve, timing_curve = mlrose.random_hill_climb(
                problem,
                max_attempts=int(max_iter_atts),
                max_iters=int(max_iter_atts),
                restarts=int(restarts),
                init_state=init_state,
                curve=True,
                random_state=1)
            if best_fitness == max_val:
                x_s.append(np.arange(0, len(learning_curve)))
                y_s.append(learning_curve)
                w_s.append(timing_curve)
                print(best_state)
                print(best_fitness)
                print(max_iter_atts)
                print(restarts)
                found_flag = True

    found_flag = False
    for sched in [mlrose.ExpDecay(), mlrose.GeomDecay(), mlrose.ArithDecay()]:
        if found_flag:
            break
        for max_iter_atts in np.arange(10, 1000, 10):
            if found_flag:
                break
            best_state, best_fitness, learning_curve, timing_curve = mlrose.simulated_annealing(
                problem,
                max_attempts=int(max_iter_atts),
                max_iters=int(max_iter_atts),
                schedule=sched,
                init_state=init_state,
                curve=True,
                random_state=1)
            if best_fitness == max_val:
                x_s.append(np.arange(0, len(learning_curve)))
                y_s.append(learning_curve)
                w_s.append(timing_curve)
                print(best_state)
                print(best_fitness)
                print(max_iter_atts)
                print(sched)
                found_flag = True

    found_flag = False
    for prob in np.arange(0.1, 1.1, 0.1):
        if found_flag:
            break
        for pop_size in np.arange(100, 5000, 100):
            if found_flag:
                break
            for max_iter_atts in np.arange(10, 1000, 10):
                if found_flag:
                    break
                best_state, best_fitness, learning_curve, timing_curve = mlrose.genetic_alg(
                    problem,
                    pop_size=int(pop_size),
                    mutation_prob=prob,
                    max_attempts=int(max_iter_atts),
                    max_iters=int(max_iter_atts),
                    curve=True,
                    random_state=1)
                if best_fitness == max_val:
                    x_s.append(np.arange(0, len(learning_curve)))
                    y_s.append(learning_curve)
                    w_s.append(timing_curve)
                    print(best_state)
                    print(best_fitness)
                    print(max_iter_atts)
                    print(prob)
                    print(pop_size)
                    found_flag = True

    found_flag = False
    for prob in np.arange(0.1, 1.1, 0.1):
        if found_flag:
            break
        for pop_size in np.arange(100, 5000, 100):
            if found_flag:
                break
            for max_iter_atts in np.arange(10, 1000, 10):
                if found_flag:
                    break
                best_state, best_fitness, learning_curve, timing_curve = mlrose.mimic(
                    problem,
                    pop_size=int(pop_size),
                    keep_pct=prob,
                    max_attempts=int(max_iter_atts),
                    max_iters=int(max_iter_atts),
                    curve=True,
                    random_state=1,
                    fast_mimic=True)
                if best_fitness == max_val:
                    x_s.append(np.arange(0, len(learning_curve)))
                    y_s.append(learning_curve)
                    w_s.append(timing_curve)
                    print(best_state)
                    print(best_fitness)
                    print(max_iter_atts)
                    print(prob)
                    print(pop_size)
                    found_flag = True

    for x, y, z in zip(x_s, y_s, z_s):
        plt.plot(x, y, label=z)
    plt.legend()
    plt.title(
        'Randomized Optimization Iterations vs Fitness Function Value for {}'.
        format(name_of_exp))
    plt.xlabel('Function iteration count')
    plt.ylabel('Fitness function value')
    plt.show()
    plt.clf()
    for x, w, z in zip(x_s, w_s, z_s):
        plt.plot(x, w, label=z)
    plt.legend()
    plt.title(
        'Randomized Optimization Time vs Fitness Function Value for {}'.format(
            name_of_exp))
    plt.xlabel('Function iteration count')
    plt.ylabel('Time in Seconds')
    plt.show()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar  1 20:30:20 2019

@author: kalyantulabandu
"""
import mlrose
import numpy as np

fitness_fn = mlrose.FlipFlop()

problem_size = [10,20,50,100,200,300,500,600]

input_space = []

for each in problem_size:
    input = np.random.randint(0,2,size=each)
    init_state = np.array(input)
    input_space.append(init_state)
    
print(input_space)

max_attempts = 50
max_iters = 1500

rhc_state = []
rhc_fitness = []
rhc_statistics = []
rhc_statistics_fn_evals = []
rhc_statistics_time = []