lemon/cplex.cc
changeset 1177 3c00344f49c9
parent 1092 dceba191c00d
     1.1 --- a/lemon/cplex.cc	Mon Jul 16 16:21:40 2018 +0200
     1.2 +++ b/lemon/cplex.cc	Wed Oct 17 19:14:07 2018 +0200
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2010
     1.8 + * Copyright (C) 2003-2013
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -37,36 +37,54 @@
    1.13      }
    1.14    }
    1.15  
    1.16 +  void CplexEnv::incCnt()
    1.17 +  {
    1.18 +    _cnt_lock->lock();
    1.19 +    ++(*_cnt);
    1.20 +    _cnt_lock->unlock();
    1.21 +  }
    1.22 +
    1.23 +  void CplexEnv::decCnt()
    1.24 +  {
    1.25 +    _cnt_lock->lock();
    1.26 +    --(*_cnt);
    1.27 +    if (*_cnt == 0) {
    1.28 +      delete _cnt;
    1.29 +      _cnt_lock->unlock();
    1.30 +      delete _cnt_lock;
    1.31 +      CPXcloseCPLEX(&_env);
    1.32 +    }
    1.33 +    else _cnt_lock->unlock();
    1.34 +  }
    1.35 +  
    1.36    CplexEnv::CplexEnv() {
    1.37      int status;
    1.38 +    _env = CPXopenCPLEX(&status);
    1.39 +    if (_env == 0)
    1.40 +      throw LicenseError(status);
    1.41      _cnt = new int;
    1.42 -    _env = CPXopenCPLEX(&status);
    1.43 -    if (_env == 0) {
    1.44 -      delete _cnt;
    1.45 -      _cnt = 0;
    1.46 -      throw LicenseError(status);
    1.47 -    }
    1.48 +    (*_cnt) = 1;
    1.49 +    _cnt_lock = new bits::Lock;
    1.50    }
    1.51  
    1.52    CplexEnv::CplexEnv(const CplexEnv& other) {
    1.53      _env = other._env;
    1.54      _cnt = other._cnt;
    1.55 -    ++(*_cnt);
    1.56 +    _cnt_lock = other._cnt_lock;
    1.57 +    incCnt();
    1.58    }
    1.59  
    1.60    CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
    1.61 +    decCnt();
    1.62      _env = other._env;
    1.63      _cnt = other._cnt;
    1.64 -    ++(*_cnt);
    1.65 +    _cnt_lock = other._cnt_lock;
    1.66 +    incCnt();
    1.67      return *this;
    1.68    }
    1.69  
    1.70    CplexEnv::~CplexEnv() {
    1.71 -    --(*_cnt);
    1.72 -    if (*_cnt == 0) {
    1.73 -      delete _cnt;
    1.74 -      CPXcloseCPLEX(&_env);
    1.75 -    }
    1.76 +    decCnt();
    1.77    }
    1.78  
    1.79    CplexBase::CplexBase() : LpBase() {
    1.80 @@ -491,6 +509,17 @@
    1.81                     _message_enabled ? CPX_ON : CPX_OFF);
    1.82    }
    1.83  
    1.84 +  void CplexBase::_write(std::string file, std::string format) const
    1.85 +  {
    1.86 +    if(format == "MPS" || format == "LP")
    1.87 +      CPXwriteprob(cplexEnv(), cplexLp(), file.c_str(), format.c_str());
    1.88 +    else if(format == "SOL")
    1.89 +      CPXsolwrite(cplexEnv(), cplexLp(), file.c_str());
    1.90 +    else throw UnsupportedFormatError(format);
    1.91 +  }
    1.92 +
    1.93 +
    1.94 +
    1.95    // CplexLp members
    1.96  
    1.97    CplexLp::CplexLp()