Guest User

Untitled

a guest
Jun 23rd, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NeuralNetwork {
  6.     public List<Layer> layers = new List<Layer>();
  7.     static float sigmoid ( float x )
  8.     {
  9.      return 1f / (1f + Mathf.Exp(-x));
  10.     }
  11.     public class Layer
  12.     {
  13.         public int index;
  14.         public NeuralNetwork _parentNetwork;
  15.         public List<Neuron> neurons = new List<Neuron>();
  16.     }
  17.     public class Neuron{
  18.         public Layer _parentLayer;
  19.         public float _Value;
  20.         public float[] weights;
  21.         public float Value{
  22.             get{
  23.                 //input layer
  24.                 if(_parentLayer.index==0)
  25.                 {
  26.                     return _Value;
  27.                 }else{
  28.                     //hidden/output layers
  29.                     float result = 0;
  30.                     for(int i=0;i<weights.Length;i++)
  31.                     {
  32.                         result+=_parentLayer._parentNetwork.layers[_parentLayer.index-1].neurons[i].Value*weights[i];//_parentLayer.neurons[i].Value*weights[i];
  33.                     }
  34.                     return sigmoid(result);
  35.                 }
  36.             }
  37.             set{
  38.                 _Value = value;
  39.             }
  40.         }
  41.         public Neuron(Layer _pl)
  42.         {
  43.             _pl.neurons.Add(this);
  44.             _parentLayer = _pl;
  45.             Value = 0;
  46.         }
  47.     }
  48.     public Layer input()
  49.     {
  50.         return layers[0];
  51.     }
  52.     public Layer output()
  53.     {
  54.         return layers[layers.Count-1];
  55.     }
  56.     public void SetInput(int index, float _value)
  57.     {
  58.         input().neurons[index].Value = _value;
  59.     }
  60.     public List<float> GetOutput()
  61.     {
  62.         Layer _l = output();
  63.         List<float> result = new List<float>();
  64.         foreach(Neuron n in _l.neurons)
  65.         {
  66.             result.Add(n.Value);
  67.         }
  68.         return result;
  69.     }
  70.     public NeuralNetwork(int[] layerCounts)
  71.     {
  72.         layers = new List<Layer>();
  73.         for(int i = 0;i<layerCounts.Length;i++)
  74.         {
  75.             layers.Add(new Layer());
  76.             layers[i].neurons = new List<Neuron>();
  77.             layers[i].index = i;
  78.             layers[i]._parentNetwork = this;
  79.             for(int d = 0;d<layerCounts[i];d++)
  80.             {
  81.                 Neuron n = new Neuron(layers[i]);
  82.                 if(i>0){
  83.                     n.weights = new float[layerCounts[i-1]];
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     //random mutations
  89.     public void Mutate()
  90.     {
  91.         for(int i = 1;i<layers.Count;i++)
  92.         {
  93.             for(int d = 0; d<layers[i].neurons.Count;d++)
  94.             {
  95.                 for(int p = 0; p<layers[i].neurons[d].weights.Length;p++)
  96.                 {
  97.                     float r = Random.Range(0f,1f);
  98.                     if(r<0.02f)
  99.                     {
  100.                         layers[i].neurons[d].weights[p] += 0.1f;
  101.                     }else
  102.                     if(r<0.04f)
  103.                     {
  104.                         layers[i].neurons[d].weights[p] *= -1;
  105.                     }else
  106.                     if(r<0.08f)
  107.                     {
  108.                         layers[i].neurons[d].weights[p] *=0.5f;
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment