Esempio n. 1
0
def run_mimic(t, samples, keep, m):
    fname = outfile.format('MIMIC{}_{}_{}'.format(samples, keep, m),
                           str(t + 1))
    ef = ContinuousPeaksEvaluationFunction(T)
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    df = DiscreteDependencyTree(m, ranges)
    pop = GenericProbabilisticOptimizationProblem(ef, odd, df)
    mimic = MIMIC(samples, keep, pop)
    fit = FixedIterationTrainer(mimic, 10)
    times = [0]
    for i in range(0, maxIters, 10):
        start = clock()
        fit.train()
        elapsed = time.clock() - start
        times.append(times[-1] + elapsed)
        fevals = ef.fevals
        score = ef.value(mimic.getOptimal())
        ef.fevals -= 1
        st = '{},{},{},{}\n'.format(i, score, times[-1], fevals)
        # print st
        base.write_to_file(fname, st)
    return
Esempio n. 2
0
def makeProblem(num):
    global N
    N = num

    global fill
    fill = [2] * N
    global ranges
    ranges = array('i', fill)
    global ef
    ef = CountOnesEvaluationFunction()
    global odd
    odd = DiscreteUniformDistribution(ranges)
    global nf
    nf = DiscreteChangeOneNeighbor(ranges)
    global mf
    mf = DiscreteChangeOneMutation(ranges)
    global cf
    cf = SingleCrossOver()
    global df
    df = DiscreteDependencyTree(.1, ranges)
    global hcp
    hcp = GenericHillClimbingProblem(ef, odd, nf)
    global gap
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    global pop
    pop = GenericProbabilisticOptimizationProblem(ef, odd, df)
    global GA_pop
    global myWriter, GA_iters, GA_mut, GA_keep
    GA_keep = int(GA_pop * .75)
    GA_mut = int(GA_pop * .15)
Esempio n. 3
0
def run_ga(t, pop, mate, mutate):
    fname = outfile.format('GA{}_{}_{}'.format(pop, mate, mutate), str(t + 1))
    with open(fname, 'a+') as f:
        content = f.read()
        if "fitness" not in content:
            f.write('iterations,fitness,time,fevals\n')
    ef = FlipFlopEvaluationFunction()
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    ga = StandardGeneticAlgorithm(pop, mate, mutate, gap)
    fit = FixedIterationTrainer(ga, 10)
    times = [0]
    for i in range(0, maxIters, 10):
        start = clock()
        fit.train()
        elapsed = time.clock() - start
        times.append(times[-1] + elapsed)
        fevals = ef.fevals
        score = ef.value(ga.getOptimal())
        ef.fevals -= 1
        st = '{},{},{},{}\n'.format(i, score, times[-1], fevals)
        # print st
        base.write_to_file(fname, st)
    return
Esempio n. 4
0
def solveit(oaname, params):
    N = 60
    T = N / 10
    fill = [2] * N
    ranges = array('i', fill)
    iterations = 10000
    tryi = 1

    ef = ContinuousPeaksEvaluationFunction(T)
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()
    df = DiscreteDependencyTree(.1, ranges)
    hcp = GenericHillClimbingProblem(ef, odd, nf)
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    pop = GenericProbabilisticOptimizationProblem(ef, odd, df)

    #  fit = FixedIterationTrainer(rhc, 200000)
    #  fit.train()

    if oaname == 'RHC':
        iterations = int(params[0])
        tryi = int(params[1])
        oa = RandomizedHillClimbing(hcp)
    if oaname == 'SA':
        oa = SimulatedAnnealing(float(params[0]), float(params[1]), hcp)
    if oaname == 'GA':
        oa = StandardGeneticAlgorithm(int(params[0]), int(params[1]),
                                      int(params[2]), gap)
    if oaname == 'MMC':
        oa = MIMIC(int(params[0]), int(params[1]), pop)

    print "Running %s using %s for %d iterations, try %d" % (
        oaname, ','.join(params), iterations, tryi)
    print "=" * 20
    starttime = timeit.default_timer()
    output = []
    for i in range(iterations):
        oa.train()
        if i % 10 == 0:
            optimal = oa.getOptimal()
            score = ef.value(optimal)
            elapsed = float(timeit.default_timer() - starttime)
            output.append([str(i), str(score), str(elapsed)])

    print 'score: %.3f' % score
    print 'train time: %.3f secs' % (int(timeit.default_timer() - starttime))

    scsv = 'cp-%s-%s.csv' % (oaname, '-'.join(params))
    print "Saving to %s" % (scsv),
    with open(scsv, 'w') as csvf:
        writer = csv.writer(csvf)
        for row in output:
            writer.writerow(row)
    print "saved."
    print "=" * 20
