Ejemplo n.º 1
0
def get_correction_heurs(of, maxeval, FSA=True, GO=True):
    overview_heurs = []

    if FSA:
        for T0 in [1e-10, 1e-2, 1, np.inf]:
            for mutation in get_correction_mutation(of):
                for n0 in [1, 2, 5]:
                    overview_heurs.append(
                        FastSimulatedAnnealing(of,
                                               maxeval=maxeval,
                                               T0=T0,
                                               n0=n0,
                                               alpha=2,
                                               mutation=mutation))
    if GO:
        for mutation in get_correction_mutation(of):
            for crossover in [UniformMultipoint(1)]:
                for N in [100, 150, 200]:
                    for Tsel1 in [0.5]:
                        overview_heurs.append(
                            GeneticOptimization(of,
                                                maxeval,
                                                N=N,
                                                M=N * 3,
                                                Tsel1=Tsel1,
                                                Tsel2=0.1,
                                                mutation=mutation,
                                                crossover=crossover))
    return overview_heurs
Ejemplo n.º 2
0
def experiment_go(of, maxeval, num_runs, N, M, Tsel1, Tsel2, mutation, crossover):
    results = []
    heur_name = 'GO_{}'.format(N)
    for i in tqdm(range(num_runs), 'Testing {}'.format(heur_name)):
        result = GeneticOptimization(of, maxeval, N=N, M=M, Tsel1=Tsel1, Tsel2=Tsel2, 
                                     mutation=mutation, crossover=crossover).search()
        result['run'] = i
        result['heur'] = heur_name
        result['N'] = N
        results.append(result)
    return pd.DataFrame(results, columns=['heur', 'run', 'N', 'best_x', 'best_y', 'neval'])
Ejemplo n.º 3
0
def get_overview_heurs(of, maxeval, RD=True, FSA=True, DE=True, GO=True):
    overview_heurs = []

    if RD:
        for hmax in [0, 5, 10, 50, np.inf]:
            for RD in [False, True]:
                overview_heurs.append(
                    ShootAndGo(of,
                               maxeval=maxeval,
                               hmax=hmax,
                               random_descent=RD))
    if FSA:
        for T0 in [1e-10, 1e-2, 1, np.inf]:
            for mutation in get_overview_mutation(of):
                for n0 in [1, 2, 5]:
                    overview_heurs.append(
                        FastSimulatedAnnealing(of,
                                               maxeval=maxeval,
                                               T0=T0,
                                               n0=n0,
                                               alpha=2,
                                               mutation=mutation))

    if DE:
        for N in [4, 10, 20]:
            for CR in [0.2, 0.5, 0.8]:
                for F in [0.5, 1, 2]:
                    overview_heurs.append(
                        DifferentialEvolution(of,
                                              maxeval=maxeval,
                                              N=N,
                                              CR=CR,
                                              F=F))

    if GO:
        for mutation in get_overview_mutation(of):
            for crossover in [
                    Crossover(),
                    UniformMultipoint(1),
                    RandomCombination()
            ]:
                for N in [1, 2, 5, 10, 30, 100]:
                    for Tsel1 in [0.5, 1]:
                        overview_heurs.append(
                            GeneticOptimization(of,
                                                maxeval,
                                                N=N,
                                                M=N * 3,
                                                Tsel1=Tsel1,
                                                Tsel2=0.1,
                                                mutation=mutation,
                                                crossover=crossover))
    return overview_heurs
Ejemplo n.º 4
0
def experiment_go(of, maxeval, num_runs, N):
    results = []
    heur_name = 'GO_{}'.format(N)
    for i in range(num_runs):
        result = GeneticOptimization(of, maxeval, N=N).search()
        print('result ', result)
        result['run'] = i
        result['heur'] = heur_name
        result['N'] = N
        results.append(result)

        # write results to csv file
        now = datetime.now()
        current_time = now.strftime('%Y%m%d%H%M')
        res = pd.DataFrame(
            results, columns=['heur', 'run', 'N', 'best_x', 'best_y', 'neval'])
        res.to_csv('../results/' + current_time + '_one_eighth_GO_' + str(N) +
                   '.csv')

    return res
