optirustic 1.2.3

This crate moved to https://github.com/s-simoncelli/nsga-rs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use rand::distr::uniform::SampleUniform;
use rand::prelude::{IndexedRandom, IteratorRandom};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;

#[cfg(feature = "python")]
use pyo3::prelude::*;

use crate::core::{OError, Problem};

/// A trait to define a decision variable.
pub trait Variable<T>: Display {
    /// Generate a new random value for the variable.
    fn generate(&self) -> T;
    /// Get the variable name
    fn name(&self) -> String;
}

pub trait BoundedNumberTrait: SampleUniform + PartialOrd + Display + Clone {}
impl<T: SampleUniform + PartialOrd + Display + Clone> BoundedNumberTrait for T {}

/// A variable between a lower and upper bound.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "python", derive(IntoPyObject))]
pub struct BoundedNumber<N: BoundedNumberTrait> {
    /// The variable name
    name: String,
    /// The minimum value bound.
    min_value: N,
    /// The maximum value bound.
    max_value: N,
}

impl<N: BoundedNumberTrait> BoundedNumber<N> {
    /// Create a new decision variable using a number bounded between a lower and upper bound.
    /// When a new value `N` for this variable is generated, the new value will be picked such that
    /// `min_value` <= `N` <= `max_value`.
    ///
    /// # Arguments
    ///
    /// * `name`: The variable name.
    /// * `min_value`: The lower bound.
    /// * `max_value`: The upper bound.
    ///
    /// returns: `Result<BoundedNumber, OError>`
    pub fn new(name: &str, min_value: N, max_value: N) -> Result<Self, OError> {
        if min_value >= max_value {
            return Err(OError::TooLargeLowerBound(
                min_value.to_string(),
                max_value.to_string(),
            ));
        }
        Ok(Self {
            name: name.to_string(),
            min_value,
            max_value,
        })
    }

    /// The variable lower bound.
    ///
    /// return: `N`
    pub fn min_value(&self) -> N {
        self.min_value.clone()
    }

    /// The variable upper bound.
    ///
    /// return: `N`
    pub fn max_value(&self) -> N {
        self.max_value.clone()
    }

    /// The variable upper and lower bound.
    ///
    /// return: `N`
    pub fn bounds(&self) -> (N, N) {
        (self.min_value.clone(), self.max_value.clone())
    }
}

impl<N: BoundedNumberTrait + Copy> Variable<N> for BoundedNumber<N> {
    /// Randomly generate a new bounded number.
    fn generate(&self) -> N {
        let mut rng = rand::rng();
        rng.random_range(self.min_value..=self.max_value)
    }
    fn name(&self) -> String {
        self.name.clone()
    }
}

impl<N: BoundedNumberTrait> Display for BoundedNumber<N> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "BoundedNumber '{}' to [{}; {}]",
            self.name, self.min_value, self.max_value
        )
    }
}

/// A boolean variable.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "python", derive(IntoPyObject))]
pub struct Boolean {
    /// The variable name.
    name: String,
}

impl Boolean {
    /// Create a new boolean variable.
    ///
    /// # Arguments
    ///
    /// * `name`: The variable name.
    ///
    /// returns: `Boolean`
    pub fn new(name: &str) -> Self {
        Boolean {
            name: name.to_string(),
        }
    }
}
impl Display for Boolean {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "Boolean '{}'", self.name)
    }
}

impl Variable<bool> for Boolean {
    /// Randomly generate a boolean value.
    ///
    /// return: `bool`
    fn generate(&self) -> bool {
        let mut rng = rand::rng();
        !matches!([0, 1].choose(&mut rng).unwrap(), 0)
    }
    fn name(&self) -> String {
        self.name.clone()
    }
}

/// A variable choice.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "python", derive(IntoPyObject))]
pub struct Choice {
    /// The variable name.
    name: String,
    /// The list of choices.
    choices: Vec<String>,
}

impl Choice {
    /// Create a new list of choices.
    ///
    /// # Arguments
    ///
    /// * `name`: The variable name.
    /// * `choices`: The list of choices.
    ///
    /// returns: `Choice`
    pub fn new(name: &str, choices: Vec<String>) -> Self {
        Self {
            name: name.to_string(),
            choices,
        }
    }
}

