gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Faster add row operation (#203) One virtual function call instead of more
0 13 0
default
13 files changed with 137 insertions and 4 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -89,16 +89,28 @@
89 89
    return copylp;
90 90
  }
91 91

	
92 92
  int CbcMip::_addRow() {
93 93
    _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
94 94
    return _prob->numberRows() - 1;
95 95
  }
96 96

	
97
  int CbcMip::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
98
    std::vector<int> indexes;
99
    std::vector<Value> values;
100

	
101
    for(ExprIterator it = b; it != e; ++it) {
102
      indexes.push_back(it->first);
103
      values.push_back(it->second);
104
    }
105

	
106
    _prob->addRow(values.size(), &indexes.front(), &values.front(), l, u);
107
    return _prob->numberRows() - 1;
108
  }
97 109

	
98 110
  void CbcMip::_eraseCol(int i) {
99 111
    _prob->deleteColumn(i);
100 112
  }
101 113

	
102 114
  void CbcMip::_eraseRow(int i) {
103 115
    _prob->deleteRow(i);
104 116
  }
Ignore white space 6 line context
... ...
@@ -57,16 +57,17 @@
57 57
    virtual CbcMip* cloneSolver() const;
58 58

	
59 59
  protected:
60 60

	
61 61
    virtual const char* _solverName() const;
62 62

	
63 63
    virtual int _addCol();
64 64
    virtual int _addRow();
65
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
65 66

	
66 67
    virtual void _eraseCol(int i);
67 68
    virtual void _eraseRow(int i);
68 69

	
69 70
    virtual void _eraseColId(int i);
70 71
    virtual void _eraseRowId(int i);
71 72

	
72 73
    virtual void _getColName(int col, std::string& name) const;
Ignore white space 6 line context
... ...
@@ -73,16 +73,29 @@
73 73
    return _prob->numberColumns() - 1;
74 74
  }
75 75

	
76 76
  int ClpLp::_addRow() {
77 77
    _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
78 78
    return _prob->numberRows() - 1;
79 79
  }
80 80

	
81
  int ClpLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
82
    std::vector<int> indexes;
83
    std::vector<Value> values;
84

	
85
    for(ExprIterator it = b; it != e; ++it) {
86
      indexes.push_back(it->first);
87
      values.push_back(it->second);
88
    }
89

	
90
    _prob->addRow(values.size(), &indexes.front(), &values.front(), l, u);
91
    return _prob->numberRows() - 1;
92
  }
93

	
81 94

	
82 95
  void ClpLp::_eraseCol(int c) {
83 96
    _col_names_ref.erase(_prob->getColumnName(c));
84 97
    _prob->deleteColumns(1, &c);
85 98
  }
86 99

	
87 100
  void ClpLp::_eraseRow(int r) {
88 101
    _row_names_ref.erase(_prob->getRowName(r));
Ignore white space 16 line context
... ...
@@ -70,16 +70,17 @@
70 70
    void _clear_temporals();
71 71

	
72 72
  protected:
73 73

	
74 74
    virtual const char* _solverName() const;
75 75

	
76 76
    virtual int _addCol();
77 77
    virtual int _addRow();
78
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
78 79

	
79 80
    virtual void _eraseCol(int i);
80 81
    virtual void _eraseRow(int i);
81 82

	
82 83
    virtual void _eraseColId(int i);
83 84
    virtual void _eraseRowId(int i);
84 85

	
85 86
    virtual void _getColName(int col, std::string& name) const;
Ignore white space 6 line context
... ...
@@ -106,16 +106,49 @@
106 106
  int CplexBase::_addRow() {
107 107
    int i = CPXgetnumrows(cplexEnv(), _prob);
108 108
    const double ub = INF;
109 109
    const char s = 'L';
110 110
    CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
111 111
    return i;
112 112
  }
113 113

	
114
  int CplexBase::_addRow(Value lb, ExprIterator b, 
115
                         ExprIterator e, Value ub) {
116
    int i = CPXgetnumrows(cplexEnv(), _prob);
117
    if (lb == -INF) {
118
      const char s = 'L';
119
      CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
120
    } else if (ub == INF) {
121
      const char s = 'G';
122
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
123
    } else if (lb == ub){
124
      const char s = 'E';
125
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
126
    } else {
127
      const char s = 'R';
128
      double len = ub - lb;
129
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, &len, 0);
130
    }
131

	
132
    std::vector<int> indices;
133
    std::vector<int> rowlist;
134
    std::vector<Value> values;
135

	
136
    for(ExprIterator it=b; it!=e; ++it) {
137
      indices.push_back(it->first);
138
      values.push_back(it->second);
139
      rowlist.push_back(i);
140
    }
141

	
142
    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
143
                   &rowlist.front(), &indices.front(), &values.front());
144

	
145
    return i;
146
  }
114 147

	
115 148
  void CplexBase::_eraseCol(int i) {
116 149
    CPXdelcols(cplexEnv(), _prob, i, i);
117 150
  }
