it.unibo.alchemist.model.cognitive.actions.CognitiveAgentAvoidLayer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemist-cognitive-agents Show documentation
Show all versions of alchemist-cognitive-agents Show documentation
Abstraction for group of pedestrians capable of influence each other emotionally.
/*
* Copyright (C) 2010-2023, Danilo Pianini and contributors
* listed, for each module, in the respective subproject's build.gradle.kts file.
*
* This file is part of Alchemist, and is distributed under the terms of the
* GNU General Public License, with a linking exception,
* as described in the file LICENSE in the Alchemist distribution's top directory.
*/
package it.unibo.alchemist.model.cognitive.actions
import it.unibo.alchemist.model.EnvironmentWithObstacles
import it.unibo.alchemist.model.Molecule
import it.unibo.alchemist.model.Node
import it.unibo.alchemist.model.Node.Companion.asPropertyOrNull
import it.unibo.alchemist.model.Reaction
import it.unibo.alchemist.model.cognitive.CognitiveProperty
import it.unibo.alchemist.model.cognitive.PedestrianProperty
import it.unibo.alchemist.model.environments.Euclidean2DEnvironment
import it.unibo.alchemist.model.positions.Euclidean2DPosition
/**
* Move the node towards positions of the environment with a low concentration of the target molecule.
*
* @param environment
* the environment inside which the node moves.
* @param reaction
* the reaction which executes this action.
* @param pedestrian
* the owner of this action.
* @param targetMolecule
* the {@link Molecule} you want to know the concentration in the different positions of the environment.
* @param viewDepth
* the depth of view of the node, defaults to infinity.
*/
class CognitiveAgentAvoidLayer @JvmOverloads constructor(
environment: Euclidean2DEnvironment,
reaction: Reaction,
override val pedestrian: PedestrianProperty,
targetMolecule: Molecule,
private val viewDepth: Double = Double.POSITIVE_INFINITY,
) : AbstractLayerAction(environment, reaction, pedestrian, targetMolecule) {
private val followScalarField = getLayerOrFail().let { layer ->
CognitiveAgentFollowScalarField(environment, reaction, pedestrian, layer.center()) {
/*
* Moves the node where the concentration is lower.
*/
-layer.concentrationIn(it)
}
}
override fun cloneAction(node: Node, reaction: Reaction): CognitiveAgentAvoidLayer =
CognitiveAgentAvoidLayer(environment, reaction, node.pedestrianProperty, targetMolecule, viewDepth)
/**
* @returns the next relative position. The node is moved only if he/she percepts the danger
* (either because it is in sight or due to social contagion), otherwise a zero vector is returned.
*/
override fun nextPosition(): Euclidean2DPosition = when {
node.wantsToEscape() || isDangerInSight() -> followScalarField.nextPosition()
else -> environment.origin
}
/**
* Checks whether the center of the layer (if there's one) is in sight. If the layer has no center true is
* returned.
*/
@Suppress("UNCHECKED_CAST")
private fun isDangerInSight(): Boolean = getLayerOrFail().center()?.let { center ->
val currentPosition = environment.getPosition(node)
/*
* environment is euclidean, so if it has obstacles it must be an
* EnvironmentWithObstacles<*, *, Euclidean2DPosition>. Since generic types can't be checked at runtime, this
* is the best we can do.
*/
val visualTrajectoryOccluded = (environment as? EnvironmentWithObstacles<*, *, Euclidean2DPosition>)
?.intersectsObstacle(currentPosition, center)
?: false
center.distanceTo(currentPosition) <= viewDepth && !visualTrajectoryOccluded
} ?: true
private fun Node.wantsToEscape(): Boolean {
val cognitiveProperty = asPropertyOrNull>()
return cognitiveProperty != null &&
cognitiveProperty.danger == targetMolecule &&
cognitiveProperty.cognitiveModel.wantsToEscape()
}
}