summaryrefslogtreecommitdiff
path: root/src/genetic/mutate.pl
blob: ea4f38e0f83e641cf8e11efee5ba2d0312f50e47 (plain)
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
#!/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;