Skip to content

Commit

Permalink
Merge pull request #43 from yaricom/trial-dir-creation
Browse files Browse the repository at this point in the history
Extracted common method to create output directory for experiment trial.
  • Loading branch information
yaricom committed Jan 9, 2022
2 parents f0e8996 + bb2badf commit 86ceebf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions examples/pole/cartpole.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
----------------------------------------------------------------------*/
Expand Down
12 changes: 6 additions & 6 deletions experiment/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 86ceebf

Please sign in to comment.