mutation_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package gago
  2. import (
  3. "testing"
  4. )
  5. func sliceContainsInt(x int, ints []int) bool {
  6. for _, v := range ints {
  7. if v == x {
  8. return true
  9. }
  10. }
  11. return false
  12. }
  13. func sliceContainsFloat64(x float64, floats []float64) bool {
  14. for _, v := range floats {
  15. if v == x {
  16. return true
  17. }
  18. }
  19. return false
  20. }
  21. func sliceContainsString(x string, strings []string) bool {
  22. for _, v := range strings {
  23. if v == x {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. func TestMutNormalFloat64All(t *testing.T) {
  30. var (
  31. rng = newRandomNumberGenerator()
  32. genome = []float64{1, 2, 3}
  33. mutated = make([]float64, len(genome))
  34. )
  35. copy(mutated, genome)
  36. MutNormalFloat64(mutated, 1, rng)
  37. for i, v := range mutated {
  38. if v == genome[i] {
  39. t.Error("Gene should have been modified but hasn't")
  40. }
  41. }
  42. }
  43. func TestMutNormalFloat64None(t *testing.T) {
  44. var (
  45. rng = newRandomNumberGenerator()
  46. genome = []float64{1, 2, 3}
  47. mutated = make([]float64, len(genome))
  48. )
  49. copy(mutated, genome)
  50. MutNormalFloat64(mutated, 0, rng)
  51. for i, v := range mutated {
  52. if v != genome[i] {
  53. t.Error("Gene has been modified but shouldn't have")
  54. }
  55. }
  56. }
  57. func TestMutUniformString(t *testing.T) {
  58. var (
  59. rng = newRandomNumberGenerator()
  60. genome = []string{"a", "b", "c"}
  61. corpus = []string{"d", "e", "f"}
  62. mutated = make([]string, len(genome))
  63. )
  64. copy(mutated, genome)
  65. MutUniformString(mutated, corpus, 3, rng)
  66. // Check the length of the mutated genome is consistent
  67. if len(mutated) != len(genome) {
  68. t.Error("Mutated genome has the wrong length")
  69. }
  70. // Check the new genes are present in the previous genome or in the corpus
  71. for _, v := range mutated {
  72. var inGenome = false
  73. for _, gene := range genome {
  74. if gene == v {
  75. inGenome = true
  76. }
  77. }
  78. var inCorpus = false
  79. for _, element := range corpus {
  80. if element == v {
  81. inCorpus = true
  82. }
  83. }
  84. if !inGenome && !inCorpus {
  85. t.Error("New genome is not present in previous genome or in corpus")
  86. }
  87. }
  88. }
  89. func TestMutPermuteSingleGene(t *testing.T) {
  90. var (
  91. rng = newRandomNumberGenerator()
  92. genome = []int{42}
  93. )
  94. MutPermuteInt(genome, 1, rng)
  95. // Check the length of the mutated genome
  96. if len(genome) != 1 {
  97. t.Error("Mutated genome has the wrong length")
  98. }
  99. // Check the value of the mutated genome
  100. for genome[0] != 42 {
  101. t.Error("Mutated genome has the wrong value")
  102. }
  103. }
  104. func TestMutPermuteInt(t *testing.T) {
  105. var (
  106. rng = newRandomNumberGenerator()
  107. genome = []int{1, 2, 3}
  108. mutated = make([]int, len(genome))
  109. )
  110. copy(mutated, genome)
  111. MutPermuteInt(mutated, 3, rng)
  112. // Check the length of the mutated genome is consistent
  113. if len(mutated) != len(genome) {
  114. t.Error("Mutated genome has the wrong length")
  115. }
  116. // Check the genes in the initial genome are still present
  117. for _, v := range genome {
  118. if !sliceContainsInt(v, mutated) {
  119. t.Error("Gene in initial genome has disappeared")
  120. }
  121. }
  122. }
  123. func TestMutPermuteFloat64(t *testing.T) {
  124. var (
  125. rng = newRandomNumberGenerator()
  126. genome = []float64{1, 2, 3}
  127. mutated = make([]float64, len(genome))
  128. )
  129. copy(mutated, genome)
  130. MutPermuteFloat64(mutated, 3, rng)
  131. // Check the length of the mutated genome is consistent
  132. if len(mutated) != len(genome) {
  133. t.Error("Mutated genome has the wrong length")
  134. }
  135. // Check the genes in the initial genome are still present
  136. for _, v := range genome {
  137. if !sliceContainsFloat64(v, mutated) {
  138. t.Error("Gene in initial genome has disappeared")
  139. }
  140. }
  141. }
  142. func TestMutPermuteString(t *testing.T) {
  143. var (
  144. rng = newRandomNumberGenerator()
  145. genome = []string{"a", "b", "c"}
  146. mutated = make([]string, len(genome))
  147. )
  148. copy(mutated, genome)
  149. MutPermuteString(mutated, 3, rng)
  150. // Check the length of the mutated genome is consistent
  151. if len(mutated) != len(genome) {
  152. t.Error("Mutated genome has the wrong length")
  153. }
  154. // Check the genes in the initial genome are still present
  155. for _, v := range genome {
  156. if !sliceContainsString(v, mutated) {
  157. t.Error("Gene in initial genome has disappeared")
  158. }
  159. }
  160. }
  161. func TestMutSpliceInt(t *testing.T) {
  162. var (
  163. rng = newRandomNumberGenerator()
  164. genome = []int{1, 2, 3}
  165. mutated = make([]int, len(genome))
  166. )
  167. copy(mutated, genome)
  168. MutSpliceInt(mutated, rng)
  169. // Check the length of the mutated genome is consistent
  170. if len(mutated) != len(genome) {
  171. t.Error("Mutated genome has the wrong length")
  172. }
  173. // Check the genes in the initial genome are still present
  174. for _, v := range genome {
  175. if !sliceContainsInt(v, mutated) {
  176. t.Error("Gene in initial genome has disappeared")
  177. }
  178. }
  179. }
  180. func TestMutSpliceFloat64(t *testing.T) {
  181. var (
  182. rng = newRandomNumberGenerator()
  183. genome = []float64{1, 2, 3}
  184. mutated = make([]float64, len(genome))
  185. )
  186. copy(mutated, genome)
  187. MutSpliceFloat64(mutated, rng)
  188. // Check the length of the mutated genome is consistent
  189. if len(mutated) != len(genome) {
  190. t.Error("Mutated genome has the wrong length")
  191. }
  192. // Check the genes in the initial genome are still present
  193. for _, v := range genome {
  194. if !sliceContainsFloat64(v, mutated) {
  195. t.Error("Gene in initial genome has disappeared")
  196. }
  197. }
  198. }
  199. func TestMutSpliceString(t *testing.T) {
  200. var (
  201. rng = newRandomNumberGenerator()
  202. genome = []string{"a", "b", "c"}
  203. mutated = make([]string, len(genome))
  204. )
  205. copy(mutated, genome)
  206. MutSpliceString(mutated, rng)
  207. // Check the length of the mutated genome is consistent
  208. if len(mutated) != len(genome) {
  209. t.Error("Mutated genome has the wrong length")
  210. }
  211. // Check the genes in the initial genome are still present
  212. for _, v := range genome {
  213. if !sliceContainsString(v, mutated) {
  214. t.Error("Gene in initial genome has disappeared")
  215. }
  216. }
  217. }