Ejemplo n.º 5
0
        mutation = {'mutation': LevyMutation(scale=parameter, correction = correction['correction']), 'name': 'levy{}_'
                   .format(parameter)+correction['name']}
        mutations.append(mutation)


# In[8]:


results = pd.DataFrame()
for crossover in crossovers:
    for mutation in mutations:
        heur_name = 'GO_mut:({})_cro:{}'.format(mutation['name'], crossover['name'])
        runs = []
        for i in tqdm_notebook(range(NUM_RUNS), 'Testing {}'.format(heur_name)):
            run = GeneticOptimization(tsp, maxeval, N=5, M=15, Tsel1=1.0, Tsel2=0.5, 
                                      mutation=mutation['mutation'],
                                      crossover=crossover['crossover']).search()
            run['run'] = i
            run['heur'] = heur_name
            run['mutation'] = mutation['name']
            run['crossover'] = crossover['name']
            runs.append(run)

        res_df = pd.DataFrame(runs, columns=['heur', 'run', 'mutation', 'crossover','best_x', 'best_y', 'neval'])
        results = pd.concat([results, res_df], axis=0)


# In[9]:


results_pivot = results.pivot_table(
Ejemplo n.º 6
0
        'name': 'rnd'
    },
]

# In[12]:

results = pd.DataFrame()
for crossover in crossovers:
    heur_name = 'GO_{}'.format(crossover['name'])
    runs = []
    for i in tqdm(range(NUM_RUNS), 'Testing {}'.format(heur_name)):
        run = GeneticOptimization(tsp,
                                  maxeval,
                                  N=5,
                                  M=15,
                                  Tsel1=1,
                                  Tsel2=0.1,
                                  mutation=CauchyMutation(
                                      r=.75, correction=Correction(tsp)),
                                  crossover=crossover['crossover']).search()
        run['run'] = i
        run['heur'] = heur_name
        run['crossover'] = crossover['name']
        runs.append(run)

    res_df = pd.DataFrame(
        runs,
        columns=['heur', 'run', 'crossover', 'best_x', 'best_y', 'neval'])
    results = pd.concat([results, res_df], axis=0)

# In[13]:
stats_go_3.sort_values(by='Tsel2')
stats_go_3


# The optimal GO parameters are following:
# * Size of the population: N = 2
# * Mutation probability  : p = 0.10
# * Selection temperatures:
#     - Tsel1 = 1e-10
#     - Tsel2 = 1e-2

# In[45]:


mutation = BinaryMutation(p=0.10, correction=Correction(scp))
heur = GeneticOptimization(of=scp, maxeval=maxeval, N=2, M=6, Tsel1=1e-10, Tsel2=1e-2, 
                        mutation=mutation, crossover=crossover)
result = heur.search()
print('neval = {}'.format(result['neval']))
print('best_x = {}'.format(result['best_x']))
print('best_y = {}'.format(result['best_y']))


# ## Fast Simulated Annealing + Genetic Optimization heursitic
# In this case, we will use FSA and GO parameters optimized separately in the previous section.

# In[46]:


from heur_go import UniformMultipoint
from heur_fsa_go import FsaGoHeuristic
from heur_aux import BinaryMutation, Correction
Ejemplo n.º 8
0
RandomCombination().crossover(x, y)

# They work as expected.

# Finally, let's run GO:

# In[30]:

from heur_go import GeneticOptimization

# In[31]:

heur = GeneticOptimization(dj,
                           maxeval=10000,
                           N=10,
                           M=30,
                           Tsel1=0.5,
                           Tsel2=0.1,
                           mutation=cauchy,
                           crossover=UniformMultipoint(1))
res = heur.search()
print(res['best_x'])
print(res['best_y'])
print(res['neval'])

# # _h_-means
#
# Heuristic cluster analysis inspired by [_k_-means](https://en.wikipedia.org/wiki/K-means_clustering). Another demonstration of how continuous heuristics can be used — example of an application.

# In[32]:

import matplotlib