// Use this for initialization
    void Start()
    {
        Debug.Log("Population size: " + populationSize);
        //int width = (int)Mathf.Round(Mathf.Sqrt(populationSize));
        //int height = (int)Mathf.Round(Mathf.Sqrt(populationSize));

        testing = new ComputeBuffer(10, Marshal.SizeOf(typeof(Individual)));

        Debug.Log("Seed " + DateTime.Now.Millisecond);

        // Fill with random genome, and run first fitness test.
        int kernel = shader.FindKernel("InitializePopulation");

        DebugAux.Assert(kernel >= 0, "Couldn't find kernel: " + "InitializePopulation " + kernel);
        shader.SetBuffer(kernel, "Population", testing);
        shader.SetFloat("seed", DateTime.Now.Millisecond);
        shader.Dispatch(kernel, 32, 32, 1);

        Individual[] tes = new Individual[10];
        testing.GetData(tes);
        for (int i = 0; i < tes.Length; i++)
        {
            Debug.Log(tes[i].genome + " " + tes[i].fitness);
        }

        // Selection..

        /*kernel = shader.FindKernel("AllOnesFitness");
         * DebugAux.Assert(kernel >= 0, "Couldn't find kernel: " + "AllOnesFitness " + kernel);
         * shader.SetBuffer(kernel, "Population", testing);
         * shader.Dispatch(kernel, 32, 32, 1);*/

        testing.Dispose();
    }
    public static BaseGenome CreateGenome(GenomeType type, int length)
    {
        switch (type)
        {
        case GenomeType.BinaryString: return(new GenomeBinaryString(length));

        case GenomeType.RawBinaryString32: return(new GenomeBinaryStringRaw32(length));

        case GenomeType.RawBinaryString64: return(new GenomeBinaryStringRaw64(length));

        case GenomeType.FloatString: return(new GenomeFloatString(length));
        }
        DebugAux.Assert(false, "There is no such genome type");
        return(null);
    }
Beispiel #3
0
    public NeuralNet(int[] layerSizes, float[] initialWeights)
    {
        // TODO: Clear distinction between the layers.
        DebugAux.Assert(layerSizes.Length >= 2, "Must have at least 2 layers");
        layers = new NeuronLayer[layerSizes.Length - 1];
        int counter = 0;

        for (int i = 0; i < layers.Length; i++)
        {
            int     size = (layerSizes[i] + 1) * layerSizes[i + 1];
            float[] dest = new float[size];
            Array.Copy(initialWeights, counter, dest, 0, size);
            counter  += size;
            layers[i] = new NeuronLayer(layerSizes[i], layerSizes[i + 1], dest);
        }
    }
	public override void Select(Population population, SelectionBuffer selection, GeneticAlgorithm.NextStepDelegate callback) {
		int size = 0;
		DebugAux.Assert(population.MaxFitness != 0, "[RouletteWheelSA] Can't have a MaxFitness of zero!");

		// The algorithm states that we should pick one at random for consideration with probability 1/N.
		int nrConsiderations = Mathf.RoundToInt(population.Size * Settings.SelectionProportion);
		for (int i = 0; i < nrConsiderations; i++) {
			int index = Random.Range(0, population.Size);
			float probability = population[index].Fitness / population.MaxFitness;
			if (Random.value <= probability) {
				selection[size].CloneFrom(population[index].Genome);
				size++;
			}
		}

		selection.Size = size;
		callback();
	}