Ejemplo n.º 1
0
/*--------------------------------------------------------------------------*/
int TSP_DecompApp::generateCutsSubtour(DecompCutList & newCuts){

   UtilPrintFuncBegin(m_osLog, m_classTag,
		      "generateCutsSubtour()", m_param.LogDebugLevel, 2);
   
   //TODO: use access methods
   TSP_Concorde     & tspConcorde      = m_tsp.m_concorde;

   vector<ConcordeSubtourCut> subtourCuts;

   int c;
   int n_subtour  = tspConcorde.generateCutsSubtour(subtourCuts);
   int n_prevcuts = static_cast<int>(newCuts.size());
   
   for(c = 0; c < n_subtour; c++){
      vector<int>  & S   = subtourCuts[c].S;
      vector<bool> & inS = subtourCuts[c].inS;

      if(S.size() >= 2){
	 TSP_SubtourCut * sec_cut = new TSP_SubtourCut(inS, S, m_infinity);
	 
	 UTIL_DEBUG(m_param.LogDebugLevel, 3,
		    sec_cut->print(m_infinity);
		    );
	 newCuts.push_back(sec_cut);	 
      }
Ejemplo n.º 2
0
int DippyDecompApp::generateCuts(const double* x, DecompCutList& cutList)
{
   if (!m_pyGenerateCuts) {
      return 0;
   }

   // PyObject *pSolutionList = pyTupleList_FromDoubleArray(x, m_colList);
   // MO (28/2/2012) - Don't need this anymore as solution is contained within node
   PyObject* pPackagedNode = pyTupleList_FromNode(getDecompAlgo(), STAT_FEASIBLE);
   PyObject* pCutList = PyObject_CallMethod(m_pProb, "generateCuts", "O", pPackagedNode);

   if (pCutList == NULL) {
      throw UtilException("Error calling method prob.generateCuts()", "generateCuts", "DippyDecompApp");
   }

   // This should never happen, pyGenerateCuts should be set to false in dippy.py
   if (pCutList == Py_None)
      // method exists, but is not implemented, return 0
   {
      return 0;
   }

   // generateCuts returns constraints, i.e., dictionary of (variable, value) pairs also with name, lb, ub
   const int len = PyObject_Length(pCutList);
   // loop through each cut
   // We can use these to construct a C++ DecompVar objects
   double lb, ub;
   PyObject *pLb, *pUb;
   string name;
   double value;

   for (int i = 0; i < len; i++) {
      PyObject* pRow = PySequence_GetItem(pCutList, i);
      pLb = PyObject_CallMethod(pRow, "getLb", NULL);

      if (pLb == NULL) {
         throw UtilException("Error calling method row.getLb()", "generateCuts", "DippyDecompApp");
      }

      pUb = PyObject_CallMethod(pRow, "getUb", NULL);

      if (pUb == NULL) {
         throw UtilException("Error calling method row.getUb()", "generateCuts", "DippyDecompApp");
      }

      lb = (pLb == Py_None) ? -m_infinity : PyFloat_AsDouble(pLb);
      ub = (pUb == Py_None) ?  m_infinity : PyFloat_AsDouble(pUb);
      int*     varInds = NULL;
      double* varVals = NULL;
      int numPairs = pyColDict_AsPackedArrays(pRow, m_colIndices, &varInds, &varVals);
      assert(numPairs == PyObject_Length(pRow));
      // arrays are now owned by the Cut object
      DippyDecompCut* cut =  new DippyDecompCut(lb, ub, numPairs, varInds, varVals);
      cutList.push_back(cut);
   }

   return len;
}