fts_solver/types/cost.rs
1use super::spvec;
2use std::cmp::Ordering;
3
4// A group is a linear combination of portfolios
5spvec!(Group);
6
7/// A basic type for representing a point on a piecewise-linear demand curve
8#[derive(Clone, PartialEq, Debug)]
9pub struct Point {
10 /// The quantity value at this point on the curve
11 pub quantity: f64,
12 /// The price value at this point on the curve
13 pub price: f64,
14}
15
16// A partial ordering for points in the context of a weakly monotone piecewise linear curve
17
18impl PartialOrd for Point {
19 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
20 match self.quantity.partial_cmp(&other.quantity) {
21 Some(Ordering::Less) => {
22 if self.price >= other.price {
23 Some(Ordering::Less)
24 } else {
25 None
26 }
27 }
28 Some(Ordering::Equal) => other.price.partial_cmp(&self.price),
29 Some(Ordering::Greater) => {
30 if self.price <= other.price {
31 Some(Ordering::Greater)
32 } else {
33 None
34 }
35 }
36 None => None,
37 }
38 }
39}
40
41/// A piecewise-linear, weakly monotone curve for representing demand.
42/// Points should be ordered ascending by quantity and the prices non-increasing.
43#[derive(Debug)]
44pub struct PiecewiseLinearCurve {
45 /// The sequence of points defining the curve segments
46 pub points: Vec<Point>,
47}
48
49/// A "flat" curve for representing demand, which may have a finite, half-line, or full-line domain.
50#[derive(Debug)]
51pub struct Constant {
52 /// The quantity range (min, max) for which this constant price applies
53 pub quantity: (f64, f64),
54 /// The constant price value
55 pub price: f64,
56}
57
58/// The currently-supported ways to define utility functions
59#[derive(Debug)]
60pub enum Cost {
61 /// A piecewise-linear curve defining price as a function of quantity
62 PiecewiseLinearCurve(PiecewiseLinearCurve),
63 /// A constant price over a fixed quantity range
64 Constant(Constant),
65}