impl Display for Choice {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "Choice '{}': {}", self.name, self.choices.join(", "))
    }
}

impl Variable<String> for Choice {
    /// Randomly pick a choice.
    fn generate(&self) -> String {
        let mut rng = rand::rng();
        let choice_index = (0..self.choices.len()).choose(&mut rng).unwrap();
        self.choices[choice_index].clone()
    }

    fn name(&self) -> String {
        self.name.clone()
    }
}

/// The types of variables to set on a problem.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
#[cfg_attr(feature = "python", derive(IntoPyObject))]
pub enum VariableType {
    /// A continuous bounded variable (f64)
    Real(BoundedNumber<f64>),
    /// A discrete bounded variable (i64)
    Integer(BoundedNumber<i64>),
    /// A boolean variable
    Boolean(Boolean),
    /// A variable representing a choice (as string)
    Choice(Choice),
}

impl VariableType {
    /// Generate a new random variable value based on its type.
    ///
    /// returns: `VariableValue`
    pub fn generate_random_value(&self) -> VariableValue {
        match &self {
            VariableType::Real(v) => VariableValue::Real(v.generate()),
            VariableType::Integer(v) => VariableValue::Integer(v.generate()),
            VariableType::Boolean(v) => VariableValue::Boolean(v.generate()),
            VariableType::Choice(v) => VariableValue::Choice(v.generate()),
        }
    }

    /// Get the variable name.
    ///
    /// return: `String`
    pub fn name(&self) -> String {
        match self {
            VariableType::Real(t) => t.name.clone(),
            VariableType::Integer(t) => t.name.clone(),
            VariableType::Boolean(t) => t.name.clone(),
            VariableType::Choice(t) => t.name.clone(),
        }
    }

    pub fn label(&self) -> String {
        let label = match &self {
            VariableType::Real(_) => "real",
            VariableType::Integer(_) => "integer",
            VariableType::Boolean(_) => "boolean",
            VariableType::Choice(_) => "choice",
        };
        label.into()
    }

    /// Check if the variable is a real number.
    ///
    /// return: `bool`
    pub(crate) fn is_real(&self) -> bool {
        matches!(self, VariableType::Real(_))
    }

    /// Check if the variable is an integer number.
    ///
    /// return: `bool`
    pub(crate) fn is_integer(&self) -> bool {
        matches!(self, VariableType::Integer(_))
    }
}

impl Display for VariableType {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            VariableType::Real(v) => write!(f, "{v}")?,
            VariableType::Integer(v) => write!(f, "{v}")?,
            VariableType::Boolean(v) => write!(f, "{v}")?,
            VariableType::Choice(v) => write!(f, "{v}")?,
        };
        Ok(())
    }
}

/// The value of a variable to set on an individual.
#[derive(Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "python", pyclass, derive(IntoPyObjectRef))]
#[serde(untagged)]
pub enum VariableValue {
    /// The value for a floating-point number. This is a f64.
    Real(f64),
    /// The value for an integer number. This is an i64.
    Integer(i64),
    /// The value for a boolean variable.
    Boolean(bool),
    /// The value for a choice variable.
    Choice(String),
}

impl PartialEq for VariableValue {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (VariableValue::Real(s), VariableValue::Real(o)) => {
                (s.is_nan() && o.is_nan()) || (*s == *o)
            }
            (VariableValue::Integer(s), VariableValue::Integer(o)) => *s == *o,
            (VariableValue::Boolean(s), VariableValue::Boolean(o)) => s == o,
            (VariableValue::Choice(s), VariableValue::Choice(o)) => s == o,
            _ => false,
        }
    }
}

