Skip to content

Commit

Permalink
Expression interface: eliminate implicit vars #237
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Aug 16, 2024
1 parent c490dec commit 45b26ba
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 13 deletions.
8 changes: 8 additions & 0 deletions include/mp/flat/constr_2_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Constraints2Expr {
MPD( GetModel() ).ConvertAllWithExpressions(*(Impl*)this);
stage_cvt2expr_ = 2; // func cons -> explicifiers
MPD( GetModel() ).ConvertAllWithExpressions(*(Impl*)this);
MPD( EliminateExprResultVars() ); // In the very end
}

/// Mark which functional constraints to be used as expressions,
Expand Down Expand Up @@ -216,6 +217,13 @@ class Constraints2Expr {
}
}

/// Mark expr result vars for elimination
void EliminateExprResultVars() {
for (auto i = MPCD(num_vars()); i--; )
if (!MPCD( IsProperVar(i) ))
MPD( MarkVarAsEliminated(i) );
}


protected:
/// Algebraic cons: no marking (when NLConstraint accepted?)
Expand Down
2 changes: 1 addition & 1 deletion include/mp/flat/constr_keeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class ConstraintKeeper final
bool IsUnused() const { return is_unused_; }
/// Mark as unused
void MarkAsUnused() {
MarkAsBridged();
MarkAsBridged(); // also inactive
is_unused_=true;
}

Expand Down
4 changes: 2 additions & 2 deletions include/mp/flat/constr_prop_down.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ class ConstraintPropagatorsDown {
void PropagateResult(AndConstraint& con, double lb, double ub, Context ctx) {
MPD( NarrowVarBounds(con.GetResultVar(), lb, ub) );
con.AddContext(ctx);
MPD( PropagateResult2Vars(con.GetArguments(), lb, 1.0, +ctx) );
MPD( PropagateResult2Vars(con.GetArguments(), lb, 1.0, +ctx) ); // in any ctx??
if (lb>0.5) // Remove, arguments are fixed
MPD( DecrementVarUsage(con.GetResultVar()) );
MPD( DecrementVarUsage(con.GetResultVar()) ); // Or, remove completely?
}

void PropagateResult(OrConstraint& con, double lb, double ub, Context ctx) {
Expand Down
2 changes: 1 addition & 1 deletion include/mp/flat/convert_functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ class BasicFCC {
auto r = int( GetConverter().AddVar(lb(), ub(), type()) );
SetResultVar( r );
GetConstraint().SetResultVar( r );
GetConverter().IncrementVarUsage(r);
}
void AddConstraint() {
GetConverter().AddConstraint( std::move(GetConstraint()) );
GetConverter().IncrementVarUsage(GetResultVar()); // ater adding the con
}
};

Expand Down
15 changes: 10 additions & 5 deletions include/mp/flat/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class FlatConverter :
/// Use "+1" a variable
void IncrementVarUsage(int v) {
++VarUsageRef(v);
assert(!IsUnused(GetInitExpression(v)));
}

/// Unuse result variable.
Expand All @@ -221,14 +222,13 @@ class FlatConverter :
}
}

/// Fix unused defined vars.
/// Mark unused defined vars for elimination.
/// Normally should delete them.
void FixUnusedDefinedVars() {
void EliminateUnusedDefinedVars() {
for (auto i=num_vars(); i--; ) {
if (HasInitExpression(i) &&
! VarUsageRef(i)) {
set_var_lb(i, 0.0); // fix to 0
set_var_ub(i, 0.0);
MPD( MarkVarAsEliminated(i) );
}
}
}
Expand Down Expand Up @@ -535,6 +535,11 @@ class FlatConverter :
ci.GetCK()->MarkAsUnused(ci.GetIndex());
}

/// Is constraint unused?
bool IsUnused(const ConInfo& ci) const {
return ci.GetCK()->IsUnused(ci.GetIndex());
}


