COIN-OR::LEMON - Graph Library

Ticket #415: network_simplex.patch

File network_simplex.patch, 6.0 KB (added by Peter Kovacs, 12 years ago)
  • .h

    old new  
    243243
    244244  private:
    245245
     246    // Handle multiplication of costs with _state and _pred_dir
     247    // elements which are in {-1, 0, 1} and {-1, 1} respectively. This
     248    // means the Cost type '*' operator is never used and need not
     249    // exist.
     250    static inline Cost unitMul(signed char state, Cost cost) {
     251      // fastest for integral costs on x86:
     252      // return cost * (int)state;
     253
     254      // fastest for integral costs on machines with predicated execution
     255      // and/or high latency integer multiplies (e.g. arm, ia64):
     256      if(state == 1)
     257        return cost;
     258      else if(state == -1)
     259        return -cost;
     260      else
     261        return 0;
     262    }
     263
    246264    // Implementation of the First Eligible pivot rule
    247265    class FirstEligiblePivotRule
    248266    {
     
    274292      bool findEnteringArc() {
    275293        Cost c;
    276294        for (int e = _next_arc; e != _search_arc_num; ++e) {
    277           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     295          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    278296          if (c < 0) {
    279297            _in_arc = e;
    280298            _next_arc = e + 1;
     
    282300          }
    283301        }
    284302        for (int e = 0; e != _next_arc; ++e) {
    285           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     303          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    286304          if (c < 0) {
    287305            _in_arc = e;
    288306            _next_arc = e + 1;
     
    322340      bool findEnteringArc() {
    323341        Cost c, min = 0;
    324342        for (int e = 0; e != _search_arc_num; ++e) {
    325           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     343          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    326344          if (c < min) {
    327345            min = c;
    328346            _in_arc = e;
     
    376394        int cnt = _block_size;
    377395        int e;
    378396        for (e = _next_arc; e != _search_arc_num; ++e) {
    379           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     397          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    380398          if (c < min) {
    381399            min = c;
    382400            _in_arc = e;
     
    387405          }
    388406        }
    389407        for (e = 0; e != _next_arc; ++e) {
    390           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     408          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    391409          if (c < min) {
    392410            min = c;
    393411            _in_arc = e;
     
    462480          min = 0;
    463481          for (int i = 0; i < _curr_length; ++i) {
    464482            e = _candidates[i];
    465             c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     483            c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    466484            if (c < min) {
    467485              min = c;
    468486              _in_arc = e;
     
    478496        min = 0;
    479497        _curr_length = 0;
    480498        for (e = _next_arc; e != _search_arc_num; ++e) {
    481           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     499          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    482500          if (c < 0) {
    483501            _candidates[_curr_length++] = e;
    484502            if (c < min) {
     
    489507          }
    490508        }
    491509        for (e = 0; e != _next_arc; ++e) {
    492           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     510          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    493511          if (c < 0) {
    494512            _candidates[_curr_length++] = e;
    495513            if (c < min) {
     
    575593        Cost c;
    576594        for (int i = 0; i != _curr_length; ++i) {
    577595          e = _candidates[i];
    578           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     596          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    579597          if (c < 0) {
    580598            _cand_cost[e] = c;
    581599          } else {
     
    588606        int limit = _head_length;
    589607
    590608        for (e = _next_arc; e != _search_arc_num; ++e) {
    591           c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     609          c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    592610          if (c < 0) {
    593611            _cand_cost[e] = c;
    594612            _candidates[_curr_length++] = e;
     
    600618          }
    601619        }
    602620        for (e = 0; e != _next_arc; ++e) {
    603           _cand_cost[e] = _state[e] *
    604             (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
     621          _cand_cost[e] = unitMul(_state[e],
     622            (_cost[e] + _pi[_source[e]] - _pi[_target[e]]));
    605623          if (_cand_cost[e] < 0) {
    606624            _candidates[_curr_length++] = e;
    607625          }
     
    9861004      Number c = 0;
    9871005      for (ArcIt a(_graph); a != INVALID; ++a) {
    9881006        int i = _arc_id[a];
    989         c += Number(_flow[i]) * Number(_cost[i]);
     1007        c += _cost[i] * _flow[i];
    9901008      }
    9911009      return c;
    9921010    }
     
    14491467    // Update potentials in the subtree that has been moved
    14501468    void updatePotential() {
    14511469      Cost sigma = _pi[v_in] - _pi[u_in] -
    1452                    _pred_dir[u_in] * _cost[in_arc];
     1470                   unitMul(_pred_dir[u_in], _cost[in_arc]);
    14531471      int end = _thread[_last_succ[u_in]];
    14541472      for (int u = u_in; u != end; u = _thread[u]) {
    14551473        _pi[u] += sigma;
     
    15361554      // Perform heuristic initial pivots
    15371555      for (int i = 0; i != int(arc_vector.size()); ++i) {
    15381556        in_arc = arc_vector[i];
    1539         if (_state[in_arc] * (_cost[in_arc] + _pi[_source[in_arc]] -
    1540             _pi[_target[in_arc]]) >= 0) continue;
     1557        if (unitMul(_state[in_arc], (_cost[in_arc] + _pi[_source[in_arc]] -
     1558            _pi[_target[in_arc]])) >= 0) continue;
    15411559        findJoinNode();
    15421560        bool change = findLeavingArc();
    15431561        if (delta >= MAX) return false;