Skip to content

Commit

Permalink
Independent values can be suggested for concurrent edits
Browse files Browse the repository at this point in the history
Such that multiple input devices can be used to attempt edits on the
same set of variables.
  • Loading branch information
arximboldi committed Feb 18, 2015
1 parent 8da0baf commit 989361f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
17 changes: 17 additions & 0 deletions rhea/simplex_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ simplex_solver& simplex_solver::suggest_value(const variable& v, double x)
return *this;
}

simplex_solver& simplex_solver::suggest_value(const constraint& c, double x)
{
if (!c.is_edit_constraint()) {
throw edit_misuse();
}
auto& e = c.as<edit_constraint>();
auto ei = std::find(edit_info_list_.rbegin(), edit_info_list_.rend(), c);
if (ei == edit_info_list_.rend())
throw edit_misuse(e.var());

double delta{x - ei->prev_constant};
ei->prev_constant = x;
delta_edit_constant(delta, ei->plus, ei->minus);

return *this;
}

simplex_solver& simplex_solver::suggest(const variable& v, double x)
{
add_edit_var(v);
Expand Down
6 changes: 6 additions & 0 deletions rhea/simplex_solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class simplex_solver : public solver, public tableau
* after resolve() or end_edit() has been called. */
simplex_solver& suggest_value(const variable& v, double x);

/** Suggest a new value for an edit constraint.
* The constraint needs to be an edit constraint and needs to
* have been added before. The tableau will not be solved
* completely until resolve() or end_edit() has been called. */
simplex_solver& suggest_value(const constraint& v, double x);

/** Suggest a new value for a variables.
* This function calls add_edit_variable(), begin_edit(), and
* end_edit() as well.
Expand Down
32 changes: 32 additions & 0 deletions unit_tests/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,35 @@ BOOST_AUTO_TEST_CASE(contains_constraint)
solver.remove_constraint(c);
BOOST_CHECK(!solver.contains_constraint(c));
}

BOOST_AUTO_TEST_CASE(independent_values_can_be_suggested_for_concurrent_edits)
{
simplex_solver s;
variable v;
constraint e1 = std::make_shared<edit_constraint>(v);
constraint e2 = std::make_shared<edit_constraint>(v, strength::medium());

s.add_constraint(e1);
s.add_constraint(e2);

s.suggest_value(e1, 42);
s.suggest_value(e2, 21);
s.resolve();
BOOST_CHECK_EQUAL(v.value(), 42);

// Other edit becomes visible after highest priority one is
// removed from the solver
s.remove_constraint(e1);
BOOST_CHECK_EQUAL(v.value(), 21);

// Edits are only remembered while the constraint is active
s.add_constraint(e1);
BOOST_CHECK_EQUAL(v.value(), 21);

// Multiple edits respect strength changes
s.suggest_value(e1, 50);
s.resolve();
BOOST_CHECK_EQUAL(v.value(), 50);
s.change_strength(e1, strength::weak());
BOOST_CHECK_EQUAL(v.value(), 21);
}

0 comments on commit 989361f

Please sign in to comment.