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
use crate::gp::{NoCstrRslts, OneObj};
use alloc::vec::Vec;
use cl_traits::Storage;

pub type GpOrMut<'a, HCR, OR, S, SCR> = GpOr<&'a mut [HCR], &'a mut [OR], &'a mut S, &'a mut [SCR]>;
pub type GpOrRef<'a, HCR, OR, S, SCR> = GpOr<&'a [HCR], &'a [OR], &'a S, &'a [SCR]>;
pub type GpOrVec<HCR, OR, S, SCR> = GpOr<Vec<HCR>, Vec<OR>, S, Vec<SCR>>;

pub type MpOr<ORS, S> = GpOr<NoCstrRslts, ORS, S, NoCstrRslts>;
pub type MpOrMut<'a, OR, S> = GpOrMut<'a, (), OR, S, ()>;
pub type MpOrRef<'a, OR, S> = GpOrRef<'a, (), OR, S, ()>;
pub type MpOrVec<OR, S> = MpOr<Vec<OR>, S>;

pub type MphOr<HCRS, ORS, S> = GpOr<HCRS, ORS, S, NoCstrRslts>;
pub type MphOrMut<'a, OR, S> = GpOrMut<'a, usize, OR, S, ()>;
pub type MphOrRef<'a, OR, S> = GpOrRef<'a, usize, OR, S, ()>;
pub type MphOrVec<OR, S> = MphOr<Vec<usize>, Vec<OR>, S>;

pub type MphsOr<HCRS, ORS, S, SCRS> = GpOr<HCRS, ORS, S, SCRS>;
pub type MphsOrMut<'a, OR, S> = GpOrMut<'a, usize, OR, S, usize>;
pub type MphsOrRef<'a, OR, S> = GpOrRef<'a, usize, OR, S, usize>;
pub type MphsOrVec<OR, S> = MphsOr<Vec<usize>, Vec<OR>, S, Vec<usize>>;

pub type SpOr<OR, S> = GpOr<NoCstrRslts, OneObj<OR>, S, NoCstrRslts>;
pub type SpOrMut<'a, OR, S> = GpOrMut<'a, (), OR, S, ()>;
pub type SpOrRef<'a, OR, S> = GpOrRef<'a, (), OR, S, ()>;

/// GP-OR (Generic Problem - Optimization Result)
///
/// This structure is generic over single or multi objective problems, constrained or not.
///
/// # Types
///
/// * `CRS`: Constraint Results Storage
/// * `ORS`: Objective Results Storage
/// * `S`: Solution
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct GpOr<HCRS, ORS, S, SCRS> {
  pub(crate) hard_cstr_rslts: HCRS,
  pub(crate) obj_rslts: ORS,
  pub(crate) soft_cstr_rslts: SCRS,
  pub(crate) solution: S,
}

impl<HCRS, ORS, S, SCRS> GpOr<HCRS, ORS, S, SCRS> {
  pub fn new(hard_cstr_rslts: HCRS, obj_rslts: ORS, soft_cstr_rslts: SCRS, solution: S) -> Self {
    Self { hard_cstr_rslts, obj_rslts, soft_cstr_rslts, solution }
  }

  pub fn solution(&self) -> &S {
    &self.solution
  }

  pub fn solution_mut(&mut self) -> &mut S {
    &mut self.solution
  }
}

impl<HCRS, ORS, S, SCRS> GpOr<HCRS, ORS, S, SCRS>
where
  HCRS: AsRef<[usize]>,
{
  pub fn hard_cstr_rslts(&self) -> &[usize] {
    self.hard_cstr_rslts.as_ref()
  }
}

impl<HCRS, OR, S, SCRS> GpOr<HCRS, [OR; 1], S, SCRS> {
  pub fn obj_rslt(&self) -> &OR {
    &self.obj_rslts[0]
  }
}

impl<HCRS, OR, ORS, S, SCRS> GpOr<HCRS, ORS, S, SCRS>
where
  ORS: AsRef<[OR]> + Storage<Item = OR>,
{
  pub fn obj_rslts(&self) -> &[OR] {
    self.obj_rslts.as_ref()
  }
}

impl<HCRS, ORS, S, SCRS> GpOr<HCRS, ORS, S, SCRS>
where
  SCRS: AsRef<[usize]>,
{
  pub fn soft_cstr_rslts(&self) -> &[usize] {
    self.soft_cstr_rslts.as_ref()
  }
}

impl<OR, S, SCR> GpOrRef<'_, usize, OR, S, SCR>
where
  OR: Clone,
  S: Clone,
{
  pub fn to_mph_vec(&self) -> MphOrVec<OR, S> {
    GpOr {
      hard_cstr_rslts: self.hard_cstr_rslts.to_vec(),
      obj_rslts: self.obj_rslts.to_vec(),
      soft_cstr_rslts: Default::default(),
      solution: self.solution.clone(),
    }
  }
}

impl<OR, S> GpOrRef<'_, usize, OR, S, usize>
where
  OR: Clone,
  S: Clone,
{
  pub fn to_mphs_vec(&self) -> MphsOrVec<OR, S> {
    GpOr {
      hard_cstr_rslts: self.hard_cstr_rslts.to_vec(),
      obj_rslts: self.obj_rslts.to_vec(),
      soft_cstr_rslts: self.soft_cstr_rslts.to_vec(),
      solution: self.solution.clone(),
    }
  }
}