diff --git a/py/Pipfile b/py/Pipfile index be22fdc..433219a 100644 --- a/py/Pipfile +++ b/py/Pipfile @@ -8,6 +8,6 @@ [packages] requests = "*" - +python-constraint = "*" [requires] python_version = "3.7" diff --git a/py/Pipfile b/py/Pipfile index be22fdc..433219a 100644 --- a/py/Pipfile +++ b/py/Pipfile @@ -8,6 +8,6 @@ [packages] requests = "*" - +python-constraint = "*" [requires] python_version = "3.7" diff --git a/py/Pipfile.lock b/py/Pipfile.lock index 7705726..75fc268 100644 --- a/py/Pipfile.lock +++ b/py/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "d6b06c358e3a82c0f18eeb9729511dfcbdfe6593ec78842704abe5a445182b84" + "sha256": "8b1fcdb84828bfaf54abbf47069942063faff55fa75532e0ce444d9100936859" }, "pipfile-spec": 6, "requires": { @@ -37,6 +37,13 @@ ], "version": "==2.8" }, + "python-constraint": { + "hashes": [ + "sha256:501d6f17afe0032dfc6ea6c0f8acc12e44f992733f00e8538961031ef27ccb8e" + ], + "index": "pypi", + "version": "==1.4.0" + }, "requests": { "hashes": [ "sha256:502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e", @@ -127,9 +134,9 @@ }, "wrapt": { "hashes": [ - "sha256:d4d560d479f2c21e1b5443bbd15fe7ec4b37fe7e53d335d3b9b0a7b1226fe3c6" + "sha256:4aea003270831cceb8a90ff27c4031da6ead7ec1886023b80ce0dfe0adf61533" ], - "version": "==1.10.11" + "version": "==1.11.1" } } } diff --git a/py/Pipfile b/py/Pipfile index be22fdc..433219a 100644 --- a/py/Pipfile +++ b/py/Pipfile @@ -8,6 +8,6 @@ [packages] requests = "*" - +python-constraint = "*" [requires] python_version = "3.7" diff --git a/py/Pipfile.lock b/py/Pipfile.lock index 7705726..75fc268 100644 --- a/py/Pipfile.lock +++ b/py/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "d6b06c358e3a82c0f18eeb9729511dfcbdfe6593ec78842704abe5a445182b84" + "sha256": "8b1fcdb84828bfaf54abbf47069942063faff55fa75532e0ce444d9100936859" }, "pipfile-spec": 6, "requires": { @@ -37,6 +37,13 @@ ], "version": "==2.8" }, + "python-constraint": { + "hashes": [ + "sha256:501d6f17afe0032dfc6ea6c0f8acc12e44f992733f00e8538961031ef27ccb8e" + ], + "index": "pypi", + "version": "==1.4.0" + }, "requests": { "hashes": [ "sha256:502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e", @@ -127,9 +134,9 @@ }, "wrapt": { "hashes": [ - "sha256:d4d560d479f2c21e1b5443bbd15fe7ec4b37fe7e53d335d3b9b0a7b1226fe3c6" + "sha256:4aea003270831cceb8a90ff27c4031da6ead7ec1886023b80ce0dfe0adf61533" ], - "version": "==1.10.11" + "version": "==1.11.1" } } } diff --git a/py/README.md b/py/README.md index daf6548..1e7491b 100644 --- a/py/README.md +++ b/py/README.md @@ -6,5 +6,5 @@ ## Skewed Random Generator -## Random Generator +## Even Random Generator diff --git a/py/Pipfile b/py/Pipfile index be22fdc..433219a 100644 --- a/py/Pipfile +++ b/py/Pipfile @@ -8,6 +8,6 @@ [packages] requests = "*" - +python-constraint = "*" [requires] python_version = "3.7" diff --git a/py/Pipfile.lock b/py/Pipfile.lock index 7705726..75fc268 100644 --- a/py/Pipfile.lock +++ b/py/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "d6b06c358e3a82c0f18eeb9729511dfcbdfe6593ec78842704abe5a445182b84" + "sha256": "8b1fcdb84828bfaf54abbf47069942063faff55fa75532e0ce444d9100936859" }, "pipfile-spec": 6, "requires": { @@ -37,6 +37,13 @@ ], "version": "==2.8" }, + "python-constraint": { + "hashes": [ + "sha256:501d6f17afe0032dfc6ea6c0f8acc12e44f992733f00e8538961031ef27ccb8e" + ], + "index": "pypi", + "version": "==1.4.0" + }, "requests": { "hashes": [ "sha256:502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e", @@ -127,9 +134,9 @@ }, "wrapt": { "hashes": [ - "sha256:d4d560d479f2c21e1b5443bbd15fe7ec4b37fe7e53d335d3b9b0a7b1226fe3c6" + "sha256:4aea003270831cceb8a90ff27c4031da6ead7ec1886023b80ce0dfe0adf61533" ], - "version": "==1.10.11" + "version": "==1.11.1" } } } diff --git a/py/README.md b/py/README.md index daf6548..1e7491b 100644 --- a/py/README.md +++ b/py/README.md @@ -6,5 +6,5 @@ ## Skewed Random Generator -## Random Generator +## Even Random Generator diff --git a/py/even_random_generator.py b/py/even_random_generator.py index ffa3c8a..1775aaa 100644 --- a/py/even_random_generator.py +++ b/py/even_random_generator.py @@ -1,3 +1,4 @@ +from constraint import * class EvenRandomGenerator: """Generate even random sequences according to a predefined TL ration (Ralph, 2014)""" @@ -25,17 +26,35 @@ return seq - def _optimize_sequence(self, seq): - """Optimize a sequence based on a desired tl ratio""" - # TODO optimize sequence according to the fitness function w/ Branch&Bound or evolutionary algorithms. + def _optimize_sequence(self, seq, tl_ratio): + """Optimize a sequence to match a desired tl ratio""" + + p = Problem() + + # TODO add all possible values for seq (its domain) + p.addVariable("seq", [seq]) + + p.addConstraint(lambda s: self.calculate_tl_ratio(s) - tl_ratio < 0.05) + return seq @staticmethod def calculate_tl_ratio(seq): """Calculates the T:L ratio of a sequence.""" - # TODO targets = 0 + lures = 0 for index, item in seq: if item == seq[index-2]: targets += 1 - return 1.0 + elif item == seq[index-1] or item == seq[index-3]: + lures += 1 + # avoid division by zero + if lures == 0: + lures = 1 + return targets/lures + + +if __name__ == '__main__': + generator = EvenRandomGenerator(['a','b','c']) + generated_seq = generator.generate() + print('Even Random Sequence: %s' % ''.join(generated_seq))