Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quadratic_assignment generator does not describe how to interpret the decision variable #101

Open
arcondello opened this issue Aug 28, 2024 · 0 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@arcondello
Copy link
Member

It maps facilities to location. See for instance:

import numpy as np

from dwave.optimization.generators import quadratic_assignment

distance_matrix = np.array([[0, 32, 68, 97, 75, 70, 75, 40, 24],
                            [32, 0, 42, 80, 53, 65, 82, 47, 29],
                            [68, 42, 0, 45, 15, 49, 79, 55, 50],
                            [97, 80, 45, 0, 30, 36, 65, 65, 73],
                            [75, 53, 15, 30, 0, 38, 69, 53, 53],
                            [70, 65, 49, 36, 38, 0, 31, 32, 46],
                            [75, 82, 79, 65, 69, 31, 0, 36, 56],
                            [40, 47, 55, 65, 53, 32, 36, 0, 19],
                            [24, 29, 50, 73, 53, 46, 56, 19, 0]])

flow_matrix = np.array([[0, 2, 4, 0, 0, 0, 2, 0, 0],
                        [2, 0, 3, 1, 0, 6, 0, 0, 2],
                        [4, 3, 0, 0, 0, 3, 0, 0, 0],
                        [0, 1, 0, 0, 1, 0, 1, 2, 0],
                        [0, 0, 0, 1, 0, 0, 0, 0, 0],
                        [0, 6, 3, 0, 0, 0, 0, 0, 2],
                        [2, 0, 0, 1, 0, 0, 0, 4, 3],
                        [0, 0, 0, 2, 0, 0, 4, 0, 0],
                        [0, 2, 0, 0, 0, 2, 3, 0, 0]])

model = quadratic_assignment(distance_matrix=distance_matrix, flow_matrix=flow_matrix)
model.states.resize(1)

x, = model.iter_decisions()


def qap(flow_matrix, distance_matrix, facilities):
    # facilities[i] == j => facility i is at location j

    n = flow_matrix.shape[0]

    out = 0
    for i in range(n):
        for j in range(n):
            out += flow_matrix[i, j] * distance_matrix[facilities[i], facilities[j]]

    return out


rng = np.random.default_rng(42)

facilities = np.arange(distance_matrix.shape[0])
for _ in range(10):
    rng.shuffle(facilities)

    x.set_state(0, facilities)
    print(model.objective.state(), qap(flow_matrix=flow_matrix, distance_matrix=distance_matrix, facilities=facilities))
@arcondello arcondello added bug Something isn't working documentation Improvements or additions to documentation labels Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant