Skip to content

Commit

Permalink
Option tech:writemodel:index #237 #239
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Aug 27, 2024
1 parent d76f85b commit 17c4522
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGES.mp.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Summary of recent updates to the AMPL MP Library


## unreleased
- Option tech:writemodel:index to choose the iteration
when solver model is exported.
- SCIP (and any solver with linear objective
and non-linear constraints): improve reformulation
of QP objectives.
Expand Down
23 changes: 18 additions & 5 deletions include/mp/backend-std.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class StdBackend :
/// Redefine this if you want some extensions
/// to be written after solving instead.
/// This is for compatibility wiht ASL drivers
/// where 'writeprob' was used for native-format model
/// where 'writeprob' was used both for native-format model
/// and solution output #218.
virtual std::set<std::string> NativeResultExtensions() const
{ return {".sol", ".ilp", ".mst", ".hnt", ".bas", ".json"}; }
Expand Down Expand Up @@ -276,9 +276,11 @@ class StdBackend :
auto get_sol = [this]() {
return GetSolution();
};
int i=0;
int i_solve=0;
while (GetMM().PrepareSolveIteration(get_stt, get_sol)) {
// ExportModel({"/tmp/model" + std::to_string(++i) + ".lp"});
if (++i_solve==storedOptions_.writemodel_index_
&& exportFileMode() > 0)
ExportModel(export_file_names());
Solve();
}
}
Expand Down Expand Up @@ -808,6 +810,7 @@ class StdBackend :

/// For write prob
std::vector<std::string> export_files_;
int writemodel_index_ = 0;
std::vector<std::string> just_export_files_;
/// For write sol
std::vector<std::string> export_sol_files_;
Expand Down Expand Up @@ -936,12 +939,22 @@ class StdBackend :
}

if (IMPL_HAS_STD_FEATURE(WRITE_PROBLEM)) {
AddListOption("tech:writemodel writeprob writemodel tech:exportfile",
AddListOption("tech:writemodel tech:writeprob writeprob writemodel tech:exportfile",
"Specifies files where to export the model before "
"solving (repeat the option for several files.) "
"File name extensions can be ``.lp[.7z]``, ``.mps``, etc.",
"File name extensions can be ``.lp[.7z]``, ``.mps``, etc."
"\n"
"To write a model during iterative solve (e.g., with obj:multi=2), "
"use tech:writemodel:index.",
storedOptions_.export_files_);

AddStoredOption("tech:writemodel:index tech:writeprob:index writeprobindex writemodelindex",
"During iterative solve (e.g., with obj:multi=2), "
"the iteration before which to write solver model. "
"0 means before iteration is initialized; positive value - "
"before solving that iteration. Default 0.",
storedOptions_.writemodel_index_);

AddListOption("tech:writemodelonly justwriteprob justwritemodel",
"Specifies files where to export the model, no solving "
"(option can be repeated.) "
Expand Down
12 changes: 8 additions & 4 deletions include/mp/flat/constr_2_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,15 @@ class Constraints2Expr {
lt_out_vars.reserve(nvars);
result.reserve(ltin.size() - nvars);
int v=0;
double c=0.0;
for (size_t i=0; i<ltin.size(); ++i) {
if (MPCD( IsProperVar(v = ltin.var(i)) )) {
lt_out_vars.add_term(ltin.coef(i), v);
} else
result.add_term(ltin.coef(i), v);
if ((c = ltin.coef(i))) { // non-0
if (MPCD( IsProperVar(v = ltin.var(i)) )) {
lt_out_vars.add_term(c, v);
} else {
result.add_term(c, v);
}
}
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion include/mp/flat/converter_multiobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class MOManager {
const auto& i0_vec = pr_level.second;
obj_new_.push_back(obj_orig.at(i0_vec.front()));
obj_new_.back().set_sense(obj_orig.front().obj_sense()); // "Legacy" obj:multi:weight
obj_new_.back().set_sense_true(obj_orig.front().obj_sense());
obj_new_.back().set_sense_true(obj_orig.front().obj_sense_true());
obj_new_.back().GetLinTerms() *= objwgt.at(i0_vec.front()); // Use weight
obj_new_.back().GetQPTerms() *= objwgt.at(i0_vec.front());
obj_new_tola_.push_back(objtola.at(i0_vec.front()));
Expand Down
11 changes: 3 additions & 8 deletions solvers/scipmp/scipmpbackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ void ScipBackend::SetInterrupter(mp::Interrupter *inter) {
}

void ScipBackend::Solve() {
if (!storedOptions_.exportFile_.empty())
ExportModel(storedOptions_.exportFile_);
if (!storedOptions_.paramRead_.empty())
SCIP_CCALL( SCIPreadParams(getSCIP(), storedOptions_.paramRead_.c_str()) );
if (!storedOptions_.logFile_.empty())
Expand Down Expand Up @@ -368,12 +366,6 @@ void ScipBackend::InitCustomOptions() {
"0*/1/2/3/4/5: Whether to write SCIP log lines (chatter) to stdout and to file (native output level of SCIP).",
"display/verblevel", 0, 5);

AddStoredOption("tech:exportfile writeprob writemodel",
"Specifies the name of a file where to export the model before "
"solving it. This file name can have extension ``.lp``, ``.mps``, etc. "
"Default = \"\" (don't export the model).",
storedOptions_.exportFile_);

AddStoredOption("tech:logfile logfile",
"Log file name.",
storedOptions_.logFile_);
Expand Down Expand Up @@ -963,6 +955,9 @@ void ScipBackend::AddMIPStart(ArrayRef<double> x0, ArrayRef<int> sparsity) {
SCIP_CCALL( SCIPaddSolFree(getSCIP(), &solution, &keep) );
}

void ScipBackend::DoWriteProblem(const std::string& name) {
ExportModel(name);
}

} // namespace mp

Expand Down
8 changes: 7 additions & 1 deletion solvers/scipmp/scipmpbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class ScipBackend :
void AddMIPStart(ArrayRef<double> x0,
ArrayRef<int> sparsity) override;

/**
* EXPORT PROBLEM
**/
ALLOW_STD_FEATURE(WRITE_PROBLEM, true)
void DoWriteProblem(const std::string& name) override;


/**
* Get MIP Gap
Expand Down Expand Up @@ -157,7 +163,7 @@ class ScipBackend :
private:
/// These options are stored in the class
struct Options {
std::string exportFile_, logFile_, paramRead_;
std::string logFile_, paramRead_;
int concurrent_ = 0;
int heuristics_ = 0;
int cuts_ = 0;
Expand Down
14 changes: 14 additions & 0 deletions test/end2end/cases/categorized/fast/multi_obj/modellist.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@
"_sobj[2]": -17
}
},
{
"name" : "obj_abs_01 obj:multi=2 writeprobindex",
"tags" : ["linear", "integer", "multiobj"],
"options": {
"ANYSOLVER_options": "multiobj=2 writeprob=obj_abs_01.lp writeprobindex=2",
"scip_options": "multiobj=2 writeprob=obj_abs_01.cip writeprobindex=2",
"gcg_options": "multiobj=2 writeprob=obj_abs_01.lp writeprobindex=2",
"mosek_options": "multiobj=2 writeprob=obj_abs_01.jtask writeprobindex=2"
},
"values": {
"_sobj[1]": 13,
"_sobj[2]": -17
}
},
{
"name" : "obj_abs_02",
"tags" : ["linear", "integer", "multiobj"],
Expand Down

0 comments on commit 17c4522

Please sign in to comment.