Skip to content

Commit

Permalink
change structure
Browse files Browse the repository at this point in the history
  • Loading branch information
roznawsk committed Aug 22, 2024
1 parent e201539 commit bc36877
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 108 deletions.
15 changes: 12 additions & 3 deletions infant_abm/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
from .position import Position
from .agent import Agent

from .infant import Infant, NoVisionInfant, SpatialVisionInfant, AbstractVisionInfant
from .parent import Parent, MoverParent, SpatialVisionParent, AbstractVisionParent
from .abstract_vision.abstract_vision_infant import AbstractVisionInfant
from .abstract_vision.abstract_vision_parent import AbstractVisionParent

from .no_vision.no_vision_infant import NoVisionInfant
from .no_vision.no_vision_parent import NoVisionParent

from .spatial_vision.spatial_vision_infant import SpatialVisionInfant
from .spatial_vision.spatial_vision_parent import SpatialVisionParent

from .infant import Infant
from .parent import Parent

__all__ = (
"Agent",
Expand All @@ -14,7 +23,7 @@
"SpatialVisionInfant",
"AbstractVisionInfant",
"Parent",
"MoverParent",
"NoVisionParent",
"SpatialVisionParent",
"AbstractVisionParent",
)
Empty file.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import math
import numpy as np

from infant_abm.agents.infant.infant import Infant, Params
from infant_abm.agents.infant.events import ToySelected, ToyThrown, ThrowEvaluation
from infant_abm.agents.infant import Infant, Params
from infant_abm.agents.events import ToySelected, ToyThrown, ThrowEvaluation
from infant_abm.agents.position import Position
from infant_abm.agents.infant import actions
from infant_abm.agents import infant_actions

from infant_abm.utils import chance

Expand All @@ -25,11 +25,11 @@ class AbstractVisionInfant(Infant):
THROW_EVALUATION_INFANT_CHANCE = 0.7

ALLOWED_ACTIONS = [
actions.LookForToy,
actions.EvaluateToy,
actions.Crawl,
actions.EvaluateThrow,
actions.InteractWithToy,
infant_actions.LookForToy,
infant_actions.EvaluateToy,
infant_actions.Crawl,
infant_actions.EvaluateThrow,
infant_actions.InteractWithToy,
]

def __init__(self, unique_id, model, pos, params: Params):
Expand All @@ -39,7 +39,7 @@ def __init__(self, unique_id, model, pos, params: Params):

self.current_persistence_boost_duration = 0

self.next_action = actions.LookForToy()
self.next_action = infant_actions.LookForToy()

def step(self):
next_action = super()._perform_action(self.next_action)
Expand All @@ -61,25 +61,25 @@ def _step_look_for_toy(self, _action):
self.velocity = Position.calc_norm_vector(self.pos, target.pos)
self.target = target

return actions.EvaluateToy()
return infant_actions.EvaluateToy()

def _step_evaluate_toy(self, action: actions.EvaluateToy):
def _step_evaluate_toy(self, action: infant_actions.EvaluateToy):
if self.parent_visible and self.model.parent.infant_visible:
self.params.persistence.boost(self.PERSISTENCE_BOOST_VALUE)

self._reset_visible()
return actions.Crawl(metadata="persistence_boost")
return infant_actions.Crawl(metadata="persistence_boost")
elif action.duration == self.TOY_EVALUATION_DURATION:
self._reset_visible()
return actions.Crawl(metadata="no_boost")
return infant_actions.Crawl(metadata="no_boost")
else:
if chance(self.TOY_EVALUATION_INFANT_CHANCE, self.TOY_EVALUATION_DURATION):
self.parent_visible = True

if chance(self.TOY_EVALUATION_PARENT_CHANCE, self.TOY_EVALUATION_DURATION):
self.model.parent.handle_event(ToySelected(self.target))

return actions.EvaluateToy(action.duration + 1)
return infant_actions.EvaluateToy(action.duration + 1)

def _step_interact_with_toy(self, _action):
self.params.coordination.reset()
Expand Down Expand Up @@ -108,34 +108,34 @@ def _step_interact_with_toy(self, _action):
self.target = None
self.bonus_target = None

return actions.LookForToy()
return infant_actions.LookForToy()

def _step_crawl(self, _action):
if self._target_in_range():
self._start_evaluating_throw()
return actions.EvaluateThrow()
return infant_actions.EvaluateThrow()

if self._gets_distracted():
self.target = None
return actions.LookForToy()
return infant_actions.LookForToy()

self._move()

self.current_persistence_boost_duration += 1
if self.current_persistence_boost_duration == self.PERSISTENCE_BOOST_DURATION:
self.params.persistence.reset()

return actions.Crawl()
return infant_actions.Crawl()

def _step_evaluate_throw(self, action: actions.EvaluateThrow):
def _step_evaluate_throw(self, action: infant_actions.EvaluateThrow):
if self.parent_visible and self.model.parent.infant_visible:
self.params.coordination.boost(self.COORDINATION_BOOST_VALUE)

self._reset_visible()
return actions.InteractWithToy(metadata="coordination_boost")
return infant_actions.InteractWithToy(metadata="coordination_boost")
elif action.duration == self.TOY_EVALUATION_DURATION:
self._reset_visible()
return actions.InteractWithToy()
return infant_actions.InteractWithToy()
else:
if chance(
self.THROW_EVALUATION_INFANT_CHANCE, self.THROW_EVALUATION_DURATION
Expand All @@ -147,7 +147,7 @@ def _step_evaluate_throw(self, action: actions.EvaluateThrow):
):
self.model.parent.handle_event(ThrowEvaluation())

