Last active
October 6, 2015 14:59
-
-
Save grational/bdd893901d046362f078 to your computer and use it in GitHub Desktop.
Generate Permutations of a given seed with some constraints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
//bin/true && SCRIPT_DIR="$(dirname $(readlink -f "${0}"))" | |
//bin/true && exec groovy -cp "${SCRIPT_DIR}/lib" "${0}" "${@}"; exit $? | |
/* | |
* Given N elements (with N = 6): | |
* | |
* ['C', 'G', 'Q', 'F', 'M', 'E'] | |
* | |
* and given a matrix NxP where P is the number of permutations of N | |
* | |
* P = N! => P = 6! | |
* | |
* find the 20 rows of the matrix which generate the minimum number of repetitions | |
* of the same element n along the columns. | |
* | |
* Additional constraints: | |
* | |
* 1) 'E' must not be the last element | |
* | |
* 2) The sequence [ 'E', 'Q' ] is not allowed | |
* | |
* 3) The sequence [ 'E', 'M' ] is not allowed | |
* | |
* Results, Levenshtein Distance = 3 | |
* ================================= | |
* [F, C, Q, M, E, G] | |
* [E, C, M, Q, G, F] | |
* [G, M, Q, F, E, C] | |
* [M, C, E, G, F, Q] | |
* [Q, E, G, F, C, M] | |
* [C, M, F, E, G, Q] | |
* [G, E, F, M, Q, C] | |
* [G, Q, C, E, F, M] | |
* [M, Q, C, G, E, F] | |
* [M, Q, E, C, F, G] | |
* [F, M, E, C, Q, G] | |
* [E, F, M, G, C, Q] | |
* [F, G, E, C, M, Q] | |
* [E, F, G, Q, M, C] | |
* [E, G, M, F, Q, C] | |
* [Q, E, F, M, C, G] | |
* [C, E, G, Q, M, F] | |
* [Q, F, C, E, G, M] | |
* | |
* Results, Levenshtein Distance = 4 | |
* ================================= | |
* [F, M, E, C, G, Q] | |
* [M, E, F, Q, C, G] | |
* [Q, C, M, E, G, F] | |
* [E, G, Q, C, F, M] | |
* [F, G, Q, M, E, C] | |
* [E, F, C, M, Q, G] | |
* [Q, E, C, F, M, G] | |
* [E, C, Q, G, M, F] | |
* [C, E, G, F, Q, M] | |
* [C, G, E, F, M, Q] | |
* [M, Q, E, G, F, C] | |
* [C, Q, F, E, G, M] | |
* [G, F, E, C, Q, M] | |
* [G, M, F, Q, E, C] | |
* [G, M, C, E, F, Q] | |
* [M, C, G, Q, E, F] | |
* [F, E, G, M, C, Q] | |
* [Q, F, M, G, E, C] | |
*/ | |
@Grab('org.apache.commons:commons-lang3:3.4') | |
import static org.apache.commons.lang3.StringUtils.* | |
def seed = ['C', 'G', 'Q', 'F', 'M', 'E'] | |
def MAX_RESULTS = 18 | |
def MAX_REPETITION = 3 | |
def MIN_LEVENSHTEIN_DISTANCE = 4 | |
def Permutations = seed.permutations() | |
def StringPermutations = Permutations.collect { it.join() } | |
def results = [] | |
def goOn = true | |
while (goOn) { | |
results = [] | |
Collections.shuffle(graphProxyList, new Random(System.nanoTime())) | |
StringPermutations.each { currentPermutation -> | |
def temp = [] | |
for ( int i = 0; i < currentPermutation.size(); i++ ) { | |
temp << currentPermutation[i] | |
} | |
currentPermutation = temp | |
//println "currentPermutation -> ${currentPermutation}" | |
def repetitionTrashed = false | |
//println "Current Permutation -> ${currentPermutation}" | |
//Condition n.1 | |
if (currentPermutation.last() == 'E') | |
{ | |
//println "Condition 1 not verified!" | |
return | |
} | |
//Conditions n.2,3 | |
if (currentPermutation.join() =~ /E[QM]/) | |
{ | |
//println "Conditions 2 or 3 not verified!" | |
return | |
} | |
//Condition on columns (MAX_REPETITION) | |
currentPermutation.eachWithIndex { it, index -> | |
def repetition = 1; | |
if ( it != 'E' ) | |
{ | |
//Skip control on the last column | |
if (index < (seed.size() - 1)) { | |
results.each{ prevResult -> | |
if (prevResult[index] == it) | |
repetition++ | |
} | |
if (repetition > MAX_REPETITION) | |
{ | |
repetitionTrashed = true | |
//println "Condition of MAX_REPETITION on column not verified, permutation skipped" | |
//println results | |
return | |
} | |
} | |
} | |
} | |
if ( ! repetitionTrashed ) | |
{ | |
//Levenshtein distance constraint | |
results.each{ compare -> | |
//println "compare -> ${compare.join()}" | |
//println "currentPermutation -> ${currentPermutation.join()}" | |
def levenshteinDistance = getLevenshteinDistance(compare.join(), currentPermutation.join()); | |
//println "levenshteinDistance -> ${levenshteinDistance}" | |
if ( levenshteinDistance < MIN_LEVENSHTEIN_DISTANCE ) { | |
repetitionTrashed = true | |
return | |
} | |
} | |
} | |
if (! repetitionTrashed ) | |
{ | |
if ( results.size() < MAX_RESULTS ) | |
results << currentPermutation | |
} | |
else | |
return | |
//System.console().readLine '---------------\nWaiting...\n---------------' | |
} | |
goOn = (results.size>=18) ? false : true | |
} | |
println "size:results -> ${results.size()}:${results}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment