dummymutagen.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package mutagens
  26. import (
  27. "math/rand"
  28. "time"
  29. neuralnetwork "git.semlanik.org/semlanik/NeuralNetwork/neuralnetwork"
  30. )
  31. // DummyMutagen is simple randomized mutagen
  32. type DummyMutagen struct {
  33. chance float64
  34. mutationCount int
  35. }
  36. // NewDummyMutagen constructs DummyMutagen with specified mutation chance and
  37. // amount of mutations that should be applied per cycle
  38. func NewDummyMutagen(chance float64, mutationCount int) (dm *DummyMutagen) {
  39. dm = &DummyMutagen{
  40. chance: chance,
  41. mutationCount: mutationCount,
  42. }
  43. return
  44. }
  45. // Dummy implementaion of Mutagen inteface Mutate method
  46. // For DummyMutagen it gets pseudo-random number and validates if number in
  47. // chance bounds. After method applies randomized mutation for random weight
  48. // and bias in neuralnetwork.NeuralNetwork
  49. func (dm *DummyMutagen) Mutate(network *neuralnetwork.NeuralNetwork) {
  50. rand.Seed(time.Now().UnixNano())
  51. for l := 1; l < network.LayerCount; l++ {
  52. randomized := rand.Float64()
  53. if randomized < dm.chance {
  54. r, c := network.Weights[l].Dims()
  55. for o := 0; o < dm.mutationCount; o++ {
  56. mutationRow := int(rand.Uint32()) % r
  57. mutationColumn := int(rand.Uint32()) % c
  58. weight := rand.NormFloat64()
  59. bias := rand.NormFloat64()
  60. network.Weights[l].Set(mutationRow, mutationColumn, weight)
  61. network.Biases[l].Set(mutationRow, 0, bias)
  62. }
  63. }
  64. }
  65. }