Skip to main content

ParamValue

Enum ParamValue 

pub enum ParamValue {
    F64(f64),
    I64(i64),
    Cat(u32),
    Bool(bool),
}
Expand description

One parameter’s value, typed.

This is the explicit fix for Optuna’s f64 union, in which a boolean and the integer 1 are the same stored value and True == 1 collapses categorical choices (the four kinds of parameter). Here a ParamValue::Bool never compares equal to a ParamValue::I64, and round-tripping through storage cannot change a parameter’s type.

ParamValue::Cat holds an index into the owning distribution’s CatChoices, not a label: labels live once, in the schema, so renaming a choice does not rewrite history and comparisons stay cheap.

§Ordering

PartialEq is derived, so F64(f64::NAN) != F64(f64::NAN) exactly as for f64. Eq and Ord are deliberately not implemented: they would have to lie about NaN. Use ParamValue::total_cmp where a total order is required (sorting, deduplication, grid enumeration); it orders by variant first and uses f64::total_cmp within F64.

Variants§

§

F64(f64)

A floating-point value.

§

I64(i64)

An integer value.

§

Cat(u32)

A categorical choice, as an index into the distribution’s CatChoices.

§

Bool(bool)

A boolean value.

Implementations§

§

impl ParamValue

pub const fn kind(&self) -> ValueKind

The value’s kind, without its payload.

pub const fn as_f64(&self) -> Option<f64>

The payload if this is an F64, else None.

Deliberately not a lossy accessor: an I64 value does not answer as_f64, because silent numeric erasure is what this type exists to prevent. Use the transform layer (Distribution::to_unit) when a sampler wants numbers.

pub const fn as_i64(&self) -> Option<i64>

The payload if this is an I64, else None.

pub const fn as_cat(&self) -> Option<u32>

The choice index if this is a Cat, else None.

pub const fn as_bool(&self) -> Option<bool>

The payload if this is a Bool, else None.

pub const fn is_finite(&self) -> bool

true if this is a float payload that is NaN or infinite.

Non-finite parameter values are never produced by atune; the check exists so that values arriving from storage or user code can be rejected at the boundary.

pub fn total_cmp(&self, other: &Self) -> Ordering

A total order over all values, NaN included.

Values of different kinds are ordered by kind (F64 < I64 < Cat < Bool); within F64 the IEEE total order (f64::total_cmp) is used, so -0.0 < 0.0 and NaN sorts to an end rather than making comparisons non-transitive.

use atune_core::space::ParamValue;

let mut v = vec![
    ParamValue::Bool(false),
    ParamValue::F64(1.0),
    ParamValue::F64(f64::NAN),
    ParamValue::I64(-3),
];
v.sort_by(ParamValue::total_cmp);
assert_eq!(v[0], ParamValue::F64(1.0));
assert_eq!(v[2], ParamValue::I64(-3));
assert_eq!(v[3], ParamValue::Bool(false));

Trait Implementations§

§

impl Clone for ParamValue

§

fn clone(&self) -> ParamValue

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
§

impl Copy for ParamValue

§

impl Debug for ParamValue

§

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

Formats the value using the given formatter. Read more
§

impl<'de> Deserialize<'de> for ParamValue

§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
§

impl Display for ParamValue

§

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

Formats the value using the given formatter. Read more
§

impl From<bool> for ParamValue

§

fn from(v: bool) -> Self

Converts to this type from the input type.
§

impl From<f64> for ParamValue

§

fn from(v: f64) -> Self

Converts to this type from the input type.
§

impl From<i64> for ParamValue

§

fn from(v: i64) -> Self

Converts to this type from the input type.
§

impl PartialEq for ParamValue

§

fn eq(&self, other: &ParamValue) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl Serialize for ParamValue

§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
§

impl StructuralPartialEq for ParamValue

Auto Trait Implementations§

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more