Esempio n. 5
0
def run_four_peaks_exploringSA():

    N=200
    T=N/5
    fill = [2] * N
    ranges = array('i', fill)

    ef = FourPeaksEvaluationFunction(T)
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()
    df = DiscreteDependencyTree(.1, ranges)
    hcp = GenericHillClimbingProblem(ef, odd, nf)
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    pop = GenericProbabilisticOptimizationProblem(ef, odd, df)

    iters = [50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 30000, 35000, 40000, 45000, 50000]
    num_repeats = 5


    all_sa_results = []
    all_sa_times = []


    coolings = [0.15, 0.35, 0.55, 0.75, 0.95]
    for cooling in coolings:
        sa_results = []
        sa_times = []
        for i in iters:
            print(i)
            for j in range(num_repeats):
                start = time.time()
                sa = SimulatedAnnealing(1E11, cooling, hcp)
                fit = FixedIterationTrainer(sa, i)
                fit.train()
                end = time.time()
                sa_results.append(ef.value(sa.getOptimal()))
                sa_times.append(end - start)
                print "SA cooling " + str(cooling) + ": "  + str(ef.value(sa.getOptimal()))
        all_sa_results.append(sa_results)
        all_sa_results.append(sa_times)
   


   

    with open('four_peaks_exploringSA.csv', 'w') as csvfile:
        writer = csv.writer(csvfile)
        for sa_results in all_sa_results:
            writer.writerow(sa_results)
        for sa_times in all_sa_times:
            writer.writerow(sa_times)

    return all_sa_results, all_sa_times
Esempio n. 6
0
    def run_experiment(self, opName):
        """Run a genetic algorithms optimization experiment for a given
        optimization problem.

        Args:
            ef (AbstractEvaluationFunction): Evaluation function.
            ranges (array): Search space ranges.
            op (str): Name of optimization problem.

        """
        outdir = 'results/OPT/{}'.format(opName)  # get results directory
        outfile = 'GA_{}_{}_{}_results.csv'.format(self.p, self.ma, self.mu)
        fname = get_abspath(outfile, outdir)  # get output filename

        # delete existing results file, if it already exists
        try:
            os.remove(fname)
        except Exception as e:
            print e
            pass

        with open(fname, 'w') as f:
            f.write('iterations,fitness,time,fevals,trial\n')

        # start experiment
        for t in range(self.numTrials):
            # initialize optimization problem and training functions
            ranges, ef = self.op.get_ef()
            mf = None
            cf = None
            if opName == 'TSP':
                mf = SwapMutation()
                cf = TravelingSalesmanCrossOver(ef)
            else:
                mf = DiscreteChangeOneMutation(ranges)
                cf = SingleCrossOver()
            odd = DiscreteUniformDistribution(ranges)
            gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
            ga = StandardGeneticAlgorithm(self.p, self.ma, self.mu, gap)
            fit = FixedIterationTrainer(ga, 10)

            # run experiment and train evaluation function
            start = time.clock()
            for i in range(0, self.maxIters, 10):
                fit.train()
                elapsed = time.clock() - start
                fe = ef.valueCallCount
                score = ef.value(ga.getOptimal())
                ef.valueCallCount -= 1

                # write results to output file
                s = '{},{},{},{},{}\n'.format(i + 10, score, elapsed, fe, t)
                with open(fname, 'a+') as f:
                    f.write(s)
Esempio n. 7
0
def flipflop():
    N = 80
    fill = [2] * N
    ranges = array('i', fill)

    ef = FlipFlopEvaluationFunction()
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()

    rhc_generic   ("FFloprhc80",   ef, odd, nf,      10.0, 10000, 10, 5)
    sa_generic    ("FFlopsa80",    ef, odd, nf,      10.0, 10000, 10, 5, ([1E12, 1E6], [0.999, 0.99, 0.95]))
    ga_generic    ("FFlopga80",    ef, odd, mf, cf,  50.0, 10000, 10, 1, ([2000, 200], [0.5, 0.25], [0.25, 0.1, 0.02]))
    mimic_discrete("FFlopmimic80", ef, odd, ranges, 300.0, 10000, 10, 1, ([200], [100], [0.1, 0.5, 0.9]))

    print "FFlop all done"
