Skip to content

Commit e3dd2ea

Browse files
committed
Adapt BranchingDynamics to use new spans
1 parent be49daf commit e3dd2ea

File tree

5 files changed

+21
-28
lines changed

5 files changed

+21
-28
lines changed

libecole/include/ecole/scip/model.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class Model {
112112

113113
nonstd::span<Var*> variables() const noexcept;
114114
nonstd::span<Var*> lp_branch_cands() const;
115+
nonstd::span<Var*> pseudo_branch_cands() const;
115116
nonstd::span<Col*> lp_columns() const;
116117
nonstd::span<Row*> lp_rows() const;
117118

libecole/src/environment/branching-dynamics.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <memory>
33
#include <utility>
44

5-
#include <nonstd/span.hpp>
65
#include <xtensor/xtensor.hpp>
76

87
#include "ecole/environment/branching-dynamics.hpp"
@@ -17,29 +16,13 @@ namespace environment {
1716
BranchingDynamics::BranchingDynamics(bool pseudo_candidates_) noexcept :
1817
pseudo_candidates(pseudo_candidates_) {}
1918

20-
static nonstd::span<SCIP_VAR*> lp_branch_cands(SCIP* scip) {
21-
SCIP_VAR** cands = nullptr;
22-
int n_cands = 0;
23-
scip::call(SCIPgetLPBranchCands, scip, &cands, nullptr, nullptr, &n_cands, nullptr, nullptr);
24-
assert(n_cands >= 0);
25-
return {cands, static_cast<std::size_t>(n_cands)};
26-
}
27-
28-
static nonstd::span<SCIP_VAR*> pseudo_branch_cands(SCIP* scip) {
29-
SCIP_VAR** cands = nullptr;
30-
int n_cands = 0;
31-
scip::call(SCIPgetPseudoBranchCands, scip, &cands, &n_cands, nullptr);
32-
assert(n_cands >= 0);
33-
return {cands, static_cast<std::size_t>(n_cands)};
34-
}
19+
namespace {
3520

36-
static nonstd::optional<xt::xtensor<std::size_t, 1>>
37-
action_set(scip::Model const& model, bool pseudo) {
21+
nonstd::optional<xt::xtensor<std::size_t, 1>> action_set(scip::Model const& model, bool pseudo) {
3822
if (model.get_stage() != SCIP_STAGE_SOLVING) {
3923
return {};
4024
}
41-
auto const scip = model.get_scip_ptr();
42-
auto const branch_cands = pseudo ? pseudo_branch_cands(scip) : lp_branch_cands(scip);
25+
auto const branch_cands = pseudo ? model.pseudo_branch_cands() : model.lp_branch_cands();
4326
auto branch_cols = xt::xtensor<std::size_t, 1>::from_shape({branch_cands.size()});
4427
std::transform( //
4528
branch_cands.begin(),
@@ -51,6 +34,8 @@ action_set(scip::Model const& model, bool pseudo) {
5134
return branch_cols;
5235
}
5336

37+
} // namespace
38+
5439
auto BranchingDynamics::reset_dynamics(scip::Model& model) -> std::tuple<bool, ActionSet> {
5540
model.solve_iter();
5641
return {model.solve_iter_is_done(), action_set(model, pseudo_candidates)};
@@ -59,10 +44,10 @@ auto BranchingDynamics::reset_dynamics(scip::Model& model) -> std::tuple<bool, A
5944
auto BranchingDynamics::step_dynamics(scip::Model& model, std::size_t const& action)
6045
-> std::tuple<bool, ActionSet> {
6146
auto const lp_cols = model.lp_columns();
62-
if (action >= lp_cols.size) {
47+
if (action >= lp_cols.size()) {
6348
throw Exception("Branching index is larger than the number of columns.");
6449
}
65-
model.solve_iter_branch(lp_cols[action].var());
50+
model.solve_iter_branch(SCIPcolGetVar(lp_cols[action]));
6651

6752
return {model.solve_iter_is_done(), action_set(model, pseudo_candidates)};
6853
}

libecole/src/scip/model.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,20 @@ nonstd::span<Var*> Model::variables() const noexcept {
189189
}
190190

191191
nonstd::span<Var*> Model::lp_branch_cands() const {
192-
int n_vars{};
193-
SCIP_VAR** vars{};
192+
int n_vars = 0;
193+
SCIP_VAR** vars = nullptr;
194194
scip::call(
195195
SCIPgetLPBranchCands, get_scip_ptr(), &vars, nullptr, nullptr, &n_vars, nullptr, nullptr);
196196
return {vars, static_cast<std::size_t>(n_vars)};
197197
}
198198

199+
nonstd::span<Var*> Model::pseudo_branch_cands() const {
200+
int n_vars = 0;
201+
SCIP_VAR** vars = nullptr;
202+
scip::call(SCIPgetPseudoBranchCands, get_scip_ptr(), &vars, &n_vars, nullptr);
203+
return {vars, static_cast<std::size_t>(n_vars)};
204+
}
205+
199206
nonstd::span<Col*> Model::lp_columns() const {
200207
auto const scip_ptr = get_scip_ptr();
201208
if (SCIPgetStage(scip_ptr) != SCIP_STAGE_SOLVING) {

libecole/tests/src/environment/test-branching-dynamics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ TEST_CASE("BranchingDynamics functional tests", "[dyn]") {
3535
REQUIRE(action_set.has_value());
3636
auto const& branch_cands = action_set.value();
3737
REQUIRE(branch_cands.size() > 0);
38-
REQUIRE(branch_cands.size() < model.lp_columns().size);
38+
REQUIRE(branch_cands.size() < model.lp_columns().size());
3939
REQUIRE(xt::all(branch_cands >= 0));
40-
REQUIRE(xt::all(branch_cands < model.lp_columns().size));
40+
REQUIRE(xt::all(branch_cands < model.lp_columns().size()));
4141
REQUIRE(xt::unique(branch_cands).size() == branch_cands.size());
4242
}
4343

@@ -54,7 +54,7 @@ TEST_CASE("BranchingDynamics functional tests", "[dyn]") {
5454
std::tie(done, action_set) = dyn.reset_dynamics(model);
5555
REQUIRE_FALSE(done);
5656
REQUIRE(action_set.has_value());
57-
auto const action = model.lp_columns().size + 1;
57+
auto const action = model.lp_columns().size() + 1;
5858
REQUIRE_THROWS_AS(dyn.step_dynamics(model, action), environment::Exception);
5959
}
6060
}

libecole/tests/src/observation/test-strongbranchingscores.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ TEST_CASE("StrongBranchingScores return correct branchig scores", "[obs]") {
2323

2424
REQUIRE(obs.has_value());
2525
auto const scores = obs.value();
26-
REQUIRE(scores.size() == model.lp_columns().size);
26+
REQUIRE(scores.size() == model.lp_columns().size());
2727
auto const not_nan_scores = xt::filter(scores, !xt::isnan(scores));
2828
REQUIRE(not_nan_scores.size() > 0);
2929
REQUIRE(xt::all(not_nan_scores >= 0));

0 commit comments

Comments
 (0)