I am currently trying to make an evolutional neural network, and for that purpose I am at first giving the objects random values. This works af first but after a few iterations they start to behave in a very strange way. Sometimes when a new iteration starts they all move in the same pattern or with the same speed.
I've researched a bit and from what I read apparently Random can output the same numbers if used in a very short time span, but I have no clue how to prevent that/work around that. I am working with only one global instance of Random, all in one thread so the problem is not there.
private void Evolve()
{
var newNetObjects = new List<NeuralNetObject>(); // make a list for new objects we create replace old ones
var netObjects =
new List<NeuralNetObject>(NeuralNetObjectManager.NeuralNetObjects); //get all neuralnetobjects
if (netObjects.Count == 0) //just stop when the list is empty
return;
//the half we remove is the one with the lower fitness values
foreach (var netObject in netObjects) //go through all net objects and do crossover of weights
{
float[] newWeigths = (float[]) netObject.GetNet().Weights.Clone(); //get original weights
;
var randomNet = rnd.Next(netObjects.Count); //random number
float[] otherWeigths = (float[]) netObjects[randomNet].GetNet().Weights.Clone(); //get a random other net
for (int i = 0; i < newWeigths.Length; i++) //for each weight
{
if (rnd.Next(2) == 1)
{
newWeigths[i] = otherWeigths[i]; // 25% to get the weight of the other net
}
}
var newNet = new NeuralNet(netObject.GetNet())
{
Weights = (float[]) newWeigths.Clone()
}; // make a new neural net with the generated weigths
newNetObjects.Add(new NeuralNetObject(100, 300, "KISTE", newNet)
{
Speed = (float) (rnd.NextDouble() - 0.5f) * 2, Direction = (float) (rnd.NextDouble() - 0.5f) * 2
}); //add new object to list
}
}
So I might be missing something, but I thought what I coded should assign every instance at
newNetObjects.Add(new NeuralNetObject(100, 300, "KISTE", newNet)
{
Speed = (float) (rnd.NextDouble() - 0.5f) * 2, Direction = (float) (rnd.NextDouble() - 0.5f) * 2
});
a random speed and direction, but as I said earlier, after a few iterations they all move with the same speed and sometimes even with the same pattern. Do you have any idea what my problem would be? If you need I can post more code, but what I posted is simplified and some stuff taken out so it's easier to read and understand. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire