-
Notifications
You must be signed in to change notification settings - Fork 3
Add new c'tors to LinExpr #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cd33ef1
Add new c'tors to LinExpr
hedtke e5c8c3d
Test new c'tors
hedtke 857973b
Add missing header for GCC
hedtke 2b29f3d
Update changelog
hedtke 098d0bf
Increase coverage
hedtke 392ca74
Increase coverage
hedtke 268ac6b
Increase coverage
hedtke cc0472d
Exclude assertions
hedtke 1e03318
Enhance wording in docu
hedtke 7bc469f
Enhance test to show the impact of the different c'tors
hedtke 5861442
Test internals of LinExpr
hedtke f9678e5
Update to Windows 2022
hedtke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #include <boost/test/unit_test.hpp> | ||
|
|
||
| #include <algorithm> | ||
|
|
||
| #include "scippp/model.hpp" | ||
| #include "scippp/solving_statistics.hpp" | ||
|
|
||
| using namespace scippp; | ||
| using namespace std; | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(LinExpression) | ||
erogelio18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| BOOST_AUTO_TEST_CASE(AddOneVar) | ||
| { | ||
| Model model("Simple"); | ||
| array coeff { 1, 1 }; | ||
erogelio18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const auto& [x1, x2] = model.addVars<2>("x_", coeff); | ||
| LinExpr l; | ||
| l += x1; // allocates memory, thus slow | ||
| l += x2; // allocates memory again, thus slow | ||
| model.addConstr(l <= 1, "capacity"); | ||
| model.setObjsense(Sense::MAXIMIZE); | ||
| model.solve(); | ||
| BOOST_TEST(model.getSolvingStatistic(statistics::PRIMALBOUND) == 1); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(CtorArray) | ||
| { | ||
| array objCoeff { 1.0, 1.0 }; | ||
|
|
||
| Model m1("m1"); | ||
| const auto VARS1 = m1.addVars<2>("x_", objCoeff); | ||
| LinExpr l1(VARS1); | ||
| m1.addConstr(l1 <= 1, "capacity"); | ||
| m1.setObjsense(Sense::MAXIMIZE); | ||
| m1.solve(); | ||
| BOOST_TEST(m1.getSolvingStatistic(statistics::PRIMALBOUND) == 1.0); | ||
|
|
||
| Model m2("m2"); | ||
| const auto VARS2 = m2.addVars<2>("x_", objCoeff); | ||
| LinExpr l2(VARS2, objCoeff); // same as before, but different c'tor | ||
| m2.addConstr(l2 <= 1, "capacity"); | ||
| m2.setObjsense(Sense::MAXIMIZE); | ||
| m2.solve(); | ||
| BOOST_TEST(m2.getSolvingStatistic(statistics::PRIMALBOUND) == 1.0); | ||
|
|
||
| const double FAC { 2.0 }; | ||
| array constrCoeff { FAC, FAC }; | ||
|
|
||
| Model m3("m3"); | ||
| const auto VARS3 = m3.addVars<2>("x_", objCoeff); | ||
| LinExpr l3(VARS3, constrCoeff); | ||
| m3.addConstr(l3 <= 1, "capacity"); | ||
| m3.setObjsense(Sense::MAXIMIZE); | ||
| m3.solve(); | ||
| BOOST_TEST(m3.getSolvingStatistic(statistics::PRIMALBOUND) == 1.0 / FAC); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(AddArray) | ||
| { | ||
| Model model("Simple"); | ||
| array coeff { 1, 1 }; | ||
| const auto VARS = model.addVars<2>("x_", coeff); | ||
| LinExpr l; | ||
| l += VARS; | ||
| model.addConstr(l <= 1, "capacity"); | ||
| model.setObjsense(Sense::MAXIMIZE); | ||
| model.solve(); | ||
| BOOST_TEST(model.getSolvingStatistic(statistics::PRIMALBOUND) == 1); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(AddVector) | ||
| { | ||
| Model model("Simple"); | ||
| array coeff { 1, 1 }; | ||
| const auto A_VARS { model.addVars<2>("x_", coeff) }; | ||
| const vector<Var> VARS { A_VARS.begin(), A_VARS.end() }; | ||
| LinExpr l1; | ||
| l1 += VARS; | ||
| model.addConstr(l1 <= 1, "capacity1"); | ||
| LinExpr l2(VARS, vector { 2.0, 2.0 }); | ||
| model.addConstr(l2 <= 2, "capacity2"); // duplicate, but different c'tor | ||
| model.setObjsense(Sense::MAXIMIZE); | ||
| model.solve(); | ||
| BOOST_TEST(model.getSolvingStatistic(statistics::PRIMALBOUND) == 1); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(AddInitializerList) | ||
| { | ||
| Model model("Simple"); | ||
| array coeff { 1, 1 }; | ||
| const auto& [x1, x2] = model.addVars<2>("x_", coeff); | ||
| LinExpr l; | ||
| l += { x1, x2 }; | ||
| model.addConstr(l <= 1, "capacity"); | ||
| // duplicate, but different c'tor: | ||
| model.addConstr(LinExpr({ x1, x2 }, { 2.0, 2.0 }) <= 2, "capacity2"); | ||
| model.setObjsense(Sense::MAXIMIZE); | ||
| model.solve(); | ||
| BOOST_TEST(model.getSolvingStatistic(statistics::PRIMALBOUND) == 1); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(CheckInternalsUsingFriendStruct) | ||
| { | ||
| const double VAL { 2.0 }; | ||
| for (size_t num { 1 }; num < 4; ++num) { | ||
| Model m("m"); | ||
| const auto VARS = m.addVars("x", num); | ||
|
|
||
| LinExpr lDef(VARS); | ||
| BOOST_TEST(lDef.getConstant() == 0); | ||
| BOOST_TEST(lDef.m_constant == 0); | ||
| BOOST_TEST(lDef.m_vars.size() == num); | ||
| BOOST_TEST(lDef.m_coeffs.size() == num); | ||
| BOOST_TEST(all_of(lDef.m_coeffs.begin(), lDef.m_coeffs.end(), [](double d) { return d == 1; })); | ||
|
|
||
| const vector COEFF(num, VAL); | ||
| LinExpr lCoeff(VARS, COEFF); | ||
| BOOST_TEST(lCoeff.getConstant() == 0); | ||
| BOOST_TEST(lCoeff.m_constant == 0); | ||
| BOOST_TEST(lCoeff.m_vars.size() == num); | ||
| BOOST_TEST(lCoeff.m_coeffs.size() == num); | ||
| BOOST_TEST(all_of(lCoeff.m_coeffs.begin(), lCoeff.m_coeffs.end(), [&VAL](double d) { return d == VAL; })); | ||
| } | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.