From bb2badf40b7c90019d3e54f8c6d4dd2c1a57c3b6 Mon Sep 17 00:00:00 2001 From: Iaroslav Omelianenko Date: Sun, 9 Jan 2022 20:39:02 +0200 Subject: [PATCH] Extracted common method to create output directory for experiment trial. --- examples/pole/cartpole.go | 10 +++++----- experiment/utils/utils.go | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/pole/cartpole.go b/examples/pole/cartpole.go index adc23fe..a6d9869 100644 --- a/examples/pole/cartpole.go +++ b/examples/pole/cartpole.go @@ -32,7 +32,7 @@ func NewCartPoleGenerationEvaluator(outDir string, randomStart bool, winBalanceS } } -// GenerationEvaluate This method evaluates one epoch for given population and prints results into output directory if any. +// GenerationEvaluate evaluates one epoch for given population and prints results into output directory if any. func (e *cartPoleGenerationEvaluator) GenerationEvaluate(pop *genetics.Population, epoch *experiment.Generation, context *neat.Options) (err error) { // Evaluate each organism on a test for _, org := range pop.Organisms { @@ -96,7 +96,7 @@ func (e *cartPoleGenerationEvaluator) GenerationEvaluate(pop *genetics.Populatio return err } -// This methods evaluates provided organism for cart pole balancing task +// orgEvaluate evaluates provided organism for cart pole balancing task func (e *cartPoleGenerationEvaluator) orgEvaluate(organism *genetics.Organism) (bool, error) { // Try to balance a pole now if fitness, err := e.runCart(organism.Phenotype); err != nil { @@ -131,7 +131,7 @@ func (e *cartPoleGenerationEvaluator) orgEvaluate(organism *genetics.Organism) ( return organism.IsWinner, nil } -// run cart emulation and return number of emulation steps pole was balanced +// runCart runs the cart emulation and return number of emulation steps pole was balanced func (e *cartPoleGenerationEvaluator) runCart(net *network.Network) (steps int, err error) { var x float64 /* cart position, meters */ var xDot float64 /* cart velocity */ @@ -188,10 +188,10 @@ func (e *cartPoleGenerationEvaluator) runCart(net *network.Network) (steps int, return steps, nil } -// cart_and_pole() was take directly from the pole simulator written by Richard Sutton and Charles Anderson. +// doAction was taken directly from the pole simulator written by Richard Sutton and Charles Anderson. // This simulator uses normalized, continuous inputs instead of discretizing the input space. /*---------------------------------------------------------------------- -cart_pole: Takes an action (0 or 1) and the current values of the +Takes an action (0 or 1) and the current values of the four state variables and updates their values by estimating the state TAU seconds later. ----------------------------------------------------------------------*/ diff --git a/experiment/utils/utils.go b/experiment/utils/utils.go index dfb032f..6bda3c7 100644 --- a/experiment/utils/utils.go +++ b/experiment/utils/utils.go @@ -13,7 +13,7 @@ import ( // WriteGenomePlain is to write genome of the organism to the genomeFile in the outDir directory using plain encoding. // The method return path to the file if successful or error if failed. func WriteGenomePlain(genomeFile, outDir string, org *genetics.Organism, epoch *experiment.Generation) (string, error) { - orgPath := fmt.Sprintf("%s/%s_%d-%d", createOutDirForTrial(outDir, epoch.TrialId), + orgPath := fmt.Sprintf("%s/%s_%d-%d", CreateOutDirForTrial(outDir, epoch.TrialId), genomeFile, org.Phenotype.NodeCount(), org.Phenotype.LinkCount()) if file, err := os.Create(orgPath); err != nil { return "", err @@ -26,7 +26,7 @@ func WriteGenomePlain(genomeFile, outDir string, org *genetics.Organism, epoch * // WriteGenomeDOT is to write genome of the organism to the genomeFile in the outDir directory using DOT encoding. // The method return path to the file if successful or error if failed. func WriteGenomeDOT(genomeFile, outDir string, org *genetics.Organism, epoch *experiment.Generation) (string, error) { - orgPath := fmt.Sprintf("%s/%s_%d-%d.dot", createOutDirForTrial(outDir, epoch.TrialId), + orgPath := fmt.Sprintf("%s/%s_%d-%d.dot", CreateOutDirForTrial(outDir, epoch.TrialId), genomeFile, org.Phenotype.NodeCount(), org.Phenotype.LinkCount()) if file, err := os.Create(orgPath); err != nil { return "", err @@ -39,7 +39,7 @@ func WriteGenomeDOT(genomeFile, outDir string, org *genetics.Organism, epoch *ex // WriteGenomeCytoscapeJSON is to write genome of the organism to the genomeFile in the outDir directory using Cytoscape JSON encoding. // The method return path to the file if successful or error if failed. func WriteGenomeCytoscapeJSON(genomeFile, outDir string, org *genetics.Organism, epoch *experiment.Generation) (string, error) { - orgPath := fmt.Sprintf("%s/%s_%d-%d.cyjs", createOutDirForTrial(outDir, epoch.TrialId), + orgPath := fmt.Sprintf("%s/%s_%d-%d.cyjs", CreateOutDirForTrial(outDir, epoch.TrialId), genomeFile, org.Phenotype.NodeCount(), org.Phenotype.LinkCount()) if file, err := os.Create(orgPath); err != nil { return "", err @@ -52,7 +52,7 @@ func WriteGenomeCytoscapeJSON(genomeFile, outDir string, org *genetics.Organism, // WritePopulationPlain is to write genomes of the entire population using plain encoding in the outDir directory. // The methods return path to the file if successful or error if failed. func WritePopulationPlain(outDir string, pop *genetics.Population, epoch *experiment.Generation) (string, error) { - popPath := fmt.Sprintf("%s/gen_%d", createOutDirForTrial(outDir, epoch.TrialId), epoch.Id) + popPath := fmt.Sprintf("%s/gen_%d", CreateOutDirForTrial(outDir, epoch.TrialId), epoch.Id) if file, err := os.Create(popPath); err != nil { return "", err } else if err = pop.WriteBySpecies(file); err != nil { @@ -61,8 +61,8 @@ func WritePopulationPlain(outDir string, pop *genetics.Population, epoch *experi return popPath, nil } -// createOutDirForTrial allows creating the output directory for specific trial using standard name. -func createOutDirForTrial(outDir string, trialID int) string { +// CreateOutDirForTrial allows creating the output directory for specific trial of the experiment using standard name. +func CreateOutDirForTrial(outDir string, trialID int) string { dir := fmt.Sprintf("%s/%d", outDir, trialID) if _, err := os.Stat(dir); err != nil { // create output dir