Permalink
Browse files

Ability to write CPLEX's, Gurobi's, or SCIP's representation of the p…

…roblem to LP and MPS files.
  • Loading branch information...
1 parent 32102dd commit e43368858e40ae342144dfa69f1eeddc54349d8a @9thbit 9thbit committed Jan 20, 2014
@@ -154,6 +154,14 @@ bool CPLEXSolver::is_unsat() {
return optimstatus == IloAlgorithm::Infeasible;
}
+void CPLEXSolver::output_lp(const char *filename) {
+ cplex->exportModel(filename);
+}
+
+void CPLEXSolver::output_mps(const char *filename) {
+ cplex->exportModel(filename);
+}
+
void CPLEXSolver::printStatistics() {
std::cout << "\td Time: " << getTime() << "\tNodes:" << getNodes() << std::endl;
}
@@ -40,6 +40,8 @@ class CPLEXSolver : public MipWrapperSolver {
bool is_opt();
bool is_sat();
bool is_unsat();
+ void output_lp(const char *filename);
+ void output_mps(const char *filename);
void printStatistics();
int getNodes();
double getTime();
@@ -157,6 +157,14 @@ bool GurobiSolver::is_opt(){
return optimstatus == GRB_OPTIMAL;
}
+void GurobiSolver::output_lp(const char *filename){
+ model->write(filename);
+}
+
+void GurobiSolver::output_mps(const char *filename){
+ model->write(filename);
+}
+
void GurobiSolver::printStatistics(){
std::cout << "\td Time: " << getTime() << "\tNodes:" << getNodes() << std::endl;
}
@@ -43,6 +43,8 @@ class GurobiSolver : public MipWrapperSolver{
bool is_sat();
bool is_unsat();
bool is_opt();
+ void output_lp(const char *filename);
+ void output_mps(const char *filename);
void printStatistics();
double getTime();
double getOptimalityGap();
@@ -135,6 +135,36 @@ bool SCIPSolver::is_opt(){
return SCIPgetStatus(_scip) == SCIP_STATUS_OPTIMAL;
}
+void SCIPSolver::output_orig_problem(const char *filename){
+ // It's only safe to call SCIPwriteOrigProblem in certain cases:
+ switch(SCIPgetStage(_scip)){
+ case SCIP_STAGE_PROBLEM:
+ case SCIP_STAGE_TRANSFORMING:
+ case SCIP_STAGE_TRANSFORMED:
+ case SCIP_STAGE_INITPRESOLVE:
+ case SCIP_STAGE_PRESOLVING:
+ case SCIP_STAGE_EXITPRESOLVE:
+ case SCIP_STAGE_PRESOLVED:
+ case SCIP_STAGE_INITSOLVE:
+ case SCIP_STAGE_SOLVING:
+ case SCIP_STAGE_SOLVED:
+ case SCIP_STAGE_EXITSOLVE:
+ case SCIP_STAGE_FREETRANS:
+ SCIPwriteOrigProblem(_scip, filename, NULL, true);
+ break;
+ default:
+ std::cerr << "Error: SCIP is not at a stage where it can output the original problem." << std::endl;
+ }
+}
+
+void SCIPSolver::output_lp(const char *filename){
+ output_orig_problem(filename);
+}
+
+void SCIPSolver::output_mps(const char *filename){
+ output_orig_problem(filename);
+}
+
void SCIPSolver::printStatistics(){
std::cout << "\td Time: " << getTime() << "\tNodes:" << getNodes()
<< std::endl;
@@ -21,6 +21,7 @@ class SCIPSolver : public MipWrapperSolver{
int _verbosity;
SCIP* get_scip();
void add_in_constraint(LinearConstraint *con, double coef=0);
+ void output_orig_problem(const char *filename);
bool has_been_added;
public:
@@ -45,6 +46,8 @@ class SCIPSolver : public MipWrapperSolver{
bool is_sat();
bool is_unsat();
bool is_opt();
+ void output_lp(const char *filename);
+ void output_mps(const char *filename);
void printStatistics();
double getTime();
double getOptimalityGap();
View
@@ -3411,6 +3411,22 @@ def output_cnf(self, filename):
raise UnsupportedSolverFunction(str(type(self)), "output_cnf", "Please load the model using a SAT solver to use this functionality.")
self.solver.output_cnf(filename)
+ def output_lp(self, filename):
+ if hasattr(self.solver, 'output_lp'):
+ if not filename.endswith(".lp"):
+ filname = "%s.lp" % filename
+ self.solver.output_lp(filename)
+ else:
+ raise UnsupportedSolverFunction(str(type(self)), "output_lp", "This solver does not support outputing LP files.")
+
+ def output_mps(self, filename):
+ if hasattr(self.solver, 'output_mps'):
+ if not filename.endswith(".mps"):
+ filname = "%s.mps" % filename
+ self.solver.output_mps(filename)
+ else:
+ raise UnsupportedSolverFunction(str(type(self)), "output_mps", "This solver does not support outputing MPS files.")
+
def num_vars(self):
return self.solver.num_vars()

0 comments on commit e433688

Please sign in to comment.