impl VariableValue {
    /// Check if the variable value matches the variable type set on the problem. This return an
    /// error if the variable name does not exist in the problem.
    ///
    /// # Arguments
    ///
    /// * `name`: The name of the variable in the problem.
    /// * `problem`: The problem being solved.
    ///
    /// returns: `Result<bool, OError>`
    pub fn match_type(&self, name: &str, problem: Arc<Problem>) -> Result<bool, OError> {
        let value = match problem.get_variable(name)? {
            VariableType::Real(_) => matches!(self, VariableValue::Real(_)),
            VariableType::Integer(_) => matches!(self, VariableValue::Integer(_)),
            VariableType::Boolean(_) => matches!(self, VariableValue::Boolean(_)),
            VariableType::Choice(_) => matches!(self, VariableValue::Choice(_)),
        };
        Ok(value)
    }

    /// Get the value if the variable is of real type. This returns an error if the variable is not
    /// real.
    ///
    /// returns: `Result<f64, OError>`
    pub fn as_real(&self) -> Result<f64, OError> {
        if let VariableValue::Real(v) = self {
            Ok(*v)
        } else {
            Err(OError::WrongVariableType("real".to_string()))
        }
    }

    /// Get the value if the variable is of discrete type. This returns an error if the variable is
    /// not an integer.
    ///
    /// returns: `Result<f64, OError>`
    pub fn as_integer(&self) -> Result<i64, OError> {
        if let VariableValue::Integer(v) = self {
            Ok(*v)
        } else {
            Err(OError::WrongVariableType("integer".to_string()))
        }
    }
}

impl Debug for VariableValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            VariableValue::Real(v) => write!(f, "{v}")?,
            VariableValue::Integer(v) => write!(f, "{v}")?,
            VariableValue::Boolean(v) => write!(f, "{v}")?,
            VariableValue::Choice(v) => write!(f, "{v}")?,
        };
        Ok(())
    }
}

/// Python classes to handle Rust variables with generics.
#[cfg(feature = "python")]
#[pyclass(name = "VariableType", eq, eq_int)]
#[derive(Clone, PartialEq)]
pub enum PyVariableType {
    Real,
    Integer,
    Boolean,
    Choice,
}

#[cfg(feature = "python")]
#[pymethods]
impl PyVariableType {
    pub fn __repr__(&self) -> String {
        let label = match &self {
            PyVariableType::Real => "real",
            PyVariableType::Integer => "integer",
            PyVariableType::Boolean => "boolean",
            PyVariableType::Choice => "choice",
        };
        label.to_string()
    }

    pub fn __str__(&self) -> String {
        self.__repr__()
    }
}

/// Python class holding the data for a problem variable.
#[cfg(feature = "python")]
#[pyclass(get_all, name = "Variable")]
pub struct PyVariable {
    /// The variable name,
    name: String,
    /// The type of variable.
    var_type: PyVariableType,
    /// The optional lower bound.
    min_value: Option<f64>,
    /// The optional upper bound.
    max_value: Option<f64>,
}

/// Convert `&VariableType` to `PyVariable`
#[cfg(feature = "python")]
impl From<&VariableType> for PyVariable {
    fn from(value: &VariableType) -> Self {
        let var_type = match value {
            VariableType::Real(_) => PyVariableType::Real,
            VariableType::Integer(_) => PyVariableType::Integer,
            VariableType::Boolean(_) => PyVariableType::Boolean,
            VariableType::Choice(_) => PyVariableType::Choice,
        };
        let (min_value, max_value) = match value {
            VariableType::Real(n) => (Some(n.min_value()), Some(n.max_value())),
            VariableType::Integer(n) => (Some(n.min_value() as f64), Some(n.max_value() as f64)),
            VariableType::Boolean(_) => (None, None),
            VariableType::Choice(_) => (None, None),
        };

        PyVariable {
            name: value.name(),
            var_type,
            min_value,
            max_value,
        }
    }
}

#[cfg(feature = "python")]
#[pymethods]
impl PyVariable {
    pub fn __repr__(&self) -> PyResult<String> {
        let args = if let (Some(min_value), Some(max_value)) = (self.min_value, self.max_value) {
            format!(", min-value={min_value}, max_value={max_value}")
        } else {
            String::from("")
        };

        Ok(format!(
            "Variable(name='{}', type='{}'{args})",
            self.name,
            self.var_type.__repr__()
        ))
    }

    pub fn __str__(&self) -> String {
        self.__repr__().unwrap()
    }
}