Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,24 @@ Rank: minor
Throw Search::SpokeNoBrancher when a non-failed PBS spoke has no branchers
after spoke() although the hub had branchers.

[ENTRY]
Module: search
What: new
Rank: minor
[DESCRIPTION]
Add Choice::alternative_failed(), which search engines call when an
alternative of a choice fails, so branchers can learn from failures.
[MORE]
Not all search engines call it; LDS does not. The default does nothing.

[ENTRY]
Module: search
What: bug
Rank: minor
[DESCRIPTION]
Report the alternative actually taken to tracers of parallel DFS and BAB after
work stealing, instead of possibly a later one.

[ENTRY]
Module: search
What: bug
Expand Down Expand Up @@ -487,6 +505,40 @@ Make IntPropLevel an enum class with underlying type unsigned int and add
operator|, operator&, and operator|=. Test bits with (ipl & IPL_BASIC) !=
IPL_DEF; remove IPL_BITS_.

[ENTRY]
Module: kernel
What: new
Rank: minor
[DESCRIPTION]
Add last-conflict variable selection: INT_VAR_LAST_CONFLICT(k, vars),
BOOL_VAR_LAST_CONFLICT(k, vars), SET_VAR_LAST_CONFLICT(k, vars), and
FLOAT_VAR_LAST_CONFLICT(k, vars) select the unassigned variable whose
alternative failed most recently, among the last k such variables, and
otherwise select as vars does.
[MORE]
Supported only by branch() (not by assign(), with INT_VALUES_MIN/MAX, LDSB,
or open lists), as the only or first selection of a tie-break. The brancher
learns only from engines that report failures, so with LDS it selects as
vars does.

[ENTRY]
Module: kernel
What: new
Rank: minor
[DESCRIPTION]
Add solution-guided value selection: INT_VAL_SOLUTION(v, fallback),
BOOL_VAL_SOLUTION(v, fallback), SET_VAL_SOLUTION(v, fallback), and
FLOAT_VAL_SOLUTION(v, fallback) follow the solution stored for the variable
in a shared handle v (IntSolutionValues, SetSolutionValues, or
FloatSolutionValues), and otherwise select as fallback does.
[MORE]
Integer and Boolean variables try the stored value. Set variables include an
undecided element of the stored set, or else exclude one. Float variables
split at the median and try the half containing the stored value first. A
restart-based search can store each new solution in v from hub(). Choices
record their value, so later updates affect neither recomputation nor
no-goods.

