Skip to content

Commit

Permalink
Multiobj emulator: before NL conversion #237 #239
Browse files Browse the repository at this point in the history
So that QP/nonlinearities can be combined before they are made expressions
  • Loading branch information
glebbelov committed Nov 21, 2024
1 parent 140cb61 commit ebc442f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
5 changes: 4 additions & 1 deletion include/mp/flat/constr_2_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ class Constraints2Expr {

/// Convert objectives
void ConvertObjectivesWithExpressions() {
auto& objs = MPD( get_objectives() );
auto& objs_emulated = MPD( get_emulated_objectives() );
auto& objs_original = MPD( get_objectives() );
auto& objs
= objs_emulated.size() ? objs_emulated : objs_original;
for (size_t iobj=0; iobj<objs.size(); ++iobj) {
HandleLogicalArgs(objs[iobj].GetLinTerms(), iobj);
HandleLogicalArgs(objs[iobj].GetQPTerms(), iobj);
Expand Down
6 changes: 3 additions & 3 deletions include/mp/flat/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ class FlatConverter :
// MP_DISPATCH( PreprocessIntermediate() ); // preprocess after each level
constr_depth_ = 1; // Workaround. TODO have maps as special constraints
MP_DISPATCH( ConvertMaps() );
MP_DISPATCH( PreprocessFlatFinal() ); // final flat model prepro
MP_DISPATCH( PreprocessFlatFinal() ); // final flat model prepro
MP_DISPATCH( ConsiderEmulatingMultiobj() ); // Before NL conversion
if constexpr (IfAcceptingNLOutput()) {
if (IfWantNLOutput()) {
MPD( Convert2NL() );
MPD( Convert2NL() ); // Possibly for emulated objectives
MPD( PreprocessNLFinal() );
}
}
MP_DISPATCH( ConsiderEmulatingMultiobj() ); // After NL conversion
} catch (const ConstraintConversionFailure& cff) {
MP_RAISE(cff.message());
}
Expand Down
22 changes: 17 additions & 5 deletions include/mp/flat/converter_multiobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class MOManager {


public:
/// Retrieve emulated objectives, const
const std::vector<QuadraticObjective>&
get_emulated_objectives() const { return obj_new_; }
/// Retrieve emulated objectives
std::vector<QuadraticObjective>&
get_emulated_objectives() { return obj_new_; }

/// Is MOManager active?
/// This is relevant after initialization via
/// ConsiderEmulatingMultiobj().
Expand Down Expand Up @@ -138,24 +145,28 @@ class MOManager {
obj_new_tolr_.reserve(pr_map.size());
for (const auto& pr_level: pr_map) {
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
const auto& obj_orig_1st = obj_orig.at(i0_vec.front());
const auto objwgt_1st = objwgt.at(i0_vec.front());
obj_new_.push_back(obj_orig_1st);
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_true());
obj_new_.back().GetLinTerms() *= objwgt.at(i0_vec.front()); // Use weight
obj_new_.back().GetQPTerms() *= objwgt.at(i0_vec.front());
obj_new_.back().GetLinTerms() *= objwgt_1st; // Use weight
obj_new_.back().GetQPTerms() *= objwgt_1st;
obj_new_tola_.push_back(objtola.at(i0_vec.front()));
obj_new_tolr_.push_back(objtolr.at(i0_vec.front()));
assert (!obj_orig_1st.HasExpr()); // should be before NL conversion
for (auto i0i=i0_vec.size(); --i0i; ) {
// Add next objective with weight and sense factor
double sensef
= (obj_orig.at(i0_vec.front()).obj_sense() == obj_orig.at(i0_vec[i0i]).obj_sense())
= (obj_orig_1st.obj_sense() == obj_orig.at(i0_vec[i0i]).obj_sense())
? 1.0 : -1.0;
auto lt1 = obj_orig.at(i0_vec[i0i]).GetLinTerms();
lt1 *= sensef * objwgt.at(i0_vec[i0i]);
auto qt1 = obj_orig.at(i0_vec[i0i]).GetQPTerms();
qt1 *= sensef * objwgt.at(i0_vec[i0i]);
obj_new_.back().GetLinTerms().add(lt1);
obj_new_.back().GetQPTerms().add(qt1);
assert (!obj_orig.at(i0_vec[i0i]).HasExpr());
// Max the tolerances
obj_new_tola_.back() = std::max(obj_new_tola_.back(), objtola.at(i0_vec[i0i]));
obj_new_tolr_.back() = std::max(obj_new_tolr_.back(), objtolr.at(i0_vec[i0i]));
Expand Down Expand Up @@ -290,6 +301,7 @@ class MOManager {
}
}


private:
MOManagerStatus status_ {MOManagerStatus::NOT_SET};
std::vector<QuadraticObjective> obj_new_; // ranked aggregated objectives
Expand Down
3 changes: 2 additions & 1 deletion include/mp/flat/nl_expr/model_api_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ class BasicExprModelAPI
/// or for the implicit InitExpression().
Expr GetInitExpression(int i_expr) {
/// FlatConverter should have marked all expressions;
assert(i_expr < (int)is_expr_stored_.size());
MP_ASSERT_ALWAYS(i_expr < (int)is_expr_stored_.size(),
"unexpected expression index");
// if (i_expr >= (int)is_expr_stored_.size()) {
// is_expr_stored_.resize(int(i_expr*1.3)+1);
// expr_stored_.resize(int(i_expr*1.3)+1);
Expand Down

0 comments on commit ebc442f

Please sign in to comment.