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
//! row-oriented matrix to build a problem constraint by constraint
use std::borrow::Borrow;
use std::convert::TryInto;
use std::ops::RangeBounds;
use std::os::raw::c_int;

use crate::matrix_col::ColMatrix;
use crate::Problem;

/// Represents a variable
#[derive(Debug, Clone, Copy)]
pub struct Col(usize);

/// A complete optimization problem stored by row
#[derive(Debug, Clone, PartialEq, Default)]
pub struct RowMatrix {
    /// column-wise sparse constraints  matrix
    /// Each element in the outer vector represents a column (a variable)
    columns: Vec<(Vec<c_int>, Vec<f64>)>,
}

/// Functions to use when first declaring variables, then constraints.
impl Problem<RowMatrix> {
    /// add a variable to the problem.
    ///  - `col_factor` is the coefficient in front of the variable in the objective function.
    ///  - `bounds` are the maximal and minimal values that the variable can take.
    pub fn add_column<N: Into<f64> + Copy, B: RangeBounds<N>>(
        &mut self,
        col_factor: f64,
        bounds: B,
    ) -> Col {
        let col = Col(self.num_cols());
        self.add_column_inner(col_factor, bounds);
        self.matrix.columns.push((vec![], vec![]));
        col
    }

    /// Add a constraint to the problem.
    ///  - `bounds` are the maximal and minimal allowed values for the linear expression in the constraint
    ///  - `row_factors` are the coefficients in the linear expression expressing the constraint
    ///
    /// ```
    /// use highs::*;
    /// let mut pb = RowProblem::new();
    /// // Optimize 3x - 2y with x<=6 and y>=5
    /// let x = pb.add_column(3., ..6);
    /// let y = pb.add_column(-2., 5..);
    /// pb.add_row(2.., &[(x, 3.), (y, 8.)]); // 2 <= x*3 + y*8
    /// ```
    pub fn add_row<
        N: Into<f64> + Copy,
        B: RangeBounds<N>,
        ITEM: Borrow<(Col, f64)>,
        I: IntoIterator<Item = ITEM>,
    >(
        &mut self,
        bounds: B,
        row_factors: I,
    ) {
        let num_rows: c_int = self.num_rows().try_into().unwrap();
        for r in row_factors {
            let &(col, factor) = r.borrow();
            let c = &mut self.matrix.columns[col.0];
            c.0.push(num_rows);
            c.1.push(factor);
        }
        self.add_row_inner(bounds);
    }
}

impl From<RowMatrix> for ColMatrix {
    fn from(m: RowMatrix) -> Self {
        let mut astart = Vec::with_capacity(m.columns.len());
        astart.push(0);
        let size: usize = m.columns.iter().map(|(v, _)| v.len()).sum();
        let mut aindex = Vec::with_capacity(size);
        let mut avalue = Vec::with_capacity(size);
        for (row_indices, factors) in m.columns {
            aindex.extend_from_slice(&row_indices);
            avalue.extend_from_slice(&factors);
            astart.push(aindex.len().try_into().unwrap());
        }
        Self {
            astart,
            aindex,
            avalue,
        }
    }
}


#[allow(clippy::float_cmp)]
#[test]
fn test_conversion() {
    use crate::status::HighsModelStatus::Optimal;
    use crate::{ColProblem, Model, RowProblem, Sense};
    let inf = f64::INFINITY;
    let neg_inf = f64::NEG_INFINITY;
    let mut p = RowProblem::default();
    let x = p.add_column(1., -1..2);
    let y = p.add_column(9., 4f64..inf);
    p.add_row(-999f64..inf, &[(x, 666.), (y, 777.)]);
    p.add_row(neg_inf..8880f64, &[(y, 888.)]);
    assert_eq!(
        p,
        RowProblem {
            colcost: vec![1., 9.],
            collower: vec![-1., 4.],
            colupper: vec![2., inf],
            rowlower: vec![-999., neg_inf],
            rowupper: vec![inf, 8880.],
            matrix: RowMatrix {
                columns: vec![(vec![0], vec![666.]), (vec![0, 1], vec![777., 888.]),],
            },
        }
    );
    let colpb = ColProblem::from(p.clone());
    assert_eq!(
        colpb,
        ColProblem {
            colcost: vec![1., 9.],
            collower: vec![-1., 4.],
            colupper: vec![2., inf],
            rowlower: vec![-999., neg_inf],
            rowupper: vec![inf, 8880.],
            matrix: ColMatrix {
                astart: vec![0, 1, 3],
                aindex: vec![0, 0, 1],
                avalue: vec![666., 777., 888.],
            },
        }
    );
    let mut m = Model::new(p);
    m.set_sense(Sense::Maximise);
    let solved = m.solve();
    assert_eq!(solved.status(), Optimal);
    assert_eq!(solved.get_solution().columns(), &[2., 10.]);
}

impl From<Problem<RowMatrix>> for Problem<ColMatrix> {
    fn from(pb: Problem<RowMatrix>) -> Problem<ColMatrix> {
        Self {
            colcost: pb.colcost,
            collower: pb.collower,
            colupper: pb.colupper,
            rowlower: pb.rowlower,
            rowupper: pb.rowupper,
            matrix: pb.matrix.into(),
        }
    }
}