OptSolutionData* CPLEXRunSolver(int ProbType) { OptSolutionData* NewSolution = NULL; int Status = 0; if (ProbType == LP) { Status = CPXsetintparam (CPLEXenv, CPX_PARAM_LPMETHOD, CPX_ALG_AUTOMATIC); if (Status) { FErrorFile() << "Failed to set the optimization method." << endl; FlushErrorFile(); return NULL; } Status = CPXsetintparam (CPLEXenv, CPX_PARAM_SIMDISPLAY, 0); if (Status) { FErrorFile() << "Failed to set the optimization method." << endl; FlushErrorFile(); return NULL; } Status = CPXchgprobtype(CPLEXenv, CPLEXModel, CPXPROB_LP); Status = CPXlpopt(CPLEXenv, CPLEXModel); } else if(ProbType == MILP || ProbType == MIQP) { //Setting the bound tightening on high Status = CPXsetintparam (CPLEXenv, CPX_PARAM_BNDSTRENIND, 1); if (Status) { FErrorFile() << "Failed to set the optimization method." << endl; FlushErrorFile(); return NULL; } //Setting tolerance to 1e-9 instead of 1e-6 double tolerance = atof(GetParameter("Solver tolerance").data()); Status = CPXsetdblparam(CPLEXenv,CPX_PARAM_EPRHS, 1e-9); if (Status) { FErrorFile() << "Failed to set the optimization method." << endl; FlushErrorFile(); return NULL; } Status = CPXsetdblparam(CPLEXenv,CPX_PARAM_EPINT, 1e-9); if (Status) { FErrorFile() << "Failed to set the optimization method." << endl; FlushErrorFile(); return NULL; } //Deactivates all messages from MIP solver if (ProbType == MIQP) { Status = CPXchgprobtype(CPLEXenv, CPLEXModel, CPXPROB_MIQP); } else { Status = CPXchgprobtype(CPLEXenv, CPLEXModel, CPXPROB_MILP); } Status = CPXmipopt (CPLEXenv, CPLEXModel); } else if(ProbType == QP) { Status = CPXchgprobtype(CPLEXenv, CPLEXModel, CPXPROB_QP); Status = CPXqpopt (CPLEXenv, CPLEXModel); } if (Status ) { cout << "Failed to optimize LP." << endl; return NULL; } int Temp = CPXgetstat (CPLEXenv, CPLEXModel); NewSolution = new OptSolutionData; if (Temp == CPX_STAT_UNBOUNDED) { cout << "Model is unbounded" << endl; FErrorFile() << "Model is unbounded" << endl; FlushErrorFile(); NewSolution->Status = UNBOUNDED; return NewSolution; } else if (Temp == CPX_STAT_INFEASIBLE) { cout << "Model is infeasible" << endl; FErrorFile() << "Model is infeasible" << endl; FlushErrorFile(); NewSolution->Status = INFEASIBLE; return NewSolution; } else if (Temp == CPX_STAT_INForUNBD ) { cout << "Model is infeasible or unbounded" << endl; FErrorFile() << "Model is infeasible or unbounded" << endl; FlushErrorFile(); NewSolution->Status = INFEASIBLE; return NewSolution; } else { NewSolution->Status = SUCCESS; } int NumberColumns = CPXgetnumcols (CPLEXenv, CPLEXModel); int NumberRows = CPXgetnumrows (CPLEXenv, CPLEXModel); NewSolution->NumVariables = NumberColumns; NewSolution->SolutionData.resize(NumberColumns); double* x = new double[NumberColumns]; if (ProbType == MILP || ProbType == MIQP) { Status = CPXgetmipobjval (CPLEXenv, CPLEXModel, &(NewSolution->Objective)); Status = CPXgetmipx (CPLEXenv, CPLEXModel, x, 0, NumberColumns-1); } else { Status = CPXsolution(CPLEXenv,CPLEXModel,NULL,&(NewSolution->Objective),x,NULL,NULL,NULL); } if ( Status ) { cout << "Failed to obtain objective value." << endl; delete [] x; NewSolution->Status = INFEASIBLE; return NewSolution; } cout << "Objective value: " << NewSolution->Objective << endl; /* string* StrNames = new string[NumberColumns]; char** Names = new char*[NumberColumns]; char* NameStore = new char[7*NumberColumns]; int Surplus = 0; Status = CPXgetcolname(CPLEXenv, CPLEXModel, Names, NameStore, 7*NumberColumns, &Surplus, 0, NumberColumns-1); if (Status) { FErrorFile() << "Failed to get column names." << endl; FlushErrorFile(); delete [] StrNames; delete [] Names; delete [] NameStore; delete [] x; delete NewSolution; return NULL; } */ for (int i=0; i < NumberColumns; i++) { //StrNames[i].assign(Names[i]); //StrNames[i] = StrNames[i].substr(1, StrNames[i].length()-1); //NewSolution->SolutionData[atoi(StrNames[i].data())-1] = x[i]; NewSolution->SolutionData[i] = x[i]; } /* delete [] StrNames; delete [] Names; delete [] NameStore; */ delete [] x; return NewSolution; }
int CPLEXLoadVariables(MFAVariable* InVariable, bool RelaxIntegerVariables,bool UseTightBounds) { int Status = 0; //First I check the number of columns. If it's larger than the index, then this variable already exists and is only being changed int NumberColumns = CPXgetnumcols (CPLEXenv, CPLEXModel); if (NumberColumns <= InVariable->Index) { string StrName = GetMFAVariableName(InVariable); double* LB = new double; LB[0] = InVariable->LowerBound; double* UB = new double; UB[0] = InVariable->UpperBound; if (UseTightBounds) { LB[0] = InVariable->Min; UB[0] = InVariable->Max; } double* Obj = new double; Obj[0] = 0; char* Temp = new char; char** Name = new char*; Name[0] = new char[StrName.length()+1]; strcpy(Name[0],StrName.data()); if (InVariable->Binary && !RelaxIntegerVariables) { Temp[0] = CPX_BINARY; Status = CPXnewcols (CPLEXenv, CPLEXModel, 1, Obj, LB, UB, Temp, Name); } else if (InVariable->Integer && !RelaxIntegerVariables) { Temp[0] = CPX_INTEGER; Status = CPXnewcols (CPLEXenv, CPLEXModel, 1, Obj, LB, UB, Temp, Name); } else { Temp[0] = CPX_CONTINUOUS; Status = CPXnewcols (CPLEXenv, CPLEXModel, 1, Obj, LB, UB, Temp, Name); } delete LB; delete UB; delete Obj; delete Temp; delete [] Name[0]; delete Name; if (Status ) { FErrorFile() << "Could not add variable " << InVariable->Index << endl; FlushErrorFile(); return FAIL; } } else { double* Bounds = new double[2]; Bounds[0] = InVariable->LowerBound; Bounds[1] = InVariable->UpperBound; int* Indices = new int[2]; Indices[0] = InVariable->Index; Indices[1] = InVariable->Index; Status = CPXchgbds (CPLEXenv, CPLEXModel, 2, Indices, "LU", Bounds); delete [] Bounds; delete [] Indices; if (Status) { FErrorFile() << "Could not change bounds on variable " << InVariable->Index << endl; FlushErrorFile(); return FAIL; } } return SUCCESS; }
OptSolutionData* GLPKRunSolver(int ProbType) { OptSolutionData* NewSolution = NULL; int NumVariables = lpx_get_num_cols(GLPKModel); int Status = 0; if (ProbType == MILP) { Status = lpx_simplex(GLPKModel); if (Status != LPX_E_OK) { FErrorFile() << "Failed to optimize problem." << endl; FlushErrorFile(); return NULL; } Status = lpx_integer(GLPKModel); if (Status != LPX_E_OK) { FErrorFile() << "Failed to optimize problem." << endl; FlushErrorFile(); return NULL; } NewSolution = new OptSolutionData; Status = lpx_mip_status(GLPKModel); if (Status == LPX_I_UNDEF || Status == LPX_I_NOFEAS) { NewSolution->Status = INFEASIBLE; return NewSolution; } else if (Status == LPX_I_FEAS) { NewSolution->Status = UNBOUNDED; return NewSolution; } else if (Status == LPX_I_OPT) { NewSolution->Status = SUCCESS; } else { delete NewSolution; FErrorFile() << "Problem status unrecognized." << endl; FlushErrorFile(); return NULL; } NewSolution->Objective = lpx_mip_obj_val(GLPKModel); NewSolution->SolutionData.resize(NumVariables); for (int i=0; i < NumVariables; i++) { NewSolution->SolutionData[i] = lpx_mip_col_val(GLPKModel, i+1); } } else if (ProbType == LP) { //First we check the basis matrix to ensure it is not sigular if (lpx_warm_up(GLPKModel) != LPX_E_OK) { lpx_adv_basis(GLPKModel); } Status = lpx_simplex(GLPKModel); if (Status == LPX_E_FAULT) { Status = lpx_warm_up(GLPKModel); if (Status == LPX_E_BADB) { /* the basis is invalid; build some valid basis */ lpx_adv_basis(GLPKModel); Status = lpx_simplex(GLPKModel); } } if (Status != LPX_E_OK) { FErrorFile() << "Failed to optimize problem." << endl; FlushErrorFile(); return NULL; } NewSolution = new OptSolutionData; Status = lpx_get_status(GLPKModel); if (Status == LPX_INFEAS || Status == LPX_NOFEAS || Status == LPX_UNDEF) { cout << "Model is infeasible" << endl; FErrorFile() << "Model is infeasible" << endl; FlushErrorFile(); NewSolution->Status = INFEASIBLE; return NewSolution; } else if (Status == LPX_FEAS || Status == LPX_UNBND) { cout << "Model is unbounded" << endl; FErrorFile() << "Model is unbounded" << endl; FlushErrorFile(); NewSolution->Status = UNBOUNDED; return NewSolution; } else if (Status == LPX_OPT) { NewSolution->Status = SUCCESS; } else { delete NewSolution; FErrorFile() << "Problem status unrecognized." << endl; FlushErrorFile(); return NULL; } NewSolution->Objective = lpx_get_obj_val(GLPKModel); NewSolution->SolutionData.resize(NumVariables); for (int i=0; i < NumVariables; i++) { NewSolution->SolutionData[i] = lpx_get_col_prim(GLPKModel, i+1); } } else { FErrorFile() << "Optimization problem type cannot be handled by GLPK solver." << endl; FlushErrorFile(); return NULL; } return NewSolution; }