Struct copper::Solution

source ·
pub struct Solution(/* private fields */);
Expand description

Assignment for decision variables that satisfies all constraints.

Implementations§

source§

impl Solution

source

pub fn get_values(&self, vs: &[VarId]) -> Vec<i32>

Get assignments for the decision variables provided as a slice.

source

pub fn get_values_array<const N: usize>(&self, vs: &[VarId; N]) -> [i32; N]

Get assignments for the decision variables provided as a reference to an array.

source

pub fn get_values_iter<'a, I>(&'a self, vs: I) -> impl Iterator<Item = i32> + 'a
where I: IntoIterator + 'a, I::Item: Borrow<VarId>,

Get assignments for the provided decision variables.

source

pub fn get_value_binary(&self, v: impl Borrow<VarIdBinary>) -> bool

Get binary assignment for the provided decision variable.

source

pub fn get_values_binary(&self, vs: &[VarIdBinary]) -> Vec<bool>

Get binary assignments for the decision variables provided as a slice.

Examples found in repository?
examples/pc.rs (line 50)
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
fn main() {
    // All problem formulations will start with a model object
    let mut m = copper::Model::default();

    // How many monitors do we buy: we need at least one, but not more than three
    let n_monitors = m.new_var(1, 3).unwrap();

    // All monitors cost the same, and each additional monitor provides the same bump to our score
    let monitor_price = 100;
    let monitor_score = 250;

    // Each GPU model has a fixed price, and an associated benchmark score
    let gpu_prices = [150, 250, 500];
    let gpu_scores = [100, 400, 800];

    // We use binary decision variables to represent "do I pick this GPU?"
    let gpus: Vec<_> = m.new_vars_binary(gpu_scores.len()).collect();

    // For each potential GPU, we multiply its price (and score) by whether or not it is selected.
    // The sum of these terms gives us the price and score of the selected GPU.
    let gpu_price = m.sum_iter(gpus.iter().zip(gpu_prices).map(|(x, price)| x.times(price)));
    let gpu_score = m.sum_iter(gpus.iter().zip(gpu_scores).map(|(x, score)| x.times(score)));

    // This expression is the overall price of our build
    let price = m.add(gpu_price, n_monitors.times(monitor_price));

    // We want to maximize this score: how much we'll value this particular build
    let score = m.add(gpu_score, n_monitors.times(monitor_score));

    // Exactly one GPU: we want to run Crysis, but our case must fit under the desk
    let n_gpus = m.sum(&gpus);
    m.equals(n_gpus, 1);

    // Grandma got us some money for our birthday, that will be our budget
    m.less_than_or_equals(price, 600);

    // Let the solver find the assignment that upholds our constraints and maximizes our score
    let solution = m.maximize(score).unwrap();

    // Our optimal build has three monitors and a mid-tier GPU. We even have some left-over cash!
    assert_eq!(solution[n_monitors], 3);
    assert_eq!(solution.get_values_binary(&gpus), vec![false, true, false]);
    assert_eq!(solution[score], 1150);
    assert_eq!(solution[price], 550);
}
source

pub fn get_values_binary_array<const N: usize>( &self, vs: &[VarIdBinary; N] ) -> [bool; N]

Get binary assignments for the decision variables provided as a reference to an array.

source

pub fn get_values_binary_iter<'a, I>( &'a self, vs: I ) -> impl Iterator<Item = bool> + 'a
where I: IntoIterator + 'a, I::Item: Borrow<VarIdBinary>,

Get binary assignments for the provided decision variables.

Trait Implementations§

source§

impl Debug for Solution

source§

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

Formats the value using the given formatter. Read more
source§

impl From<Vec<i32>> for Solution

source§

fn from(value: Vec<i32>) -> Self

Converts to this type from the input type.
source§

impl Index<VarId> for Solution

§

type Output = i32

The returned type after indexing.
source§

fn index(&self, index: VarId) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Ord for Solution

source§

fn cmp(&self, other: &Solution) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Solution

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Solution

source§

fn partial_cmp(&self, other: &Solution) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for Solution

source§

impl StructuralEq for Solution

source§

impl StructuralPartialEq for Solution

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
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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.