-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mutators.fs
83 lines (66 loc) · 3.16 KB
/
Mutators.fs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(*
GeneticFramework
Copyright 2016 Ivan Sarno
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
///Generic mutation operators
module GeneticFramework.Generic.Mutators
let private rand = System.Random()
///apply a basic mutator with a probability
let optionalMutator mutator probability element =
if rand.NextDouble() <= probability then
mutator element
///Swaps 2 random elements
let swapper parent =
let index1 = rand.Next (Array.length parent)
let index2 = rand.Next (Array.length parent)
let temp = parent.[index1]
parent.[index1] <- parent.[index2]
parent.[index2] <- temp
///Applay all mutators on the element in sequence
let sequenceMutator mutators element =
for m in mutators do m element
///Apply a mutation on a random gene
let singleMutator mutation chromosoma =
let index = rand.Next (Array.length chromosoma)
chromosoma.[index] <- mutation chromosoma.[index]
///Apply a mutation on random genes
let multiMutator mutation mutNumber chromosoma =
for i in 1..mutNumber do
let index = rand.Next (Array.length chromosoma)
chromosoma.[index] <- mutation chromosoma.[index]
///Like multiMutator with a random number of mutations
let randMultiMutator mutation maxMut chromosoma =
multiMutator mutation (rand.Next(1, maxMut)) chromosoma
///Apply a mutation to all genes
let allMutator mutation probability chromosoma =
let length = Array.length chromosoma - 1
for i in 0..length do
chromosoma.[i] <- if rand.NextDouble() >= probability
then mutation chromosoma.[i] else chromosoma.[i]
///Apply a random mutation from the mutations array to all genes
let randMutator mutations probability chromosoma =
let length = Array.length chromosoma - 1
let mutNumber = Array.length mutations
for i in 0..length do
let mut = rand.Next(mutNumber)
chromosoma.[i] <- if rand.NextDouble() >= probability
then mutations.[mut] chromosoma.[i] else chromosoma.[i]
///Apply a random mutation from the mutations array to a random gene
let randMutation mutations chromosoma =
let length = Array.length chromosoma - 1
let mutNumber = Array.length mutations
let i = rand.Next length
chromosoma.[i] <- mutations.[rand.Next(mutNumber)] chromosoma.[i]
///Apply a random mutator from the mutators array
let randomMutator mutatiors chromosoma =
let mutNumber = Array.length mutatiors
mutatiors.[rand.Next(mutNumber)] chromosoma