Skip to content
Merged
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
2 changes: 1 addition & 1 deletion examples/13_qinter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main()

DefaultFigure::set_axes(axis(0,{-6,6}), axis(1,{-6,6}));

int q = 2;
int q = 1;
DefaultFigure::pave(
{{-6,6},{-6,6}},
SepQInter(q, s1,s2,s3),
Expand Down
2 changes: 1 addition & 1 deletion examples/13_qinter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_sep(b, d):

DefaultFigure.set_axes(axis(0, Interval(-6,6)), axis(1, Interval(-6,6)))

q = 2
q = 1
DefaultFigure.pave(
[[-6,6],[-6,6]],
SepQInter(q, s1,s2,s3),
Expand Down
1 change: 1 addition & 0 deletions src/core/contractors/codac2_CtcInter.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ namespace codac2
{
for(const auto& ci : _ctcs)
{
(void)ci;
assert_release(ci->size() == this->size());
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/contractors/codac2_CtcQInter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace codac2

void CtcQInter::contract(IntervalVector& x) const
{
if(_q == 0)
// If all constraints may be violated, no contraction can be performed.
if(_q == _ctcs.size())
return;

std::list<IntervalVector> l;
Expand All @@ -35,4 +36,4 @@ namespace codac2

x = qinter(_q, l);
}
}
}
5 changes: 4 additions & 1 deletion src/core/contractors/codac2_CtcQInter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ namespace codac2
{
public:

// q is the maximum number of constraints that may be violated.
CtcQInter(unsigned int q, const Collection<CtcBase<IntervalVector>>& c)
: Ctc<CtcQInter,IntervalVector>([&c]() {
assert_release(!c.empty());
Index n = size_of(c.front());
for(const auto& s : c) {
(void)s;
assert_release(size_of(s) == n && "all contractors must be of same size");
}
return n;
Expand All @@ -34,6 +36,7 @@ namespace codac2
assert_release(q <= c.size());
}

// q is the maximum number of constraints that may be violated.
explicit CtcQInter(unsigned int q, Index n, const Collection<CtcBase<IntervalVector>>& ctcs = {})
: Ctc<CtcQInter,IntervalVector>(n), _q(q), _ctcs(ctcs)
{
Expand Down Expand Up @@ -64,4 +67,4 @@ namespace codac2
size_t _q;
Collection<CtcBase<IntervalVector>> _ctcs;
};
}
}
13 changes: 11 additions & 2 deletions src/core/contractors/codac2_CtcUnion.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace codac2
{
for(const auto& ci : _ctcs)
{
(void)ci;
assert_release(ci->size() == this->size());
}
}
Expand All @@ -65,7 +66,9 @@ namespace codac2

void contract(X&... x) const
{
auto result = std::tuple<X...>(x...);
const auto input = std::tuple<X...>(x...);

auto result = input;
std::apply([](auto&... xi)
{
(xi.set_empty(), ...);
Expand All @@ -78,14 +81,20 @@ namespace codac2

for(const auto& ci : _ctcs)
{
auto saved = std::tuple<X...>(x...);
auto saved = input;

std::apply([&](auto&... xi)
{
ci->contract(xi...);
}, saved);

accumulate_union(saved, std::index_sequence_for<X...>{});

// Each contractor is contractant, hence every remaining branch
// can only return a subset of input. Once the accumulated union
// has reached input, the final result is already known.
if(result == input)
return;
}

std::tie(x...) = result;
Expand Down
4 changes: 4 additions & 0 deletions src/core/contractors/codac2_CtcWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ namespace codac2
: Ctc<CtcWrapper<Y>,X>(y.size()), _y(y)
{ }

CtcWrapper(Y&& y)
: Ctc<CtcWrapper<Y>,X>(y.size()), _y(std::move(y))
{ }

void contract(X& x) const
{
assert_release(x.size() == this->size());
Expand Down
28 changes: 20 additions & 8 deletions src/core/proj/codac2_qinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ namespace codac2
{
assert(!l.empty());
Index n = l.begin()->size();

if(q > l.size())
return IntervalVector::empty(n);

assert(([&l,n](){
for(const auto& xi : l) {
Expand All @@ -33,18 +30,33 @@ namespace codac2
return true;
}()));

if(q > l.size())
return IntervalVector::empty(n);

// q is the maximum number of boxes that may be violated.
// Hence, at least l.size()-q boxes must contain the point.
const size_t min_satisfied = l.size()-q;

if(min_satisfied == 0)
return IntervalVector(n);

unsigned int p = 0;
for(const auto& li : l)
if(!li.is_empty())
p++;

// Empty boxes cannot be satisfied.
if(min_satisfied > p)
return IntervalVector::empty(n);

IntervalVector res(n);
std::vector<std::pair<double,ProjBound>> b(2*p);

// Main loop: solve the q-inter independently on each dimension, and return the Cartesian product
// Main loop: solve the q-relaxed intersection independently on each dimension,
// and return the Cartesian product
for(Index i = 0 ; i < n ; i++)
{
// Solve the q-inter for dimension i
// Solve the q-relaxed intersection for dimension i

int j = 0;
for(const auto& xj : l)
Expand All @@ -64,7 +76,7 @@ namespace codac2
for(unsigned int k = 0 ; k < 2*p ; k++)
{
(b[k].second == ProjBound::LEFT) ? c++ : c--;
if(c == (int)q)
if(c == static_cast<int>(min_satisfied))
{
lb0 = b[k].first;
break;
Expand All @@ -82,7 +94,7 @@ namespace codac2
for(int k = 2*p-1 ; k >= 0 ; k--)
{
(b[k].second == ProjBound::RIGHT) ? c++ : c--;
if(c == (int)q)
if(c == static_cast<int>(min_satisfied))
{
rb0 = b[k].first;
break;
Expand All @@ -94,4 +106,4 @@ namespace codac2

return res;
}
}
}
8 changes: 4 additions & 4 deletions src/core/proj/codac2_qinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

namespace codac2
{
// Approximate Q-intersection, using Luc Jaulin's algorithm
// Approximate q-relaxed intersection, using Luc Jaulin's algorithm
// Author: Clement Carbonnel, Benoit Desrochers, Simon Rohou
// The q-intersection of n boxes corresponds to the set of all elements
// which belong to at least q of these boxes.
// The q-relaxed intersection of n boxes corresponds to the set of all elements
// which belong to at least n-q of these boxes, i.e. which violate at most q boxes.
IntervalVector qinter(unsigned int q, const std::list<IntervalVector>& l);
}
}
7 changes: 4 additions & 3 deletions src/core/separators/codac2_SepQInter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace codac2

BoxPair SepQInter::separate(const IntervalVector& x) const
{
if(_q == 0)
// If all constraints may be violated, the represented set is the whole space.
if(_q == _seps.size())
return { IntervalVector::empty(x.size()), x };

std::list<IntervalVector> l_inner, l_outer;
Expand All @@ -34,11 +35,11 @@ namespace codac2
}

BoxPair x_sep {
qinter(_seps.size()-_q+1, l_inner),
qinter(_seps.size()-_q-1, l_inner),
qinter(_q, l_outer)
};

assert((x_sep.inner | x_sep.outer) == x);
return x_sep;
}
}
}
5 changes: 4 additions & 1 deletion src/core/separators/codac2_SepQInter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ namespace codac2
{
public:

// q is the maximum number of constraints that may be violated.
SepQInter(unsigned int q, const Collection<SepBase>& c)
: Sep<SepQInter>([&c]() {
assert_release(!c.empty());
Index n = size_of(c.front());
for(const auto& s : c) {
(void)s;
assert_release(size_of(s) == n && "all separators must be of same size");
}
return n;
Expand All @@ -33,6 +35,7 @@ namespace codac2
assert_release(q <= c.size());
}

// q is the maximum number of constraints that may be violated.
explicit SepQInter(unsigned int q, Index n, const Collection<SepBase>& sep = {})
: Sep<SepQInter>(n), _q(q), _seps(sep)
{
Expand Down Expand Up @@ -62,4 +65,4 @@ namespace codac2
size_t _q;
Collection<SepBase> _seps;
};
}
}
2 changes: 1 addition & 1 deletion src/graphics/styles/codac2_Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace codac2
*/
static Color random(float alpha = 1.)
{
return Color({(float)Interval(0,360).rand(),100,100, (float)(alpha * 255.)}, Model::HSV);
return Color({(float)Interval(0,360).rand(),100,100, (float)(alpha * 100.)}, Model::HSV);
};

#define DEFINE_COLOR(NAME, R, G, B) \
Expand Down
Loading