示例#1
0
// --------------------------------------------------------------------- //
bool DecompVarPool::isDuplicate(const DecompVarList&     vars,
                                const DecompWaitingCol& wcol)
{
   DecompVarList::const_iterator vi;

   for (vi = vars.begin(); vi != vars.end(); vi++) {
      if ((*vi)->getStrHash() == wcol.getVarPtr()->getStrHash()) {
         return true;
      }
   }

   return false;
}
示例#2
0
// --------------------------------------------------------------------- //
bool DecompVarPool::isDuplicate(const DecompVarList&     vars,
                                const DecompWaitingCol& wcol)
{
   DecompVarList::const_iterator vi;

   for (vi = vars.begin(); vi != vars.end(); vi++) {
      //TODO: this is very expensive
      //TODO: override DecompWaitingCol operator==
      //printf("\nHERE isDup");
      if ((*vi)->isEquivalent(*wcol.getVarPtr())) { //checks if s is equivalent
         return true;
      }
   }

   return false;
}
示例#3
0
/**
 * generateInitVars callback
 *
 * Called by DIP, we interface with Python
 */
int DippyDecompApp::generateInitVars(DecompVarList& initVars)
{
   if (!m_pyInitVars) {
      return 0;
   }

   PyObject* pVarList = PyObject_CallMethod(m_pProb, "generateInitVars", NULL);

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

   if (pVarList == Py_None)
      // method exists, but is not implemented, return 0
   {
      return 0;
   }

   int nVars = PyObject_Length(pVarList);
   // generateInitVars returns 2-tuples (index, (cost, dictionary of (variable, value) pairs))
   // We can use these to construct a C++ DecompVar objects

   for (int i = 0; i < nVars; i++) {
      PyObject* pTuple = PyList_GetItem(pVarList, i);
      int whichBlock = m_relaxIndices[PyTuple_GetItem(pTuple, 0)];
      PyObject* pVarTuple = PyTuple_GetItem(pTuple, 1);
      double cost   = PyFloat_AsDouble(PyTuple_GetItem(pVarTuple, 0));
      PyObject* pColDict = PyTuple_GetItem(pVarTuple, 1);
      int*     varInds = NULL;
      double* varVals = NULL;
      DecompVarType varType; 
      
      int numPairs = pyColDict_AsPackedArrays(pColDict, m_colIndices, &varInds, &varVals, varType);
      assert(numPairs == PyObject_Length(pColDict));
      DecompVar* var =  new DecompVar(numPairs, varInds, varVals, cost, varType);
      var->setBlockId(whichBlock);
      initVars.push_back(var);
   }

   return nVars;
}
示例#4
0
int OSDipApp::generateInitVars(DecompVarList & initVars) {

	//---
	//--- generateInitVars is a virtual method and can be overriden
	//---   if the user has some idea how to initialize the list of 
	//---   initial variables (columns in the DW master)
	//---
	std::cout << "GENERATE INIT VARS" << std::endl;
	UtilPrintFuncBegin(m_osLog, m_classTag, "generateInitVars()",
			m_appParam.LogLevel, 2);

	//---
	//--- Get the initial solution from the OSOption object
	//--- we want the variable other option where name="initialCol"
	//---

	//std::vector<OtherVariableOption*> getOtherVariableOptions(std::string solver_name); 
	std::vector<OtherVariableOption*> otherVarOptions;
	std::vector<OtherVariableOption*>::iterator vit;
	int *index = NULL;
	double *value = NULL;
	int i;
	double objValue;
	int whichBlock;
	DecompVar *var;

	try{
		if (m_osInterface.m_osoption != NULL
				&& m_osInterface.m_osoption->getNumberOfOtherVariableOptions() > 0) {
			std::cout << "Number of other variable options = "
					<< m_osInterface.m_osoption->getNumberOfOtherVariableOptions()
					<< std::endl;
			otherVarOptions = m_osInterface.m_osoption->getOtherVariableOptions(
					"Dip");
			//iterate over the vector
	
			for (vit = otherVarOptions.begin(); vit != otherVarOptions.end(); vit++) {
	
				// see if we have an initialCol option
	
				if ((*vit)->name.compare("initialCol") == 0) {
	
					index = new int[(*vit)->numberOfVar];
					value = new double[(*vit)->numberOfVar];
	
					objValue = 0.0;
	
					for (i = 0; i < (*vit)->numberOfVar; i++) {
	
						index[i] = (*vit)->var[i]->idx;
	
						//convert the string to integer
						value[ i] = atoi((*vit)->var[i]->value.c_str());
						objValue += m_objective[index[i]];
	
					}
					
					whichBlock = atoi( (*vit)->value.c_str() );
					var = new DecompVar((*vit)->numberOfVar, index, value, objValue);
					var->setBlockId(whichBlock);
					initVars.push_back(var);
					
					//free local memory
	 				UTIL_DELARR( index);
	 				UTIL_DELARR( value);
					
	
				}
	
			}
	
		}
	
	}//end try
	catch(const ErrorClass& eclass){
		
		throw ErrorClass( eclass.errormsg);
		
	} 

	UtilPrintFuncEnd(m_osLog, m_classTag, "generateInitVars()",
			m_appParam.LogLevel, 2);
	return static_cast<int> (initVars.size());
}
示例#5
0
/**
 * solveRelaxed callback
 *
 * This is called by DIP. This function interfaces with Python to
 * call the user defined function if it's present
 *
 * We're expected to populate varList (basically a vector) and
 * return the status of the subproblem solver.
 */
