c# – deep cloning related question – Education Career Blog

I have implemented an evolutionary algorithm in C# which kind of works but I have the assumption that the cloning does not. The algorithm maintains a population of object trees. Each object (tree) can be the result of cloning (e.g. after ‘natural selection’) and should be a unique object consisting of unique objects. Is there a simple way to determine whether the ‘object population’ contains unique/distinct objects – in other words whether objects are shared by more than one tree? I hope my question makes sense.

Thanks.

Best wishes,

Christian

PS: I implemented (I think) deep copy cloning via serialization see:

http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx

,

The way to verify whether two objects are the same objects in memory is by comparing them using Object.ReferenceEquals. This checks whether the “Pointers” are the same.

,

Cloning in C# is by default a shallow copy. The keyword you probably need to google for tutorials is “deep cloning”, in order to create object graphs that don’t share references.

,

What about following: Add a static counter to every class for member references of your ‘main tree class’. Increment counter it in every counstructor. Determine how many of a ‘subobjects’should be contained in tree object (or all tree-objects) and compare that to the counter.

,

OK, for first, let me see if I get you correctly:

RESULT is a object tree that has some data.

GENERATION is a collection of a result objects.

You have some ‘evolution’ method that moves each GENERATION to the next step.

If you want to check if one RESULT is equal to the other, you should implement IComparable on it, and for each of its members, do the same.

ADDITION:

Try to get rid of that kind of cloning, and make the clones manually – it WILL be faster. And speed is crucial to you here, because all heuristic comes down to muscle.

,

If what you’re asking is “how do i check if two object variables refer to the same bit of memory, you can call Object.ReferenceEquals

Leave a Comment