Skip to content

Commit

Permalink
Account for number of constants in Gene
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlewis committed Aug 31, 2018
1 parent 9d939cb commit 6d30c92
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
70 changes: 70 additions & 0 deletions experiments/openai-gym/algorithms/Copy-v0/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// -*- compile-command: "go run main.go"; -*-

// Copy-v0 runs a GEP algorithm on the OpenAI Gym "Copy-v0" Algorithm problem.
// https://gym.openai.com/envs/Copy-v0/
package main

import (
"log"

gym "github.com/openai/gym-http-api/binding-go"
)

const (
baseURL = "http://localhost:5000"
env = "Copy-v0"
)

func main() {
c, err := gym.NewClient(baseURL)
if err != nil {
log.Fatalf("gym.NewClient(%q): %v", baseURL, err)
}

id, err := c.Create(env)
if err != nil {
log.Fatalf("Unable to create environment %q: %v", env, err)
}
defer c.Close(id)
log.Printf("id=%q", id)

actionSpace, err := c.ActionSpace(id)
if err != nil {
log.Fatalf("ActionSpace(%q): %v", id, err)
}
log.Printf("Action space: %+v", actionSpace)
observationSpace, err := c.ObservationSpace(id)
if err != nil {
log.Fatalf("ObservationSpace(%q): %v", id, err)
}
log.Printf("Observation space: %+v", observationSpace)

if err := c.StartMonitor(id, "/tmp/"+env, true, false, false); err != nil {
log.Fatalf(`StartMonitor(%q, "/tmp/%v"): %v`, id, env)
}

obs, err := c.Reset(id)
if err != nil {
log.Fatalf("Reset(%q): %v", id, err)
}
log.Printf("1st observation: %v", obs)
var done bool
for !done {
// TODO: Replace SampleAction with GEP algorithm.
act, err := c.SampleAction(id)
if err != nil {
log.Fatalf("SampleAction(%q): %v", err)
}

var reward float64
obs, reward, done, _, err = c.Step(id, act, false)
if err != nil {
log.Fatalf("Step(%q, %v, false): %v", id, act, err)
}
log.Printf("reward=%v, observatioan=%v", reward, obs)
}

if err := c.CloseMonitor(id); err != nil {
log.Fatalf("CloseMonitor(%q): %v", id, err)
}
}
2 changes: 1 addition & 1 deletion gene/gene.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func New(x string) *Gene {
// properties of the gene, and functions provide the available functions and
// their respective weights to be used in the creation of the gene.
func RandomNew(headSize, tailSize, numTerminals, numConstants int, functions []FuncWeight) *Gene {
totalWeight := numTerminals
totalWeight := numTerminals + numConstants
for _, f := range functions {
totalWeight += f.Weight
}
Expand Down

0 comments on commit 6d30c92

Please sign in to comment.