コード例 #1
0
    public void Save(IntWeightDictionary weights, IntNeuronDictionary neurons)
    {
        this.weights = weights;
        this.neurons = neurons;

        BinaryFormatter bf   = new BinaryFormatter();
        FileStream      file = File.Create(dataPath);

        GameData data = new GameData();

        data.weights = weights;
        data.neurons = neurons;

        bf.Serialize(file, data);
        file.Close();
        Debug.Log("Network saved!");
    }
コード例 #2
0
ファイル: NetworkVisualizer.cs プロジェクト: jonasstr/NEAT
    public void Draw(IntNeuronDictionary neurons, IntWeightDictionary weights)
    {
        if (neuronObjects != null)
        {
            foreach (var neuron in neuronObjects)
            {
                Destroy(neuron);
            }
        }

        if (weightObjects != null)
        {
            foreach (var weight in weightObjects)
            {
                Destroy(weight);
            }
        }

        neuronObjects = new List <GameObject>();
        foreach (var neuron in neurons.Values)
        {
            neuronPositions[neuron.ID] = (neuron.splitX * maxX - 0.5f * maxX) * 2f;//Random.Range(-.35f, 1.4f) * maxX - 0.5f * maxX;
            var neuronObject = Instantiate(neuronPrefab, transform.localPosition + new Vector3(neuronPositions[neuron.ID], neuron.splitY * maxY - 0.5f * maxY),
                                           Quaternion.identity, transform).gameObject;
            neuronObject.name = "Neuron" + neuron.ID;
            neuronObjects.Add(neuronObject);
        }

        foreach (var weight in weights.Values)
        {
            var connection = Instantiate(weightPrefab, Vector3.zero, Quaternion.identity, transform);
            renderer            = connection.GetComponent <LineRenderer>();
            renderer.startWidth = renderer.endWidth = lineWidth * weight.value;
            var color = weight.value < 0f ? Color.red : Color.green;
            color = weight.enabled ? color : Color.black;
            renderer.startColor = renderer.endColor = color;
            weightObjects.Add(connection);

            var splitXNeuronIn  = neuronPositions[weight.neuronIn];  //Random.Range(0f, 1f) * maxX - 0.5f * maxX;//neurons[weight.neuronIn].splitX * maxX - 0.5f * maxX;
            var splitYNeuronIn  = neurons[weight.neuronIn].splitY * maxY - 0.5f * maxY;
            var splitXNeuronOut = neuronPositions[weight.neuronOut]; //Random.Range(0f, 1f) * maxX;// - 0.5f * maxX;//neurons[weight.neuronOut].splitX * maxX - 0.5f * maxX;
            var splitYNeuronOut = neurons[weight.neuronOut].splitY * maxY - 0.5f * maxY;
            DrawLine(new Vector3(splitXNeuronIn, splitYNeuronIn) + transform.localPosition, new Vector3(splitXNeuronOut, splitYNeuronOut) + transform.localPosition);
        }
    }
コード例 #3
0
    public bool Load()
    {
        if (File.Exists(dataPath))
        {
            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file = File.Open(dataPath, FileMode.Open);

            if (file.Length > 0)
            {
                GameData data = (GameData)bf.Deserialize(file);
                file.Close();

                weights = data.weights;
                neurons = data.neurons;
            }
            return(true);
        }
        return(false);
    }
コード例 #4
0
ファイル: Genome.cs プロジェクト: jonasstr/NEAT
 public Genome(IntNeuronDictionary neurons, IntWeightDictionary weights)
 {
     innovationDB = InnovationDB.instance;
     this.neurons = neurons;
     this.weights = weights;
 }
コード例 #5
0
ファイル: Genome.cs プロジェクト: jonasstr/NEAT
 public Genome()
 {
     innovationDB = InnovationDB.instance;
     weights      = new IntWeightDictionary();
     neurons      = new IntNeuronDictionary();
 }
コード例 #6
0
ファイル: Genome.cs プロジェクト: jonasstr/NEAT
    public Genome Crossover(Genome other)
    {
        var    parentA = this;
        var    parentB = other;
        Genome best    = new Genome();

        if (parentA.fitness == parentB.fitness)
        {
            // if same fitness pick shortest genome
            if (parentA.NumGenes() < parentB.NumGenes())
            {
                best = parentA;
            }
            else if (parentB.NumGenes() < parentA.NumGenes())
            {
                best = parentB;
            }
            else
            {
                int randomElement = Random.Range(0, 2);
                best = randomElement == 0 ? parentA : parentB;
            }
        }

        else if (parentA.fitness > parentB.fitness)
        {
            best = parentA;
        }
        else
        {
            best = parentB;
        }

        var    childWeights = new IntWeightDictionary();
        var    childNeurons = new IntNeuronDictionary();
        int    parentAIndex = 0, parentBIndex = 0;
        Weight parentAWeight = null, parentBWeight = null;

        // while end of both genes is not reached
        while (parentAIndex < parentA.weights.Count || parentBIndex < parentB.weights.Count)
        {
            if (parentAIndex < parentA.weights.Count)
            {
                parentAWeight = parentA.weights.ElementAt(parentAIndex).Value.Copy();
            }
            if (parentBIndex < parentB.weights.Count)
            {
                parentBWeight = parentB.weights.ElementAt(parentBIndex).Value.Copy();
            }
            Weight selectedWeight = null;

            if (parentAIndex == parentA.weights.Count && parentBIndex != parentB.weights.Count)
            {
                // add excess genes
                if (parentB == best)
                {
                    selectedWeight = parentBWeight;
                }
                parentBIndex++;
            }

            else if (parentBIndex == parentB.weights.Count && parentAIndex != parentA.weights.Count)
            {
                // add excess genes
                if (parentA == best)
                {
                    selectedWeight = parentAWeight;
                }
                parentAIndex++;
            }

            else if (parentAWeight.innovID < parentBWeight.innovID)
            {
                // add parentA disjoint genes
                if (parentA == best)
                {
                    selectedWeight = parentAWeight;
                }
                parentAIndex++;
            }
            else if (parentBWeight.innovID < parentAWeight.innovID)
            {
                // add parentB disjoint genes
                if (parentB == best)
                {
                    selectedWeight = parentBWeight;
                }
                parentBIndex++;
            }

            else
            {
                int randomElement = Random.Range(0, 2);
                selectedWeight = randomElement == 0 ? parentAWeight : parentBWeight;

                if (!parentAWeight.enabled && !parentBWeight.enabled)
                {
                    float random = Random.value;
                    if (random <= pWeightEnabled)
                    {
                        selectedWeight.enabled = true;
                    }
                }

                parentAIndex++;
                parentBIndex++;
            }

            if (selectedWeight != null && !childWeights.ContainsKey(selectedWeight.innovID))
            {
                childWeights.Add(selectedWeight.innovID, selectedWeight);
            }

            if (selectedWeight != null)
            {
                if (!childNeurons.ContainsKey(selectedWeight.neuronIn))
                {
                    Neuron neuron = null;
                    if (parentA.weights.Any(x => x.Value.neuronIn == selectedWeight.neuronIn))
                    {
                        neuron = parentA.neurons[selectedWeight.neuronIn].Copy();
                    }
                    else if (parentB.weights.Any(x => x.Value.neuronIn == selectedWeight.neuronIn))
                    {
                        neuron = parentB.neurons[selectedWeight.neuronIn].Copy();
                    }

                    if (neuron == null)
                    {
                        Debug.LogError("NEURON IS NULL!");
                    }

                    childNeurons.Add(neuron.ID, neuron);
                }

                if (!childNeurons.ContainsKey(selectedWeight.neuronOut))
                {
                    Neuron neuron = null;
                    if (parentA.weights.Any(x => x.Value.neuronOut == selectedWeight.neuronOut))
                    {
                        neuron = parentA.neurons[selectedWeight.neuronOut].Copy();
                    }
                    else if (parentB.weights.Any(x => x.Value.neuronOut == selectedWeight.neuronOut))
                    {
                        neuron = parentB.neurons[selectedWeight.neuronOut].Copy();
                    }

                    if (neuron == null)
                    {
                        Debug.LogError("NEURON IS NULL!");
                    }

                    childNeurons.Add(neuron.ID, neuron);
                }
            }
        }
        return(new Genome(childNeurons, childWeights));
    }