118 151

	
119 152
  void CplexBase::_eraseRow(int i) {
120 153
    CPXdelrows(cplexEnv(), _prob, i, i);
121 154
  }
Ignore white space 6 line context
... ...
@@ -88,16 +88,17 @@
88 88

	
89 89
    CplexBase();
90 90
    CplexBase(const CplexEnv&);
91 91
    CplexBase(const CplexBase &);
92 92
    virtual ~CplexBase();
93 93

	
94 94
    virtual int _addCol();
95 95
    virtual int _addRow();
96
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
96 97

	
97 98
    virtual void _eraseCol(int i);
98 99
    virtual void _eraseRow(int i);
99 100

	
100 101
    virtual void _eraseColId(int i);
101 102
    virtual void _eraseRowId(int i);
102 103

	
103 104
    virtual void _getColName(int col, std::string& name) const;
Ignore white space 6 line context
... ...
@@ -54,16 +54,52 @@
54 54
  }
55 55

	
56 56
  int GlpkBase::_addRow() {
57 57
    int i = glp_add_rows(lp, 1);
58 58
    glp_set_row_bnds(lp, i, GLP_FR, 0.0, 0.0);
59 59
    return i;
60 60
  }
61 61

	
62
  int GlpkBase::_addRow(Value lo, ExprIterator b, 
63
                        ExprIterator e, Value up) {
64
    int i = glp_add_rows(lp, 1);
65

	
66
    if (lo == -INF) {
67
      if (up == INF) {
68
        glp_set_row_bnds(lp, i, GLP_FR, lo, up);
69
      } else {
70
        glp_set_row_bnds(lp, i, GLP_UP, lo, up);
71
      }    
72
    } else {
73
      if (up == INF) {
74
        glp_set_row_bnds(lp, i, GLP_LO, lo, up);
75
      } else if (lo != up) {        
76
        glp_set_row_bnds(lp, i, GLP_DB, lo, up);
77
      } else {
78
        glp_set_row_bnds(lp, i, GLP_FX, lo, up);
79
      }
80
    }
81

	
82
    std::vector<int> indexes;
83
    std::vector<Value> values;
84

	
85
    indexes.push_back(0);
86
    values.push_back(0);
87

	
88
    for(ExprIterator it = b; it != e; ++it) {
89
      indexes.push_back(it->first);
90
      values.push_back(it->second);
91
    }
92

	
93
    glp_set_mat_row(lp, i, values.size() - 1,
94
                    &indexes.front(), &values.front());
95
    return i;
96
  }
97

	
62 98
  void GlpkBase::_eraseCol(int i) {
63 99
    int ca[2];
64 100
    ca[1] = i;
65 101
    glp_del_cols(lp, 1, ca);
66 102
  }
67 103

	
68 104
  void GlpkBase::_eraseRow(int i) {
69 105
    int ra[2];
Ignore white space 6 line context
... ...
@@ -49,16 +49,17 @@
49 49
    GlpkBase();
50 50
    GlpkBase(const GlpkBase&);
51 51
    virtual ~GlpkBase();
52 52

	
53 53
  protected:
54 54

	
55 55
    virtual int _addCol();
56 56
    virtual int _addRow();
57
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
57 58

	
58 59
    virtual void _eraseCol(int i);
59 60
    virtual void _eraseRow(int i);
60 61

	
61 62
    virtual void _eraseColId(int i);
62 63
    virtual void _eraseRowId(int i);
63 64

	
64 65
    virtual void _getColName(int col, std::string& name) const;
Ignore white space 6 line context
... ...
@@ -938,16 +938,24 @@
938 938
    virtual int _addRowId(int row) { return rows.addIndex(row); }
939 939

	
940 940
    virtual void _eraseColId(int col) { cols.eraseIndex(col); }
941 941
    virtual void _eraseRowId(int row) { rows.eraseIndex(row); }
942 942

	
943 943
    virtual int _addCol() = 0;
944 944
    virtual int _addRow() = 0;
945 945

	
946
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
947
      int row = _addRow();
948
      _setRowCoeffs(row, b, e);
949
      _setRowLowerBound(row, l);
950
      _setRowUpperBound(row, u);
951
      return row;
952
    }
953

	
946 954
    virtual void _eraseCol(int col) = 0;
947 955
    virtual void _eraseRow(int row) = 0;
948 956

	
949 957
    virtual void _getColName(int col, std::string& name) const = 0;
950 958
    virtual void _setColName(int col, const std::string& name) = 0;
951 959
    virtual int _colByName(const std::string& name) const = 0;
952 960

	
953 961
    virtual void _getRowName(int row, std::string& name) const = 0;
... ...
@@ -1202,28 +1210,34 @@
1202 1210

	
1203 1211
    ///Add a new row (i.e a new constraint) to the LP
1204 1212

	
1205 1213
    ///\param l is the lower bound (-\ref INF means no bound)
1206 1214
    ///\param e is a linear expression (see \ref Expr)