DecompSolverStatus DippyDecompApp::solveRelaxed(const int whichBlock,
						const double* redCostX, 
						const double convexDual, 
						DecompVarList& varList)
{
   if (!m_pySolveRelaxed) {
      return DecompSolStatNoSolution;
   }

   PyObject* pRelaxKey = PyList_GetItem(m_relaxedKeys, whichBlock);
   PyObject* pRedCostList = pyTupleList_FromDoubleArray(redCostX, m_colList);
   PyObject* pConvexDual = PyFloat_FromDouble(convexDual);
   // call solveRelaxed on DipProblem

   PyObject* pStatandVarList = PyObject_CallMethod(m_pProb, "solveRelaxed", "OOd", 
					             pRelaxKey,
					             pRedCostList,
					             pConvexDual);

   Py_DECREF(pRedCostList);
   Py_DECREF(pConvexDual);

   if ( (pStatandVarList == NULL) || (pStatandVarList == Py_None) ){
      throw UtilException("Error calling method prob.solveRelaxed()", "solveRelaxed", "DippyDecompApp");
   }

   // [status, varList] = relaxed_solver(...)
   PyObject * pStatus = PyTuple_GetItem(pStatandVarList, 0);

   int cStatus = PyInt_AsLong(pStatus);

   DecompSolverStatus status = (DecompSolverStatus)cStatus;

   PyObject * pVarList = PyTuple_GetItem(pStatandVarList, 1);

   int nVars = PyObject_Length(pVarList);

   // In the new design, we need to allow the possibility that the user will solve
   // the problem exactly, but not find any solutions with reduced costs zero
   // The below is is commented out and left in the source for posterity
   // tkr 11/11/15
   //if (nVars == 0) {
   //   throw UtilException("Empty variable list", "solveRelaxed", "DippyDecompApp");
   //}

   // solveRelaxed returns 3-tuples (cost, reduced cost, dictionary of (variable, value) pairs)
   // We can use these to construct a C++ DecompVar objects
   double cost, rc;
   PyObject *pDict, *pKeys, *pCol;
   string name;
   double value;

   for (int j = 0; j < nVars; j++) {
      PyObject* pTuple = PySequence_GetItem(pVarList, j);
      cost   = PyFloat_AsDouble(PyTuple_GetItem(pTuple, 0));
      rc     = PyFloat_AsDouble(PyTuple_GetItem(pTuple, 1));

      pDict = PyTuple_GetItem(pTuple, 2);
      pKeys = PyDict_Keys(pDict);
      vector<int>    varInds;
      vector<double> varVals;

      for (int n = 0; n < PyObject_Length(pDict); n++) {
         pCol  = PyList_GetItem(pKeys, n);
         value = PyFloat_AsDouble(PyDict_GetItem(pDict, pCol));
         varInds.push_back(m_colIndices[pCol]);
         varVals.push_back(value);
      }

      Py_DECREF(pKeys);
      Py_DECREF(pTuple);

      DecompVar* var =  new DecompVar(varInds, varVals, rc, cost);
      var->setBlockId(whichBlock);
      varList.push_back(var);
   }

   Py_DECREF(pStatandVarList);

   return status;
}