Skip to main content

Policy

Struct Policy 

Source
pub struct Policy<K> { /* private fields */ }
Expand description

A persistent table of learned weights, one per key, nudged by feedback.

Decision logic is stateless per call; Policy is the small mutable state that lets an agent improve over time. Read a key’s learned weight and fold it into an action (as its base or a consideration); after an outcome, call reward to move that weight toward what actually worked.

The update is a bounded integer moving average, so it is deterministic and can never run away — it is not machine learning, just `weight += rate * (outcome

  • weight)in fixed point, clamped to0.0..=1.0`.

§Example

use reliakit_decide::{Policy, Score};

// Start every key at 0.5, learning at rate 0.5.
let mut policy = Policy::new(Score::from_ratio(1, 2), Score::from_ratio(1, 2));
assert_eq!(policy.weight(&"route_a"), Score::from_ratio(1, 2)); // unseen -> default

// "route_a" worked well (outcome 1.0): its weight rises toward 1.0.
policy.reward("route_a", Score::MAX);
assert_eq!(policy.weight(&"route_a").raw(), 7_500); // 0.5 + 0.5*(1.0-0.5)

Implementations§

Source§

impl<K> Policy<K>

Source

pub fn new(rate: Score, default: Score) -> Policy<K>

Creates an empty policy with a learning rate and a default weight for keys that have not been seen yet.

Source

pub fn len(&self) -> usize

The number of keys that have learned weights.

Source

pub fn is_empty(&self) -> bool

Returns true if no key has a learned weight yet.

Source

pub fn entries(&self) -> &[(K, Score)]

The learned (key, weight) pairs, for snapshotting to storage. The host serializes these however it likes — the engine pulls in no serializer. Restore them later with set.

Source§

impl<K: PartialEq> Policy<K>

Source

pub fn weight(&self, key: &K) -> Score

The learned weight for key, or the configured default if it is unseen.

Source

pub fn reward(&mut self, key: K, outcome: Score)

Nudges key’s weight toward outcome by the learning rate. A previously unseen key starts from the default before moving.

Source

pub fn set(&mut self, key: K, weight: Score)

Sets key’s weight directly (insert or replace) — used to restore learned weights from storage. Unlike reward this does not apply the learning rate; it stores the value as given.

Trait Implementations§

Source§

impl<K: Clone> Clone for Policy<K>

Source§

fn clone(&self) -> Policy<K>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug> Debug for Policy<K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K> Freeze for Policy<K>

§

impl<K> RefUnwindSafe for Policy<K>
where K: RefUnwindSafe,

§

impl<K> Send for Policy<K>
where K: Send,

§

impl<K> Sync for Policy<K>
where K: Sync,

§

impl<K> Unpin for Policy<K>
where K: Unpin,

§

impl<K> UnsafeUnpin for Policy<K>

§

impl<K> UnwindSafe for Policy<K>
where K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.