| 103 | |
| 104 | |
| 105 | bool LinearProgram::save(const std::string& file_name, bool simple_name /* = false*/) const { |
| 106 | std::ofstream output(file_name.c_str()); |
| 107 | if (output.fail()) { |
| 108 | std::cerr << "could not create/open file to save:\'" << file_name << "\'" << std::endl; |
| 109 | output.close(); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | const std::string& ext = details::extension(file_name); |
| 114 | if (ext != "lp" && ext != "mps" && ext != "cip") { |
| 115 | std::cerr << "unsupported format: \'" << ext << "\'" << std::endl; |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | Scip* scip = 0; |
| 120 | SCIP_CALL(SCIPcreate(&scip)); |
| 121 | SCIP_CALL(SCIPincludeDefaultPlugins(scip)); |
| 122 | |
| 123 | // disable scip output to stdout |
| 124 | SCIPmessagehdlrSetQuiet(SCIPgetMessagehdlr(scip), TRUE); |
| 125 | |
| 126 | // create empty problem |
| 127 | SCIP_CALL(SCIPcreateProbBasic(scip, name_.c_str())); |
| 128 | |
| 129 | // set the objective sense to maximize, default is minimize |
| 130 | // SCIP_CALL(SCIPfreeTransform(scip)); |
| 131 | SCIP_CALL(SCIPsetObjsense(scip, objective_->sense() == LinearObjective::MINIMIZE ? SCIP_OBJSENSE_MINIMIZE : SCIP_OBJSENSE_MAXIMIZE)); |
| 132 | |
| 133 | // create variables |
| 134 | std::vector<SCIP_VAR*> scip_variables; |
| 135 | for (std::size_t i = 0; i < variables_.size(); ++i) { |
| 136 | const Variable* var = variables_[i]; |
| 137 | |
| 138 | std::string name = var->name(); |
| 139 | if (simple_name) |
| 140 | name = "x" + std::to_string(i); |
| 141 | |
| 142 | double lb, ub; |
| 143 | var->get_bounds(lb, ub); |
| 144 | |
| 145 | SCIP_VAR* v = 0; |
| 146 | // SCIP_CALL(SCIPfreeTransform(scip)); |
| 147 | switch (var->variable_type()) |
| 148 | { |
| 149 | case Variable::CONTINUOUS: |
| 150 | SCIP_CALL(SCIPcreateVar(scip, &v, name.c_str(), lb, ub, 0.0, SCIP_VARTYPE_CONTINUOUS, TRUE, FALSE, 0, 0, 0, 0, 0)); |
| 151 | break; |
| 152 | case Variable::INTEGER: |
| 153 | SCIP_CALL(SCIPcreateVar(scip, &v, name.c_str(), lb, ub, 0.0, SCIP_VARTYPE_INTEGER, TRUE, FALSE, 0, 0, 0, 0, 0)); |
| 154 | break; |
| 155 | case Variable::BINARY: |
| 156 | SCIP_CALL(SCIPcreateVar(scip, &v, name.c_str(), 0, 1, 0.0, SCIP_VARTYPE_BINARY, TRUE, FALSE, 0, 0, 0, 0, 0)); |
| 157 | break; |
| 158 | } |
| 159 | // add the SCIP_VAR object to the scip problem |
| 160 | SCIP_CALL(SCIPaddVar(scip, v)); |
| 161 | |
| 162 | // storing the SCIP_VAR pointer for later access |
nothing calls this directly
no test coverage detected