PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 22, 2022

[FIXED] How to do a permutation with restrictions?

 November 22, 2022     matlab, multiple-conditions, permutation, traveling-salesman     No comments   

Issue

I have a vector using Matlab whose values are from 1 to 18, I need to perform permutations of those values considering that certain numbers cannot go together. For example:

vector = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18]

In the conditions I have that the following numbers cannot go together:

1 and 7
1 and 8
1 and 17
1 and 18
2 and 6
2 and 7
2 and 8
2 and 17
2 and 18

So a valid permutation could be:

[1 2 4 3 5 6 7 8 9 10 11 12 13 14 15 17 16 18]

An invalid permutation could be:

[1 7 4 3 5 6 2 8 9 10 11 12 13 14 15 17 16 18] 1 and 7 together
[1 8 4 3 5 6 2 7 9 10 11 12 13 14 15 17 16 18] 1 and 8 together
[1 17 4 3 5 6 2 8 9 10 11 12 13 14 15 7 16 18] 1 and 17 together

These conditions must apply in any position of the vector. I have no ideas how to do this, if someone could give me ideas I would really appreciate it.


Solution

Taking into account a previous comment, my code generates a random permutation of your vector and checks that all these combinations are not in the permutation. If some condition is found a new permutation is generated:

vector = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18];

not_valid = 1;
while not_valid == 1
    randpermutation = vector(randperm(18))

    not_valid = 0;
    for i = 1:length(randpermutation)-1
        if randpermutation(i) == 1 && randpermutation(i+1) == 7 || randpermutation(i) == 7 && randpermutation(i+1) == 1
            not_valid = 1;
        end
        if randpermutation(i) == 1 && randpermutation(i+1) == 8 || randpermutation(i) == 8 && randpermutation(i+1) == 1
            not_valid = 1;
        end
        if randpermutation(i) == 1 && randpermutation(i+1) == 17 || randpermutation(i) == 17 && randpermutation(i+1) == 1
            not_valid = 1;
        end
        if randpermutation(i) == 1 && randpermutation(i+1) == 18 || randpermutation(i) == 18 && randpermutation(i+1) == 1
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 7 || randpermutation(i) == 7 && randpermutation(i+1) == 2
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 8 || randpermutation(i) == 8 && randpermutation(i+1) == 2
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 17 || randpermutation(i) == 17 && randpermutation(i+1) == 2
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 18 || randpermutation(i) == 18 && randpermutation(i+1) == 2
            not_valid = 1;
        end
        if randpermutation(i) == 2 && randpermutation(i+1) == 6 || randpermutation(i) == 6 && randpermutation(i+1) == 2
            not_valid = 1;
        end
    end
end


Answered By - PedroRodriguez
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing