Exemple #1
0
        public void TestSignReceptiveField()
        {
            var rf = new SignReceptiveField(60.0, 30.0);

            Assert.AreEqual(1.0, rf.Normalize(60.0));
            Assert.AreEqual(1.0, rf.Normalize(75.0));
            Assert.AreEqual(1.0, rf.Normalize(45.0));
            Assert.AreEqual(0.0, rf.Normalize(90.0));
            Assert.AreEqual(0.0, rf.Normalize(30.0));
        }
        public void TestNetworkInputPort_Sign()
        {
            var rfs = new Dictionary<int, IReceptiveField>();
              rfs[0] = new SignReceptiveField(0.0, 1.0f);
              rfs[1] = new SignReceptiveField(1.0, 1.0f);
              rfs[2] = new SignReceptiveField(2.0, 1.0f);
              rfs[3] = new SignReceptiveField(3.0, 1.0f);
              rfs[4] = new SignReceptiveField(4.0, 1.0f);

              var arr = new []{0.0, 0.0, 0.0, 0.0, 0.0};
              Assert.AreEqual(arr.Length, rfs.Count);

              var port = new NetworkInputPort(arr, rfs);

              port.Set(3.5);
              Assert.IsTrue(arr.SequenceEqual(new []{0.0, 0.0, 0.0, 30.0, 30.0}));

              port.Set(3.0);
              Assert.IsTrue(arr.SequenceEqual(new []{0.0, 0.0, 0.0, 30.0, 0.0}));
        }
Exemple #3
0
        public void TestNetworkInputPort_Sign()
        {
            var rfs = new Dictionary <int, IReceptiveField>();

            rfs[0] = new SignReceptiveField(0.0, 1.0f);
            rfs[1] = new SignReceptiveField(1.0, 1.0f);
            rfs[2] = new SignReceptiveField(2.0, 1.0f);
            rfs[3] = new SignReceptiveField(3.0, 1.0f);
            rfs[4] = new SignReceptiveField(4.0, 1.0f);

            var arr = new [] { 0.0, 0.0, 0.0, 0.0, 0.0 };

            Assert.AreEqual(arr.Length, rfs.Count);

            var port = new NetworkInputPort(arr, rfs);

            port.Set(3.5);
            Assert.IsTrue(arr.SequenceEqual(new [] { 0.0, 0.0, 0.0, 30.0, 30.0 }));

            port.Set(3.0);
            Assert.IsTrue(arr.SequenceEqual(new [] { 0.0, 0.0, 0.0, 30.0, 0.0 }));
        }
Exemple #4
0
    public static NetworkPorts FromGenotype(NEAT.Genotype genotype)
    {
        var neuronGenes  = genotype.NeuronGenes.ToList();
        var synapseGenes = genotype.SynapseGenes.ToList();

        var network = new Neural.Network(20ul);

        var upperNeurons    = new Dictionary <int, IReceptiveField>();
        var lowerNeurons    = new Dictionary <int, IReceptiveField>();
        var positionNeurons = new Dictionary <int, IReceptiveField>();
        var speedNeurons    = new Dictionary <int, IReceptiveField>();

        foreach (var neuronGene in neuronGenes)
        {
            float a = NumberHelper.Scale(neuronGene.a, 0.02f, 0.1f);    // 0.1
            float b = NumberHelper.Scale(neuronGene.b, 0.2f, 0.25f);    // 0.2
            float c = NumberHelper.Scale(neuronGene.c, -65.0f, -50.0f); // -65.0
            float d = NumberHelper.Scale(neuronGene.d, 0.05f, 8.0f);    // 2.0

            try {
                var id = (int)network.AddNeuron(Neural.IzhikevichConfig.Of(a, b, c, d));

                var mean  = 0.0f;
                var sigma = 0.0f;
                switch (neuronGene.type)
                {
                case NeuronType.UpperNeuron:
                    mean             = NumberHelper.Scale(neuronGene.mean, -180.0f, 180.0f);
                    sigma            = NumberHelper.Scale(neuronGene.sigma, 0.0f, 180.0f);
                    upperNeurons[id] = new SignReceptiveField(mean, sigma);
                    break;

                case NeuronType.LowerNeuron:
                    mean             = NumberHelper.Scale(neuronGene.mean, -180.0f, 180.0f);
                    sigma            = NumberHelper.Scale(neuronGene.sigma, 0.0f, 180.0f);
                    lowerNeurons[id] = new SignReceptiveField(mean, sigma);
                    break;

                case NeuronType.PositionNeuron:
                    mean  = NumberHelper.Scale(neuronGene.mean, -12.0f, 12.0f);
                    sigma = NumberHelper.Scale(neuronGene.sigma, 0.0f, 12.0f);
                    positionNeurons[id] = new SignReceptiveField(mean, sigma);
                    break;

                case NeuronType.SpeedNeuron:
                    mean             = NumberHelper.Scale(neuronGene.mean, -1.0f, 1.0f);
                    sigma            = NumberHelper.Scale(neuronGene.sigma, 0.0f, 1000.0f);
                    speedNeurons[id] = new MulReceptiveField(mean, sigma);
                    break;
                }
            } catch (Exception e) {
                Debug.LogException(e);
            }
        }

        // Connect each input neuron to the output neuron.
        foreach (var synapseGene in synapseGenes)
        {
            if (!synapseGene.isEnabled)
            {
                continue;
            }

            var fromNeuronId = neuronGenes.FindIndex(n => n.InnovationId == synapseGene.fromNeuronId);
            var toNeuronId   = neuronGenes.FindIndex(n => n.InnovationId == synapseGene.toNeuronId);

            Assert.AreNotEqual(fromNeuronId, -1, "Must find from-neuron id");
            Assert.AreNotEqual(toNeuronId, -1, "Must find to-neuron id");

            float weight = NumberHelper.Scale(synapseGene.weight, -40.0f, 40.0f);

            try {
                network.AddSynapse((ulong)fromNeuronId, (ulong)toNeuronId, Neural.STDPConfig.Of(weight, -40.0f, 40.0f));
            } catch (Exception e) {
                Debug.LogException(e);
            }
        }

        return(new NetworkPorts(network, upperNeurons, lowerNeurons, positionNeurons, speedNeurons));
    }
    public static NetworkPorts FromGenotype(NEAT.Genotype genotype)
    {
        var neuronGenes = genotype.NeuronGenes.ToList();
        var synapseGenes = genotype.SynapseGenes.ToList();

        var network = new Neural.Network(20ul);

        var upperNeurons = new Dictionary<int, IReceptiveField>();
        var lowerNeurons = new Dictionary<int, IReceptiveField>();
        var positionNeurons = new Dictionary<int, IReceptiveField>();
        var speedNeurons = new Dictionary<int, IReceptiveField>();

        foreach (var neuronGene in neuronGenes) {
          float a = NumberHelper.Scale(neuronGene.a, 0.02f, 0.1f); // 0.1
          float b = NumberHelper.Scale(neuronGene.b, 0.2f, 0.25f); // 0.2
          float c = NumberHelper.Scale(neuronGene.c, -65.0f, -50.0f); // -65.0
          float d = NumberHelper.Scale(neuronGene.d, 0.05f, 8.0f); // 2.0

          try {
          var id = (int)network.AddNeuron(Neural.IzhikevichConfig.Of(a, b, c, d));

        var mean = 0.0f;
        var sigma = 0.0f;
        switch (neuronGene.type) {
          case NeuronType.UpperNeuron:
            mean = NumberHelper.Scale(neuronGene.mean, -180.0f, 180.0f);
            sigma = NumberHelper.Scale(neuronGene.sigma, 0.0f, 180.0f);
            upperNeurons[id] = new SignReceptiveField(mean, sigma);
            break;
          case NeuronType.LowerNeuron:
            mean = NumberHelper.Scale(neuronGene.mean, -180.0f, 180.0f);
            sigma = NumberHelper.Scale(neuronGene.sigma, 0.0f, 180.0f);
            lowerNeurons[id] = new SignReceptiveField(mean, sigma);
            break;
          case NeuronType.PositionNeuron:
            mean = NumberHelper.Scale(neuronGene.mean, -12.0f, 12.0f);
            sigma = NumberHelper.Scale(neuronGene.sigma, 0.0f, 12.0f);
            positionNeurons[id] = new SignReceptiveField(mean, sigma);
            break;
          case NeuronType.SpeedNeuron:
            mean = NumberHelper.Scale(neuronGene.mean, -1.0f, 1.0f);
            sigma = NumberHelper.Scale(neuronGene.sigma, 0.0f, 1000.0f);
            speedNeurons[id] = new MulReceptiveField(mean, sigma);
            break;
        }
          } catch (Exception e) {
        Debug.LogException(e);
          }
        }

        // Connect each input neuron to the output neuron.
        foreach (var synapseGene in synapseGenes) {
          if (!synapseGene.isEnabled) {
        continue;
          }

          var fromNeuronId = neuronGenes.FindIndex(n => n.InnovationId == synapseGene.fromNeuronId);
          var toNeuronId = neuronGenes.FindIndex(n => n.InnovationId == synapseGene.toNeuronId);

          Assert.AreNotEqual(fromNeuronId, -1, "Must find from-neuron id");
          Assert.AreNotEqual(toNeuronId, -1, "Must find to-neuron id");

          float weight = NumberHelper.Scale(synapseGene.weight, -40.0f, 40.0f);

          try {
        network.AddSynapse((ulong)fromNeuronId, (ulong)toNeuronId, Neural.STDPConfig.Of(weight, -40.0f, 40.0f));
          } catch (Exception e) {
        Debug.LogException(e);
          }
        }

        return new NetworkPorts(network, upperNeurons, lowerNeurons, positionNeurons, speedNeurons);
    }