[ENTRY]
Module: int
What: bug
Expand Down
4 changes: 4 additions & 0 deletions cmake/GecodeSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(GECODE_KERNEL_SOURCES
gecode/kernel/branch/afc.cpp
gecode/kernel/branch/chb.cpp
gecode/kernel/branch/function.cpp
gecode/kernel/branch/last-conflict.cpp
gecode/kernel/core.cpp
gecode/kernel/data/array.cpp
gecode/kernel/exception.cpp
Expand Down Expand Up @@ -70,6 +71,7 @@ set(GECODE_INT_SOURCES
gecode/int/branch.cpp
gecode/int/branch/action.cpp
gecode/int/branch/chb.cpp
gecode/int/branch/solution-values.cpp
gecode/int/branch/val-sel-commit.cpp
gecode/int/branch/view-sel.cpp
gecode/int/branch/view-values.cpp
Expand Down Expand Up @@ -132,6 +134,7 @@ set(GECODE_SET_SOURCES
gecode/set/branch/action.cpp
gecode/set/branch/chb.cpp
gecode/set/branch/ngl.cpp
gecode/set/branch/solution-values.cpp
gecode/set/branch/val-sel-commit.cpp
gecode/set/branch/view-sel.cpp
gecode/set/cardinality.cpp
Expand Down Expand Up @@ -184,6 +187,7 @@ set(GECODE_FLOAT_SOURCES
gecode/float/branch.cpp
gecode/float/branch/action.cpp
gecode/float/branch/chb.cpp
gecode/float/branch/solution-values.cpp
gecode/float/branch/val-sel-commit.cpp
gecode/float/branch/view-sel.cpp
gecode/float/channel.cpp
Expand Down
76 changes: 75 additions & 1 deletion gecode/float.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1804,12 +1804,63 @@ namespace Gecode {
FloatVarBranch FLOAT_VAR_CHB_SIZE_MAX(BranchTbl tbl = nullptr);
/// Select variable with largest CHB Q-score divided by domain size
FloatVarBranch FLOAT_VAR_CHB_SIZE_MAX(const FloatCHB& c, BranchTbl tbl = nullptr);
/**
* \brief Select the variable that failed most recently, otherwise as
* \a vars
*
* As INT_VAR_LAST_CONFLICT, with any float value selection.
*/
FloatVarBranch FLOAT_VAR_LAST_CONFLICT(int k, const FloatVarBranch& vars);
//@}

} // namespace Gecode

#include <gecode/float/branch/var.hpp>

namespace Gecode {

/**
* \brief Stored solution values for solution-guided value selection
*
* Holds one value per variable position for FLOAT_VAL_SOLUTION. Empty
* until update() stores values, typically from a solution, and updated as
* IntSolutionValues is.
*
* Copies share the stored values across clones and threads. Updates are
* serialized and reads take no lock.
*
* \ingroup TaskModelFloatBranch
*/
class FloatSolutionValues : public SharedHandle {
protected:
class Storage;
/// Return object of correct type
Storage& object() const;

public:
/// Construct as not yet initialized; only assignment may be used
FloatSolutionValues();
/// Create handle for \a n positions with no stored values
GECODE_FLOAT_EXPORT explicit FloatSolutionValues(int n);
/**
* \brief Store the medians of \a x
*
* Throws Float::ArgumentSizeMismatch if sizes differ, leaving stored
* values unchanged.
*/
GECODE_FLOAT_EXPORT void update(const FloatVarArgs& x);
/// Whether no values have been stored yet
bool empty() const;
/// Return number of positions
int size() const;
/// Return stored value at position \a i
FloatNum operator[](int i) const;
};

} // namespace Gecode

#include <gecode/float/branch/solution-values.hpp>

namespace Gecode {

/**
Expand All @@ -1824,13 +1875,18 @@ namespace Gecode {
SEL_SPLIT_MIN, ///< Select values not greater than mean of smallest and largest value
SEL_SPLIT_MAX, ///< Select values greater than mean of smallest and largest value
SEL_SPLIT_RND, ///< Select values randomly which are not greater or not smaller than mean of largest and smallest value
SEL_VAL_COMMIT ///< Select value according to user-defined functions
SEL_VAL_COMMIT, ///< Select value according to user-defined functions
SEL_SOLUTION_SPLIT_MIN, ///< Follow stored solution value, otherwise as SEL_SPLIT_MIN
SEL_SOLUTION_SPLIT_MAX, ///< Follow stored solution value, otherwise as SEL_SPLIT_MAX
SEL_SOLUTION_SPLIT_RND ///< Follow stored solution value, otherwise as SEL_SPLIT_RND
};
using enum Select;

protected:
/// Which value to select
Select s;
/// Stored solution values for solution-guided selection
FloatSolutionValues sv;

public:
/// Initialize with selection strategy \a s
Expand All @@ -1839,8 +1895,12 @@ namespace Gecode {
FloatValBranch(const Rnd& r);
/// Initialize with value function \a f and commit function \a c
FloatValBranch(FloatBranchVal v, FloatBranchCommit c);
/// Initialize to follow \a v, otherwise as \a fallback
FloatValBranch(const FloatSolutionValues& v, const FloatValBranch& fallback);
/// Return selection strategy
Select select() const;
/// Return stored solution values
const FloatSolutionValues& solution_values() const;
};

/**
Expand All @@ -1861,6 +1921,20 @@ namespace Gecode {
* alternative and that \a x must be greater or equal than \a n otherwise.
*/
FloatValBranch FLOAT_VAL(FloatBranchVal v, FloatBranchCommit c = nullptr);
/**
* \brief Follow the value stored in \a v, otherwise as \a fallback
*
* Splits at the median and tries first the half containing the value
* stored at the variable's position \a i in \a v. Falls back to
* \a fallback when \a v is empty, \a i is at least v.size(), or the
* stored value is outside the domain.
*
* \a fallback must be FLOAT_VAL_SPLIT_MIN, FLOAT_VAL_SPLIT_MAX, or
* FLOAT_VAL_SPLIT_RND; others throw UnsupportedFallback. An uninitialized
* \a v throws UninitializedSolutionValues.
*/
FloatValBranch FLOAT_VAL_SOLUTION(const FloatSolutionValues& v,
const FloatValBranch& fallback = FLOAT_VAL_SPLIT_MIN());
//@}

} // namespace Gecode
Expand Down
22 changes: 14 additions & 8 deletions gecode/float/branch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ namespace Gecode {
vars.expand(home, x);
ViewArray<FloatView> xv(home, x);
ViewSel<FloatView>* vs[1] = {Branch::view_selection(home, vars)};
post_view_value_brancher<FloatView, 1, FloatNumBranch, 2>(
home, xv, vs, Branch::value_selection_commit(home, vals), std::move(bf), std::move(vvp));
post_binary_brancher<FloatView, 1, FloatNumBranch>(home, xv, vs, Branch::value_selection_commit(home, vals),
vars.last_conflict(), std::move(bf), std::move(vvp));
}

void
Expand All @@ -53,6 +53,7 @@ namespace Gecode {
using namespace Float;
if (home.failed())
return;
check_last_conflict_tie_break(vars, "Float::branch");
vars.a.expand(home, x);
if ((vars.a.select() == FloatVarBranch::SEL_NONE) || (vars.a.select() == FloatVarBranch::SEL_RND))
vars.b = FLOAT_VAR_NONE();
Expand All @@ -71,19 +72,19 @@ namespace Gecode {
if (vars.c.select() == FloatVarBranch::SEL_NONE) {
ViewSel<FloatView>* vs[2] = {Branch::view_selection(home, vars.a),
Branch::view_selection(home, vars.b)};
post_view_value_brancher<FloatView, 2, FloatNumBranch, 2>(home, xv, vs, vsc, std::move(bf),
std::move(vvp));
post_binary_brancher<FloatView, 2, FloatNumBranch>(home, xv, vs, vsc, vars.a.last_conflict(),
std::move(bf), std::move(vvp));
} else if (vars.d.select() == FloatVarBranch::SEL_NONE) {
ViewSel<FloatView>* vs[3] = {Branch::view_selection(home, vars.a), Branch::view_selection(home, vars.b),
Branch::view_selection(home, vars.c)};
post_view_value_brancher<FloatView, 3, FloatNumBranch, 2>(home, xv, vs, vsc, std::move(bf),
std::move(vvp));
post_binary_brancher<FloatView, 3, FloatNumBranch>(home, xv, vs, vsc, vars.a.last_conflict(),
std::move(bf), std::move(vvp));
} else {
ViewSel<FloatView>* vs[4] = {Branch::view_selection(home, vars.a), Branch::view_selection(home, vars.b),
Branch::view_selection(home, vars.c),
Branch::view_selection(home, vars.d)};
post_view_value_brancher<FloatView, 4, FloatNumBranch, 2>(home, xv, vs, vsc, std::move(bf),
std::move(vvp));
post_binary_brancher<FloatView, 4, FloatNumBranch>(home, xv, vs, vsc, vars.a.last_conflict(),
std::move(bf), std::move(vvp));
}
}
}
Expand All @@ -101,6 +102,8 @@ namespace Gecode {
using namespace Float;
if (home.failed())
return;
if (vars.last_conflict() > 0)
throw UnsupportedLastConflict("Float::assign");
vars.expand(home, x);
ViewArray<FloatView> xv(home, x);
ViewSel<FloatView>* vs[1] = {Branch::view_selection(home, vars)};
Expand All @@ -114,6 +117,9 @@ namespace Gecode {
using namespace Float;
if (home.failed())
return;
if (vars.a.last_conflict() > 0)
throw UnsupportedLastConflict("Float::assign");
check_last_conflict_tie_break(vars, "Float::assign");
vars.a.expand(home, x);
if ((vars.a.select() == FloatVarBranch::SEL_NONE) || (vars.a.select() == FloatVarBranch::SEL_RND))
vars.b = FLOAT_VAR_NONE();
Expand Down
42 changes: 42 additions & 0 deletions gecode/float/branch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,48 @@ namespace Gecode::Float::Branch {
void dispose(Space& home);
};

/**
* \brief Value selection class for the stored solution value of a view
*
* Splits at the median and tries first the half containing the stored
* value at the view's position when available and in the domain;
* otherwise delegates to \a Fallback.
*
* Requires \code #include <gecode/float/branch.hh> \endcode
* \ingroup FuncFloatValSel
*/
template <class Fallback>
class ValSelSolution : public ValSel<FloatView, FloatNumBranch> {
protected:
/// The stored solution values
FloatSolutionValues sv;
/// The fallback value selection
Fallback fb;

public:
/// Constructor for initialization
ValSelSolution(Space& home, const ValBranch<Var>& vb);
/// Constructor for cloning
ValSelSolution(Space& home, ValSelSolution& vs);
/// Split the random state of the fallback by \a a
void
random_split(unsigned int a) {
fb.random_split(a);
}
/// Whether the fallback has random state
bool
random() const {
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.rand): a member random(), not the C library function
return fb.random();
}
/// Return value of view \a x at position \a i
FloatNumBranch val(const Space& home, FloatView x, int i);
/// Whether dispose must always be called (that is, notice is needed)
bool notice() const;
/// Delete value selection
void dispose(Space& home);
};

} // namespace Gecode::Float::Branch

#include <gecode/float/branch/val-sel.hpp>
Expand Down
63 changes: 63 additions & 0 deletions gecode/float/branch/solution-values.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Main authors:
* Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
*
* Copyright:
* Mikael Zayenz Lagerkvist, 2026
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.dev
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

#include <gecode/float.hh>

namespace Gecode {

// An empty value array allocates nothing (heap allocations must be positive)
FloatSolutionValues::Storage::Storage(int n0)
: n(n0), filled(false), v(n0 > 0 ? heap.alloc<std::atomic<FloatNum>>(n0) : nullptr) {
for (int i = 0; i < n; i++)
v[i].store(0.0, std::memory_order_relaxed);
}

FloatSolutionValues::Storage::~Storage() {
heap.free<std::atomic<FloatNum>>(v, n);
}

FloatSolutionValues::FloatSolutionValues(int n) : SharedHandle(new Storage(n)) {}

void
FloatSolutionValues::update(const FloatVarArgs& x) {
Storage& o = object();
if (x.size() != o.n)
throw Float::ArgumentSizeMismatch("FloatSolutionValues::update");
Support::Lock lock(o.m);
for (int i = 0; i < o.n; i++)
o.v[i].store(x[i].med(), std::memory_order_relaxed);
o.filled.store(true, std::memory_order_release);
}

} // namespace Gecode

// STATISTICS: float-branch
Loading