/** * \brief Load the generation * \details load the complete population and the best individual in two files. The path depends on GeneratePath() */ public void LoadGeneration(string path, int generationIndex, float[,] floatArr, int rowIndex) { UIO.LoadFloatArray(path + generationIndex, "/population.gene", floatArr); Matrix <float> partialPopulation = Matrix <float> .Build.DenseOfArray(floatArr); population.SetSubMatrix(rowIndex, 0, partialPopulation); }
public void LoadGeneration(int generationIndex, float[,] floatArr) { UIO.LoadFloatArray(GeneratePath() + generationIndex, "/population.gene", floatArr); population = Matrix <float> .Build.DenseOfArray(floatArr); //population.SetSubMatrix(rowIndex, 0, Matrix<float>.Build.DenseOfArray(floatArr)); }
public override void CloseSessionWriter() { if (save) { UIO.CloseStreamWriter(writerSession); } }
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 override void OnDestroy() { if (save) { Debug.Log("Destroy Genetic Writer"); UIO.CloseStreamWriter(gene.writer); UIO.CloseStreamWriter(writerEnv); } }
/** * */ 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 SetParametersFromCommandLine() { UIO.CheckBeforeReplaceCommandLineArguments(this, "save"); UIO.CheckBeforeReplaceCommandLineArguments(this, "task"); UIO.CheckBeforeReplaceCommandLineArguments(this, "fromTask"); UIO.CheckBeforeReplaceCommandLineArguments(this, "seed"); UIO.CheckBeforeReplaceCommandLineArguments(this, "intervalSave"); UIO.CheckBeforeReplaceCommandLineArguments(this, "loadGeneration"); UIO.CheckBeforeReplaceCommandLineArguments(this, "timeScale"); }
private void btnOK_Click(object sender, EventArgs e) { var strategyFilter = new StrategyFilter(); strategyFilter.Days = Day; strategyFilter.Sensors = Sensors; var str = UXmlConvert.GetString(strategyFilter); var filename = UWorkspace.GetStrategyXmlName(_wksdef); UIO.SaveFile(str, filename); DialogResult = DialogResult.OK; Close(); }
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); }
public static void LoadBest(string path, int generationIndex, float[] floatArr) { UIO.LoadFloatArray(path + generationIndex, "/best.gene", floatArr); }
/** * \brief Load the best individual * \details load the complete population and the best individual in two files. The path depends on GeneratePath() */ public void LoadBest(int generationIndex, float[] floatArr) { UIO.LoadFloatArray(GeneratePath() + generationIndex, "/best.gene", floatArr); }
/** * \brief Save the generation * \details Save the complete population and the best individual in two files. The path depends on GeneratePath() */ public void SaveGeneration() { UIO.SaveFloatArray(GeneratePath() + generation, "population.gene", population.ToArray()); UIO.SaveFloatArray(GeneratePath() + generation, "best.gene", GetBestIndividual().ToArray()); }