return actions.EvaluateThrow(action.duration + 1)
return infant_actions.EvaluateThrow(action.duration + 1)

# Helper functions

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import math
import numpy as np

from infant_abm.agents.parent.parent import Parent, Action
from infant_abm.agents.infant.events import ToyThrown, ToySelected, ThrowEvaluation
from infant_abm.agents.parent import Parent, Action
from infant_abm.agents.events import ToyThrown, ToySelected, ThrowEvaluation
from infant_abm.agents import Toy
from infant_abm.agents.position import Position

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass, asdict


from infant_abm.agents.infant import actions, Parameter
from infant_abm.agents import Agent, Position
from infant_abm.agents.parameter import Parameter
from infant_abm.agents.agent import Agent
from infant_abm.agents.position import Position
from infant_abm.agents import infant_actions


@dataclass
Expand Down Expand Up @@ -73,15 +74,15 @@ def step(self):

def _perform_action(self, action):
match action:
case actions.LookForToy():
case infant_actions.LookForToy():
return self._step_look_for_toy(action)
case actions.EvaluateToy():
case infant_actions.EvaluateToy():
return self._step_evaluate_toy(action)
case actions.Crawl():
case infant_actions.Crawl():
return self._step_crawl(action)
case actions.InteractWithToy():
case infant_actions.InteractWithToy():
return self._step_interact_with_toy(action)
case actions.EvaluateThrow():
case infant_actions.EvaluateThrow():
return self._step_evaluate_throw(action)

def _move(self):
Expand Down
19 changes: 0 additions & 19 deletions infant_abm/agents/infant/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
class Action:
class InfantAction:
def __init__(self, metadata=None):
self.metadata = metadata


class Crawl(Action):
class Crawl(InfantAction):
pass


class LookForToy(Action):
class LookForToy(InfantAction):
pass


class InteractWithToy(Action):
class InteractWithToy(InfantAction):
pass


class EvaluateToy(Action):
class EvaluateToy(InfantAction):
def __init__(self, duration=0, metadata=None):
super().__init__(metadata)
self.duration = duration


class EvaluateThrow(Action):
class EvaluateThrow(InfantAction):
def __init__(self, duration=0, metadata=None):
super().__init__(metadata)
self.duration = duration
Empty file.
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import math
import numpy as np

from infant_abm.agents.infant import Infant, Params, actions
from infant_abm.agents.infant.events import ToySelected, ToyThrown
from infant_abm.agents import infant_actions
from infant_abm.agents.infant import Infant, Params
from infant_abm.agents.events import ToySelected, ToyThrown
from infant_abm.agents.position import Position
from infant_abm.agents.toy import Toy


class NoVisionInfant(Infant):
ALLOWED_ACTIONS = [
actions.LookForToy,
actions.Crawl,
actions.InteractWithToy,
infant_actions.LookForToy,
infant_actions.Crawl,
infant_actions.InteractWithToy,
]

def __init__(self, unique_id, model, pos, params: Params):
super().__init__(unique_id, model, pos, params)

self.target: Toy = None

self.next_action = actions.LookForToy()
self.next_action = infant_actions.LookForToy()

def step(self):
next_action = super()._perform_action(self.next_action)
Expand Down Expand Up @@ -52,7 +53,7 @@ def _step_interact_with_toy(self, _action):
self.target.interact()
self.target = None

return actions.LookForToy()
return infant_actions.LookForToy()

def _step_look_for_toy(self, _action):
toys = self.model.get_toys()
Expand All @@ -64,15 +65,15 @@ def _step_look_for_toy(self, _action):
self.velocity = Position.calc_norm_vector(self.pos, target.pos)
self.target = target
self.model.parent.handle_event(ToySelected(self.target))
return actions.Crawl()
return infant_actions.Crawl()

def _step_crawl(self, _action):
if self._target_in_range():
return actions.InteractWithToy()
return infant_actions.InteractWithToy()

if self._gets_distracted():
self.target = None
return actions.LookForToy()
return infant_actions.LookForToy()

self._move()
return actions.Crawl()
return infant_actions.Crawl()
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import math
import numpy as np

from infant_abm.agents.parent.parent import Parent, Action
from infant_abm.agents.infant.events import ToyThrown
from infant_abm.agents.parent import Parent, Action
from infant_abm.agents.events import ToyThrown
from infant_abm.agents.position import Position


class MoverParent(Parent):
class NoVisionParent(Parent):
ALLOWED_ACTIONS = [Action.WAIT, Action.FETCH_TOY, Action.PASS_TOY]

def __init__(self, unique_id, model, pos):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from infant_abm.agents.agent import Agent
from infant_abm.agents.position import Position
from infant_abm.agents.infant.events import ToySelected, ToyThrown, ThrowEvaluation
from infant_abm.agents.events import ToySelected, ToyThrown, ThrowEvaluation


class Action(Enum):
Expand Down
6 changes: 0 additions & 6 deletions infant_abm/agents/parent/__init__.py

This file was deleted.

Empty file.
Loading

0 comments on commit bc36877

Please sign in to comment.