protected:
USE_BASE_MAP_FINDERS( BaseConverter )
Expand Down Expand Up @@ -575,7 +580,7 @@ class FlatConverter :
MPD( ConvertModel() );
if (relax())
GetModel().RelaxIntegrality();
FixUnusedDefinedVars(); // Until we have proper var deletion
EliminateUnusedDefinedVars(); // Until we have proper var deletion
CheckLinearCons();
PresolveNames();
GetModel().PushModelTo(GetModelAPI());
Expand Down
29 changes: 26 additions & 3 deletions include/mp/flat/converter_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class FlatModel
AutoExpand(var_result_, v) = false;
}

/// Variable has marking?
/// Variable has expr marking?
bool VarHasMarking(int v) const {
assert(v>=0);
return v < (int)var_result_.size();
Expand All @@ -258,8 +258,12 @@ class FlatModel
/// (Otheriwse, it's marked as implicit
/// - the init expr will be an expression)
bool IsProperVar(int v) const {
assert(VarHasMarking(v));
return var_result_[v];
return !VarHasMarking(v) || var_result_[v];
}

/// Mark var as eliminated.
void MarkVarAsEliminated(int v) {
AutoExpand(var_elim_, v) = true;
}


Expand Down Expand Up @@ -373,6 +377,20 @@ class FlatModel

template <class Backend>
void PushVariablesTo(Backend& backend) const {
/// Fix 'eliminated' variables - no proper deletion
var_lb_subm_ = var_lb_;
var_ub_subm_ = var_ub_;
for (auto i=std::min(var_elim_.size(), var_lb_subm_.size()); i--; ) {
if (var_elim_[i]) {
if (var_lb_subm_[i] > -1e20)
var_ub_subm_[i] = var_lb_subm_[i];
else if (var_ub_subm_[i] < 1e20)
var_lb_subm_[i] = var_ub_subm_[i];
else
var_lb_subm_[i] = var_ub_subm_[i] = 0.0;
}
}
/// Push variables
if (var_names_storage_.size() > 0) {
// Convert names to c-str if needed
for (const std::string& s : var_names_storage_)
Expand Down Expand Up @@ -424,13 +442,18 @@ class FlatModel
private:
/// Variables' bounds
VarBndVec var_lb_, var_ub_;
/// Variables' submitted bounds - what goes to the solver
mutable VarBndVec var_lb_subm_, var_ub_subm_;
/// Variables' types
VarTypeVec var_type_;
/// Whether the variable, being the result variable of a functional constraint,
/// needs to stay a variable (vs being eliminated because the constraint
/// is becoming an expression.)
/// Normal variables are marked too.
std::vector<bool> var_result_;
/// Eliminated variables.
/// Currenlty they are just fixed.
std::vector<bool> var_elim_;
/// Variables' names
mutable VarNameVec var_names_;
std::vector<std::string> var_names_storage_;
Expand Down
1 change: 1 addition & 0 deletions include/mp/utils-vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using SmallVecDefSz = gch::small_vector<T>;

/// Grow vector capacity by a factor if needed.
/// @return reference to the element at \a i.
/// @note better preallocate, or call in the reverse order of indexes.
template <class Vec>
auto AutoExpand(Vec& vec, typename Vec::size_type i) {
if (vec.size()<=i) {
Expand Down
2 changes: 1 addition & 1 deletion solvers/scipmp/scipmpmodelapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ScipModelAPI :
/// 'AcceptedButNotRecommended' would outline each expression
/// with an auxiliary variable.
/// See also per-expression type switches.
ACCEPT_EXPRESSION_INTERFACE(NotAccepted);
ACCEPT_EXPRESSION_INTERFACE(AcceptedButNotRecommended);

/// For each suppoted constraint type, add the ACCEPT_CONSTRAINT macro
/// and the relative AddConstraint function.
Expand Down

0 comments on commit 45b26ba

Please sign in to comment.