Esempio n. 8
0
def countones():
    N = 80
    fill = [2] * N
    ranges = array('i', fill)

    ef = CountOnesEvaluationFunction()
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()

    rhc_generic("COrhc50", ef, odd, nf, 1.0, 10000, 10, 5)
    sa_generic("COsa50", ef, odd, nf, 1.0, 10000, 10, 5,
               ([1E12, 1E6], [0.999, 0.99, 0.95]))
    ga_generic("COga50", ef, odd, mf, cf, 50.0, 10000, 10, 1,
               ([2000, 200], [0.5, 0.25], [0.25, 0.1, 0.02]))
    mimic_discrete("COmimic50", ef, odd, ranges, 300.0, 10000, 10, 1,
                   ([200], [100], [0.1, 0.5, 0.9]))

    print "CO all done"
Esempio n. 9
0
def run_algorithm_test(T,
                       ranges,
                       algorithms,
                       output_file_name,
                       trial_number,
                       iterations=False):

    with open(output_file_name, 'w') as f:
        f.write('algorithm,optimal_result,iterations,time,trial\n')

    ef = FourPeaksEvaluationFunction(T)
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()
    df = DiscreteDependencyTree(.1, ranges)

    hcp = GenericHillClimbingProblem(ef, odd, nf)
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    pop = GenericProbabilisticOptimizationProblem(ef, odd, df)

    for trial in range(trial_number):
        if iterations is False:
            for item in algorithms:
                start_time = time.time()
                if item in ['rhc']:
                    optimal_result, run_iters = run_rhc(hcp, ef)
                elif item in ['sa']:
                    optimal_result, run_iters = run_sa(hcp, ef)
                elif item in ['ga']:
                    optimal_result, run_iters = run_ga(gap, ef)
                elif item in ['mimic']:
                    optimal_result, run_iters = run_mimic(pop, ef)
                else:
                    print "The algorithm type {} is not supported.".format(
                        item)

                end_time = time.time()
                time_elapsed = end_time - start_time

                run_output = '{},{},{},{},{}\n'.format(item, optimal_result,
                                                       run_iters, time_elapsed,
                                                       trial)
                with open(output_file_name, 'a') as f:
                    f.write(run_output)
        else:
            for iter in iterations:
                for item in algorithms:
                    start_time = time.time()
                    if item in ['rhc']:
                        optimal_result, run_iters = run_rhc(hcp, ef, iter)
                    elif item in ['sa']:
                        optimal_result, run_iters = run_sa(hcp, ef, iter)
                    elif item in ['ga']:
                        optimal_result, run_iters = run_ga(gap, ef, iter)
                    elif item in ['mimic']:
                        optimal_result, run_iters = run_mimic(pop, ef, iter)
                    else:
                        print "The algorithm type {} is not supported.".format(
                            item)

                    end_time = time.time()
                    time_elapsed = end_time - start_time

                    run_output = '{},{},{},{},{}\n'.format(
                        item, optimal_result, run_iters, time_elapsed, trial)

                    with open(output_file_name, 'a') as f:
                        f.write(run_output)

    print "time elapsed is {}".format(time_elapsed)

    return
Esempio n. 10
0
"""
Commandline parameter(s):
   none
"""

N = 1000
maxIters = 3001
numTrials = 5
fill = [2] * N
ranges = array('i', fill)
outfile = OUTPUT_DIRECTORY + '/FLIPFLOP/FLIPFLOP_{}_{}_LOG.csv'
ef = FlipFlopEvaluationFunction()
odd = DiscreteUniformDistribution(ranges)
nf = DiscreteChangeOneNeighbor(ranges)
mf = DiscreteChangeOneMutation(ranges)
cf = SingleCrossOver()
df = DiscreteDependencyTree(.1, ranges)
hcp = GenericHillClimbingProblem(ef, odd, nf)
gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
pop = GenericProbabilisticOptimizationProblem(ef, odd, df)

