Skip to content

Commit

Permalink
Merge patch update system into foraging (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 authored May 16, 2024
1 parent 33f1f5c commit ed01e97
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 80 deletions.
4 changes: 1 addition & 3 deletions model/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ func Default(p params.Params, m *model.Model) *model.Model {
m.AddSystem(&sys.MortalityCohorts{})
m.AddSystem(&sys.MortalityForagers{})

m.AddSystem(&sys.Foraging{
PatchUpdater: &sys.UpdatePatchesForaging{},
})
m.AddSystem(&sys.Foraging{})
m.AddSystem(&sys.HoneyConsumption{})
m.AddSystem(&sys.PollenConsumption{})

Expand Down
50 changes: 40 additions & 10 deletions sys/foraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package sys
import (
"math"

"github.com/mlange-42/arche-model/model"
"github.com/mlange-42/arche-model/resource"
"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/arche/generic"
"github.com/mlange-42/beecs/comp"
"github.com/mlange-42/beecs/enum/activity"
"github.com/mlange-42/beecs/globals"
"github.com/mlange-42/beecs/params"
"github.com/mlange-42/beecs/util"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

type Foraging struct {
PatchUpdater model.System
rng *rand.Rand
rng *rand.Rand

foragerParams *params.Foragers
forageParams *params.Foraging
Expand Down Expand Up @@ -48,6 +47,7 @@ type Foraging struct {
foragerFilterLoad generic.Filter4[comp.Activity, comp.KnownPatch, comp.Milage, comp.NectarLoad]
foragerFilterSimple generic.Filter2[comp.Activity, comp.KnownPatch]
patchFilter generic.Filter2[comp.Resource, comp.PatchProperties]
patchUpdateFilter generic.Filter7[comp.PatchProperties, comp.PatchDistance, comp.Resource, comp.HandlingTime, comp.Trip, comp.Mortality, comp.Dance]

maxHoneyStore float64
}
Expand All @@ -70,6 +70,7 @@ func (s *Foraging) Initialize(w *ecs.World) {
s.foragerFilterLoad = *generic.NewFilter4[comp.Activity, comp.KnownPatch, comp.Milage, comp.NectarLoad]()
s.foragerFilterSimple = *generic.NewFilter2[comp.Activity, comp.KnownPatch]()
s.patchFilter = *generic.NewFilter2[comp.Resource, comp.PatchProperties]()
s.patchUpdateFilter = *generic.NewFilter7[comp.PatchProperties, comp.PatchDistance, comp.Resource, comp.HandlingTime, comp.Trip, comp.Mortality, comp.Dance]()

s.patchResourceMapper = generic.NewMap1[comp.Resource](w)
s.patchDanceMapper = generic.NewMap2[comp.Resource, comp.Dance](w)
Expand All @@ -83,8 +84,6 @@ func (s *Foraging) Initialize(w *ecs.World) {

s.maxHoneyStore = storeParams.MaxHoneyStoreKg * 1000.0 * energyParams.Honey
s.rng = rand.New(ecs.GetResource[resource.Rand](w))

s.PatchUpdater.Initialize(w)
}

func (s *Foraging) Update(w *ecs.World) {
Expand Down Expand Up @@ -128,9 +127,7 @@ func (s *Foraging) Update(w *ecs.World) {
}
}

func (s *Foraging) Finalize(w *ecs.World) {
s.PatchUpdater.Finalize(w)
}
func (s *Foraging) Finalize(w *ecs.World) {}

func (s *Foraging) calcForagingProb() float64 {
if s.stores.Pollen/s.stores.IdealPollen > 0.5 && s.stores.Honey/s.stores.DecentHoney > 1 {
Expand All @@ -153,8 +150,7 @@ func (s *Foraging) foragingRound(w *ecs.World, forageProb float64) (duration flo
probCollectPollen *= s.stores.Honey / s.stores.DecentHoney
}

s.PatchUpdater.Update(w)

s.updatePatches(w)
s.decisions(w, forageProb, probCollectPollen)
s.searching(w)
s.collecting(w)
Expand All @@ -165,6 +161,40 @@ func (s *Foraging) foragingRound(w *ecs.World, forageProb float64) (duration flo
return
}

func (s *Foraging) updatePatches(w *ecs.World) {
query := s.patchUpdateFilter.Query(w)
for query.Next() {
conf, dist, r, ht, trip, mort, dance := query.Get()

if s.handlingTimeParams.ConstantHandlingTime {
ht.Pollen = s.handlingTimeParams.PollenGathering
ht.Nectar = s.handlingTimeParams.NectarGathering
} else {
ht.Pollen = s.handlingTimeParams.PollenGathering * r.MaxPollen / r.Pollen
ht.Nectar = s.handlingTimeParams.NectarGathering * r.MaxNectar / r.Nectar
}

trip.CostNectar = (2 * dist.DistToColony * s.foragerParams.FlightCostPerM) +
(s.foragerParams.FlightCostPerM * ht.Nectar *
s.foragerParams.FlightVelocity * s.forageParams.EnergyOnFlower) // [kJ] = [m*kJ/m + kJ/m * s * m/s]

trip.CostPollen = (2 * dist.DistToColony * s.foragerParams.FlightCostPerM) +
(s.foragerParams.FlightCostPerM * ht.Pollen *
s.foragerParams.FlightVelocity * s.forageParams.EnergyOnFlower) // [kJ] = [m*kJ/m + kJ/m * s * m/s]

r.EnergyEfficiency = (conf.NectarConcentration*s.foragerParams.NectarLoad*s.energyParams.Scurose - trip.CostNectar) / trip.CostNectar

trip.DurationNectar = 2*dist.DistToColony/s.foragerParams.FlightVelocity + ht.Nectar
trip.DurationPollen = 2*dist.DistToColony/s.foragerParams.FlightVelocity + ht.Pollen

mort.Nectar = 1.0 - (math.Pow(1.0-s.forageParams.MortalityPerSec, trip.DurationNectar))
mort.Pollen = 1.0 - (math.Pow(1.0-s.forageParams.MortalityPerSec, trip.DurationPollen))

circ := r.EnergyEfficiency*s.danceParams.Slope + s.danceParams.Intercept
dance.Circuits = util.Clamp(circ, 0, float64(s.danceParams.MaxCircuits))
}
}

func (s *Foraging) decisions(w *ecs.World, probForage, probCollectPollen float64) {
query := s.foragerFilter.Query(w)
for query.Next() {
Expand Down
67 changes: 0 additions & 67 deletions sys/update_patches.go

This file was deleted.

0 comments on commit ed01e97

Please sign in to comment.