Saving and Loading State #38
Replies: 3 comments
-
Yeah, so part of the problem is if I try to naively dump an organism to json: ... Ok, I was able to dump an organism to JSON, but I had to delete a bunch of fields in order to remove the cycles. What I would highly recommend is going to a pure-data-structure-based model. I would add a This arrangement has the nice property that the ID can actually the hash of the gene (e.g. md5sum of Those cycles are might make it challenging to de/serialize the game board, so that will take some thought, but it shouldn't be too hard to get started with a copy/paste organism from clipboard. ... If that sounds interesting to you, I can take a stab at the refactor (shouldn't be too involved) and send a PR your way. |
Beta Was this translation helpful? Give feedback.
-
That might be a bit abstract still so I took some time to put together a rough sketch of what I'm thinking. Basically at the "nucleus" of each organism is its genome, a data-only object which is easy to dump to json. Something like:
Then organisms are built from genes. Since genome is now a static data structure (read: hashable), keeping track of organisms becomes much easier - instead of having to maintain state, you just have a hashmap. Makes things a lot easier to reason about. OH, and unit testing. Makes unit testing way easier! |
Beta Was this translation helpful? Give feedback.
-
Hey @xkortex. Sorry its taken so long to respond to this. I've been thinking about it a lot and you are absolutely correct. I would like to implement saving/loading organisms, and this would be a huge pain to do with the current structure. Your system of having an inner immutable and easily serializable "gene" object would not only make this much easier, but radically improve reproduction, in which a parent simply passes a deep copy of its gene object down to its offspring, which constructs itself from this alone. This would be a significant refactor, but a deep improvement. Anyway just wanted to say your thoughts have been very helpful, and when I get around to implementing this I may use your code mockup. |
Beta Was this translation helpful? Give feedback.
-
I'm sure it's already on your mind, but I think it's worth starting a thread, especially for getting into the weeds with the implementation: saving and loading state. There's a few main pieces of state I've identified:
That list is in rough order of the difficulty of implementing saving/loading. I dug a bit into the code and it's very OOP-style code, which makes serialization/deserialization a bit tricky. It looks like to reproduce, you make a new "blank" organism and then literally mutate state (pun intended =D).
Imagine each Gene is a data structure, separate from the Organism. It is immutable from the perspective of the Organism. The Organism's state comprises its immutable state (genome, traits, cell layout) and its mutable state (health, location, actual cells). You don't ever actually mutate the Gene directly, what you do is make a copy of the gene, splice what you need to, and return a new one.
Edit: I realized you can just JSON.stringify arbitrary objects so for starters we can probably just go that route to shuttle data in and out.
Beta Was this translation helpful? Give feedback.
All reactions