[Python] Generate a random matrix in Python which satisfies a condition

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I manually define the following 16 matrices in Python :

    matrices = {
    "Simulation 1": [
    [1, 1, 1, 1, 1, 2],
    [1, 1, 1, 1, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 2, 3, 2],
    [1, 1, 1, 1, 3, 3],
    [1, 1, 1, 1, 3, 3]
    ],

    "Simulation 2": [
    [1, 1, 2, 2, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 2, 2, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 2, 2, 3],
    [1, 1, 1, 3, 3, 3]
    ],

    "Simulation 3": [
    [1, 1, 2, 2, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 2, 3, 3],
    [1, 1, 2, 2, 3, 3],
    [1, 1, 1, 3, 3, 3],
    [1, 1, 3, 3, 3, 3]
    ],

    "Simulation 4": [
    [1, 1, 1, 1, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [3, 1, 3, 3, 3, 2],
    [3, 3, 3, 3, 3, 2],
    [3, 3, 3, 3, 3, 2],
    [3, 3, 3, 3, 3, 3]
    ],

    "Simulation 5": [
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 1, 3, 2],
    [1, 3, 3, 3, 3, 3],
    [3, 3, 3, 3, 3, 3],
    [3, 3, 3, 3, 3, 3]
    ],

    "Simulation 6": [
    [1, 1, 1, 1, 1, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 1, 2, 2],
    [1, 1, 1, 1, 2, 3],
    [1, 3, 3, 3, 3, 3],
    [1, 3, 3, 3, 3, 3]
    ],

    "Simulation 7": [
    [1, 1, 1, 1, 2, 2],
    [1, 1, 1, 1, 1, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 3, 2, 2, 3],
    [1, 1, 3, 3, 3, 3],
    [3, 3, 3, 3, 3, 3]
    ],

    "Simulation 8": [
    [1, 1, 2, 2, 2, 2],
    [2, 2, 2, 2, 2, 2],
    [2, 2, 2, 2, 2, 2],
    [2, 2, 2, 2, 3, 3],
    [2, 2, 3, 3, 3, 3],
    [2, 2, 2, 3, 3, 3]
    ],

    "Simulation 9": [
    [1, 1, 2, 2, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 1, 2, 2],
    [1, 1, 1, 1, 3, 2],
    [1, 1, 1, 1, 3, 3],
    [1, 1, 3, 3, 3, 3]
    ],

    "Simulation 10": [
    [1, 1, 1, 2, 2, 2],
    [1, 1, 2, 2, 2, 2],
    [1, 1, 2, 2, 2, 2],
    [1, 1, 2, 2, 2, 3],
    [1, 1, 1, 1, 3, 3],
    [1, 1, 1, 3, 3, 3]
    ],

    "Simulation 11": [
    [1, 1, 1, 2, 2, 2],
    [1, 1, 2, 2, 2, 2],
    [1, 1, 2, 2, 2, 3],
    [1, 1, 1, 2, 3, 3],
    [1, 1, 1, 3, 3, 3],
    [1, 1, 1, 3, 3, 3]
    ],

    "Simulation 12": [
    [1, 1, 1, 1, 2, 2],
    [1, 1, 1, 1, 2, 2],
    [1, 1, 1, 1, 2, 2],
    [3, 1, 1, 3, 3, 3],
    [3, 3, 3, 3, 3, 3],
    [3, 3, 3, 3, 3, 3]
    ],

    "Simulation 13": [
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 1, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 3, 3, 3],
    [1, 3, 3, 3, 3, 3],
    [3, 3, 3, 3, 3, 3]
    ],

    "Simulation 14": [
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 1, 1, 2],
    [1, 1, 1, 1, 1, 2],
    [1, 1, 1, 3, 3, 2],
    [1, 3, 3, 3, 3, 3],
    [1, 3, 3, 3, 3, 3]
    ],

    "Simulation 15": [
    [1, 1, 1, 2, 2, 2],
    [1, 2, 2, 2, 2, 2],
    [1, 1, 1, 2, 2, 2],
    [1, 1, 1, 1, 3, 3],
    [1, 1, 1, 3, 3, 3],
    [1, 1, 1, 3, 3, 3]
    ],

    "Simulation 16": [
    [1, 1, 3, 2, 2, 2],
    [1, 1, 3, 2, 3, 3],
    [1, 1, 3, 3, 3, 3],
    [1, 1, 3, 3, 3, 3],
    [1, 1, 3, 3, 3, 3],
    [1, 1, 3, 3, 3, 3]
    ]
    }


    When visualized, these look like this:

    [​IMG]

    Positions in the matrix are understood like this:

    positions = [
    [1, 2, 3, 4, 5, 6],
    [7, 8, 9, 10, 11, 12],
    [13, 14, 15, 16, 17, 18],
    [19, 20, 21, 22, 23, 24],
    [25, 26, 27, 28, 29, 30],
    [31, 32, 33, 34, 35, 36]
    ]


    These matrices have the following properties:

    • 1 = Red, 2 = Blue, 3 = Green
    • Position 1 is always Red, Position 6 is always Blue and Position 36 is always green
    • All circles of the same color can reach all other circles of the same color without touching any other color

    Here is an example of an invalid matrix (i.e. node 1 (red) can not reach the other red nodes without stepping on blue):

    [​IMG]

    I have the following question: Is there some algorithm which can be used to rapidly generate (random) valid matrices for this problem? Can something like a tree/graph be used which can efficiently generate 100 hundred such solutions?

    Continue reading...

Compartilhe esta Página