1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
//! Considerations are used to tell possibility, how important certain fact about the world is.
//!
//! See [`Consideration`] for more info about considerations.
//!
//! See [`crate::evaluators`] for more info about evaluators (operations on sets of considerations).
use crate::{condition::*, score_mapping::*, Scalar};
/// Consideration represent the score (also called weight, possibility, likeliness) of certain fact
/// about the state of the world.
///
/// Imagine memory stores information about number of bannanas in the backpack, and you want to know
/// how important for you is to get new one to not get hungry during the day - the more bannanas you
/// have, the less likely it is for you to end up hungry and that score is used to decide if you need
/// to go and get new one or even estimate how many of them you would need.
///
/// User should make considerations as lightweight and as small as possible. The reason for that is
/// to make them reused and combined into bigger sets of considerations using evaluators
/// ([`crate::evaluators`]).
///
/// # Example
/// ```
/// use emergent::prelude::*;
///
/// struct Memory { counter: usize }
///
/// struct Hunger;
///
/// impl Consideration<Memory> for Hunger {
/// fn score(&self, memory: &Memory) -> Scalar {
/// 1.0 / memory.counter as Scalar
/// }
/// }
///
/// let mut memory = Memory { counter: 10 };
/// assert_eq!(Hunger.score(&memory), 0.1);
/// ```
pub trait Consideration<M = ()>: Send + Sync {
// Scores probability of certain fact.
fn score(&self, memory: &M) -> Scalar;
// Applies score mapping to this consideration.
fn remap<T>(self, mapping: T) -> ConsiderationRemap<M, T>
where
T: ScoreMapping,
Self: Sized + 'static,
{
ConsiderationRemap::new(self, mapping)
}
}
impl<M> Consideration<M> for Scalar {
fn score(&self, _: &M) -> Scalar {
*self
}
}
/// Consideration that wraps a closure.
///
/// # Example
/// ```
/// use emergent::prelude::*;
///
/// struct Memory { counter: usize }
///
/// let mut memory = Memory { counter: 10 };
/// let consideration = ClosureConsideration::new(
/// |memory: &Memory| 1.0 / memory.counter as Scalar,
/// );
/// assert_eq!(consideration.score(&memory), 0.1);
/// ```
pub struct ClosureConsideration<M = ()>(pub Box<dyn Fn(&M) -> Scalar + Send + Sync>);
impl<M> ClosureConsideration<M> {
pub fn new<F>(f: F) -> Self
where
F: Fn(&M) -> Scalar + 'static + Send + Sync,
{
Self(Box::new(f))
}
}
impl<M> Consideration<M> for ClosureConsideration<M> {
fn score(&self, memory: &M) -> Scalar {
(self.0)(memory)
}
}
impl<M> std::fmt::Debug for ClosureConsideration<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ClosureConsideration").finish()
}
}
/// Consideration uses [`ScoreMapping`] to remap score of wrapped consideration.
///
/// # Example
/// ```
/// use emergent::prelude::*;
///
/// struct Memory { counter: usize }
///
/// struct Hunger;
///
/// impl Consideration<Memory> for Hunger {
/// fn score(&self, memory: &Memory) -> Scalar {
/// 1.0 / memory.counter as Scalar
/// }
/// }
///
/// let mut memory = Memory { counter: 10 };
/// let consideration = ConsiderationRemap::new(Hunger, ReverseScoreMapping);
/// assert_eq!(consideration.score(&memory), 0.9);
/// ```
pub struct ConsiderationRemap<M = (), T = NoScoreMapping>
where
T: ScoreMapping,
{
pub consideration: Box<dyn Consideration<M>>,
pub mapping: T,
}
impl<M, T> ConsiderationRemap<M, T>
where
T: ScoreMapping,
{
pub fn new<C>(consideration: C, mapping: T) -> Self
where
C: Consideration<M> + 'static,
{
Self {
consideration: Box::new(consideration),
mapping,
}
}
}
impl<M, T> Consideration<M> for ConsiderationRemap<M, T>
where
T: ScoreMapping,
{
fn score(&self, memory: &M) -> Scalar {
self.mapping.remap(self.consideration.score(memory))
}
}
impl<M, T> std::fmt::Debug for ConsiderationRemap<M, T>
where
T: ScoreMapping + std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConsiderationRemap")
.field("mapping", &self.mapping)
.finish()
}
}
/// Consideration wraps [`Condition`] and converts it into consideration.
///
/// If condition returns true, it gives score of [`ConditionConsideration::positive`], if not then
/// it gives [`ConditionConsideration::negative`].
///
/// # Example
/// ```
/// use emergent::prelude::*;
///
/// struct Memory { counter: usize }
///
/// let mut memory = Memory { counter: 1 };
/// let consideration = ConditionConsideration::new(
/// ClosureCondition::new(|memory: &Memory| memory.counter > 0),
/// 1.0,
/// 0.0,
/// );
/// assert_eq!(consideration.score(&memory), 1.0);
/// ```
pub struct ConditionConsideration<M = ()> {
pub condition: Box<dyn Condition<M>>,
pub positive: Scalar,
pub negative: Scalar,
}
impl<M> ConditionConsideration<M> {
pub fn new<C>(condition: C, positive: Scalar, negative: Scalar) -> Self
where
C: Condition<M> + 'static,
{
Self {
condition: Box::new(condition),
positive,
negative,
}
}
pub fn unit<C>(condition: C) -> Self
where
C: Condition<M> + 'static,
{
Self {
condition: Box::new(condition),
positive: 1.0,
negative: 0.0,
}
}
}
impl<M> Consideration<M> for ConditionConsideration<M> {
fn score(&self, memory: &M) -> Scalar {
if self.condition.validate(memory) {
self.positive
} else {
self.negative
}
}
}
impl<M> std::fmt::Debug for ConditionConsideration<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConditionConsideration")
.field("positive", &self.positive)
.field("negative", &self.negative)
.finish()
}
}