Skip to content

Commit

Permalink
Allow unconstrained variables to be edited
Browse files Browse the repository at this point in the history
  • Loading branch information
arximboldi committed Jan 6, 2015
1 parent ee2f1ef commit 1cb41a0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rhea/simplex_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ solver& simplex_solver::add_constraint_(const constraint& c)
if (c.is_edit_constraint()) {
auto& ec = c.as<edit_constraint>();
const auto& v = ec.var();
if (!v.is_external() || (!is_basic_var(v) && !columns_has_key(v)))
if (!v.is_external())
throw edit_misuse(v);

auto i(std::find(edit_info_list_.begin(), edit_info_list_.end(), v));
Expand Down
43 changes: 42 additions & 1 deletion unit_tests/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ BOOST_AUTO_TEST_CASE(simple2_test)

simplex_solver solver;

BOOST_CHECK_THROW((solver.add_edit_var(x), solver.begin_edit(),
BOOST_CHECK_THROW((solver.begin_edit(),
solver.suggest_value(x, 100), solver.end_edit()),
edit_misuse);

Expand Down Expand Up @@ -871,3 +871,44 @@ BOOST_AUTO_TEST_CASE(change_weight_test) // issue 33
solver.change_weight(c1, 3);
BOOST_CHECK_EQUAL(x.value(), 1);
}

BOOST_AUTO_TEST_CASE(edit_unconstrained_variable)
{
auto v = variable{ 0 };
auto solver = simplex_solver{};

solver.add_edit_var(v);
BOOST_CHECK_EQUAL(v.value(), 0);
BOOST_CHECK(solver.is_valid());

solver.suggest_value(v, 2);
solver.resolve();
BOOST_CHECK_EQUAL(v.value(), 2);
BOOST_CHECK(solver.is_valid());
}

BOOST_AUTO_TEST_CASE(add_constraints_after_marking_edit_variable)
{
auto v = variable{ 0 };
auto solver = simplex_solver{};

solver.add_edit_var(v);
solver.suggest_value(v, 2);
solver.resolve();
BOOST_CHECK_EQUAL(v.value(), 2);
BOOST_CHECK(solver.is_valid());

// Constraint overrides users desire
auto fixed = constraint { v == 42 };
solver.add_constraint(fixed);
BOOST_CHECK_EQUAL(v.value(), 42);
solver.suggest_value(v, 3);
solver.resolve();
BOOST_CHECK_EQUAL(v.value(), 42);
BOOST_CHECK(solver.is_valid());

// Goes back to last edited value
solver.remove_constraint(fixed);
BOOST_CHECK_EQUAL(v.value(), 3);
BOOST_CHECK(solver.is_valid());
}

0 comments on commit 1cb41a0

Please sign in to comment.