Skip to content

Commit

Permalink
Implement force budget option.
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-martins committed Nov 9, 2023
1 parent dd01298 commit 1512d26
Show file tree
Hide file tree
Showing 20 changed files with 781 additions and 886 deletions.
4 changes: 2 additions & 2 deletions ad3qp/ad3/Factor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ FactorBUDGET::SolveMAP(const vector<double>& variable_log_potentials,
sum = 0.0;
for (size_t k = 0; k < GetBudget(); ++k) {
valaux = -scores[k].first;
if (valaux < 0.0)
if (valaux < 0.0 && !ForcedBudget())
break;
int f = scores[k].second;
(*variable_posteriors)[f] = negated_[f] ? 0.0 : 1.0;
Expand Down Expand Up @@ -1045,7 +1045,7 @@ FactorBUDGET::SolveQP(const vector<double>& variable_log_potentials,
double budget = GetBudget();
tight_ = false;

if (s > budget) {
if (s > budget || ForcedBudget()) {
tight_ = true;

FlipNegatedForQP(variable_log_potentials, variable_posteriors);
Expand Down
7 changes: 7 additions & 0 deletions ad3qp/ad3/Factor.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ class FactorBUDGET : public Factor
size_t GetBudget() { return budget_; }
void SetBudget(size_t budget) { budget_ = budget; }

// Get/set forced budget (false means can be <=, true means must be =).
bool ForcedBudget() { return forced_budget_; }
void ForceBudget(bool forced) { forced_budget_ = forced; }

// Add evidence information to the factor.
int AddEvidence(vector<bool>* active_links,
vector<int>* evidence,
Expand Down Expand Up @@ -602,6 +606,9 @@ class FactorBUDGET : public Factor
vector<double>& out_add);

private:
// Forced budget (false means can be <=, true means must be =).
bool forced_budget_;

// Budget value.
size_t budget_;

Expand Down
6 changes: 5 additions & 1 deletion ad3qp/ad3/FactorGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,24 @@ class FactorGraph
// Create a new BUDGET factor.
Factor* CreateFactorBUDGET(const vector<BinaryVariable*>& variables,
int budget,
bool force_budget = false,
bool owned_by_graph = true)
{
vector<bool> negated;
return CreateFactorBUDGET(variables, negated, budget, owned_by_graph);
return CreateFactorBUDGET(variables, negated, budget, force_budget,
owned_by_graph);
}

Factor* CreateFactorBUDGET(const vector<BinaryVariable*>& variables,
const vector<bool>& negated,
int budget,
bool force_budget = false,
bool owned_by_graph = true)
{
Factor* factor = new FactorBUDGET;
DeclareFactor(factor, variables, negated, owned_by_graph);
static_cast<FactorBUDGET*>(factor)->SetBudget(budget);
static_cast<FactorBUDGET*>(factor)->ForceBudget(force_budget);
return factor;
}

Expand Down
60 changes: 17 additions & 43 deletions lpsmap/ad3ext/sequence.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 17 additions & 43 deletions lpsmap/ad3ext/sequencebudget.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1512d26

Please sign in to comment.