# RHC
for t in range(numTrials):
    fname = outfile.format('RHC', str(t + 1))
    with open(fname, 'w') as f:
        f.write('iterations,fitness,time,fevals\n')
    ef = FlipFlopEvaluationFunction()
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    hcp = GenericHillClimbingProblem(ef, odd, nf)
    rhc = RandomizedHillClimbing(hcp)
Esempio n. 11
0
def run_count_ones_experiments():
    OUTPUT_DIRECTORY = './output'
    N = 80
    fill = [2] * N
    ranges = array('i', fill)
    ef = CountOnesEvaluationFunction()
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()
    hcp = GenericHillClimbingProblem(ef, odd, nf)
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    max_iter = 5000
    outfile = OUTPUT_DIRECTORY + '/count_ones_{}_log.csv'

    # Randomized Hill Climber
    filename = outfile.format('rhc')
    with open(filename, 'w') as f:
        f.write('iteration,fitness,time\n')
    for it in range(0, max_iter, 10):
        rhc = RandomizedHillClimbing(hcp)
        fit = FixedIterationTrainer(rhc, it)
        start_time = time.clock()
        fit.train()
        elapsed_time = time.clock() - start_time
        # fevals = ef.fevals
        score = ef.value(rhc.getOptimal())
        data = '{},{},{}\n'.format(it, score, elapsed_time)
        print(data)
        with open(filename, 'a') as f:
            f.write(data)

    # Simulated Annealing
    filename = outfile.format('sa')
    with open(filename, 'w') as f:
        f.write('iteration,cooling_value,fitness,time\n')
    for cooling_value in (.19, .38, .76, .95):
        for it in range(0, max_iter, 10):
            sa = SimulatedAnnealing(100, cooling_value, hcp)
            fit = FixedIterationTrainer(sa, it)
            start_time = time.clock()
            fit.train()
            elapsed_time = time.clock() - start_time
            # fevals = ef.fevals
            score = ef.value(sa.getOptimal())
            data = '{},{},{},{}\n'.format(it, cooling_value, score, elapsed_time)
            print(data)
            with open(filename, 'a') as f:
                f.write(data)

    # Genetic Algorithm
    filename = outfile.format('ga')
    with open(filename, 'w') as f:
        f.write('iteration,population_size,to_mate,to_mutate,fitness,time\n')
    for population_size, to_mate, to_mutate in itertools.product([20], [4, 8, 16, 20], [0, 2, 4, 6]):
        for it in range(0, max_iter, 10):
            ga = StandardGeneticAlgorithm(population_size, to_mate, to_mutate, gap)
            fit = FixedIterationTrainer(ga, it)
            start_time = time.clock()
            fit.train()
            elapsed_time = time.clock() - start_time
            # fevals = ef.fevals
            score = ef.value(ga.getOptimal())
            data = '{},{},{},{},{},{}\n'.format(it, population_size, to_mate, to_mutate, score, elapsed_time)
            print(data)
            with open(filename, 'a') as f:
                f.write(data)

    # MIMIC
    filename = outfile.format('mm')
    with open(filename, 'w') as f:
        f.write('iterations,samples,to_keep,m,fitness,time\n')
    for samples, to_keep, m in itertools.product([50], [10], [0.1, 0.3, 0.5, 0.7, 0.9]):
        for it in range(0, 500, 10):
            df = DiscreteDependencyTree(m, ranges)
            pop = GenericProbabilisticOptimizationProblem(ef, odd, df)
            mm = MIMIC(samples, 20, pop)
            fit = FixedIterationTrainer(mm, it)
            start_time = time.clock()
            fit.train()
            elapsed_time = time.clock() - start_time
            # fevals = ef.fevals
            score = ef.value(mm.getOptimal())
            data = '{},{},{},{},{},{}\n'.format(it, samples, to_keep, m, score, elapsed_time)
            print(data)
            with open(filename, 'a') as f:
                f.write(data)
