Example #1
0
def execute(input_file, solution_file, permutations_count, iterations,
            distance_factor):
    input_reader = InputFileReader(input_file)
    dimension, weights, distances = input_reader.read()
    distance = dimension * distance_factor

    qap = QAP(weights, distances)

    optimal_value = None
    optimal_permutation = None

    if solution_file is not None:
        solution_reader = SolutionFileReader(solution_file)
        solution_dimension, solution_value, solution_permutation = solution_reader.read(
        )

        if solution_dimension != dimension:
            raise RuntimeError(
                "Solution dimension is different than input dimension")

        if qap.get_value(solution_permutation) != solution_value:
            raise RuntimeError(
                "Solution value does not match calculated solution permutation value"
            )

        optimal_value = solution_value
        optimal_permutation = solution_permutation

    permutations = generate_permutations(permutations_count, dimension)

    start_time = time.time()

    for iteration in range(iterations):
        best_permutation, best_permutation_index, best_value = find_best_permutation(
            permutations, qap)

        next_permutations = []

        for i in range(len(permutations)):
            if i == best_permutation_index:
                next_permutations.append(permutations[i])
                continue
            else:
                surroundings = get_surroundings(i, permutations, distance)
                new_permutation = attraction_injection(permutations[i],
                                                       surroundings, qap)
                next_permutations.append(new_permutation)

        permutations = next_permutations

    end_time = time.time()

    values = [qap.get_value(p) for p in permutations]
    best = min(values)
    diff = (best - optimal_value) / best * 100

    return best, round(diff, 2), round(end_time - start_time, 2)
Example #2
0
def attraction_injection(permutation, surroundings, qap: QAP):
    result = permutation.copy()
    permutation_value = qap.get_value(permutation)
    dimension = len(permutation)

    for p in surroundings:
        if permutation_value >= qap.get_value(p):
            lower, upper = get_random_range(dimension - 1)
            result = pmx(p, result, lower, upper)
        else:
            result = repulsion(p, result)

    return result
Example #3
0
def find_best_permutation(permutations, qap: QAP):
    best_permutation = permutations[0]
    best_permutation_index = 0
    best_value = qap.get_value(best_permutation)

    for i in range(len(permutations)):
        new_value = qap.get_value(permutations[i])

        if new_value < best_value:
            best_value = new_value
            best_permutation = permutations[i]
            best_permutation_index = i

    return best_permutation.copy(), best_permutation_index, best_value
Example #4
0
from arguments import parse_arguments
from qap.input_file_reader import InputFileReader
from qap.solution_file_reader import SolutionFileReader
from qap.qap import QAP
from em_discrete.em_discrete import generate_permutations
from em_discrete.em_discrete_cuda import em_discrete
from plot import plot_results

