Beispiel #1
0
def test_random(size: int,
                dists: np.ndarray,
                costs: np.ndarray,
                reruns_number: int = 3) -> Tuple[float, float, int]:
    results = []
    times = []
    for i in range(reruns_number):
        print("Random algorithm run: " + str(i + 1))
        random_results = []
        s = time.time()
        for _ in range(100000):
            random_results.append(
                objective(dists, costs, np.random.permutation(size)))
        e = time.time()
        times.append(e - s)

        results.append(min(random_results))

    return sum(results) / len(results), sum(times) / len(times), min(results)
Beispiel #2
0
from QAP.utils import load_solution, load_example
from QAP.objective import objective
from QAP.solvers.genetic import genetic_solver
from QAP.solvers.selection_mechanisms import BestFit
from QAP.solvers.mutation_mechanisms import ShiftMutation, SwapMutation, UniformMutationScheduler
import numpy as np

if __name__ == '__main__':
    n, dists, costs = load_example('data/qapdata/kra30a.dat', dist_first=True)
    _, opt, permutation = load_solution('data/qapsoln/kra30a.sln')

    assert opt == objective(dists, costs,
                            permutation), "something wrong with objective"

    # TODO: hiperparameter tuning with WandB?
    mutation_mutations = (SwapMutation(mutation_prob=0.3), ShiftMutation())
    res = genetic_solver(
        n,
        dists,
        costs,
        objective,
        max_iterations=1000,
        population_size=100,
        verbose=True,
        print_every=100,
        selection_size=100,
        crossover_count=50,
        mutation_mechanism=UniformMutationScheduler,
        # mutation_prob=0.1,
        mutation_mutations=mutation_mutations,
    )
from QAP.utils import load_solution, load_example
from QAP.objective import objective, naive_objective
import numpy as np

if __name__ == '__main__':

    n, dists, costs = load_example('data/qapdata/kra30a.dat', dist_first=True)
    _, opt, permutation = load_solution('data/qapsoln/kra30a.sln')

    assert opt == objective(dists, costs,
                            permutation), "something wrong with objective"

    np.random.seed(1234)
    diffs = []
    for _ in range(10000):
        diffs.append(objective(dists, costs, np.random.permutation(n)) - opt)

    print(
        f'mean difference for random permutations is {np.mean(diffs)}, min {np.min(diffs)}, max {np.max(diffs)}\n'
        f'optimal is {opt}')
Beispiel #4
0
        'lipa60b.sln',
        'lipa70b.sln',
        'lipa80b.sln',
        'lipa90b.sln',
    ]

    results = []

    for problem, solution in zip(problems, solutions):
        size, dists, costs = load_example(os.path.join(problems_folder,
                                                       problem),
                                          dist_first=True)
        _, opt, permutation = load_solution(
            os.path.join(solutions_folder, solution))

        if not opt == objective(dists, costs, permutation):
            size, dists, costs = load_example(os.path.join(
                problems_folder, problem),
                                              dist_first=False)
            _, opt, permutation = load_solution(
                os.path.join(solutions_folder, solution))

        if not opt == objective(dists, costs, permutation):
            print(problem + " could not be read!")
            continue

        print("Processing problem: " + problem)

        bees_result, bees_time, bees_best = test_bees(size, dists, costs,
                                                      reruns_number)
        genetic_result, genetic_time, genetic_best = test_genetic(