summaryrefslogtreecommitdiff
path: root/src/genetic/mutate.pl
diff options
context:
space:
mode:
Diffstat (limited to 'src/genetic/mutate.pl')
-rwxr-xr-xsrc/genetic/mutate.pl31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/genetic/mutate.pl b/src/genetic/mutate.pl
new file mode 100755
index 0000000..ea4f38e
--- /dev/null
+++ b/src/genetic/mutate.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+open(FILE, $ARGV[0]) || die $!;
+open(OUT, ">$ARGV[1]") || die $!;
+$mutation_chance = 0.20;
+$mutation_allowed = 1;
+
+while(<FILE>) {
+ next if (/^\s*$/);
+ if (/^\s*#/) {
+ $mutation_allowed = 1 if (/^#\s*BEGIN MUTATION ALLOWED/);
+ $mutation_allowed = 0 if (/^#\s*END MUTATION ALLOWED/);
+ print OUT;
+ next;
+ }
+ $should_mutate = rand() < $mutation_chance;
+ $mutation_amount = (rand() - 0.5) / 2.7;
+ if (($should_mutate) && ($mutation_allowed)) {
+ @arr = split(/,/);
+ for $a (@arr) {
+ $x = ($a * $mutation_amount);
+ $x += $a;
+ $x = int($x);
+ print OUT "$x,";
+ }
+ print OUT "\n";
+ } else {
+ print OUT;
+ }
+}
+exit 0;