Nocte- / rhea

A constraint solver based on Cassowary
MIT License
200 stars 19 forks source link

Adding stay with required strength #25

Closed hfossli closed 9 years ago

hfossli commented 9 years ago

I'm not sure if this is a bug or not. I wrote some unit tests derived from the test required_strength which I expect to pass. Am I missing something?

BOOST_AUTO_TEST_CASE(required_strength_stay)
{
    variable v(0);
    simplex_solver solver;

    solver.add_stay(v, strength::required(), 1);

    BOOST_CHECK_EQUAL(v.value(), 0);

    solver.add_edit_var(v, strength::required(), 2);
    solver.begin_edit();
    solver.suggest_value(v, 2);
    solver.end_edit();

    BOOST_CHECK_EQUAL(v.value(), 2);
}

BOOST_AUTO_TEST_CASE(required_strength_stay_and_constraint)
{
    variable v(0), y(0);
    simplex_solver solver;

    solver.add_stay(v, strength::required(), 2);

    BOOST_CHECK_EQUAL(v.value(), 0);
    BOOST_CHECK_EQUAL(y.value(), 2);

    solver.add_edit_var(v, strength::required(), 3);
    solver.begin_edit();
    solver.suggest_value(v, 2);
    solver.end_edit();

    solver.add_constraint(y == v + 2);

    BOOST_CHECK_EQUAL(v.value(), 2);
    BOOST_CHECK_EQUAL(y.value(), 4);
}
hfossli commented 9 years ago

Test report

Running 39 test cases...
/Users/hfossli/Projects/Rhea/unit_tests/unit_tests.cpp:758: error in "required_strength_stay": check v.value() == 2 failed [0 != 2]
/Users/hfossli/Projects/Rhea/unit_tests/unit_tests.cpp:769: error in "required_strength_stay_and_constraint": check y.value() == 2 failed [0 != 2]
/Users/hfossli/Projects/Rhea/unit_tests/unit_tests.cpp:778: error in "required_strength_stay_and_constraint": check v.value() == 2 failed [0 != 2]
/Users/hfossli/Projects/Rhea/unit_tests/unit_tests.cpp:779: error in "required_strength_stay_and_constraint": check y.value() == 4 failed [2 != 4]

*** 4 failures detected in test suite "rhea"
Nocte- commented 9 years ago

'required' is a special value, the weight factor isn't meaningful here. (Now we know what happens when the immovable object meets the unstoppable force, it appears the stay constraint wins. ;) )

hfossli commented 9 years ago

If I got you right you are saying that the weight factor is the problem. Removing that from the unit test still yields the same output/error.

BOOST_AUTO_TEST_CASE(required_strength_stay)
{
    variable v(0);
    simplex_solver solver;

    solver.add_stay(v, strength::required());

    BOOST_CHECK_EQUAL(v.value(), 0);

    solver.add_edit_var(v);
    solver.begin_edit();
    solver.suggest_value(v, 2);
    solver.end_edit();

    BOOST_CHECK_EQUAL(v.value(), 2);
}

Result

 error in "required_strength_stay": check v.value() == 2 failed [0 != 2]
Nocte- commented 9 years ago

The value remains 0 because the edit constraint is weaker than the stay constraint in this case. (Stays are weak and edits are strong by default.)

hfossli commented 9 years ago

Aha. It's details like this that would be awesome to have documented.

This is quite good, but it too short IMO http://cassowary.readthedocs.org/en/latest/

Nocte- commented 9 years ago

For some reason, people started starring this project left and right in the past couple of days. Maybe this is a good time to get some documentation going. ;)

That Python implementation you linked to explains the concepts of Cassowary very well, that's definitely a good basis.