Esempio n. 12
0
def fourpeaksfunc(N, iterations):

    rhcMult = 200
    saMult = 200
    gaMult = 2
    mimicMult = 1

    optimalOut = []
    timeOut = []
    evalsOut = []

    T = N / 5
    fill = [2] * N
    ranges = array('i', fill)

    ef = FourPeaksEvaluationFunction(T)
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()
    df = DiscreteDependencyTree(.1, ranges)
    hcp = GenericHillClimbingProblem(ef, odd, nf)
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    pop = GenericProbabilisticOptimizationProblem(ef, odd, df)

    for niter in iterations:

        iterOptimalOut = [N, niter]
        iterTimeOut = [N, niter]
        iterEvals = [N, niter]

        start = time.time()
        rhc = RandomizedHillClimbing(hcp)
        fit = FixedIterationTrainer(rhc, niter * rhcMult)
        fit.train()
        end = time.time()
        rhcOptimal = ef.value(rhc.getOptimal())
        rhcTime = end - start
        print "RHC optimum: " + str(rhcOptimal)
        print "RHC time: " + str(rhcTime)
        iterOptimalOut.append(rhcOptimal)
        iterTimeOut.append(rhcTime)
        functionEvals = ef.getNumEvals()
        ef.zeroEvals()
        iterEvals.append(functionEvals)

        start = time.time()
        sa = SimulatedAnnealing(1E20, .8, hcp)
        fit = FixedIterationTrainer(sa, niter * saMult)
        fit.train()
        end = time.time()
        saOptimal = ef.value(sa.getOptimal())
        saTime = end - start
        print "SA optimum: " + str(saOptimal)
        print "SA time: " + str(saTime)
        iterOptimalOut.append(saOptimal)
        iterTimeOut.append(saTime)
        functionEvals = ef.getNumEvals()
        ef.zeroEvals()
        iterEvals.append(functionEvals)

        start = time.time()
        ga = StandardGeneticAlgorithm(200, 100, 10, gap)
        fit = FixedIterationTrainer(ga, niter * gaMult)
        fit.train()
        end = time.time()
        gaOptimal = ef.value(ga.getOptimal())
        gaTime = end - start
        print "GA optimum: " + str(gaOptimal)
        print "GA time: " + str(gaTime)
        iterOptimalOut.append(gaOptimal)
        iterTimeOut.append(gaTime)
        functionEvals = ef.getNumEvals()
        ef.zeroEvals()
        iterEvals.append(functionEvals)

        start = time.time()
        mimic = MIMIC(200, 20, pop)
        fit = FixedIterationTrainer(mimic, niter * mimicMult)
        fit.train()
        end = time.time()
        mimicOptimal = ef.value(mimic.getOptimal())
        mimicTime = end - start
        print "MIMIC optimum: " + str(mimicOptimal)
        print "MIMIC time: " + str(mimicTime)
        iterOptimalOut.append(mimicOptimal)
        iterTimeOut.append(mimicTime)
        functionEvals = ef.getNumEvals()
        ef.zeroEvals()
        iterEvals.append(functionEvals)

        optimalOut.append(iterOptimalOut)
        timeOut.append(iterTimeOut)
        evalsOut.append(iterEvals)

    return [optimalOut, timeOut, evalsOut]
Esempio n. 13
0
def run_four_peaks():

    N = 200
    T = N / 5
    fill = [2] * N
    ranges = array('i', fill)

    ef = FourPeaksEvaluationFunction(T)
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    cf = SingleCrossOver()
    df = DiscreteDependencyTree(.1, ranges)
    hcp = GenericHillClimbingProblem(ef, odd, nf)
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    pop = GenericProbabilisticOptimizationProblem(ef, odd, df)

    iters = [50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000, 100000]
    num_repeats = 5

    rhc_results = []
    rhc_times = []
    for i in iters:
        print(i)
        for j in range(num_repeats):
            start = time.time()
            rhc = RandomizedHillClimbing(hcp)
            fit = FixedIterationTrainer(rhc, i)
            fit.train()
            end = time.time()
            rhc_results.append(ef.value(rhc.getOptimal()))
            rhc_times.append(end - start)
            print "RHC: " + str(ef.value(rhc.getOptimal()))

    sa_results = []
    sa_times = []
    for i in iters:
        print(i)
        for j in range(num_repeats):
            start = time.time()
            sa = SimulatedAnnealing(1E11, .95, hcp)
            fit = FixedIterationTrainer(sa, i)
            fit.train()
            end = time.time()
            sa_results.append(ef.value(sa.getOptimal()))
            sa_times.append(end - start)
            print "SA: " + str(ef.value(sa.getOptimal()))

    ga_results = []
    ga_times = []
    for i in iters:
        print(i)
        for j in range(num_repeats):
            start = time.time()
            ga = StandardGeneticAlgorithm(200, 100, 10, gap)
            fit = FixedIterationTrainer(ga, i)
            fit.train()
            end = time.time()
            ga_results.append(ef.value(ga.getOptimal()))
            ga_times.append(end - start)
            print "GA: " + str(ef.value(ga.getOptimal()))

    mimic_results = []
    mimic_times = []
    for i in iters[0:6]:
        print(i)
        for j in range(num_repeats):
            start = time.time()
            mimic = MIMIC(200, 20, pop)
            fit = FixedIterationTrainer(mimic, i)
            fit.train()
            end = time.time()
            mimic_results.append(ef.value(mimic.getOptimal()))
            mimic_times.append(end - start)
            print "MIMIC: " + str(ef.value(mimic.getOptimal()))

    with open('four_peaks.csv', 'w') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(rhc_results)
        writer.writerow(rhc_times)
        writer.writerow(sa_results)
        writer.writerow(sa_times)
        writer.writerow(ga_results)
        writer.writerow(ga_times)
        writer.writerow(mimic_results)
        writer.writerow(mimic_times)

    return rhc_results, rhc_times, sa_results, sa_times, ga_results, ga_times, mimic_results, mimic_times
