int main(int argC, char* argV[])
{
	WindowsErrorPopupBlocker();
	FileUtil *fileUtil = NULL;  
  	const char dirsep =  CoinFindDirSeparator();
	std::string osilFileNameWithPath;
	std::string osilFileName;
	std::string osil;
	std::string uploadResult;
	std::string actualServer;

	/* Replace this URL as needed */
	std::string defaultServer = "http://128.135.130.17:8080/os/servlet/OSFileUpload";

	try{
		if( argC == 1 || argC > 3 || argV[1] == "-?") 
			throw ErrorClass( "usage: OSFileUpload <filename> [<serverURL>]");
		fileUtil = new FileUtil(); 
		time_t start, finish, tmp;
		osilFileNameWithPath = argV[ 1];
		std::cout << "FILE NAME = " << argV[1] << std::endl;
		std::cout << "Read the file into a string" << std::endl; 
		osil = fileUtil->getFileAsString( osilFileNameWithPath.c_str() ); 
		OSSolverAgent* osagent = NULL;
		if (argC == 2)
			actualServer = defaultServer;
		else
			actualServer = argV[2];
		osagent = new OSSolverAgent(actualServer);

		// strip off just the file name
		// modify to into a file C:filename
		int index = osilFileNameWithPath.find_last_of( dirsep);
		int slength = osilFileNameWithPath.size();
		osilFileName = osilFileNameWithPath.substr( index + 1, slength) ;
		std::cout << std::endl << std::endl;
		std::cout << "Place remote synchronous call" << std::endl;

		start = time( &tmp);
		uploadResult = osagent->fileUpload(osilFileName, osil);
		finish = time( &tmp);
		std::cout << "File Upload took (seconds): "<< difftime(finish, start) << std::endl;
		std::cout << uploadResult << std::endl;

		if(fileUtil != NULL) delete fileUtil;
		return 0;
	}
	catch( const ErrorClass& eclass){
		std::cout << eclass.errormsg <<  std::endl;
		if(fileUtil != NULL) delete fileUtil;
		return 0;
	}
}
Esempio n. 2
0
int main( ){
	WindowsErrorPopupBlocker();
// test OS code samples here
	FileUtil *fileUtil = NULL; 
	fileUtil = new FileUtil();
	const char dirsep =  CoinFindDirSeparator();
 	// Set directory containing mps data files.
 	std::string dataDir;
   dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
	cout << "Start Building the Model" << endl;
	try{
		// get the p0033 problem
		std::string osilFileName;
		osilFileName =  dataDir  +   "p0033.osil";
		std::cout << "Try to read a sample file" << std::endl;
		std::cout << "The file is: " ;
		std::cout <<  osilFileName << std::endl;
		std::string osil = fileUtil->getFileAsString( osilFileName.c_str() );
		OSiLReader *osilreader = NULL;
		osilreader = new OSiLReader(); 
		OSInstance *osinstance;
		osinstance = osilreader->readOSiL( osil);
		// done writing the model
		cout << "Done writing the Model" << endl;
		// now solve the model
		CoinSolver *solver  = NULL;
		solver = new CoinSolver();  
		solver->sSolverName ="cbc"; 
		solver->osinstance = osinstance;
		solver->buildSolverInstance();
		solver->osiSolver->setHintParam(OsiDoReducePrint, true, OsiHintTry);
		solver->osiSolver->initialSolve();
		cout << "Here is the initial objective value "  << solver->osiSolver->getObjValue()  << endl;
		
		CglKnapsackCover cover;
	    CglSimpleRounding round;  
		CglGomory gomory;
		CbcModel *model = new CbcModel( *solver->osiSolver);

		//model->setBestObjectiveValue(4000);
		model->setMaximumNodes(100000);
		//
		model->addCutGenerator(&gomory, 1, "Gomory");
		model->addCutGenerator(&cover, 1, "Cover");
		model->addCutGenerator(&round, 1, "Round");
		model->branchAndBound();	
		// now create a result object
		OSResult *osresult = new OSResult();
		// if we are throw an exception if the problem is nonlinear
		double *x = NULL;
		double *y = NULL;
		double *z = NULL;
		//int i = 0;
		std::string *rcost = NULL;
		// resultHeader infomration
		if(osresult->setServiceName("Solved with Coin Solver: " + solver->sSolverName) != true)
			throw ErrorClass("OSResult error: setServiceName");
		if(osresult->setInstanceName(  solver->osinstance->getInstanceName()) != true)
			throw ErrorClass("OSResult error: setInstanceName");
		if(osresult->setVariableNumber( solver->osinstance->getVariableNumber()) != true)
			throw ErrorClass("OSResult error: setVariableNumer");
		if(osresult->setObjectiveNumber( 1) != true)
			throw ErrorClass("OSResult error: setObjectiveNumber");
		if(osresult->setConstraintNumber( solver->osinstance->getConstraintNumber()) != true)
			throw ErrorClass("OSResult error: setConstraintNumber");
		if(osresult->setSolutionNumber(  1) != true)
			throw ErrorClass("OSResult error: setSolutionNumer");	
		int solIdx = 0;
		std::string description = "";
		osresult->setGeneralStatusType("success");
		std::cout << "PROVEN OPTIMAL " << model->isProvenOptimal() << std::endl;
		int i;
		if (model->isProvenOptimal() == 1){
			osresult->setSolutionStatus(solIdx, "optimal", description);
			/* Retrieve the solution */
			x = new double[solver->osinstance->getVariableNumber() ];
			y = new double[solver->osinstance->getConstraintNumber() ];
			z = new double[1];
			rcost = new std::string[ solver->osinstance->getVariableNumber()];
			//
			*(z + 0)  =  model->getObjValue();
			osresult->setObjectiveValuesDense(solIdx, z);
			for(i=0; i < solver->osinstance->getVariableNumber(); i++){
				*(x + i) = model->getColSolution()[i];
			}
			osresult->setPrimalVariableValuesDense(solIdx, x );
			//if( solver->sSolverName.find( "symphony") == std::string::npos){
			for(i=0; i <  solver->osinstance->getConstraintNumber(); i++){
				*(y + i) = model->getRowPrice()[ i];
			}
			osresult->setDualVariableValuesDense(solIdx, y);
			//
			// now put the reduced costs into the osrl
			int numberOfOtherVariableResult = 1;
			int otherIdx = 0;
			// first set the number of Other Variable Results
			osresult->setNumberOfOtherVariableResults(solIdx, numberOfOtherVariableResult);
			
			std::ostringstream outStr;
			int numberOfVar =  solver->osinstance->getVariableNumber();
			for(i=0; i < numberOfVar; i++){
				outStr << model->getReducedCost()[ i]; 
				rcost[ i] = outStr.str();
				outStr.str("");
			}
			osresult->setAnOtherVariableResultDense(solIdx, otherIdx, "reduced costs", "", "the variable reduced costs",  
				rcost);			
			// end of settiing reduced costs			
		}
		else{ 
			if(solver->osiSolver->isProvenPrimalInfeasible() == true) 
				osresult->setSolutionStatus(solIdx, "infeasible", description);
			else
				if(solver->osiSolver->isProvenDualInfeasible() == true) 
					osresult->setSolutionStatus(solIdx, "dualinfeasible", description);
				else
					osresult->setSolutionStatus(solIdx, "other", description);
		}
		OSrLWriter *osrlwriter = new OSrLWriter();
		std::cout <<  osrlwriter->writeOSrL( osresult) << std::endl;
		if(solver->osinstance->getVariableNumber() > 0){
			delete[] x;
			x = NULL;
		}
		if(solver->osinstance->getConstraintNumber()) delete[] y;
		y = NULL;
		delete[] z;	
		z = NULL;
		if(solver->osinstance->getVariableNumber() > 0){
			delete[] rcost;
			rcost = NULL;
		}
    	// do garbage collection
		delete osresult;
		osresult = NULL;
		delete osrlwriter;
		osrlwriter = NULL;
		delete solver;
		solver = NULL;
		delete osilreader;
		osilreader = NULL;
		delete fileUtil;
		fileUtil  = NULL;
		delete model;
		model = NULL;
		cout << "Done with garbage collection" << endl;
		return 0;
	}
	catch(const ErrorClass& eclass){
		delete fileUtil;
		std::cout << eclass.errormsg <<  std::endl;
		return 0;
	} 
}// end main
OSServiceMethods::OSServiceMethods(OSCommandLine *oscommandline): resultString("") 
{
#ifdef DEBUG_OSSERVICEMETHODS
    cout << "Inside the OSServiceMethods Constructor" << endl;
#endif

    FileUtil   *fileUtil   = NULL;
	OSiLReader *osilreader = NULL;
	OSoLReader *osolreader = NULL;
	OSiLWriter *osilwriter = NULL;
	OSoLWriter *osolwriter = NULL;
	OSmps2osil *mps2osil   = NULL;

#ifdef COIN_HAS_ASL
//    OSnl2os *nl2os = NULL;
    OSnl2osil *nl2osil = NULL;
#endif

#ifdef COIN_HAS_GAMSUTILS
    OSgams2os *gams2os = NULL;
#endif

    try
	{
	    fileUtil = new FileUtil();

		/** Prepare the OSInstance and OSOption objects if needed.
		 *  The objects are needed if
		 *  1. any output is based on the instance ((i.e, printModel, 
		 *     printRow or osilOutputFile is specified) OR
		 *  2. a solve/send command is based on input other than OSiL format
		 */
		if ( (oscommandline->printModel || oscommandline->printRowNumberAsString != "" || oscommandline->osilOutputFile != "") ||
			((oscommandline->serviceMethod[0] = 's') && (oscommandline->osilFile == "")))
		{
		/** Search for an instance in the following order
		 *  1. osil file
		 *  2. non-proprietary formats (only MPS for now)
		 *  3. proprietary formats (AMPL nl, GAMS dat, etc.)
		 */
			if (oscommandline->osilFile != "") 
			{
				osilreader = new OSiLReader();			
	            oscommandline->osil = fileUtil->getFileAsString(
                                  (oscommandline->osilFile).c_str());
				oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
				if (oscommandline->osolFile != "") 
				{
		            oscommandline->osol = fileUtil->getFileAsString(
			                      (oscommandline->osolFile).c_str());
					osolreader = new OSoLReader();			
					oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
				}
			}
		    else if (oscommandline->mpsFile != "") 
			{
                mps2osil = new OSmps2osil(oscommandline->mpsFile);
                mps2osil->createOSInstance();
                oscommandline->osinstance = mps2osil->osinstance;
				if (oscommandline->osolFile != "") 
				{
		            oscommandline->osol = fileUtil->getFileAsString(
			                      (oscommandline->osolFile).c_str());
					osolreader = new OSoLReader();			
					oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
				}
			}
		    else if (oscommandline->nlFile != "")
			{
#ifdef COIN_HAS_ASL
//                nl2os = new OSnl2os(oscommandline);
//                nl2os->createOSObjects();
//                osinstance = nl2os->osinstance;
//                osoption   = nl2os->osoption;
                nl2osil = new OSnl2osil(oscommandline->nlFile);
                nl2osil->createOSInstance();
                oscommandline->osinstance = nl2osil->osinstance;
#else
                throw ErrorClass(
                    "nl file specified locally but ASL not present");
#endif
			}
		    else if (oscommandline->gamsControlFile != "")
			{
#ifdef COIN_HAS_GAMSUTILS
                gams2os = new OSgams2os(oscommandline);
                gams2os->createOSObjects();
                osinstance = gams2os->osinstance;
                osoption   = gams2os->osoption;
#else
                throw ErrorClass(
                    "a Gams Control specified locally but GAMSIP not present");
#endif
			}
			else
			{
				if (oscommandline->osolFile != "" && oscommandline->serviceLocation != "") 
				{
		            oscommandline->osol = fileUtil->getFileAsString(
                                  (oscommandline->osolFile).c_str());
					osolreader = new OSoLReader();			
					oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
					if (oscommandline->solverName != "")
						oscommandline->osoption->setSolverToInvoke(oscommandline->solverName);
				}
                if (oscommandline->osol.find( "<instanceLocation") == std::string::npos)
                    throw ErrorClass(
                        "Error: no optimization instance found");
			}

			// Make sure the solver name is recorded properly
			if (oscommandline->solverName != "") 
			{	
				if (oscommandline->osol == "" && oscommandline->osolFile != "") 
		            oscommandline->osol = fileUtil->getFileAsString(
                                  (oscommandline->osolFile).c_str());
				oscommandline->osoption->setSolverToInvoke(oscommandline->solverName);
			}	

			// convert OS objects to strings if necessary
			if (oscommandline->serviceLocation != "" && oscommandline->osil == "") 
			{	
				osilwriter= new OSiLWriter();
				oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
			}	
			if (oscommandline->serviceLocation != "" && oscommandline->osoption != NULL) 
			{	
				osolwriter= new OSoLWriter();
				oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);
			}	
		}

		// cleanup
	    if (fileUtil != NULL) delete fileUtil;
		fileUtil = NULL;
	    if (osilreader != NULL) delete osilreader;
		osilreader = NULL;
	    if (osolreader != NULL) delete osolreader;
		osolreader = NULL;
	    if (osilwriter != NULL) delete osilwriter;
		osilwriter = NULL;
	    if (osolwriter != NULL) delete osolwriter;
		osolwriter = NULL;
	    if (mps2osil != NULL) delete mps2osil;
		mps2osil = NULL;

#ifdef COIN_HAS_ASL
//	    if (nl2os != NULL) delete nl2os;
//	    nl2os = NULL;
	    if (nl2osil != NULL) delete nl2osil;
	    nl2osil = NULL;
#endif

#ifdef COIN_HAS_GAMSUTILS
	    if (gams2os != NULL) delete gams2os;
		gams2os = NULL;
#endif
	}
	catch(const ErrorClass& eclass)
	{
	    if (fileUtil != NULL) delete fileUtil;
		fileUtil = NULL;
	    if (osilreader != NULL) delete osilreader;
		osilreader = NULL;
	    if (osolreader != NULL) delete osolreader;
		osolreader = NULL;
	    if (osilwriter != NULL) delete osilwriter;
		osilwriter = NULL;
	    if (osolwriter != NULL) delete osolwriter;
		osolwriter = NULL;
	    if (mps2osil != NULL) delete mps2osil;
		mps2osil = NULL;

#ifdef COIN_HAS_ASL
	    if (nl2osil != NULL) delete nl2osil;
	    nl2osil = NULL;
#endif

#ifdef COIN_HAS_GAMSUTILS
	    if (gams2os != NULL) delete gams2os;
		gams2os = NULL;
#endif

		throw ErrorClass(eclass.errormsg);
	}
}//end nonstandard OSServiceMethods constructor