Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move experiment to package, apply values to a world #35

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions _examples/experiment/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
amodel "github.com/mlange-42/arche-model/model"
"github.com/mlange-42/arche-model/reporter"
"github.com/mlange-42/arche-model/system"
"github.com/mlange-42/beecs/experiment"
"github.com/mlange-42/beecs/model"
"github.com/mlange-42/beecs/obs"
"github.com/mlange-42/beecs/params"
Expand All @@ -20,7 +21,7 @@ import (

func main() {
// Define parameter variations.
vars := []params.ParameterVariation{
vars := []experiment.ParameterVariation{
{
Parameter: "params.InitialPopulation.Count",
Type: "sequence-values",
Expand All @@ -34,12 +35,12 @@ func main() {
}

// Create an experiment.
experiment, err := params.NewExperiment(vars, rand.New(nil))
exp, err := experiment.New(vars, rand.New(nil))
if err != nil {
log.Fatal(err)
}
// Get the number of parameter sets in the experiment.
sets := experiment.ParameterSets()
sets := exp.ParameterSets()

// Create an empty model.
m := amodel.New()
Expand All @@ -48,26 +49,22 @@ func main() {

for i := 0; i < sets; i++ {
// Get and print the parameter values for the current run.
values := experiment.Values(i)
values := exp.Values(i)
fmt.Printf("Running set %v\n", values)

// Initialize and run the model with the given parameters.
run(m, values, i)
run(m, &exp, i)
}
}

func run(m *amodel.Model, values map[string]any, idx int) {
func run(m *amodel.Model, exp *experiment.Experiment, idx int) {
// Get the default parameters.
p := params.Default()
// Create a model with the default sub-models.
m = model.Default(&p, m)

// Set/overwrite parameters from the experiment.
for par, value := range values {
if err := model.SetParameter(&m.World, par, value); err != nil {
log.Fatal(err)
}
}
exp.ApplyValues(idx, &m.World)

// Add a system ("sub-model") to terminate after 365 ticks.
m.AddSystem(&system.FixedTermination{Steps: 365})
Expand Down
33 changes: 25 additions & 8 deletions params/experiment.go → experiment/experiment.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package params
package experiment

import "golang.org/x/exp/rand"
import (
"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/beecs/model"
"golang.org/x/exp/rand"
)

type Experiment struct {
values map[string]any
rng *rand.Rand
parameters []string
functions []ParameterFunction
parameterSets int
}

func NewExperiment(vars []ParameterVariation, rng *rand.Rand) (Experiment, error) {
func New(vars []ParameterVariation, rng *rand.Rand) (Experiment, error) {
pars := []string{}
f := []ParameterFunction{}

Expand All @@ -30,7 +33,6 @@ func NewExperiment(vars []ParameterVariation, rng *rand.Rand) (Experiment, error
functions: f,
parameterSets: stride,
rng: rng,
values: map[string]any{},
}, nil
}

Expand All @@ -42,10 +44,25 @@ func (e *Experiment) Parameters() []string {
return e.parameters
}

func (e Experiment) Values(idx int) map[string]any {
func (e *Experiment) Seed(seed uint64) {
e.rng.Seed(seed)
}

func (e *Experiment) Values(idx int) map[string]any {
values := map[string]any{}
for i, par := range e.parameters {
fn := e.functions[i]
e.values[par] = fn.Next(idx, e.rng)
values[par] = fn.Next(idx, e.rng)
}
return values
}

func (e *Experiment) ApplyValues(idx int, world *ecs.World) error {
values := e.Values(idx)
for par, value := range values {
if err := model.SetParameter(world, par, value); err != nil {
return err
}
}
return e.values
return nil
}
4 changes: 2 additions & 2 deletions params/experiment_test.go → experiment/experiment_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package params
package experiment

import (
"testing"
Expand All @@ -23,7 +23,7 @@ func TestExperiment(t *testing.T) {
},
}

e, err := NewExperiment(vars, rng)
e, err := New(vars, rng)

assert.Nil(t, err)

Expand Down
2 changes: 1 addition & 1 deletion params/variation.go → experiment/variation.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package params
package experiment

import (
"fmt"
Expand Down
Loading