Esempio n. 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)
Esempio n. 2
0
import time
from numba import cuda
from em_qap_gpu import local_search, find_best_point
from tests.test_points import test_points
from qap.input_file_reader import InputFileReader
from numba.cuda.random import create_xoroshiro128p_states


if __name__ == '__main__':
    """
    Tests if in every iteration new value from local search is equal or smaller than in previous one
    """

    reader = InputFileReader("../../test_instances/Chr/chr20a.dat")
    dimension, weights, distances = reader.read()

    upper_bound = 10
    lower_bound = 0

    device_weights = cuda.to_device(weights)
    device_distances = cuda.to_device(distances)
    device_points = cuda.to_device(test_points)
    random_states = create_xoroshiro128p_states(1 * 10, seed=time.time())

    previous_value = 100000000

    for i in range(100):
        local_search[1, 10](device_points, device_weights, device_distances, upper_bound, lower_bound, random_states)
        points = device_points.copy_to_host()
        best_point, best_value, best_point_index = find_best_point(points, weights, distances)
        print(best_value)
Esempio n. 3
0
from tqdm import tqdm
from numba import cuda
from numba.cuda.random import create_xoroshiro128p_states
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")
Esempio n. 4
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)
Esempio n. 5
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)