1207 1215
    ///\param u is the upper bound (\ref INF means no bound)
1208 1216
    ///\return The created row.
1209 1217
    Row addRow(Value l,const Expr &e, Value u) {
1210
      Row r=addRow();
1211
      row(r,l,e,u);
1218
      Row r;
1219
      e.simplify();
1220
      r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), cols),
1221
                                ExprIterator(e.comps.end(), cols), u - *e));
1212 1222
      return r;
1213 1223
    }
1214 1224

	
1215 1225
    ///Add a new row (i.e a new constraint) to the LP
1216 1226

	
1217 1227
    ///\param c is a linear expression (see \ref Constr)
1218 1228
    ///\return The created row.
1219 1229
    Row addRow(const Constr &c) {
1220
      Row r=addRow();
1221
      row(r,c);
1230
      Row r;
1231
      c.expr().simplify();
1232
      r._id = _addRowId(_addRow(c.lowerBounded()?c.lowerBound():-INF, 
1233
                                ExprIterator(c.expr().comps.begin(), cols),
1234
                                ExprIterator(c.expr().comps.end(), cols),
1235
                                c.upperBounded()?c.upperBound():INF));
1222 1236
      return r;
1223 1237
    }
1224 1238
    ///Erase a column (i.e a variable) from the LP
1225 1239

	
1226 1240
    ///\param c is the column to be deleted
1227 1241
    void erase(Col c) {
1228 1242
      _eraseCol(cols(id(c)));
1229 1243
      _eraseColId(cols(id(c)));
Ignore white space 6 line context
... ...
@@ -27,16 +27,21 @@
27 27
    return ++col_num;
28 28
  }
29 29

	
30 30
  int SkeletonSolverBase::_addRow()
31 31
  {
32 32
    return ++row_num;
33 33
  }
34 34

	
35
  int SkeletonSolverBase::_addRow(Value, ExprIterator, ExprIterator, Value)
36
  {
37
    return ++row_num;
38
  }
39

	
35 40
  void SkeletonSolverBase::_eraseCol(int) {}
36 41
  void SkeletonSolverBase::_eraseRow(int) {}
37 42

	
38 43
  void SkeletonSolverBase::_getColName(int, std::string &) const {}
39 44
  void SkeletonSolverBase::_setColName(int, const std::string &) {}
40 45
  int SkeletonSolverBase::_colByName(const std::string&) const { return -1; }
41 46

	
42 47
  void SkeletonSolverBase::_getRowName(int, std::string &) const {}
Ignore white space 6 line context
... ...
@@ -40,16 +40,18 @@
40 40
    SkeletonSolverBase()
41 41
      : col_num(-1), row_num(-1) {}
42 42

	
43 43
    /// \e
44 44
    virtual int _addCol();
45 45
    /// \e
46 46
    virtual int _addRow();
47 47
    /// \e
48
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
49
    /// \e
48 50
    virtual void _eraseCol(int i);
49 51
    /// \e
50 52
    virtual void _eraseRow(int i);
51 53

	
52 54
    /// \e
53 55
    virtual void _getColName(int col, std::string& name) const;
54 56
    /// \e
55 57
    virtual void _setColName(int col, const std::string& name);
Ignore white space 6 line context
... ...
@@ -86,16 +86,29 @@
86 86
    r.setRhs(soplex::infinity);
87 87
    soplex->addRow(r);
88 88

	
89 89
    _row_names.push_back(std::string());
90 90

	
91 91
    return soplex->nRows() - 1;
92 92
  }
93 93

	
94
  int SoplexLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
95
    soplex::DSVector v;
96
    for (ExprIterator it = b; it != e; ++it) {
97
      v.add(it->first, it->second);
98
    }
99
    soplex::LPRow r(l, v, u);
100
    soplex->addRow(r);
101

	
102
    _row_names.push_back(std::string());
103

	
104
    return soplex->nRows() - 1;
105
  }
106

	
94 107

	
95 108
  void SoplexLp::_eraseCol(int i) {
96 109
    soplex->removeCol(i);
97 110
    _col_names_ref.erase(_col_names[i]);
98 111
    _col_names[i] = _col_names.back();
99 112
    _col_names_ref[_col_names.back()] = i;
100 113
    _col_names.pop_back();
101 114
  }
Ignore white space 6 line context
... ...
@@ -79,16 +79,17 @@
79 79
    virtual SoplexLp* cloneSolver() const;
80 80

	
81 81
  protected:
82 82

	
83 83
    virtual const char* _solverName() const;
84 84

	
85 85
    virtual int _addCol();
86 86
    virtual int _addRow();
87
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
87 88

	
88 89
    virtual void _eraseCol(int i);
89 90
    virtual void _eraseRow(int i);
90 91

	
91 92
    virtual void _eraseColId(int i);
92 93
    virtual void _eraseRowId(int i);
93 94

	
94 95
    virtual void _getColName(int col, std::string& name) const;
0 comments (0 inline)