-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from yaricom/experiment-utils
Extracted common genome write functions used by examples into utils.
- Loading branch information
Showing
5 changed files
with
106 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Package utils provides common utilities to be used by experiments. | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yaricom/goNEAT/v2/experiment" | ||
"github.com/yaricom/goNEAT/v2/neat/genetics" | ||
"github.com/yaricom/goNEAT/v2/neat/network/formats" | ||
"log" | ||
"os" | ||
) | ||
|
||
// 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), | ||
genomeFile, org.Phenotype.NodeCount(), org.Phenotype.LinkCount()) | ||
if file, err := os.Create(orgPath); err != nil { | ||
return "", err | ||
} else if err = org.Genotype.Write(file); err != nil { | ||
return "", err | ||
} | ||
return orgPath, nil | ||
} | ||
|
||
// 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), | ||
genomeFile, org.Phenotype.NodeCount(), org.Phenotype.LinkCount()) | ||
if file, err := os.Create(orgPath); err != nil { | ||
return "", err | ||
} else if err = formats.WriteDOT(file, org.Phenotype); err != nil { | ||
return "", err | ||
} | ||
return orgPath, nil | ||
} | ||
|
||
// 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), | ||
genomeFile, org.Phenotype.NodeCount(), org.Phenotype.LinkCount()) | ||
if file, err := os.Create(orgPath); err != nil { | ||
return "", err | ||
} else if err = formats.WriteCytoscapeJSON(file, org.Phenotype); err != nil { | ||
return "", err | ||
} | ||
return orgPath, nil | ||
} | ||
|
||
// 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) | ||
if file, err := os.Create(popPath); err != nil { | ||
return "", err | ||
} else if err = pop.WriteBySpecies(file); err != nil { | ||
return "", err | ||
} | ||
return popPath, nil | ||
} | ||
|
||
// createOutDirForTrial allows creating the output directory for specific trial 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 | ||
if err = os.MkdirAll(dir, os.ModePerm); err != nil { | ||
log.Fatal("Failed to create output directory: ", err) | ||
} | ||
} | ||
return dir | ||
} |