public override void Build() { if (save) { writerEnv = UIO.CreateStreamWriter(GeneratePath(task, true), "GeneSessionResults.csv", false); UIO.WriteLine(writerEnv, "Generation;Angle Random Rotation;Wind"); } Debug.Log("Build DroneSession"); signal = new ControlSignal(); tsignal = new ThrustSignal(); tmpBuildCustomWeights = new List <Matrix <float> >(); externalEvaluations = Vector <float> .Build.Dense(populationSize); //Debug.Log(fromTask); taskObject = (DroneTask)Activator.CreateInstance(Type.GetType("Lexmou.MachineLearning.Drone" + task), rndGenerator, fromTask); //Debug.Log(taskObject.fromTask); dronePopulation = new GameObject[populationSize]; droneRigid = new Rigidbody[populationSize]; targetPosition = new Vector3[populationSize]; mlpPopulation = new MultiLayerMathsNet[populationSize]; gene = new Genetic(seed, rndGenerator, populationSize, taskObject.individualSize, initialValueWeights, mutationRate, randomIndividualsRate, bestIndividualsRate, emptyRate, GeneratePath(task, false), save); if (loadGeneration != 0) { float[,] floatArr = new float[taskObject.individualSize - taskObject.rowIndex, populationSize]; Debug.Log(taskObject.fromTask); //Debug.Log(taskObject.individualSize); gene.LoadGeneration(GeneratePath(taskObject.fromTask, true), loadGeneration, floatArr, taskObject.rowIndex); gene.generation = loadGeneration; } }
/** * */ public Genetic(int seed, SystemRandomSource rndGenerator, int populationSize, int individualSize, float initialValueWeights, float mutationRate = 0.1f, float randomIndividualsRate = 0.05f, float bestIndividualsRate = 0.05f, float emptyRate = 0.0f, string path = "", bool save = true) { this.save = save; tmp = Matrix <float> .Build.Dense(individualSize, populationSize); this.path = path; this.bestScore = 0.0f; this.generation = 1; this.seed = seed; this.initialValueWeights = initialValueWeights; if (rndGenerator != null) { this.rndGenerator = rndGenerator; } else { this.rndGenerator = new SystemRandomSource(seed); } distribution = new ContinuousUniform(-initialValueWeights, initialValueWeights, rndGenerator); subDistribution = new ContinuousUniform(-0.01, 0.01, rndGenerator); drawDistribution = new ContinuousUniform(0.0f, 1.0f, rndGenerator); this.populationSize = populationSize; this.individualSize = individualSize; this.population = Matrix <float> .Build.Random(individualSize, populationSize, distribution); for (int i = 0; i < populationSize; i++) { distEmpty = Combinatorics.GenerateCombination(individualSize, Mathf.RoundToInt(emptyRate * individualSize), rndGenerator); for (int j = 0; j < individualSize; j++) { if (distEmpty[j]) { population[j, i] = 0.000f; } } } this.evaluations = Vector <float> .Build.Dense(populationSize); this.sumEvaluations = 0.0f; this.mutationRate = mutationRate; this.randomIndividualsRate = randomIndividualsRate; this.bestIndividualsRate = bestIndividualsRate; this.emptyRate = emptyRate; if (save) { writer = UIO.CreateStreamWriter(GeneratePath(), "GeneticResults.csv", false); UIO.WriteLine(writer, "Generation;Best;Mean;Std Deviation;Median"); } indexPermutation = Combinatorics.GeneratePermutation(populationSize); }
public override void RunEachIntervalUpdate() { generationInfos = gene.Run(externalEvaluations, theoricBestScore, intervalSave); hudManager.UpdateTextLayout("Generation", generationInfos); theoricBestScore = 0; if (save) { UIO.WriteLine(writerEnv, gene.generation + ";" + taskObject.AngleRandomRotation + ";" + taskObject.WindStrength); } }
public override void BuildSessionWriter() { if (save) { writerSession = UIO.CreateStreamWriter(GeneratePath(task, true), "DroneSessionParams.csv", false); UIO.WriteLine(writerSession, "Task : " + task + ";" + "Seed : " + seed + ";" + "Population Size : " + populationSize + ";" + "Individual Size : " + taskObject.individualSize + ";" + "Mutation Rate : " + mutationRate + ";" + "Random Rate : " + randomIndividualsRate + ";" + "Best Individual Rate : " + bestIndividualsRate + ";" + "Empty Coeff Individual Rate : " + emptyRate + " (" + Mathf.RoundToInt(emptyRate * taskObject.individualSize) + ");" + "MLP layers : [" + string.Join("-", taskObject.shapes.Select(x => x.ToString()).ToArray()) + "] ; Intitial Value Weights : [" + -initialValueWeights + "," + initialValueWeights + "]"); } }
public string Run(Vector <float> externalEvaluations, float theoricBestScore, int intervalSave = 0) { string generationInfos; Evaluation(externalEvaluations, "max"); if (save) { UIO.WriteLine(writer, generation + ";" + bestScore + ";" + mean + ";" + stdDeviation + ";" + median); } if (intervalSave > 0 && generation % intervalSave == 0) { Debug.Log("Save in Progress"); SaveGeneration(); } generationInfos = generation + " | Best Score : " + bestScore + "/" + theoricBestScore + " | Index : " + bestIndividualIndex + "\r\n" + "Score Mean : " + (sumEvaluations / populationSize); Selection(); CrossOver(); Mutation(); generation += 1; return(generationInfos); }