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

Add option to terminate on extinction of all bees #75

Merged
merged 2 commits into from
May 27, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [[unpublished]](https://github.com/mlange-42/beecs/compare/v0.2.0...main)

### Features

- Add option to terminate on extinction of all bees (#75)

### Documentation

- Document package registry (#73)
Expand Down
3 changes: 2 additions & 1 deletion params/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func Default() DefaultParams {
Path: ".",
},
Termination: Termination{
MaxTicks: 365,
MaxTicks: 365,
OnExtinction: false,
},
RandomSeed: RandomSeed{
Seed: 0,
Expand Down
5 changes: 3 additions & 2 deletions params/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type RandomSeed struct {

// Termination criteria.
type Termination struct {
MaxTicks int // Maximum number of ticks to run [d].
MaxTicks int // Maximum number of ticks to run [d].
OnExtinction bool // Whether to terminate when there are no bees anymore.
}

// AgeFirstForaging (AFF) parameters.
Expand Down Expand Up @@ -136,7 +137,7 @@ type PollenNeeds struct {
type Nursing struct {
MaxBroodNurseRatio float64 // Maximum brood per nurse.
ForagerNursingContribution float64 // Contribution fraction of foragers to nursing.
MaxEggsPerDay int // Maximum eggs layed by a queen per day.
MaxEggsPerDay int // Maximum eggs laid by a queen per day.
DroneEggsProportion float64 // Proportion of drone eggs.
EggNursingLimit bool // Whether to limit egg laying by the number of available nurses.
MaxBroodCells int // Maximum number of brood cells in the hive.
Expand Down
7 changes: 6 additions & 1 deletion sys/terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sys
import (
"github.com/mlange-42/arche-model/resource"
"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/beecs/globals"
"github.com/mlange-42/beecs/params"
)

Expand All @@ -11,17 +12,21 @@ import (
type FixedTermination struct {
termRes *resource.Termination
termParams *params.Termination
popStats *globals.PopulationStats
step int64
}

func (s *FixedTermination) Initialize(w *ecs.World) {
s.termRes = ecs.GetResource[resource.Termination](w)
s.termParams = ecs.GetResource[params.Termination](w)
s.popStats = ecs.GetResource[globals.PopulationStats](w)
s.step = 0
}

func (s *FixedTermination) Update(w *ecs.World) {
if s.termParams.MaxTicks > 0 && s.step+1 >= int64(s.termParams.MaxTicks) {
if s.termParams.OnExtinction && s.popStats.TotalPopulation == 0 {
s.termRes.Terminate = true
} else if s.termParams.MaxTicks > 0 && s.step+1 >= int64(s.termParams.MaxTicks) {
s.termRes.Terminate = true
}
s.step++
Expand Down
58 changes: 58 additions & 0 deletions sys/terminate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package sys

import (
"testing"

"github.com/mlange-42/arche-model/resource"
"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/beecs/globals"
"github.com/mlange-42/beecs/params"
"github.com/stretchr/testify/assert"
)

func TestFixedTermination(t *testing.T) {
world := ecs.NewWorld()

ecs.AddResource(&world, &params.Termination{MaxTicks: 10})
ecs.AddResource(&world, &globals.PopulationStats{TotalPopulation: 100})

termRes := resource.Termination{}
ecs.AddResource(&world, &termRes)

term := FixedTermination{}

term.Initialize(&world)

for i := 0; i < 9; i++ {
term.Update(&world)
}
assert.False(t, termRes.Terminate)

term.Update(&world)
assert.True(t, termRes.Terminate)
}

func TestExtinctionTermination(t *testing.T) {
world := ecs.NewWorld()

ecs.AddResource(&world, &params.Termination{OnExtinction: true})

pop := globals.PopulationStats{TotalPopulation: 100}
ecs.AddResource(&world, &pop)

termRes := resource.Termination{}
ecs.AddResource(&world, &termRes)

term := FixedTermination{}

term.Initialize(&world)

for i := 0; i < 10; i++ {
term.Update(&world)
}
assert.False(t, termRes.Terminate)

pop.TotalPopulation = 0
term.Update(&world)
assert.True(t, termRes.Terminate)
}
Loading