Esempio n. 14
0
def run_all_2(N=200,T=40,fout=None):
    problem = 'flipflop'
    # N=200
    # T=N/10
    maxEpochs = 10**5
    maxTime = 300 #5 minutes
    fill = [2] * N
    ranges = array('i', fill)

    # ef = FourPeaksEvaluationFunction(T)
    ef = FlipFlopEvaluationFunction()
    
    odd = DiscreteUniformDistribution(ranges)
    nf = DiscreteChangeOneNeighbor(ranges)
    mf = DiscreteChangeOneMutation(ranges)
    # mf = SwapMutation()
    cf = SingleCrossOver()
    df = DiscreteDependencyTree(.1, ranges)
    hcp = GenericHillClimbingProblem(ef, odd, nf)
    gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
    pop = GenericProbabilisticOptimizationProblem(ef, odd, df)
    

    def run_algo(alg,fit,label,difficulty,iters):
        trainTimes = [0.]
        trainTime = []
        scoreChange = [0.]
        stuckCount = 10**3
        prev = 0.
        for epoch in range(0,maxEpochs,1):
        
            st = time.clock()
            fit.train()
            et = time.clock()
            
            trainTimes.append(trainTimes[-1]+(et-st))
            trainTime.append((et-st))
            rollingMean = 10
            avgTime = (math.fsum(trainTime[-rollingMean:]) / float(rollingMean))
        
            score = ef.value(alg.getOptimal())
            # print(score,difficulty)
            
            # trialString = '{}-{}-{}-{}'.format(label,score,epoch,trainTimes[-1])
            trialData = [problem,difficulty,label,score,epoch,trainTimes[-1],avgTime,iters]
            print(trialData)
            # fout.writerow(trialData)
            
            optimum = (difficulty-1)
            if score >= optimum: break
            
            scoreChange.append(abs(score-prev))
            prev = score
            scoreChange = scoreChange[-stuckCount:]
            # print(scoreChange)
            if max(scoreChange) == 0: break
            
            if trainTimes[-1] > maxTime: break
            
    
        # print(trialData)
        fout.writerow(trialData)
        
        
    iters = 10
    rhc = RandomizedHillClimbing(hcp)
    fit = FixedIterationTrainer(rhc, iters)
    run_algo(rhc,fit,'RHC',N,iters)


    iters = 10
    startTemp = 1E10
    coolingFactor = .99
    sa = SimulatedAnnealing(startTemp, coolingFactor, hcp)
    fit = FixedIterationTrainer(sa, iters)
    run_algo(sa,fit,'SA',N,iters)

    iters = 10
    population = 300
    mates = 40
    mutations = 10
    ga = StandardGeneticAlgorithm(population, mates, mutations, gap)
    fit = FixedIterationTrainer(ga, iters)
    run_algo(ga,fit,'GA',N,iters)
    
    iters = 10
    samples = 200
    keep = 5
    mimic = MIMIC(samples, keep, pop)
    fit = FixedIterationTrainer(mimic, iters)
    run_algo(mimic,fit,'MIMIC',N,iters)