Example #1
0
    /// <summary>
    /// Initialises a new agent from given genotype, constructing a new feedfoward neural network from
    /// the parameters of the genotype.
    /// </summary>
    /// <param name="genotype">The genotpye to initialise this agent from.</param>
    /// <param name="topology">The topology of the feedforward neural network to be constructed from given genotype.</param>
    public Agent(Genotype genotype, NeuralLayer.ActivationFunction defaultActivation, params uint[] topology)
    {
        IsAlive       = false;
        this.Genotype = genotype;
        FNN           = new NeuralNetwork(topology);
        foreach (NeuralLayer layer in FNN.Layers)
        {
            layer.NeuronActivationFunction = defaultActivation;
        }

        //Check if topology is valid
        if (FNN.WeightCount != genotype.ParameterCount)
        {
            throw new ArgumentException("The given genotype's parameter count must match the neural network topology's weight count.");
        }

        //Construct FNN from genotype
        IEnumerator <float> parameters = genotype.GetEnumerator();

        foreach (NeuralLayer layer in FNN.Layers)                    //Loop over all layers
        {
            for (int i = 0; i < layer.Weights.GetLength(0); i++)     //Loop over all nodes of current layer
            {
                for (int j = 0; j < layer.Weights.GetLength(1); j++) //Loop over all nodes of next layer
                {
                    layer.Weights[i, j] = parameters.Current;
                    parameters.MoveNext();
                }
            }
        }
    }
Example #2
0
    public static Agent LoadFromFile(string filePath)
    {
        AgentData ad = Save.ReadFromXmlFile <AgentData>(filePath);

        NeuralLayer.ActivationFunction af = NeuralLayer.GetActivitionFunction(ad.activationFunctionType);
        return(new Agent(ad.genotype, af, ad.useRNN, ad.topology));
    }
Example #3
0
    public void CreateAgents(out List <Agent> agents, IEnumerable <Genotype> population)
    {
        //Create new agents from currentPopulation
        agents = new List <Agent>();
        NeuralLayer.ActivationFunction af = NeuralLayer.GetActivitionFunction(activationFunctionType);

        foreach (Genotype genotype in population)
        {
            agents.Add(new Agent(genotype, af, useRNN, NNTopology));
        }
        isNewAgents = true;
    }
 /// <summary>
 /// 构造遗传代理
 /// </summary>
 /// <param name="_genotype">基因</param>
 /// <param name="_activation">激活函数</param>
 /// <param name="_topogy">拓扑结构</param>
 public Agent(Genotype _genotype, NeuralLayer.ActivationFunction _activation, params uint[] _topogy)
 {
     Genotype = _genotype;
     FNN = new NeuralNetwork(_topogy);
     foreach (NeuralLayer layer in FNN.Layers)
     {
         layer.NeuronActivationFunction = _activation;
     }
     if(FNN.WeightCount != _genotype.ParameterCount)
         throw new ArgumentException("WeightCount != Topology");
     //使用基因参数初始化神经网络
     IEnumerator<float> parameters = _genotype.GetEnumerator();
     foreach (NeuralLayer layer in FNN.Layers)
     {
         for (int i = 0; i < layer.Weights.GetLength(0); i++)
         {
             for (int j = 0; j < layer.Weights.GetLength(1); j++)
             {
                 layer.Weights[i, j] = parameters.Current;
                 parameters.MoveNext();
             }
         }
     }
 }