Source: connection.js

  1. /**
  2. * Connections help:
  3. * * a) control the flow information inside of a neural network
  4. * * b) describe the shape of a neural network
  5. * * c) ease the use of Evolutionary Algoriths
  6. *
  7. * To facilitate the use of Evolutionary Algoriths, Connections are given Unique
  8. * _Temporal-Structural IDs_ using the [Cantor Pairing Algorithm](https://en.wikipedia.org/wiki/Pairing_function).
  9. *
  10. * _Temporal-Structural IDs_: are not only a method of uniquely identifying a
  11. * connection, they also allow us to identify a) where in the network the
  12. * connection exists (i.e. between what neurons), and b) when it was
  13. * "created-ish".
  14. *
  15. * Connection IDs created using the _Cantor Pairing Algorithm_ enable stronger
  16. * algorithms, i.e. NEAT/HyperNEAT, to create networks of arbitrary
  17. * sizes/shapes.
  18. *
  19. * @constructs Connection
  20. *
  21. * @prop {string} id Unique connection ID
  22. * @prop {Neuron|Group} a Side "A" of connection(s)
  23. * @prop {Neuron|Group} b Side "B" of connection(s)
  24. * @prop {number|number[]} [weight] Weight of connection(s)
  25. *
  26. * @param {Neuron|Group} a Neuron(s) on one edge of the connection
  27. * @param {Neuron|Group} b Neruon(s) on another edge of the connection
  28. * @param {number|number[]} [weight] Weight of connection(s)
  29. * @param {Object} [options]
  30. *
  31. * @example
  32. * const connection = new Connection(neuron, other) // Connection { a: neuron, b: other }
  33. *
  34. * const connection = new Connection(neuron, other, 0.3) // Connection { a: neuron, b: other, weight: 0.3 }
  35. */
  36. function Connection(from, to, weight, options) {
  37. this.id = Connection.uid(from.id, to.id);
  38. this.from = from;
  39. this.to = to;
  40. this.weight = weight == undefined ? Math.random() * 2 - 1 : weight;
  41. //================================================
  42. // UTILITY FUNCTIONS =============================
  43. //================================================
  44. this.toJSON = function() {
  45. return {
  46. id: this.id,
  47. from: this.from.id,
  48. to: this.to.id,
  49. weight: this.weight
  50. }
  51. }
  52. //================================================
  53. // EXPERIMENTAL ==================================
  54. //================================================
  55. // this.queue = {
  56. // forward: [],
  57. // backward: []
  58. // }
  59. // this.stream = {
  60. // forward: undefined,
  61. // backward: undefined
  62. // }
  63. // this.push = function(payload, forward=true) {
  64. // if(forward) this.queue.forward.unshift(payload);
  65. // else this.queue.backward.unshift(payload)
  66. // }
  67. // this.pull = function(forward=false) {
  68. // if(forward) return this.queue.forward.shift(payload);
  69. // else return this.queue.backward.shift(payload);
  70. // }
  71. //================================================
  72. // END EXPERIMENTAL ==============================
  73. //================================================
  74. }
  75. /**
  76. * Creates a unique structural ID for connection between two neurons using the
  77. * [Cantor Pairing Algorithm](https://en.wikipedia.org/wiki/Pairing_function).
  78. *
  79. * The _Cantor Pairing Algorithm_ us to a) mathematically, map any two
  80. * non-negative integers to a unique positive integer - it even is sensetive to
  81. * order (i.e. `Connection.uid([2,3]) !== Connection.uid([3,2])`), and b) "AI-ly"
  82. * it allows to keep track of unique structural connections across time as a
  83. * neural network mutates (i.e. changes "shape").
  84. *
  85. * @param {number} fromID - ID of _source_ neuron
  86. * @param {number} toID - ID of _destination_ neuron
  87. *
  88. * @returns {number} A unique integer ID created using the [Cantor Pairing Algorithm](https://en.wikipedia.org/wiki/Pairing_function)
  89. */
  90. Connection.uid = function(fromID, toID) {
  91. return 0.5 * (fromID + toID) * (fromID + toID + 1) + toID;
  92. }
  93. module.exports = Connection;