if __name__ == '__main__':
    input_file, solution_file, permutations_count, iterations, hamming_distance = parse_arguments(
        sys.argv[1:])

    input_reader = InputFileReader(input_file)
    dimension, weights, distances = input_reader.read()

    qap = QAP(weights, distances)

    optimal_value = None
    optimal_permutation = None

    if solution_file is not None:
        solution_reader = SolutionFileReader(solution_file)
        solution_dimension, solution_value, solution_permutation = solution_reader.read(
        )

        if solution_dimension != dimension:
            raise RuntimeError(
                "Solution dimension is different than input dimension")

        if qap.get_value(solution_permutation) != solution_value:
            raise RuntimeError(
Example #5
0
import numpy as np
from numba import cuda
from em_qap_gpu import calculate_forces, find_best_point, calculate_charges, qap_host
from qap.input_file_reader import InputFileReader
from em_cpu.em_solver import EMSolver
from qap.qap import QAP
from tests.test_points import test_points

if __name__ == '__main__':
    """
    Checks if forces calculated by cpu and gpu versions are same
    """
    reader = InputFileReader("../../test_instances/Chr/chr20a.dat")
    dimension, weights, distances = reader.read()

    qap = QAP(weights, distances)
    qap_function = qap.get_em_function()

    solver = EMSolver(10, 20, [0] * 20, [10] * 20, qap_function)
    solver._EMSolver__points = np.array(test_points)
    calculated_forces = solver._EMSolver__calculate_forces()

    charges = np.zeros((len(test_points), 1))
    forces = np.zeros((len(test_points), 20))

    device_points = cuda.to_device(test_points)
    device_weights = cuda.to_device(weights)
    device_distances = cuda.to_device(distances)
    device_charges = cuda.to_device(charges)
    device_forces = cuda.to_device(forces)
Example #6
0
def execute(input_file, solution_file, permutations_count, iterations,
            hamming_distance_factor):
    #input_file, solution_file, permutations_count, iterations, hamming_distance = parse_arguments(sys.argv[1:])

    input_reader = InputFileReader(input_file)
    dimension, weights, distances = input_reader.read()

    hamming_distance = int(dimension * hamming_distance_factor)

    qap = QAP(weights, distances)

    optimal_value = None
    optimal_permutation = None

    if solution_file is not None:
        solution_reader = SolutionFileReader(solution_file)
        solution_dimension, solution_value, solution_permutation = solution_reader.read(
        )

        if solution_dimension != dimension:
            raise RuntimeError(
                "Solution dimension is different than input dimension")

        if qap.get_value(solution_permutation) != solution_value:
            raise RuntimeError(
                "Solution value does not match calculated solution permutation value"
            )

        optimal_value = solution_value
        optimal_permutation = solution_permutation

    permutations = generate_permutations(permutations_count, dimension)
    values = [0] * permutations_count

    start_time1 = time.time()

    previous_permutations = cuda.to_device(permutations)
    next_permutations = cuda.to_device(permutations)
    pmx_buffer = cuda.to_device(permutations)

    device_values = cuda.to_device(values)
    device_weights = cuda.to_device(weights)
    device_distances = cuda.to_device(distances)

    threads_per_block = permutations_count
    blocks = 1

    random_states = create_xoroshiro128p_states(threads_per_block * blocks,
                                                seed=time.time())

    start_time2 = time.time()

    for _ in range(iterations):
        em_discrete[blocks,
                    threads_per_block](previous_permutations,
                                       next_permutations, device_values,
                                       device_weights, device_distances,
                                       hamming_distance, random_states,
                                       pmx_buffer)

        tmp = previous_permutations
        previous_permutations = next_permutations
        next_permutations = tmp

    end_time2 = time.time()

    host_values = device_values.copy_to_host()
    end_time1 = time.time()

    best = min(host_values)
    diff = (best - optimal_value) / best * 100

    return best, round(diff, 2), round(end_time2 - start_time2,
                                       2), round(end_time1 - start_time1, 2)
Example #7
0
def solve(input_file, solution_file, points_count, iterations, upper_bound,
          lower_bound):
    input_reader = InputFileReader(input_file)
    dimension, weights, distances = input_reader.read()

    optimal_value = None
    optimal_permutation = None

    if solution_file is not None:
        solution_reader = SolutionFileReader(solution_file)
        solution_dimension, solution_value, solution_permutation = solution_reader.read(
        )

        qap = QAP(weights, distances)

        if solution_dimension != dimension:
            raise RuntimeError(
                "Solution dimension is different than input dimension")

        if qap.get_value(solution_permutation) != solution_value:
            raise RuntimeError(
                "Solution value does not match calculated solution permutation value"
            )

        optimal_value = solution_value
        optimal_permutation = solution_permutation

    random.seed(time.time())
    start_points = initialize(points_count, dimension, lower_bound,
                              upper_bound)
    charges = np.zeros((points_count, 1))
    forces = np.zeros((points_count, dimension))

    start1 = time.time()

    device_points = cuda.to_device(start_points)
    device_weights = cuda.to_device(weights)
    device_distances = cuda.to_device(distances)
    device_charges = cuda.to_device(charges)
    device_forces = cuda.to_device(forces)

    # TODO: add check if points_count is lower than max threads per block
    threads_per_block = points_count
    blocks = 1

    random_states = create_xoroshiro128p_states(threads_per_block * blocks,
                                                seed=time.time())

    start2 = time.time()

    for it in range(iterations):
        local_search[blocks, threads_per_block](device_points, device_weights,
                                                device_distances, upper_bound,
                                                lower_bound, random_states)

        points = device_points.copy_to_host()
        best_point, best_value, best_index = find_best_point(
            points, weights, distances)
        denominator = sum([
            qap_host(point, weights, distances) - best_value
            for point in points
        ])

        calculate_charges[blocks,
                          threads_per_block](device_points, device_weights,
                                             device_distances, best_value,
                                             denominator, device_charges)

        calculate_forces[blocks,
                         threads_per_block](device_points, device_weights,
                                            device_distances, best_index,
                                            device_charges, device_forces)

        move[blocks, threads_per_block](device_points, best_index,
                                        device_forces, random_states)

    end2 = time.time()
    end1 = time.time()

    points = device_points.copy_to_host()
    best_point, best_value, best_index = find_best_point(
        points, weights, distances)

    diff = (best_value - optimal_value) / best_value * 100

    return best_value, round(diff, 2), round(end2 - start2,
                                             2), round(end1 - start1, 2)
Example #8
0
from tqdm import tqdm
from qap.input_file_reader import InputFileReader
from qap.solution_file_reader import SolutionFileReader
from qap.qap import QAP
from em_discrete.arguments import parse_arguments
from em_discrete.em_discrete import generate_permutations, find_best_permutation, get_surroundings, attraction_injection
from em_qap_plot import plot_results

if __name__ == '__main__':
    input_file, solution_file, permutations_count, iterations, distance = parse_arguments(
        sys.argv[1:])

    input_reader = InputFileReader(input_file)
    dimension, weights, distances = input_reader.read()

    qap = QAP(weights, distances)

    optimal_value = None
    optimal_permutation = None

    if solution_file is not None:
        solution_reader = SolutionFileReader(solution_file)
        solution_dimension, solution_value, solution_permutation = solution_reader.read(
        )

        if solution_dimension != dimension:
            raise RuntimeError(
                "Solution dimension is different than input dimension")

        if qap.get_value(solution_permutation) != solution_value:
            raise RuntimeError(