generic-github-user / Caesium

General-purpose AI library with NEAT-style genetic algorithm.
https://generic-github-user.github.io/Caesium/src/versions/javascript/projects/network-visualization/
MIT License
1 stars 1 forks source link

Cloning a network removes its methods #87

Closed generic-github-user closed 5 years ago

generic-github-user commented 5 years ago

"Cloning" a network, including its functions, may not be possible: https://stackoverflow.com/questions/18089033/json-stringify-does-not-process-object-methods

generic-github-user commented 5 years ago

Could create a new network, then copy over all information from the first network (nodes, connections, etc.), or clone the network with JSON.parse(JSON.stringify(network)) and use a separate function to add methods to it, as these should be the same for all networks.

generic-github-user commented 5 years ago

Could also use Object.assign(), as this seems to be working well in testing.

generic-github-user commented 5 years ago

New cloning function:

const clone = function (object) {
      return Object.assign({}, object);
}
generic-github-user commented 5 years ago

This should be tested.

generic-github-user commented 5 years ago

This doesn't work - object.assign() does a "shallow" copy of an object, so any modifications to the network more than one level deep affect all clones of the network. See this question on StackOverflow: https://stackoverflow.com/questions/34504682/js-does-object-assign-create-deep-copy-or-shallow-copy

generic-github-user commented 5 years ago

An immutable data structure may be needed instead of cloning network objects.

generic-github-user commented 5 years ago

See these threads on Hacker News: https://news.ycombinator.com/item?id=16233330 https://news.ycombinator.com/item?id=17126981

generic-github-user commented 5 years ago

This Stack Overflow answer further explains some of the issues with deep cloning an object in JavaScript: https://stackoverflow.com/a/728694

generic-github-user commented 5 years ago

An immutable data structure will be used for the initial release for future stability and expandability.

89

generic-github-user commented 5 years ago

For now, a mutable data structure will be used for network objects: https://github.com/generic-github-user/Caesium/issues/91#issuecomment-415371880

generic-github-user commented 5 years ago

It turns out that setting up an immutable data structure isn't as simple as it sounds . . .

generic-github-user commented 5 years ago

JSON.parse(JSON.stringify()) is being used with a a separate function to add methods to each cloned network object.

generic-github-user commented 5 years ago

Looks like we will actually be using an immutable data structure . . . references within network objects being cloned are causing issues: #92

generic-github-user commented 5 years ago

UUID system for all networks, nodes, and connections has been implemented; all internal references within network objects have been removed. See #91 and #93.