Skip to content

Commit

Permalink
Update changelog for v0.2.0 release, structure package params (#72)
Browse files Browse the repository at this point in the history
* update changelog for v0.2.0 release
* structure and document package params
  • Loading branch information
mlange-42 authored May 26, 2024
1 parent 7415d20 commit 753d5da
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 99 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [[unpublished]](https://github.com/mlange-42/beecs/compare/v0.1.0...main)
## [[v0.2.0]](https://github.com/mlange-42/beecs/compare/v0.1.0...v0.2.0)

### Features

Expand Down
9 changes: 9 additions & 0 deletions params/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (e entry) MarshalJSON() ([]byte, error) {
return e.Bytes, nil
}

// CustomParams contains all default parameters of BEEHAVE and a map of additional custom parameter resources.
//
// CustomParams implements [Params].
type CustomParams struct {
Parameters DefaultParams
Custom map[reflect.Type]any
Expand All @@ -35,6 +38,10 @@ type customParamsJs struct {
Custom map[string]entry
}

// FromJSON fills the parameter set with values from a JSON file.
//
// Only values present in the file are overwritten,
// all other values remain unchanged.
func (p *CustomParams) FromJSON(path string) error {
file, err := os.Open(path)
if err != nil {
Expand Down Expand Up @@ -76,6 +83,7 @@ func (p *CustomParams) FromJSON(path string) error {
return nil
}

// ToJSON marshals all parameters to JSON format.
func (p *CustomParams) ToJSON() ([]byte, error) {
par := customParamsJs{
Parameters: p.Parameters,
Expand All @@ -97,6 +105,7 @@ func (p *CustomParams) ToJSON() ([]byte, error) {
return js, nil
}

// Apply the parameters to a world by adding them as resources.
func (p *CustomParams) Apply(world *ecs.World) {
p.Parameters.Apply(world)

Expand Down
97 changes: 96 additions & 1 deletion params/default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
package params

import "github.com/mlange-42/beecs/comp"
import (
"encoding/json"
"math/rand"
"os"

"github.com/mlange-42/arche-model/resource"
"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/beecs/comp"
)

// Params is an interface for parameter sets.
type Params interface {
// Apply the parameters to a world.
Apply(world *ecs.World)
// FromJSON fills the parameter set with values from a JSON file.
FromJSON(path string) error
}

// DefaultParams contains all default parameters of BEEHAVE.
//
// DefaultParams implements [Params].
type DefaultParams struct {
WorkingDirectory WorkingDirectory
Termination Termination
InitialPatches InitialPatches
Nursing Nursing
Foraging Foraging
Foragers Foragers
ForagingPeriod ForagingPeriod
Dance Dance
HandlingTime HandlingTime
WorkerMortality WorkerMortality
DroneMortality DroneMortality
HoneyNeeds HoneyNeeds
PollenNeeds PollenNeeds
Stores Stores
WorkerDevelopment WorkerDevelopment
DroneDevelopment DroneDevelopment
InitialPopulation InitialPopulation
AgeFirstForaging AgeFirstForaging
Energy EnergyContent
InitialStores InitialStores
RandomSeed RandomSeed
}

// Default returns the complete default parameter set of BEEHAVE.
func Default() DefaultParams {
Expand Down Expand Up @@ -153,3 +196,55 @@ func Default() DefaultParams {
},
}
}

// FromJSON fills the parameter set with values from a JSON file.
//
// Only values present in the file are overwritten,
// all other values remain unchanged.
func (p *DefaultParams) FromJSON(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()

decoder := json.NewDecoder(file)
decoder.DisallowUnknownFields()
return decoder.Decode(p)
}

// Apply the parameters to a world by adding them as resources.
func (p *DefaultParams) Apply(world *ecs.World) {
// Random seed
seed := p.RandomSeed
if seed.Seed <= 0 {
seed.Seed = int(rand.Int31())
}
rng := ecs.GetResource[resource.Rand](world)
rng.Seed(uint64(seed.Seed))

pCopy := *p

// Resources
ecs.AddResource(world, &seed)
ecs.AddResource(world, &pCopy.WorkingDirectory)
ecs.AddResource(world, &pCopy.Termination)
ecs.AddResource(world, &pCopy.WorkerDevelopment)
ecs.AddResource(world, &pCopy.DroneDevelopment)
ecs.AddResource(world, &pCopy.WorkerMortality)
ecs.AddResource(world, &pCopy.DroneMortality)
ecs.AddResource(world, &pCopy.AgeFirstForaging)
ecs.AddResource(world, &pCopy.Foragers)
ecs.AddResource(world, &pCopy.Foraging)
ecs.AddResource(world, &pCopy.ForagingPeriod)
ecs.AddResource(world, &pCopy.HandlingTime)
ecs.AddResource(world, &pCopy.Dance)
ecs.AddResource(world, &pCopy.Energy)
ecs.AddResource(world, &pCopy.Stores)
ecs.AddResource(world, &pCopy.HoneyNeeds)
ecs.AddResource(world, &pCopy.PollenNeeds)
ecs.AddResource(world, &pCopy.Nursing)
ecs.AddResource(world, &pCopy.InitialPopulation)
ecs.AddResource(world, &pCopy.InitialStores)
ecs.AddResource(world, &pCopy.InitialPatches)
}
97 changes: 0 additions & 97 deletions params/params.go

This file was deleted.

0 comments on commit 753d5da

Please sign in to comment.